forked from drewwalton19216801/rush-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpansion_tests.rs
More file actions
71 lines (55 loc) · 2.44 KB
/
expansion_tests.rs
File metadata and controls
71 lines (55 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Tests for expansion functionality (variable expansion, wildcards, etc.)
use crate::executor::expand_variables_in_string;
use crate::state::ShellState;
#[test]
fn test_here_document_with_variable_expansion() {
// Test that variables are expanded in here-document content
let mut shell_state = ShellState::new();
shell_state.set_var("PWD", "/test/path".to_string());
// Simulate here-doc content with variable
let content = "Working dir: $PWD";
let expanded = expand_variables_in_string(content, &mut shell_state);
assert_eq!(expanded, "Working dir: /test/path");
}
#[test]
fn test_here_document_with_command_substitution_builtin() {
// Test that builtin command substitutions work in here-document content
let mut shell_state = ShellState::new();
shell_state.set_var("PWD", "/test/dir".to_string());
// Simulate here-doc content with pwd builtin command substitution
let content = "Current directory: `pwd`";
let expanded = expand_variables_in_string(content, &mut shell_state);
// The pwd builtin should be executed and expanded
assert!(expanded.contains("Current directory: "));
}
#[test]
fn test_last_background_pid_expansion() {
let mut shell_state = ShellState::new();
// Initially, $! should expand to empty string
let expanded = expand_variables_in_string("PID: $!", &mut shell_state);
assert_eq!(expanded, "PID: ");
// Set last background PID
shell_state.last_background_pid = Some(1234);
let expanded = expand_variables_in_string("PID: $!", &mut shell_state);
assert_eq!(expanded, "PID: 1234");
// Test with brace syntax
let expanded = expand_variables_in_string("PID: ${!}", &mut shell_state);
assert_eq!(expanded, "PID: 1234");
}
#[test]
fn test_last_background_pid_in_arithmetic() {
let mut shell_state = ShellState::new();
// Set last background PID
shell_state.last_background_pid = Some(100);
// Test in arithmetic expansion
let expanded = expand_variables_in_string("Result: $(($! + 50))", &mut shell_state);
assert_eq!(expanded, "Result: 150");
}
#[test]
fn test_last_background_pid_multiple_references() {
let mut shell_state = ShellState::new();
shell_state.last_background_pid = Some(5678);
// Test multiple references in one string
let expanded = expand_variables_in_string("PID $! and again $! and ${!}", &mut shell_state);
assert_eq!(expanded, "PID 5678 and again 5678 and 5678");
}