From 131e0f8fec6bbce21dc66fbdd5f2140834199cf2 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 5 Dec 2021 11:27:51 +0200 Subject: fix: make first scenario work Sponsored-by: author --- Cargo.lock | 2 ++ Cargo.toml | 3 +++ build.rs | 12 ++++++++++-- obnam-benchmark.md | 17 ++++++++--------- src/bin/obnam-benchmark.rs | 39 +++++++++++++++++++++++++++++++-------- subplot/benchmark.rs | 31 +++++++++++++++++++++++++++++-- subplot/benchmark.yaml | 5 +++++ tests/obnam-benchmark.rs | 1 + 8 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 tests/obnam-benchmark.rs diff --git a/Cargo.lock b/Cargo.lock index 922b71e..c6da964 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -578,11 +578,13 @@ dependencies = [ name = "obnam-benchmark" version = "0.1.0" dependencies = [ + "anyhow", "fehler", "glob", "serde", "serde_json", "serde_yaml", + "structopt", "subplot-build", "subplotlib", ] diff --git a/Cargo.toml b/Cargo.toml index cfded29..9f1e1ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,11 +7,14 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.51" serde = { version = "1.0.101", features = ["derive"] } serde_json = "1.0.72" serde_yaml = "0.8.21" +structopt = "0.3.25" [build-dependencies] +anyhow = "1.0.51" glob = "0.3.0" subplot-build = "0.1.0" diff --git a/build.rs b/build.rs index 0f5c264..1d7729e 100644 --- a/build.rs +++ b/build.rs @@ -2,17 +2,25 @@ use glob::glob; use std::path::Path; fn main() { + if let Err(err) = real_main() { + eprintln!("ERROR: {}", err); + std::process::exit(1); + } +} + +fn real_main() -> anyhow::Result<()> { println!("cargo:rerun-if-env-changed=DEB_BUILD_OPTIONS"); let subplots = glob("[a-z]*.md").expect("failed to find subplots"); let tests = Path::new("tests"); for entry in subplots { - let entry = entry.expect("failed to get subplot dir entry"); + let entry = entry?; 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"); + subplot_build::codegen(Path::new(&entry))?; } + Ok(()) } diff --git a/obnam-benchmark.md b/obnam-benchmark.md index cf00fc4..e80ae91 100644 --- a/obnam-benchmark.md +++ b/obnam-benchmark.md @@ -39,20 +39,20 @@ how to prepare the test data for each backup generation. Example: ```{.yaml .numberLines} - benchmark: maildir backups: - - change: + - changes: - create: files: 100000 file_size: 0 - rename: files: 1000 - - change: [] + - changes: [] - benchmark: video-footage backups: - - change: + - changes: - create: files: 1000 filee_size: 1G - - change: [] + - changes: [] ``` The example above specifies two benchmarks: "maildir", and @@ -88,12 +88,11 @@ correctly. ```scenario given an installed Rust program obnam-benchmark given file spec.yaml -when I run obnam-benchmark dump-spec spec.yaml -then stdout, as JSON, matches spec.json - +given file expected.json +when I run obnam-benchmark spec.yaml --output spec.json +then JSON files spec.yaml and expected.json match ``` - ```{#spec.yaml .yaml .file .numberLines} benchmarks: - benchmark: foo @@ -101,7 +100,7 @@ benchmarks: - changes: [] ``` -```{#spec.json .yaml .file .numberLines} +```{#expected.json .yaml .file .numberLines} { "benchmarks": [ { diff --git a/src/bin/obnam-benchmark.rs b/src/bin/obnam-benchmark.rs index 4f2a9ed..617a0a3 100644 --- a/src/bin/obnam-benchmark.rs +++ b/src/bin/obnam-benchmark.rs @@ -1,12 +1,35 @@ use obnam_benchmark::Specification; +use std::fs::File; +use std::path::PathBuf; +use std::process::exit; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +struct Opt { + #[structopt(parse(from_os_str))] + spec: PathBuf, + + #[structopt(long, parse(from_os_str))] + output: PathBuf, +} fn main() { - let yaml = r#" -benchmarks: -- benchmark: foo - backups: [] -"#; - let spec: Specification = serde_yaml::from_str(yaml).unwrap(); - serde_json::to_writer(std::io::stdout(), &spec).unwrap(); - println!(); + if let Err(err) = real_main() { + eprintln!("ERROR: {}", err); + exit(1); + } +} + +fn real_main() -> anyhow::Result<()> { + let opt = Opt::from_args(); + + println!("reading {}", opt.spec.display()); + let input = File::open(&opt.spec)?; + let spec: Specification = serde_yaml::from_reader(&input)?; + + println!("writing {}", opt.output.display()); + let output = File::create(&opt.output)?; + serde_json::to_writer(&output, &spec)?; + + Ok(()) } diff --git a/subplot/benchmark.rs b/subplot/benchmark.rs index 1a64ecc..7ea4a94 100644 --- a/subplot/benchmark.rs +++ b/subplot/benchmark.rs @@ -1,8 +1,10 @@ // Implementations of Subplot scenario steps for sshca.md. -use subplotlib::steplibrary::runcmd::Runcmd; - +use serde_json::value::Value; +use std::fs::File; use std::path::Path; +use subplotlib::steplibrary::datadir::Datadir; +use subplotlib::steplibrary::runcmd::Runcmd; #[derive(Default)] struct SubplotContext {} @@ -24,3 +26,28 @@ fn install_rust_program(context: &ScenarioContext) { false, )?; } + +#[step] +#[context(Datadir)] +#[context(SubplotContext)] +fn json_files_match(context: &ScenarioContext, first: &str, second: &str) { + context.with( + |context: &Datadir| { + let first = context.canonicalise_filename(&first).expect("can't use first filename"); + let first = File::open(&first)?; + let first: Value = serde_json::from_reader(&first)?; + + let second = context.canonicalise_filename(&second).expect("can't use second filename"); + let second = File::open(&second)?; + let second: Value = serde_json::from_reader(&second)?; + + if first != second { + eprintln!("JSON files differ:\n{:#?}\n\n{:#?}\n", first, second,); + panic!("ERROR: JSON files differ"); + }; + + Ok(()) + }, + false, + ); +} diff --git a/subplot/benchmark.yaml b/subplot/benchmark.yaml index 079abd2..b0e6727 100644 --- a/subplot/benchmark.yaml +++ b/subplot/benchmark.yaml @@ -2,3 +2,8 @@ impl: rust: function: install_rust_program + +- then: JSON files {first} and {second} match + impl: + rust: + function: json_files_match diff --git a/tests/obnam-benchmark.rs b/tests/obnam-benchmark.rs new file mode 100644 index 0000000..b6200da --- /dev/null +++ b/tests/obnam-benchmark.rs @@ -0,0 +1 @@ +include!(concat!(env!("OUT_DIR"), "/obnam-benchmark.rs")); -- cgit v1.2.1