Skip to content

Commit

Permalink
test reading from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 10, 2020
1 parent da2260d commit cea1250
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
12 changes: 7 additions & 5 deletions test-cargo-miri/run-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ def cargo_miri(cmd):
args += ["--target", os.environ['MIRI_TEST_TARGET']]
return args

def test(name, cmd, stdout_ref, stderr_ref):
def test(name, cmd, stdout_ref, stderr_ref, stdin=b''):
print("==> Testing `{}` <==".format(name))
## Call `cargo miri`, capture all output
p = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
(stdout, stderr) = p.communicate()
(stdout, stderr) = p.communicate(input=stdin)
stdout = stdout.decode("UTF-8")
stderr = stderr.decode("UTF-8")
# Show output
Expand All @@ -47,12 +48,13 @@ def test(name, cmd, stdout_ref, stderr_ref):

def test_cargo_miri_run():
test("cargo miri run",
cargo_miri("run"),
"stdout.ref", "stderr.ref"
cargo_miri("run") + ["--", "-Zmiri-disable-isolation"],
"stdout.ref", "stderr.ref",
stdin=b'12\n21\n'
)
test("cargo miri run (with arguments)",
cargo_miri("run") + ["--", "--", "hello world", '"hello world"'],
"stdout.ref", "stderr.ref2"
"stdout.ref2", "stderr.ref2"
)

def test_cargo_miri_test():
Expand Down
9 changes: 9 additions & 0 deletions test-cargo-miri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use byteorder::{BigEndian, ByteOrder};
use std::io::{self, BufRead};

fn main() {
// Exercise external crate, printing to stdout.
Expand All @@ -11,6 +12,14 @@ fn main() {
for arg in std::env::args() {
eprintln!("{}", arg);
}

// If there were no arguments, access stdin.
if std::env::args().len() <= 1 {
for line in io::stdin().lock().lines() {
let num: i32 = line.unwrap().parse().unwrap();
println!("{}", 2*num);
}
}
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions test-cargo-miri/stdout.ref
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
0x01020304
24
42
1 change: 1 addition & 0 deletions test-cargo-miri/stdout.ref2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x01020304

0 comments on commit cea1250

Please sign in to comment.