Skip to content

Migrate tests to use python scripts instead of rust test helper #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ categories = ["filesystem", "os"]
exclude = ["/.github"]

[workspace]
members = [".", "memory-tests"]
members = [".", "context-integration-tests", "memory-tests"]

[dependencies]
rustversion = "1.0.4"
Expand All @@ -28,11 +28,6 @@ unindent = "0.1.7"
# So we put in this exact dependency to force a working bitflags version.
bitflags = "=1.2.1"

[[bin]]
name = "context_integration_tests"
path = "src/context_integration_tests.rs"
required-features = ["test_executables"]

[[bin]]
name = "test_executables_helper"
path = "src/test_executables/helper.rs"
Expand All @@ -43,17 +38,9 @@ name = "test_executables_panic"
path = "src/test_executables/panic.rs"
required-features = ["test_executables"]

[target.'cfg(unix)'.dependencies.gag]
version = "0.1.10"
optional = true

[target.'cfg(unix)'.dependencies.executable-path]
version = "1.0.0"
optional = true

[target.'cfg(unix)'.dependencies.nix]
version = "0.22.2"
optional = true

[features]
test_executables = ["gag", "executable-path", "nix"]
test_executables = ["nix"]
12 changes: 12 additions & 0 deletions context-integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "context-integration-tests"
version = "0.0.0"
edition = "2018"
publish = false

