Skip to content

Commit bccdf2b

Browse files
authored
Merge pull request #21 from Zeegomo/fix-process-lines
2 parents 0d3c2f6 + 13a023b commit bccdf2b

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## Unreleased
77

8+
### Fixed
9+
10+
- Fix `Command::process_lines` not working in sandboxed enviroments.
11+
812
## [0.6.0] - 2020-04-01
913

1014
### Added

src/cmd/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,12 @@ impl<'w, 'pl> Command<'w, 'pl> {
345345
.env("CARGO_HOME", container_dirs::CARGO_HOME.to_str().unwrap())
346346
.env("RUSTUP_HOME", container_dirs::RUSTUP_HOME.to_str().unwrap());
347347

348-
builder.run(workspace, self.timeout, self.no_output_timeout)?;
348+
builder.run(
349+
workspace,
350+
self.timeout,
351+
self.no_output_timeout,
352+
self.process_lines,
353+
)?;
349354
Ok(ProcessOutput {
350355
stdout: Vec::new(),
351356
stderr: Vec::new(),

src/cmd/sandbox.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ impl SandboxBuilder {
262262
workspace: &Workspace,
263263
timeout: Option<Duration>,
264264
no_output_timeout: Option<Duration>,
265+
process_lines: Option<&mut dyn FnMut(&str)>,
265266
) -> Result<(), Error> {
266267
let container = self.create(workspace)?;
267268

@@ -276,7 +277,7 @@ impl SandboxBuilder {
276277
}
277278
}}
278279

279-
container.run(timeout, no_output_timeout)?;
280+
container.run(timeout, no_output_timeout, process_lines)?;
280281
Ok(())
281282
}
282283
}
@@ -323,12 +324,18 @@ impl Container<'_> {
323324
&self,
324325
timeout: Option<Duration>,
325326
no_output_timeout: Option<Duration>,
327+
process_lines: Option<&mut dyn FnMut(&str)>,
326328
) -> Result<(), Error> {
327-
let res = Command::new(self.workspace, "docker")
329+
let mut cmd = Command::new(self.workspace, "docker")
328330
.args(&["start", "-a", &self.id])
329331
.timeout(timeout)
330-
.no_output_timeout(no_output_timeout)
331-
.run();
332+
.no_output_timeout(no_output_timeout);
333+
334+
if let Some(f) = process_lines {
335+
cmd = cmd.process_lines(f);
336+
}
337+
338+
let res = cmd.run();
332339
let details = self.inspect()?;
333340

334341
// Return a different error if the container was killed due to an OOM

tests/buildtest/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ fn test_hello_world() {
2323
});
2424
}
2525

26+
#[test]
27+
fn test_process_lines() {
28+
runner::run("hello-world", |run| {
29+
run.build(SandboxBuilder::new().enable_networking(false), |build| {
30+
let storage = rustwide::logging::LogStorage::new(LevelFilter::Info);
31+
let mut ex = false;
32+
rustwide::logging::capture(&storage, || -> Result<_, Error> {
33+
build
34+
.cargo()
35+
.process_lines(&mut |line: &str| {
36+
if line.contains("Hello, world!") {
37+
ex = true;
38+
}
39+
})
40+
.args(&["run"])
41+
.run()?;
42+
Ok(())
43+
})?;
44+
45+
assert!(ex);
46+
Ok(())
47+
})?;
48+
Ok(())
49+
});
50+
}
51+
2652
#[test]
2753
#[cfg(not(windows))]
2854
fn test_sandbox_oom() {

0 commit comments

Comments
 (0)