Skip to content

Commit 703a356

Browse files
sheredomdylanmckay
authored andcommitted
Allow shell to changed via config.
This commit allows me (and others!) to change which shell to use to run the tests via the `config`. This is useful for platforms that do not have the default shell, `bash`, present. To test this I decided that if the `lit` crate is happy to rely on `bash`, it makes sense that `sh` would also be present. So I added an additional run of the integration tests using `sh` to prove that it works.
1 parent e547563 commit 703a356

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub struct Config
4747
/// Whether messages on the standard error streams emitted during test runs
4848
/// should always be shown.
4949
pub always_show_stderr: bool,
50+
/// Which shell to use (defaults to 'bash').
51+
pub shell: String,
5052
}
5153

5254
/// A function which can dynamically define newly used variables in a test.
@@ -151,6 +153,7 @@ impl Default for Config
151153
always_show_stderr: false,
152154
truncate_output_context_to_number_of_lines: Some(DEFAULT_MAX_OUTPUT_CONTEXT_LINE_COUNT),
153155
extra_executable_search_paths,
156+
shell: "bash".to_string(),
154157
}
155158
}
156159
}

src/run/legacy_test_evaluator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use crate::{parse, vars};
77

88
use std;
99

10-
const SHELL: &'static str = "bash";
11-
1210
pub struct TestEvaluator
1311
{
1412
pub invocation: Invocation,
@@ -40,7 +38,7 @@ impl TestEvaluator
4038
Err(e) => match e.kind() {
4139
std::io::ErrorKind::NotFound => {
4240
return TestResultKind::Error(
43-
format!("shell '{}' does not exist", SHELL).into(),
41+
format!("shell '{}' does not exist", &config.shell).into(),
4442
);
4543
},
4644
_ => return TestResultKind::Error(e.into()),
@@ -73,7 +71,7 @@ impl TestEvaluator
7371

7472
let command_line: String = vars::resolve::invocation(&self.invocation, &config, &mut variables);
7573

76-
let mut cmd = process::Command::new("bash");
74+
let mut cmd = process::Command::new(&config.shell);
7775
cmd.args(&["-c", &command_line]);
7876

7977
if let Ok(current_exe) = env::current_exe() {

src/run/test_evaluator.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use std::{collections::HashMap, env, fs, process};
1010
mod state;
1111
#[cfg(test)] mod state_tests;
1212

13-
const DEFAULT_SHELL: &'static str = "bash";
14-
1513
/// Responsible for evaluating specific tests and collecting
1614
/// the results.
1715
#[derive(Clone)]
@@ -32,7 +30,7 @@ pub fn execute_tests<'test>(test_file: &'test TestFile, config: &Config) -> Vec<
3230
let mut test_run_state = TestRunState::new(initial_variables);
3331
let (command, command_line) = self::build_command(invocation, test_file, config);
3432

35-
let (program_output, execution_result) = self::collect_output(command, command_line.clone());
33+
let (program_output, execution_result) = self::collect_output(command, command_line.clone(), config);
3634

3735
test_run_state.append_program_output(&program_output.stdout);
3836
test_run_state.append_program_stderr(&program_output.stderr);
@@ -97,14 +95,15 @@ fn run_test_checks(
9795
fn collect_output(
9896
mut command: process::Command,
9997
command_line: CommandLine,
98+
config: &Config,
10099
) -> (ProgramOutput, TestResultKind) {
101100
let mut test_result_kind = TestResultKind::Pass;
102101

103102
let output = match command.output() {
104103
Ok(o) => o,
105104
Err(e) => {
106105
let error_message = match e.kind() {
107-
std::io::ErrorKind::NotFound => format!("shell '{}' does not exist", DEFAULT_SHELL).into(),
106+
std::io::ErrorKind::NotFound => format!("shell '{}' does not exist", &config.shell).into(),
108107
_ => e.to_string(),
109108
};
110109

@@ -142,7 +141,7 @@ fn build_command(invocation: &Invocation,
142141

143142
let command_line: String = vars::resolve::invocation(invocation, &config, &mut variables);
144143

145-
let mut cmd = process::Command::new(DEFAULT_SHELL);
144+
let mut cmd = process::Command::new(&config.shell);
146145
cmd.args(&["-c", &command_line]);
147146

148147
if !config.extra_executable_search_paths.is_empty() {

tests/integration_tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,14 @@ fn integration_tests() {
1414
config.add_extension(ext);
1515
}
1616
}).expect("unit test(s) failed");
17+
18+
// Now run the tests again but use a custom shell instead.
19+
run::tests(lit::event_handler::Default::default(), |config| {
20+
config.add_search_path(format!("{}/integration-tests", CRATE_PATH));
21+
for ext in lit::INTEGRATION_TEST_FILE_EXTENSIONS {
22+
config.add_extension(ext);
23+
}
24+
25+
config.shell = "sh".to_string();
26+
}).expect("unit test(s) failed");
1727
}

0 commit comments

Comments
 (0)