[target.'cfg(unix)'.dependencies]
cradle = { path = ".." }
executable-path = "1.0.0"
gag = "0.1.10"
tempfile = "3.2.0"
unindent = "0.1.7"
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
fn main() {
#[cfg(unix)]
{
{
use cradle::prelude::*;
run!(
LogCommand,
%"cargo build --bin test_executables_helper --features test_executables",
);
}

use cradle::*;
use executable_path::executable_path;
use gag::BufferRedirect;
use std::io::{self, Read};
use std::{
fs,
io::{self, Read},
};
use tempfile::TempDir;
use unindent::Unindent;

fn with_gag<F>(mk_buf: fn() -> io::Result<BufferRedirect>, f: F) -> String
where
Expand All @@ -33,11 +29,19 @@ fn main() {
}

{
let temp_dir = TempDir::new().unwrap();
let script = temp_dir.path().join("script.py");
fs::write(
&script,
"
import sys
print('foo', file=sys.stderr)
"
.unindent(),
)
.unwrap();
assert_eq!(
with_gag(BufferRedirect::stderr, || run_output!(
executable_path("test_executables_helper").to_str().unwrap(),
"write to stderr"
)),
with_gag(BufferRedirect::stderr, || run_output!("python3", script)),
"foo\n"
);
}
Expand Down
5 changes: 1 addition & 4 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ci: test build doc clippy fmt context-integration-tests run-examples forbidden-words render-readme-check
ci: test build doc clippy fmt run-examples forbidden-words render-readme-check

build:
cargo build --all-targets --all-features --workspace
Expand All @@ -9,9 +9,6 @@ test +pattern="":
test-lib-fast +pattern="":
cargo test --lib {{ pattern }}

context-integration-tests:
cargo run --features "test_executables" --bin context_integration_tests

doc +args="":
cargo doc --workspace {{args}}

Expand Down
49 changes: 39 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ mod macros;
pub mod output;
pub mod prelude;

#[cfg(test)]
mod test_script;

include!("common_re_exports.rs.snippet");

#[cfg(test)]
Expand All @@ -245,6 +248,7 @@ mod tests {
context::Context,
input::{run_result_with_context, run_result_with_context_unit},
prelude::*,
test_script::TestScript,
};
use lazy_static::lazy_static;
use std::{
Expand Down Expand Up @@ -344,7 +348,8 @@ mod tests {
#[test]
#[should_panic(expected = "exited with exit code: 42")]
fn other_exit_codes() {
run!(test_helper(), "exit code 42");
let script = TestScript::new("import sys; sys.exit(42)");
run!(&script);
}

#[test]
Expand Down Expand Up @@ -461,7 +466,8 @@ mod tests {

#[test]
fn other_exit_codes() {
let result: Result<(), Error> = run_result!(test_helper(), "exit code 42");
let script = TestScript::new("import sys; sys.exit(42)");
let result: Result<(), Error> = run_result!(&script);
assert!(result
.unwrap_err()
.to_string()
Expand Down Expand Up @@ -802,8 +808,13 @@ mod tests {
#[test]
fn relays_stderr_by_default() {
let context = Context::test();
run_result_with_context_unit(context.clone(), (test_helper(), "write to stderr"))
.unwrap();
let script = TestScript::new(
"
import sys
print('foo', file=sys.stderr)
",
);
run_result_with_context_unit(context.clone(), &script).unwrap();
assert_eq!(context.stderr(), "foo\n");
}

Expand Down Expand Up @@ -850,7 +861,13 @@ mod tests {

#[test]
fn capture_stderr() {
let Stderr(stderr) = run_output!(test_helper(), "write to stderr");
let script = TestScript::new(
"
import sys
print('foo', file=sys.stderr)
",
);
let Stderr(stderr) = run_output!(&script);
assert_eq!(stderr, "foo\n");
}

Expand All @@ -875,9 +892,13 @@ mod tests {
#[test]
fn does_not_relay_stderr_when_catpuring() {
let context = Context::test();
let Stderr(_) =
run_result_with_context(context.clone(), (test_helper(), "write to stderr"))
.unwrap();
let script = TestScript::new(
"
import sys
print('foo', file=sys.stderr)
",
);
let Stderr(_) = run_result_with_context(context.clone(), &script).unwrap();
assert_eq!(context.stderr(), "");
}
}
Expand Down Expand Up @@ -947,7 +968,8 @@ mod tests {

#[test]
fn forty_two() {
let Status(exit_status) = run_output!(test_helper(), "exit code 42");
let script = TestScript::new("import sys; sys.exit(42)");
let Status(exit_status) = run_output!(&script);
assert!(!exit_status.success());
assert_eq!(exit_status.code(), Some(42));
}
Expand Down Expand Up @@ -1326,7 +1348,14 @@ mod tests {
#[test]
#[cfg(unix)]
fn stdin_is_closed_by_default() {
let StdoutTrimmed(output) = run_output!(test_helper(), "wait until stdin is closed");
let script = TestScript::new(
"
import sys
sys.stdin.read(-1)
print('stdin is closed')
",
);
let StdoutTrimmed(output) = run_output!(&script);
assert_eq!(output, "stdin is closed");
}

Expand Down
24 changes: 0 additions & 24 deletions src/test_executables/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ fn main() {
match args.next().unwrap().as_str() {
"invalid utf-8 stdout" => io::stdout().write_all(&[0x80]).unwrap(),
"invalid utf-8 stderr" => io::stderr().write_all(&[0x80]).unwrap(),
"exit code 42" => std::process::exit(42),
"stream chunk then wait for file" => {
println!("foo");
io::stdout().flush().unwrap();
Expand All @@ -24,9 +23,6 @@ fn main() {
println!("foo");
std::process::exit(42)
}
"write to stderr" => {
eprintln!("foo");
}
"write to stderr and exit with 42" => {
eprintln!("foo");
std::process::exit(42)
Expand All @@ -45,10 +41,6 @@ fn main() {
io::stdout().write_all(&input).unwrap();
io::stdout().flush().unwrap();
}
"wait until stdin is closed" => {
while !stdin_is_closed() {}
println!("stdin is closed");
}
"echo" => {
for variable in args {
match std::env::var(&variable).unwrap().as_str() {
Expand All @@ -60,19 +52,3 @@ fn main() {
arg => panic!("cradle_test_helper: invalid arg: {}", arg),
}
}

fn stdin_is_closed() -> bool {
#[cfg(unix)]
{
use nix::poll::{poll, PollFd, PollFlags};
let mut poll_fds = [PollFd::new(0, PollFlags::all())];
poll(&mut poll_fds, 0).unwrap();
if let Some(events) = poll_fds[0].revents() {
events.contains(PollFlags::POLLHUP)
} else {
false
}
}
#[cfg(windows)]
panic!("stdin_is_closed is not supported on windows")
}
27 changes: 27 additions & 0 deletions src/test_script.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::{config::Config, input::Input};
use std::{fs, path::PathBuf};
use tempfile::TempDir;
use unindent::Unindent;

pub(crate) struct TestScript {
temp_dir: TempDir,
}

impl TestScript {
pub(crate) fn new(code: &str) -> Self {
let temp_dir = TempDir::new().unwrap();
let result = Self { temp_dir };
fs::write(result.script_path(), code.unindent()).unwrap();
result
}

fn script_path(&self) -> PathBuf {
self.temp_dir.path().join("test-script.py")
}
}

impl Input for &TestScript {
fn configure(self, config: &mut Config) {
("python3", self.script_path()).configure(config)
}
}
9 changes: 8 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,15 @@ fn root_re_exports_everything_from_the_prelude() {

#[cfg(target_os = "linux")]
#[test]
fn memory_test() {
fn memory_tests() {
use cradle::prelude::*;
run!(%"cargo build -p memory-tests --release");
run!(%"cargo run -p memory-tests --bin run");
}

#[test]
fn context_integration_tests() {
use cradle::prelude::*;
run!(%"cargo build -p context-integration-tests --release");
run!(%"cargo run -p context-integration-tests");
}