Skip to content

Commit 0e370ac

Browse files
committed
test reading from stdin
1 parent 4d42e0a commit 0e370ac

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

test-cargo-miri/run-test.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ def cargo_miri(cmd):
2121
args += ["--target", os.environ['MIRI_TEST_TARGET']]
2222
return args
2323

24-
def test(name, cmd, stdout_ref, stderr_ref, env={}):
24+
def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}):
2525
print("==> Testing `{}` <==".format(name))
2626
## Call `cargo miri`, capture all output
2727
p_env = os.environ.copy()
2828
p_env.update(env)
2929
p = subprocess.Popen(
3030
cmd,
31+
stdin=subprocess.PIPE,
3132
stdout=subprocess.PIPE,
3233
stderr=subprocess.PIPE,
3334
env=p_env,
3435
)
35-
(stdout, stderr) = p.communicate()
36+
(stdout, stderr) = p.communicate(input=stdin)
3637
stdout = stdout.decode("UTF-8")
3738
stderr = stderr.decode("UTF-8")
3839
# Show output
@@ -51,15 +52,13 @@ def test(name, cmd, stdout_ref, stderr_ref, env={}):
5152
def test_cargo_miri_run():
5253
test("cargo miri run",
5354
cargo_miri("run"),
54-
"stdout.ref", "stderr.ref"
55-
)
56-
test("cargo miri run (with target)",
57-
cargo_miri("run") + ["--bin", "cargo-miri-test"],
58-
"stdout.ref", "stderr.ref"
55+
"stdout.ref", "stderr.ref",
56+
stdin=b'12\n21\n',
57+
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
5958
)
60-
test("cargo miri run (with arguments)",
61-
cargo_miri("run") + ["--", "hello world", '"hello world"'],
62-
"stdout.ref", "stderr.ref2"
59+
test("cargo miri run (with arguments and target)",
60+
cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"'],
61+
"stdout.ref2", "stderr.ref2"
6362
)
6463

6564
def test_cargo_miri_test():

test-cargo-miri/src/main.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use byteorder::{BigEndian, ByteOrder};
2+
#[cfg(unix)]
3+
use std::io::{self, BufRead};
24

35
fn main() {
46
// Exercise external crate, printing to stdout.
@@ -11,6 +13,22 @@ fn main() {
1113
for arg in std::env::args() {
1214
eprintln!("{}", arg);
1315
}
16+
17+
// If there were no arguments, access stdin.
18+
if std::env::args().len() <= 1 {
19+
#[cfg(unix)]
20+
for line in io::stdin().lock().lines() {
21+
let num: i32 = line.unwrap().parse().unwrap();
22+
println!("{}", 2*num);
23+
}
24+
// On non-Unix, reading from stdin is not support. So we hard-code the right answer.
25+
#[cfg(not(unix))]
26+
{
27+
println!("24");
28+
println!("42");
29+
}
30+
}
31+
1432
}
1533

1634
#[cfg(test)]

test-cargo-miri/stdout.ref

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
0x01020304
2+
24
3+
42

test-cargo-miri/stdout.ref2

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0x01020304

0 commit comments

Comments
 (0)