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 Sep 9, 2020
1 parent 4acab80 commit 495aa93
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
19 changes: 9 additions & 10 deletions test-cargo-miri/run-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ def cargo_miri(cmd):
args += ["--target", os.environ['MIRI_TEST_TARGET']]
return args

def test(name, cmd, stdout_ref, stderr_ref, env={}):
def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):
print("==> Testing `{}` <==".format(name))
## Call `cargo miri`, capture all output
p_env = os.environ.copy()
p_env.update(env)
p = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=p_env,
)
(stdout, stderr) = p.communicate()
(stdout, stderr) = p.communicate(input=stdin)
stdout = stdout.decode("UTF-8")
stderr = stderr.decode("UTF-8")
# Show output
Expand All @@ -51,15 +52,13 @@ def test(name, cmd, stdout_ref, stderr_ref, env={}):
def test_cargo_miri_run():
test("cargo miri run",
cargo_miri("run"),
"stdout.ref", "stderr.ref"
)
test("cargo miri run (with target)",
cargo_miri("run") + ["--bin", "cargo-miri-test"],
"stdout.ref", "stderr.ref"
"stdout.ref", "stderr.ref",
stdin=b'12\n21\n',
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
)
test("cargo miri run (with arguments)",
cargo_miri("run") + ["--", "hello world", '"hello world"'],
"stdout.ref", "stderr.ref2"
test("cargo miri run (with arguments and target)",
cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'],
"stdout.ref2", "stderr.ref2"
)

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

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

// If there were no arguments, access stdin.
if std::env::args().len() <= 1 {
#[cfg(unix)]
for line in io::stdin().lock().lines() {
let num: i32 = line.unwrap().parse().unwrap();
println!("{}", 2*num);
}
// On non-Unix, reading from stdin is not support. So we hard-code the right answer.
#[cfg(not(unix))]
{
println!("24");
println!("42");
}
}

}

#[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 495aa93

Please sign in to comment.