Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.

Commit 18e654a

Browse files
committed
implement executable_mock wrapper that executes given commands
1 parent 2d0375b commit 18e654a

File tree

4 files changed

+86
-34
lines changed

4 files changed

+86
-34
lines changed

Justfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ dev:
2323
clear ; printf "\e[3J"
2424
cargo test --all --color=always --features 'dev test' -- --test-threads=1 --quiet
2525

26+
unit_test:
27+
clear ; printf "\e[3J"
28+
cargo build --color=always --features=dev
29+
cargo test --lib --all --color=always --features 'dev test' -- --test-threads=1 --quiet
30+
2631
run_bigger:
2732
cargo run -- tests/examples/bigger/script
2833

src/executable_mock.rs

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ use std::fs;
99
use std::io::Write;
1010
use std::os::unix::ffi::OsStrExt;
1111
use std::path::{Path, PathBuf};
12+
use std::process::Command;
1213

1314
#[derive(Debug, Serialize, Deserialize)]
14-
pub struct Config {
15-
pub stdout: Vec<u8>,
16-
pub exitcode: i32,
15+
pub enum Config {
16+
Config { stdout: Vec<u8>, exitcode: i32 },
17+
Wrapper { executable: PathBuf },
1718
}
1819

1920
#[derive(Debug)]
@@ -37,6 +38,15 @@ impl ExecutableMock {
3738
Ok(ExecutableMock { temp_file })
3839
}
3940

41+
fn wrapper(context: &Context, executable: &Path) -> R<ExecutableMock> {
42+
ExecutableMock::new(
43+
context,
44+
Config::Wrapper {
45+
executable: executable.to_owned(),
46+
},
47+
)
48+
}
49+
4050
pub fn path(&self) -> PathBuf {
4151
self.temp_file.path()
4252
}
@@ -57,8 +67,16 @@ impl ExecutableMock {
5767
let config: Config = deserialize(&ExecutableMock::skip_hashbang_line(fs::read(
5868
executable_mock_path,
5969
)?))?;
60-
context.stdout().write_all(&config.stdout)?;
61-
Ok(ExitCode(config.exitcode))
70+
match config {
71+
Config::Config { stdout, exitcode } => {
72+
context.stdout().write_all(&stdout)?;
73+
Ok(ExitCode(exitcode))
74+
}
75+
Config::Wrapper { executable } => {
76+
Command::new(&executable).output()?;
77+
Ok(ExitCode(0))
78+
}
79+
}
6280
}
6381

6482
fn skip_hashbang_line(input: Vec<u8>) -> Vec<u8> {
@@ -75,33 +93,63 @@ impl ExecutableMock {
7593
mod test {
7694
use super::*;
7795
use std::process::Command;
78-
use test_utils::TempFile;
96+
use test_utils::{trim_margin, TempFile};
7997

80-
#[test]
81-
fn creates_an_executable_that_outputs_the_given_stdout() -> R<()> {
82-
let mock_executable = ExecutableMock::new(
83-
&Context::new_mock(),
84-
Config {
85-
stdout: b"foo".to_vec(),
86-
exitcode: 0,
87-
},
88-
)?;
89-
let output = Command::new(mock_executable.path()).output()?;
90-
assert_eq!(output.stdout, b"foo");
91-
Ok(())
98+
mod new {
99+
use super::*;
100+
101+
#[test]
102+
fn creates_an_executable_that_outputs_the_given_stdout() -> R<()> {
103+
let executable_mock = ExecutableMock::new(
104+
&Context::new_mock(),
105+
Config::Config {
106+
stdout: b"foo".to_vec(),
107+
exitcode: 0,
108+
},
109+
)?;
110+
let output = Command::new(&executable_mock.path()).output();
111+
assert_eq!(output?.stdout, b"foo");
112+
Ok(())
113+
}
114+
115+
#[test]
116+
fn creates_an_executable_that_exits_with_the_given_exitcode() -> R<()> {
117+
let executable_mock = ExecutableMock::new(
118+
&Context::new_mock(),
119+
Config::Config {
120+
stdout: b"foo".to_vec(),
121+
exitcode: 42,
122+
},
123+
)?;
124+
let output = Command::new(executable_mock.path()).output()?;
125+
assert_eq!(output.status.code(), Some(42));
126+
Ok(())
127+
}
92128
}
93129

94-
#[test]
95-
fn creates_an_executable_that_exits_with_the_given_exitcode() -> R<()> {
96-
let mock_executable = ExecutableMock::new(
97-
&Context::new_mock(),
98-
Config {
99-
stdout: b"foo".to_vec(),
100-
exitcode: 42,
101-
},
102-
)?;
103-
let output = Command::new(mock_executable.path()).output()?;
104-
assert_eq!(output.status.code(), Some(42));
105-
Ok(())
130+
mod wrapper {
131+
use super::*;
132+
use crate::utils::path_to_string;
133+
use tempdir::TempDir;
134+
135+
#[test]
136+
fn executes_the_given_command() -> R<()> {
137+
let temp_dir = TempDir::new("test")?;
138+
let path = temp_dir.path().join("foo.txt");
139+
let script = TempFile::write_temp_script(
140+
trim_margin(&format!(
141+
"
142+
|#!/usr/bin/env bash
143+
|echo foo > {}
144+
",
145+
path_to_string(&path)?
146+
))?
147+
.as_bytes(),
148+
)?;
149+
let executable_mock = ExecutableMock::wrapper(&Context::new_mock(), &script.path())?;
150+
Command::new(executable_mock.path()).status()?;
151+
assert_eq!(String::from_utf8(fs::read(&path)?)?, "foo\n");
152+
Ok(())
153+
}
106154
}
107155
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ mod run_main {
106106
let context = Context::new_mock();
107107
let executable_mock = ExecutableMock::new(
108108
&context,
109-
executable_mock::Config {
109+
executable_mock::Config::Config {
110110
stdout: b"foo".to_vec(),
111111
exitcode: 0,
112112
},

src/test_checker/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::test_spec;
77
use crate::test_spec::Test;
88
use crate::tracer::stdio_redirecting::Redirector;
99
use crate::tracer::{tracee_memory, SyscallMock};
10-
use crate::utils::short_temp_files::ShortTempFile;
1110
use crate::R;
1211
use checker_result::CheckerResult;
1312
use libc::{c_ulonglong, user_regs_struct};
@@ -38,7 +37,7 @@ impl TestChecker {
3837
}
3938

4039
fn allow_failing_scripts_to_continue() -> executable_mock::Config {
41-
executable_mock::Config {
40+
executable_mock::Config::Config {
4241
stdout: vec![],
4342
exitcode: 0,
4443
}
@@ -53,7 +52,7 @@ impl TestChecker {
5352
&received.format(),
5453
);
5554
}
56-
executable_mock::Config {
55+
executable_mock::Config::Config {
5756
stdout: next_test_step.stdout,
5857
exitcode: next_test_step.exitcode,
5958
}

0 commit comments

Comments
 (0)