-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiline_script_tests.rs
More file actions
164 lines (137 loc) · 5.36 KB
/
Copy pathmultiline_script_tests.rs
File metadata and controls
164 lines (137 loc) · 5.36 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//! Regression tests for multi-line script and sourced-file execution.
//!
//! The previous `execute_script_content` and `Command::Source` iterated
//! `content.lines()` and parsed each line in isolation. That broke every
//! control structure spanning multiple lines — the canonical POSIX shape:
//!
//! ```sh
//! if true
//! then
//! mkdir d
//! fi
//! ```
//!
//! Fix: strip whole-line comments, then split the *entire* content on
//! `split_on_statement_separators` (top-level `;` and `\n`, respecting
//! quotes, parens, control-structure depth, and brace depth — so
//! `foo() { ...; }` stays on a single statement too).
use anyhow::Result;
use std::fs;
use tempfile::tempdir;
use vsh::executable::ExecutableCommand;
use vsh::parser::{parse_command, split_on_statement_separators};
use vsh::state::ShellState;
// -------------------------------------------------------------------------
// Statement splitter unit tests
// -------------------------------------------------------------------------
#[test]
fn statement_splitter_splits_on_newline() {
let parts = split_on_statement_separators("echo a\necho b");
let segs: Vec<&str> = parts.iter().map(|s| s.trim()).collect();
assert_eq!(segs, vec!["echo a", "echo b"]);
}
#[test]
fn statement_splitter_keeps_multiline_if_together() {
let parts = split_on_statement_separators("if true\nthen\n mkdir d\nfi\necho after");
let segs: Vec<&str> = parts
.iter()
.map(|s| s.trim())
.filter(|s| !s.is_empty())
.collect();
// The whole if/fi block is one segment, echo is a second.
assert_eq!(segs.len(), 2);
assert!(segs[0].starts_with("if") && segs[0].ends_with("fi"));
assert_eq!(segs[1], "echo after");
}
#[test]
fn statement_splitter_keeps_function_def_together() {
let parts = split_on_statement_separators("foo() { mkdir a; mkdir b; }\necho x");
let segs: Vec<&str> = parts
.iter()
.map(|s| s.trim())
.filter(|s| !s.is_empty())
.collect();
assert_eq!(segs.len(), 2);
assert_eq!(segs[0], "foo() { mkdir a; mkdir b; }");
assert_eq!(segs[1], "echo x");
}
#[test]
fn statement_splitter_ignores_newlines_inside_quotes() {
let parts = split_on_statement_separators("echo 'a\nb'\necho c");
let segs: Vec<&str> = parts
.iter()
.map(|s| s.trim())
.filter(|s| !s.is_empty())
.collect();
assert_eq!(segs.len(), 2);
assert_eq!(segs[0], "echo 'a\nb'");
assert_eq!(segs[1], "echo c");
}
// -------------------------------------------------------------------------
// End-to-end: sourced scripts with multi-line control structures
// -------------------------------------------------------------------------
#[test]
fn sourced_script_executes_multiline_if_then_fi() -> Result<()> {
let temp = tempdir()?;
let mut state = ShellState::new(temp.path().to_str().unwrap())?;
let script = temp.path().join("multi.sh");
fs::write(&script, "if true\nthen\n mkdir from_multi\nfi\n")?;
parse_command(&format!("source {}", script.display()))?.execute(&mut state)?;
assert!(state.resolve_path("from_multi").exists());
Ok(())
}
#[test]
fn sourced_script_executes_multiline_for_loop() -> Result<()> {
let temp = tempdir()?;
let mut state = ShellState::new(temp.path().to_str().unwrap())?;
let script = temp.path().join("loop.sh");
fs::write(&script, "for x in a b c\ndo\n mkdir $x\ndone\n")?;
parse_command(&format!("source {}", script.display()))?.execute(&mut state)?;
assert!(state.resolve_path("a").exists());
assert!(state.resolve_path("b").exists());
assert!(state.resolve_path("c").exists());
Ok(())
}
#[test]
fn sourced_script_defines_and_invokes_multiline_function() -> Result<()> {
let temp = tempdir()?;
let mut state = ShellState::new(temp.path().to_str().unwrap())?;
let script = temp.path().join("fns.sh");
fs::write(&script, "greet() { mkdir hi; }\n# a comment\ngreet\n")?;
parse_command(&format!("source {}", script.display()))?.execute(&mut state)?;
assert!(state.functions.is_defined("greet"));
assert!(state.resolve_path("hi").exists());
Ok(())
}
#[test]
fn sourced_script_skips_comment_only_lines() -> Result<()> {
let temp = tempdir()?;
let mut state = ShellState::new(temp.path().to_str().unwrap())?;
let script = temp.path().join("commented.sh");
fs::write(
&script,
"# leading comment\n\n# another\nmkdir real\n # indented comment\nmkdir also\n",
)?;
parse_command(&format!("source {}", script.display()))?.execute(&mut state)?;
assert!(state.resolve_path("real").exists());
assert!(state.resolve_path("also").exists());
Ok(())
}
#[test]
fn sourced_script_preserves_semicolon_inline() -> Result<()> {
let temp = tempdir()?;
let mut state = ShellState::new(temp.path().to_str().unwrap())?;
// Mixed: a single-line if + a multi-line for in the same file.
let script = temp.path().join("mixed.sh");
fs::write(
&script,
"if true; then mkdir one; fi\nfor x in two three\ndo\n mkdir $x\ndone\n",
)?;
parse_command(&format!("source {}", script.display()))?.execute(&mut state)?;
assert!(state.resolve_path("one").exists());
assert!(state.resolve_path("two").exists());
assert!(state.resolve_path("three").exists());
Ok(())
}