diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-11-27 09:17:58 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-11-27 14:10:35 +0200 |
commit | 99c0d2c80a476e13866cbd74b6e35ff95a1e5d58 (patch) | |
tree | 62249bc5c5a3b6a47bffc723dc21849f8f83689f /subplotlib | |
parent | e3a8468282771369b5fb37be5c6e23c1e2833433 (diff) | |
download | subplot-99c0d2c80a476e13866cbd74b6e35ff95a1e5d58.tar.gz |
refactor: use build.rs for testing subplotlib subplots
A bit of dog-fooding: use subplot-build to test subplotlib.
Add a subplotlib/build.rs script to generate test code in Rust for the
subplotlib/*.md subplots. The generated code is included in
tests/*.rs, and run with "cargo test". Drop the same functionality
from the ./check script..
Sponsored-by: author
Diffstat (limited to 'subplotlib')
-rw-r--r-- | subplotlib/Cargo.toml | 5 | ||||
-rw-r--r-- | subplotlib/build.rs | 30 | ||||
-rw-r--r-- | subplotlib/tests/files.rs | 417 | ||||
-rw-r--r-- | subplotlib/tests/runcmd.rs | 640 | ||||
-rw-r--r-- | subplotlib/tests/subplotlib.rs | 194 |
5 files changed, 38 insertions, 1248 deletions
diff --git a/subplotlib/Cargo.toml b/subplotlib/Cargo.toml index 465f0de..781a017 100644 --- a/subplotlib/Cargo.toml +++ b/subplotlib/Cargo.toml @@ -28,3 +28,8 @@ regex = "1.4" shell-words = "1.0" unescape = "0.1" remove_dir_all = "0.7" + + +[build-dependencies] +glob = "0.3" +subplot-build = { version="0.1", path = "../subplot-build" } diff --git a/subplotlib/build.rs b/subplotlib/build.rs new file mode 100644 index 0000000..5f94883 --- /dev/null +++ b/subplotlib/build.rs @@ -0,0 +1,30 @@ +// Build script for subplotlib. +// +// We use the `subplot_build` crate to generate a Rust test code file +// with functions for the scenarios for each subplot file +// (subplot/*.md in the source tree). The generated file is written to +// the Cargo target directory. For each subplot foo.md there should be +// a tests/foo.rs in the source tree, which includes the generated +// file from the Cargo target tree. The source file should look like this: +// +// ```rust +// include!(concat!(env!("OUT_DIR"), "/foo.rs")); +// ``` + +use glob::glob; +use std::path::Path; + +fn main() { + let subplots = glob("*.md").expect("failed to find subplots in subplotlib"); + let tests = Path::new("tests"); + for entry in subplots { + let entry = entry.expect("failed to get subplot dir entry in subplotlib"); + let mut inc = tests.join(&entry); + inc.set_extension("rs"); + if !inc.exists() { + panic!("missing include file: {}", inc.display()); + } + println!("cargo:rerun-if-changed={}", inc.display()); + subplot_build::codegen(Path::new(&entry)).expect("failed to generate code with Subplot"); + } +} diff --git a/subplotlib/tests/files.rs b/subplotlib/tests/files.rs index a8fce3d..b80b04c 100644 --- a/subplotlib/tests/files.rs +++ b/subplotlib/tests/files.rs @@ -1,416 +1 @@ -use subplotlib::prelude::*; - -// -------------------------------- - -lazy_static! { - static ref SUBPLOT_EMBEDDED_FILES: Vec<SubplotDataFile> = - vec![SubplotDataFile::new("aGVsbG8udHh0", "aGVsbG8sIHdvcmxkCg=="),]; -} - -// --------------------------------- - -// Create on-disk files from embedded files -#[test] -fn create_on_disk_files_from_embedded_files() { - let mut scenario = Scenario::new(&base64_decode( - "Q3JlYXRlIG9uLWRpc2sgZmlsZXMgZnJvbSBlbWJlZGRlZCBmaWxlcw==", - )); - - let step = subplotlib::steplibrary::files::create_from_embedded::Builder::default() - .embedded_file({ - use std::path::PathBuf; - // hello.txt - let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::file_exists::Builder::default() - .filename( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::file_contains::Builder::default() - .filename( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .data( - // "hello, world" - &base64_decode("aGVsbG8sIHdvcmxk"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::file_does_not_exist::Builder::default() - .filename( - // "other.txt" - &base64_decode("b3RoZXIudHh0"), - ) - .build(); - scenario.add_step(step, None); - - let step = - subplotlib::steplibrary::files::create_from_embedded_with_other_name::Builder::default() - .filename_on_disk( - // "other.txt" - &base64_decode("b3RoZXIudHh0"), - ) - .embedded_file({ - use std::path::PathBuf; - // hello.txt - let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::file_exists::Builder::default() - .filename( - // "other.txt" - &base64_decode("b3RoZXIudHh0"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::file_match::Builder::default() - .filename1( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .filename2( - // "other.txt" - &base64_decode("b3RoZXIudHh0"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::only_these_exist::Builder::default() - .filenames( - // "hello.txt, other.txt" - &base64_decode("aGVsbG8udHh0LCBvdGhlci50eHQ="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// File metadata -#[test] -fn file_metadata() { - let mut scenario = Scenario::new(&base64_decode("RmlsZSBtZXRhZGF0YQ==")); - - let step = subplotlib::steplibrary::files::create_from_embedded::Builder::default() - .embedded_file({ - use std::path::PathBuf; - // hello.txt - let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::remember_metadata::Builder::default() - .filename( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::has_remembered_metadata::Builder::default() - .filename( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::create_from_text::Builder::default() - .text( - // "yo" - &base64_decode("eW8="), - ) - .filename( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::has_different_metadata::Builder::default() - .filename( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// File modification time -#[test] -fn file_modification_time() { - let mut scenario = Scenario::new(&base64_decode("RmlsZSBtb2RpZmljYXRpb24gdGltZQ==")); - - let step = subplotlib::steplibrary::files::touch_with_timestamp::Builder::default() - .filename( - // "foo.dat" - &base64_decode("Zm9vLmRhdA=="), - ) - .mtime( - // "1970-01-02 03:04:05" - &base64_decode("MTk3MC0wMS0wMiAwMzowNDowNQ=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::mtime_is_ancient::Builder::default() - .filename( - // "foo.dat" - &base64_decode("Zm9vLmRhdA=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::touch::Builder::default() - .filename( - // "foo.dat" - &base64_decode("Zm9vLmRhdA=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::mtime_is_recent::Builder::default() - .filename( - // "foo.dat" - &base64_decode("Zm9vLmRhdA=="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// File contents -#[test] -fn file_contents() { - let mut scenario = Scenario::new(&base64_decode("RmlsZSBjb250ZW50cw==")); - - let step = subplotlib::steplibrary::files::create_from_embedded::Builder::default() - .embedded_file({ - use std::path::PathBuf; - // hello.txt - let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::file_contains::Builder::default() - .filename( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .data( - // "hello, world" - &base64_decode("aGVsbG8sIHdvcmxk"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::file_matches_regex::Builder::default() - .filename( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .regex( - // "hello, .*" - &base64_decode("aGVsbG8sIC4q"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::file_matches_regex::Builder::default() - .filename( - // "hello.txt" - &base64_decode("aGVsbG8udHh0"), - ) - .regex( - // "hello, .*" - &base64_decode("aGVsbG8sIC4q"), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Directories -#[test] -fn directories() { - let mut scenario = Scenario::new(&base64_decode("RGlyZWN0b3JpZXM=")); - - let step = subplotlib::steplibrary::files::make_directory::Builder::default() - .path( - // "first" - &base64_decode("Zmlyc3Q="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_exists::Builder::default() - .path( - // "first" - &base64_decode("Zmlyc3Q="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_is_empty::Builder::default() - .path( - // "first" - &base64_decode("Zmlyc3Q="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_does_not_exist::Builder::default() - .path( - // "second" - &base64_decode("c2Vjb25k"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::remove_directory::Builder::default() - .path( - // "first" - &base64_decode("Zmlyc3Q="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_does_not_exist::Builder::default() - .path( - // "first" - &base64_decode("Zmlyc3Q="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::make_directory::Builder::default() - .path( - // "second" - &base64_decode("c2Vjb25k"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_exists::Builder::default() - .path( - // "second" - &base64_decode("c2Vjb25k"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_is_empty::Builder::default() - .path( - // "second" - &base64_decode("c2Vjb25k"), - ) - .build(); - scenario.add_step(step, None); - - let step = - subplotlib::steplibrary::files::create_from_embedded_with_other_name::Builder::default() - .filename_on_disk( - // "second/third/hello.txt" - &base64_decode("c2Vjb25kL3RoaXJkL2hlbGxvLnR4dA=="), - ) - .embedded_file({ - use std::path::PathBuf; - // hello.txt - let target_name: PathBuf = base64_decode("aGVsbG8udHh0").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_is_not_empty::Builder::default() - .path( - // "second" - &base64_decode("c2Vjb25k"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_exists::Builder::default() - .path( - // "second/third" - &base64_decode("c2Vjb25kL3RoaXJk"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_is_not_empty::Builder::default() - .path( - // "second/third" - &base64_decode("c2Vjb25kL3RoaXJk"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::remove_directory::Builder::default() - .path( - // "second" - &base64_decode("c2Vjb25k"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::files::path_does_not_exist::Builder::default() - .path( - // "second" - &base64_decode("c2Vjb25k"), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} +include!(concat!(env!("OUT_DIR"), "/files.rs")); diff --git a/subplotlib/tests/runcmd.rs b/subplotlib/tests/runcmd.rs index 0c677ba..b94f9b7 100644 --- a/subplotlib/tests/runcmd.rs +++ b/subplotlib/tests/runcmd.rs @@ -1,639 +1 @@ -use subplotlib::prelude::*; - -// -------------------------------- - -lazy_static! { - static ref SUBPLOT_EMBEDDED_FILES: Vec<SubplotDataFile> = vec![SubplotDataFile::new( - "ZXJyLnNo", - "IyEvYmluL3NoCmVjaG8gIiRAIiAxPiYyCg==" - ),]; -} - -// --------------------------------- - -// Successful execution -#[test] -fn successful_execution() { - let mut scenario = Scenario::new(&base64_decode("U3VjY2Vzc2Z1bCBleGVjdXRpb24=")); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "true" - &base64_decode("dHJ1ZQ=="), - ) - .args( - // "" - &base64_decode(""), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::exit_code_is::Builder::default() - .exit(0) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::exit_code_is_zero::Builder::default().build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Successful execution in a sub-directory -#[test] -fn successful_execution_in_a_sub_directory() { - let mut scenario = Scenario::new(&base64_decode( - "U3VjY2Vzc2Z1bCBleGVjdXRpb24gaW4gYSBzdWItZGlyZWN0b3J5", - )); - - let step = subplotlib::steplibrary::files::make_directory::Builder::default() - .path( - // "xyzzy" - &base64_decode("eHl6enk="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::run_in::Builder::default() - .dirname( - // "xyzzy" - &base64_decode("eHl6enk="), - ) - .argv0( - // "pwd" - &base64_decode("cHdk"), - ) - .args( - // "" - &base64_decode(""), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::exit_code_is::Builder::default() - .exit(0) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::exit_code_is_zero::Builder::default().build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stdout_contains::Builder::default() - .text( - // "/xyzzy" - &base64_decode("L3h5enp5"), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Failed execution -#[test] -fn failed_execution() { - let mut scenario = Scenario::new(&base64_decode("RmFpbGVkIGV4ZWN1dGlvbg==")); - - let step = subplotlib::steplibrary::runcmd::try_to_run::Builder::default() - .argv0( - // "false" - &base64_decode("ZmFsc2U="), - ) - .args( - // "" - &base64_decode(""), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::exit_code_is_not::Builder::default() - .exit(0) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::exit_code_is_nonzero::Builder::default().build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Failed execution in a sub-directory -#[test] -fn failed_execution_in_a_sub_directory() { - let mut scenario = Scenario::new(&base64_decode( - "RmFpbGVkIGV4ZWN1dGlvbiBpbiBhIHN1Yi1kaXJlY3Rvcnk=", - )); - - let step = subplotlib::steplibrary::files::make_directory::Builder::default() - .path( - // "xyzzy" - &base64_decode("eHl6enk="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::try_to_run_in::Builder::default() - .dirname( - // "xyzzy" - &base64_decode("eHl6enk="), - ) - .argv0( - // "false" - &base64_decode("ZmFsc2U="), - ) - .args( - // "" - &base64_decode(""), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::exit_code_is_not::Builder::default() - .exit(0) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::exit_code_is_nonzero::Builder::default().build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stdout is exactly as wanted -#[test] -fn check_stdout_is_exactly_as_wanted() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3Rkb3V0IGlzIGV4YWN0bHkgYXMgd2FudGVk", - )); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "echo" - &base64_decode("ZWNobw=="), - ) - .args( - // " hello, world" - &base64_decode("IGhlbGxvLCB3b3JsZA=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stdout_is::Builder::default() - .text( - // "hello, world\n" - &base64_decode("aGVsbG8sIHdvcmxkXG4="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stderr is exactly as wanted -#[test] -fn check_stderr_is_exactly_as_wanted() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3RkZXJyIGlzIGV4YWN0bHkgYXMgd2FudGVk", - )); - - let step = subplotlib::steplibrary::runcmd::helper_script::Builder::default() - .script({ - use std::path::PathBuf; - // err.sh - let target_name: PathBuf = base64_decode("ZXJyLnNo").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "sh" - &base64_decode("c2g="), - ) - .args( - // " err.sh hello, world" - &base64_decode("IGVyci5zaCBoZWxsbywgd29ybGQ="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stderr_is::Builder::default() - .text( - // "hello, world\n" - &base64_decode("aGVsbG8sIHdvcmxkXG4="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stdout using sub-string search -#[test] -fn check_stdout_using_sub_string_search() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3Rkb3V0IHVzaW5nIHN1Yi1zdHJpbmcgc2VhcmNo", - )); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "echo" - &base64_decode("ZWNobw=="), - ) - .args( - // " hello, world" - &base64_decode("IGhlbGxvLCB3b3JsZA=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stdout_contains::Builder::default() - .text( - // "world\n" - &base64_decode("d29ybGRcbg=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::exit_code_is::Builder::default() - .exit(0) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stderr using sub-string search -#[test] -fn check_stderr_using_sub_string_search() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3RkZXJyIHVzaW5nIHN1Yi1zdHJpbmcgc2VhcmNo", - )); - - let step = subplotlib::steplibrary::runcmd::helper_script::Builder::default() - .script({ - use std::path::PathBuf; - // err.sh - let target_name: PathBuf = base64_decode("ZXJyLnNo").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "sh" - &base64_decode("c2g="), - ) - .args( - // " err.sh hello, world" - &base64_decode("IGVyci5zaCBoZWxsbywgd29ybGQ="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stderr_contains::Builder::default() - .text( - // "world\n" - &base64_decode("d29ybGRcbg=="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stdout using regular expressions -#[test] -fn check_stdout_using_regular_expressions() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3Rkb3V0IHVzaW5nIHJlZ3VsYXIgZXhwcmVzc2lvbnM=", - )); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "echo" - &base64_decode("ZWNobw=="), - ) - .args( - // " hello, world" - &base64_decode("IGhlbGxvLCB3b3JsZA=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stdout_matches_regex::Builder::default() - .regex( - // "world$" - &base64_decode("d29ybGQk"), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stderr using regular expressions -#[test] -fn check_stderr_using_regular_expressions() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3RkZXJyIHVzaW5nIHJlZ3VsYXIgZXhwcmVzc2lvbnM=", - )); - - let step = subplotlib::steplibrary::runcmd::helper_script::Builder::default() - .script({ - use std::path::PathBuf; - // err.sh - let target_name: PathBuf = base64_decode("ZXJyLnNo").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "sh" - &base64_decode("c2g="), - ) - .args( - // " err.sh hello, world" - &base64_decode("IGVyci5zaCBoZWxsbywgd29ybGQ="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stderr_matches_regex::Builder::default() - .regex( - // "world$" - &base64_decode("d29ybGQk"), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stdout is not exactly something -#[test] -fn check_stdout_is_not_exactly_something() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3Rkb3V0IGlzIG5vdCBleGFjdGx5IHNvbWV0aGluZw==", - )); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "echo" - &base64_decode("ZWNobw=="), - ) - .args( - // " hi" - &base64_decode("IGhp"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stdout_isnt::Builder::default() - .text( - // "hello, world\n" - &base64_decode("aGVsbG8sIHdvcmxkXG4="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stderr is not exactly something -#[test] -fn check_stderr_is_not_exactly_something() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3RkZXJyIGlzIG5vdCBleGFjdGx5IHNvbWV0aGluZw==", - )); - - let step = subplotlib::steplibrary::runcmd::helper_script::Builder::default() - .script({ - use std::path::PathBuf; - // err.sh - let target_name: PathBuf = base64_decode("ZXJyLnNo").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "sh" - &base64_decode("c2g="), - ) - .args( - // " err.sh hi" - &base64_decode("IGVyci5zaCBoaQ=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stderr_isnt::Builder::default() - .text( - // "hello, world\n" - &base64_decode("aGVsbG8sIHdvcmxkXG4="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stdout doesn’t contain sub-string -#[test] -fn check_stdout_doesn_t_contain_sub_string() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3Rkb3V0IGRvZXNu4oCZdCBjb250YWluIHN1Yi1zdHJpbmc=", - )); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "echo" - &base64_decode("ZWNobw=="), - ) - .args( - // " hi" - &base64_decode("IGhp"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stdout_doesnt_contain::Builder::default() - .text( - // "world" - &base64_decode("d29ybGQ="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stderr doesn’t contain sub-string -#[test] -fn check_stderr_doesn_t_contain_sub_string() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3RkZXJyIGRvZXNu4oCZdCBjb250YWluIHN1Yi1zdHJpbmc=", - )); - - let step = subplotlib::steplibrary::runcmd::helper_script::Builder::default() - .script({ - use std::path::PathBuf; - // err.sh - let target_name: PathBuf = base64_decode("ZXJyLnNo").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "sh" - &base64_decode("c2g="), - ) - .args( - // " err.sh hi" - &base64_decode("IGVyci5zaCBoaQ=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stderr_doesnt_contain::Builder::default() - .text( - // "world" - &base64_decode("d29ybGQ="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stdout doesn’t match regular expression -#[test] -fn check_stdout_doesn_t_match_regular_expression() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3Rkb3V0IGRvZXNu4oCZdCBtYXRjaCByZWd1bGFyIGV4cHJlc3Npb24=", - )); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "echo" - &base64_decode("ZWNobw=="), - ) - .args( - // " hi" - &base64_decode("IGhp"), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stdout_doesnt_match_regex::Builder::default() - .regex( - // "world$" - &base64_decode("d29ybGQk"), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Check stderr doesn’t match regular expressions -#[test] -fn check_stderr_doesn_t_match_regular_expressions() { - let mut scenario = Scenario::new(&base64_decode( - "Q2hlY2sgc3RkZXJyIGRvZXNu4oCZdCBtYXRjaCByZWd1bGFyIGV4cHJlc3Npb25z", - )); - - let step = subplotlib::steplibrary::runcmd::helper_script::Builder::default() - .script({ - use std::path::PathBuf; - // err.sh - let target_name: PathBuf = base64_decode("ZXJyLnNo").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::run::Builder::default() - .argv0( - // "sh" - &base64_decode("c2g="), - ) - .args( - // " err.sh hi" - &base64_decode("IGVyci5zaCBoaQ=="), - ) - .build(); - scenario.add_step(step, None); - - let step = subplotlib::steplibrary::runcmd::stderr_doesnt_match_regex::Builder::default() - .regex( - // "world$" - &base64_decode("d29ybGQk"), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} +include!(concat!(env!("OUT_DIR"), "/runcmd.rs")); diff --git a/subplotlib/tests/subplotlib.rs b/subplotlib/tests/subplotlib.rs index cc2cf23..1738748 100644 --- a/subplotlib/tests/subplotlib.rs +++ b/subplotlib/tests/subplotlib.rs @@ -1,193 +1 @@ -use subplotlib::prelude::*; - -// -------------------------------- -// This came from helpers/subplotlib_context.rs - -use std::collections::HashMap; - -struct Context { - counter: usize, - files: HashMap<String, SubplotDataFile>, - this_file: Option<SubplotDataFile>, -} - -impl Default for Context { - fn default() -> Self { - Self { - counter: 0, - files: HashMap::new(), - this_file: None, - } - } -} - -impl Context { - fn remember_file(&mut self, name: &str, content: SubplotDataFile) { - self.files.insert(name.to_string(), content); - } -} - -impl ContextElement for Context { - // An empty implementation is sufficient for now -} - -// -------------------------------- -// This came from helpers/subplotlib_impl.rs - -#[step] -fn a_trivial_setup(context: &mut Context, initial: usize) { - context.counter = initial; -} - -#[step] -fn a_trivial_cleanup(context: &mut Context, _initial: usize) {} - -#[step] -fn increment_counter(context: &mut Context) { - context.counter += 1; -} - -#[step] -fn internal_check_counter(context: &Context, num: usize) { - if context.counter != num { - throw!(format!( - "Counter was wrong, it was {} but {} was expected", - context.counter, num - )); - } -} - -#[step] -fn check_counter(context: &ScenarioContext, num: usize) { - internal_check_counter::call(context, num)?; -} - -#[step] -fn acquire_file_content(context: &mut Context, somename: &str, file: SubplotDataFile) { - context.remember_file(somename, file); -} - -#[step] -fn remember_target(context: &mut Context, somename: &str) { - if let Some(file) = context.files.get(somename) { - context.this_file = Some(file.clone()); - } else { - throw!(format!("Unknown file {}", somename)); - } -} - -#[step] -fn check_contents(context: &mut Context, text: &str) { - if let Some(file) = context.this_file.as_ref() { - let body_as_text = String::from_utf8_lossy(file.data()); - if !body_as_text.as_ref().contains(text) { - throw!(format!( - "Failed to find {} when looking at {}", - text, - file.name().display() - )); - } - } else { - throw!("Not looking at a file"); - } -} - -// -------------------------------- - -lazy_static! { - static ref SUBPLOT_EMBEDDED_FILES: Vec<SubplotDataFile> = vec![SubplotDataFile::new( - "ZXhhbXBsZS50eHQ=", - "VGhpcyBkYXRhIGZpbGUgd2lsbCBiZSBlbWJlZGRlZCBpbnRvIHRoZSB0ZXN0IHN1aXRlCg==" - ),]; -} - -// --------------------------------- - -// Fundamentals -#[test] -fn fundamentals() { - let mut scenario = Scenario::new(&base64_decode("RnVuZGFtZW50YWxz")); - - let step = a_trivial_setup::Builder::default().initial(0).build(); - let cleanup = a_trivial_cleanup::Builder::default().initial(0).build(); - scenario.add_step(step, Some(cleanup)); - - let step = increment_counter::Builder::default().build(); - scenario.add_step(step, None); - - let step = check_counter::Builder::default().num(1).build(); - scenario.add_step(step, None); - - let step = increment_counter::Builder::default().build(); - scenario.add_step(step, None); - - let step = check_counter::Builder::default().num(2).build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Embedded files -#[test] -fn embedded_files() { - let mut scenario = Scenario::new(&base64_decode("RW1iZWRkZWQgZmlsZXM=")); - - let step = acquire_file_content::Builder::default() - .file({ - use std::path::PathBuf; - // example.txt - let target_name: PathBuf = base64_decode("ZXhhbXBsZS50eHQ=").into(); - SUBPLOT_EMBEDDED_FILES - .iter() - .find(|df| df.name() == target_name) - .expect("Unable to find file at runtime") - .clone() - }) - .somename( - // "EXAMPLE" - &base64_decode("RVhBTVBMRQ=="), - ) - .build(); - scenario.add_step(step, None); - - let step = remember_target::Builder::default() - .somename( - // "EXAMPLE" - &base64_decode("RVhBTVBMRQ=="), - ) - .build(); - scenario.add_step(step, None); - - let step = check_contents::Builder::default() - .text( - // "will be embedded" - &base64_decode("d2lsbCBiZSBlbWJlZGRlZA=="), - ) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} - -// --------------------------------- - -// Data directory -#[test] -fn data_directory() { - let mut scenario = Scenario::new(&base64_decode("RGF0YSBkaXJlY3Rvcnk=")); - - let step = subplotlib::steplibrary::datadir::datadir_has_enough_space::Builder::default() - .bytes(1024000) - .build(); - scenario.add_step(step, None); - - let step = - subplotlib::steplibrary::datadir::datadir_has_enough_space_megabytes::Builder::default() - .megabytes(1) - .build(); - scenario.add_step(step, None); - - scenario.run().unwrap(); -} +include!(concat!(env!("OUT_DIR"), "/subplotlib.rs")); |