|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +//! Regression tests for multi-line script and sourced-file execution. |
| 3 | +//! |
| 4 | +//! The previous `execute_script_content` and `Command::Source` iterated |
| 5 | +//! `content.lines()` and parsed each line in isolation. That broke every |
| 6 | +//! control structure spanning multiple lines — the canonical POSIX shape: |
| 7 | +//! |
| 8 | +//! ```sh |
| 9 | +//! if true |
| 10 | +//! then |
| 11 | +//! mkdir d |
| 12 | +//! fi |
| 13 | +//! ``` |
| 14 | +//! |
| 15 | +//! Fix: strip whole-line comments, then split the *entire* content on |
| 16 | +//! `split_on_statement_separators` (top-level `;` and `\n`, respecting |
| 17 | +//! quotes, parens, control-structure depth, and brace depth — so |
| 18 | +//! `foo() { ...; }` stays on a single statement too). |
| 19 | +
|
| 20 | +use anyhow::Result; |
| 21 | +use std::fs; |
| 22 | +use tempfile::tempdir; |
| 23 | +use vsh::executable::ExecutableCommand; |
| 24 | +use vsh::parser::{parse_command, split_on_statement_separators}; |
| 25 | +use vsh::state::ShellState; |
| 26 | + |
| 27 | +// ------------------------------------------------------------------------- |
| 28 | +// Statement splitter unit tests |
| 29 | +// ------------------------------------------------------------------------- |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn statement_splitter_splits_on_newline() { |
| 33 | + let parts = split_on_statement_separators("echo a\necho b"); |
| 34 | + let segs: Vec<&str> = parts.iter().map(|s| s.trim()).collect(); |
| 35 | + assert_eq!(segs, vec!["echo a", "echo b"]); |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn statement_splitter_keeps_multiline_if_together() { |
| 40 | + let parts = |
| 41 | + split_on_statement_separators("if true\nthen\n mkdir d\nfi\necho after"); |
| 42 | + let segs: Vec<&str> = parts.iter().map(|s| s.trim()).filter(|s| !s.is_empty()).collect(); |
| 43 | + // The whole if/fi block is one segment, echo is a second. |
| 44 | + assert_eq!(segs.len(), 2); |
| 45 | + assert!(segs[0].starts_with("if") && segs[0].ends_with("fi")); |
| 46 | + assert_eq!(segs[1], "echo after"); |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn statement_splitter_keeps_function_def_together() { |
| 51 | + let parts = split_on_statement_separators("foo() { mkdir a; mkdir b; }\necho x"); |
| 52 | + let segs: Vec<&str> = parts.iter().map(|s| s.trim()).filter(|s| !s.is_empty()).collect(); |
| 53 | + assert_eq!(segs.len(), 2); |
| 54 | + assert_eq!(segs[0], "foo() { mkdir a; mkdir b; }"); |
| 55 | + assert_eq!(segs[1], "echo x"); |
| 56 | +} |
| 57 | + |
| 58 | +#[test] |
| 59 | +fn statement_splitter_ignores_newlines_inside_quotes() { |
| 60 | + let parts = split_on_statement_separators("echo 'a\nb'\necho c"); |
| 61 | + let segs: Vec<&str> = parts.iter().map(|s| s.trim()).filter(|s| !s.is_empty()).collect(); |
| 62 | + assert_eq!(segs.len(), 2); |
| 63 | + assert_eq!(segs[0], "echo 'a\nb'"); |
| 64 | + assert_eq!(segs[1], "echo c"); |
| 65 | +} |
| 66 | + |
| 67 | +// ------------------------------------------------------------------------- |
| 68 | +// End-to-end: sourced scripts with multi-line control structures |
| 69 | +// ------------------------------------------------------------------------- |
| 70 | + |
| 71 | +#[test] |
| 72 | +fn sourced_script_executes_multiline_if_then_fi() -> Result<()> { |
| 73 | + let temp = tempdir()?; |
| 74 | + let mut state = ShellState::new(temp.path().to_str().unwrap())?; |
| 75 | + |
| 76 | + let script = temp.path().join("multi.sh"); |
| 77 | + fs::write(&script, "if true\nthen\n mkdir from_multi\nfi\n")?; |
| 78 | + |
| 79 | + parse_command(&format!("source {}", script.display()))?.execute(&mut state)?; |
| 80 | + |
| 81 | + assert!(state.resolve_path("from_multi").exists()); |
| 82 | + Ok(()) |
| 83 | +} |
| 84 | + |
| 85 | +#[test] |
| 86 | +fn sourced_script_executes_multiline_for_loop() -> Result<()> { |
| 87 | + let temp = tempdir()?; |
| 88 | + let mut state = ShellState::new(temp.path().to_str().unwrap())?; |
| 89 | + |
| 90 | + let script = temp.path().join("loop.sh"); |
| 91 | + fs::write(&script, "for x in a b c\ndo\n mkdir $x\ndone\n")?; |
| 92 | + |
| 93 | + parse_command(&format!("source {}", script.display()))?.execute(&mut state)?; |
| 94 | + |
| 95 | + assert!(state.resolve_path("a").exists()); |
| 96 | + assert!(state.resolve_path("b").exists()); |
| 97 | + assert!(state.resolve_path("c").exists()); |
| 98 | + Ok(()) |
| 99 | +} |
| 100 | + |
| 101 | +#[test] |
| 102 | +fn sourced_script_defines_and_invokes_multiline_function() -> Result<()> { |
| 103 | + let temp = tempdir()?; |
| 104 | + let mut state = ShellState::new(temp.path().to_str().unwrap())?; |
| 105 | + |
| 106 | + let script = temp.path().join("fns.sh"); |
| 107 | + fs::write( |
| 108 | + &script, |
| 109 | + "greet() { mkdir hi; }\n# a comment\ngreet\n", |
| 110 | + )?; |
| 111 | + |
| 112 | + parse_command(&format!("source {}", script.display()))?.execute(&mut state)?; |
| 113 | + |
| 114 | + assert!(state.functions.is_defined("greet")); |
| 115 | + assert!(state.resolve_path("hi").exists()); |
| 116 | + Ok(()) |
| 117 | +} |
| 118 | + |
| 119 | +#[test] |
| 120 | +fn sourced_script_skips_comment_only_lines() -> Result<()> { |
| 121 | + let temp = tempdir()?; |
| 122 | + let mut state = ShellState::new(temp.path().to_str().unwrap())?; |
| 123 | + |
| 124 | + let script = temp.path().join("commented.sh"); |
| 125 | + fs::write( |
| 126 | + &script, |
| 127 | + "# leading comment\n\n# another\nmkdir real\n # indented comment\nmkdir also\n", |
| 128 | + )?; |
| 129 | + |
| 130 | + parse_command(&format!("source {}", script.display()))?.execute(&mut state)?; |
| 131 | + |
| 132 | + assert!(state.resolve_path("real").exists()); |
| 133 | + assert!(state.resolve_path("also").exists()); |
| 134 | + Ok(()) |
| 135 | +} |
| 136 | + |
| 137 | +#[test] |
| 138 | +fn sourced_script_preserves_semicolon_inline() -> Result<()> { |
| 139 | + let temp = tempdir()?; |
| 140 | + let mut state = ShellState::new(temp.path().to_str().unwrap())?; |
| 141 | + |
| 142 | + // Mixed: a single-line if + a multi-line for in the same file. |
| 143 | + let script = temp.path().join("mixed.sh"); |
| 144 | + fs::write( |
| 145 | + &script, |
| 146 | + "if true; then mkdir one; fi\nfor x in two three\ndo\n mkdir $x\ndone\n", |
| 147 | + )?; |
| 148 | + |
| 149 | + parse_command(&format!("source {}", script.display()))?.execute(&mut state)?; |
| 150 | + |
| 151 | + assert!(state.resolve_path("one").exists()); |
| 152 | + assert!(state.resolve_path("two").exists()); |
| 153 | + assert!(state.resolve_path("three").exists()); |
| 154 | + Ok(()) |
| 155 | +} |
0 commit comments