forked from drewwalton19216801/rush-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_tests.rs
More file actions
81 lines (74 loc) · 2.37 KB
/
command_tests.rs
File metadata and controls
81 lines (74 loc) · 2.37 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
72
73
74
75
76
77
78
79
80
81
//! Tests for command execution (single commands, pipelines, etc.)
use crate::executor::{execute, execute_pipeline, execute_single_command};
use crate::parser::{Ast, ShellCommand};
use crate::state::ShellState;
#[test]
fn test_execute_single_command_builtin() {
let cmd = ShellCommand {
args: vec!["true".to_string()],
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
let exit_code = execute_single_command(&cmd, &mut shell_state);
assert_eq!(exit_code, 0);
}
// For external commands, test with a command that exists
#[test]
fn test_execute_single_command_external() {
let cmd = ShellCommand {
args: vec!["true".to_string()], // Assume true exists
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
let exit_code = execute_single_command(&cmd, &mut shell_state);
assert_eq!(exit_code, 0);
}
#[test]
fn test_execute_single_command_external_nonexistent() {
let cmd = ShellCommand {
args: vec!["nonexistent_command".to_string()],
redirections: Vec::new(),
compound: None,
};
let mut shell_state = ShellState::new();
let exit_code = execute_single_command(&cmd, &mut shell_state);
assert_eq!(exit_code, 1); // Command not found
}
#[test]
fn test_execute_pipeline() {
let commands = vec![
ShellCommand {
args: vec!["printf".to_string(), "hello".to_string()],
redirections: Vec::new(),
compound: None,
},
ShellCommand {
args: vec!["cat".to_string()], // cat reads from stdin
redirections: Vec::new(),
compound: None,
},
];
let mut shell_state = ShellState::new();
let exit_code = execute_pipeline(&commands, &mut shell_state);
assert_eq!(exit_code, 0);
}
#[test]
fn test_execute_empty_pipeline() {
let commands = vec![];
let mut shell_state = ShellState::new();
let exit_code = execute(Ast::Pipeline(commands), &mut shell_state);
assert_eq!(exit_code, 0);
}
#[test]
fn test_execute_single_command() {
let ast = Ast::Pipeline(vec![ShellCommand {
args: vec!["true".to_string()],
redirections: Vec::new(),
compound: None,
}]);
let mut shell_state = ShellState::new();
let exit_code = execute(ast, &mut shell_state);
assert_eq!(exit_code, 0);
}