test reading from stdin

This commit is contained in:
Ralf Jung 2020-09-09 09:18:10 +02:00
parent 74fdb5cf2c
commit 504c617cd4
4 changed files with 30 additions and 10 deletions

View File

@ -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
@ -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"
"stdout.ref", "stderr.ref",
stdin=b'12\n21\n',
env={'MIRIFLAGS': "-Zmiri-disable-isolation"},
)
test("cargo miri run (with target)",
cargo_miri("run") + ["--bin", "cargo-miri-test"],
"stdout.ref", "stderr.ref"
)
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():

View File

@ -1,4 +1,6 @@
use byteorder::{BigEndian, ByteOrder};
#[cfg(unix)]
use std::io::{self, BufRead};
fn main() {
// Exercise external crate, printing to stdout.
@ -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)]

View File

@ -1 +1,3 @@
0x01020304
24
42

View File

@ -0,0 +1 @@
0x01020304