Use insta-cmd for benchmark report snapshots
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent c8f8c4e · Browse files · 3 files·+115 added−65 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+115 added−65 removed
- tests:+81 added−64 removed
- other:+34 added−1 removed
Provenance
- Commit
a24f76c- Tree
2d55f62- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
c8f8c4e
3 files changed+115 added−65 removed
modified:tests/integration_tests.rs+81 added-64 removed
1 mod common;2 use common::{hyperfine, hyperfine_raw_command};3 4+use assert_cmd::assert::OutputAssertExt;5+use insta_cmd::assert_cmd_snapshot;6 use predicates::prelude::*;7 8 /// Platform-specific I/O utility.9 /// - On Unix-like systems, defaults to `cat`.10 /// - On Windows, uses `findstr` as an alternative.11 /// See: <https://superuser.com/questions/853580/real-windows-equivalent-to-cat-stdin>12 const STDIN_READ_COMMAND: &str = if cfg!(windows) { "findstr x*" } else { "cat" };13 12-pub fn hyperfine_debug() -> assert_cmd::Command {13- let mut cmd = hyperfine();14+pub fn hyperfine_debug() -> std::process::Command {15+ let mut cmd = hyperfine_raw_command();16 cmd.arg("--debug-mode");17 cmd18 }19 18-// Ignore trailing padding on report lines, including the space-only separators.19-// Keep indentation and internal spacing so snapshots still check the layout.20-fn stdout(output: &assert_cmd::assert::Assert) -> String {21- String::from_utf8_lossy(&output.get_output().stdout)22- .lines()23- .map(str::trim_end)24- .collect::<Vec<_>>()25- .join("\n")20+fn snapshot_settings() -> insta::Settings {21+ let mut settings = insta::Settings::clone_current();22+ // Ignore trailing report padding while preserving indentation and internal spacing.23+ settings.add_filter(r"(?m)[ \t]+$", "");24+ settings25 }26 27 #[test]28 fn runs_successfully() {573 unmodified lines602 }603 604 #[test]605 fn shows_benchmark_comparison_with_relative_times() {607- let output = hyperfine_debug()606+ let _settings = snapshot_settings().bind_to_scope();607+ assert_cmd_snapshot!(hyperfine_debug()608 .arg("--style=basic")609 .arg("sleep 1.0")610 .arg("sleep 2.0")611- .arg("sleep 3.0")612- .assert()613- .success();614-615- insta::assert_snapshot!(stdout(&output), @r"611+ .arg("sleep 3.0"), @r"612+ success: true613+ exit_code: 0614+ ----- stdout -----615 Benchmark 1: sleep 1.0616 Time (mean ± σ): 1.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]617 Range (min … max): 1.000 s … 1.000 s 10 runs618 8 unmodified lines627 Summary628 sleep 1.0 ran629 2.00 ± 0.00 times faster than sleep 2.0630 3.00 ± 0.00 times faster than sleep 3.0631+632+ ----- stderr -----633 ");634 }635 636 #[test]637 fn shows_benchmark_comparison_with_same_time() {637- let output = hyperfine_debug()638+ let _settings = snapshot_settings().bind_to_scope();639+ assert_cmd_snapshot!(hyperfine_debug()640 .arg("--style=basic")641 .arg("--command-name=A")642 .arg("--command-name=B")643 .arg("sleep 1.0")644 .arg("sleep 1.0")645 .arg("sleep 2.0")644- .arg("sleep 1000.0")645- .assert()646- .success();647-648- insta::assert_snapshot!(stdout(&output), @r"646+ .arg("sleep 1000.0"), @r"647+ success: true648+ exit_code: 0649+ ----- stdout -----650 Benchmark 1: A651 Time (mean ± σ): 1.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]652 Range (min … max): 1.000 s … 1.000 s 10 runs653 13 unmodified lines667 A ran668 As fast (1.00 ± 0.00) as B669 2.00 ± 0.00 times faster than sleep 2.0670 1000.00 ± 0.00 times faster than sleep 1000.0671+672+ ----- stderr -----673 ");674 }675 676 #[test]677 fn shows_benchmark_comparison_relative_to_reference() {675- let output = hyperfine_debug()678+ let _settings = snapshot_settings().bind_to_scope();679+ assert_cmd_snapshot!(hyperfine_debug()680 .arg("--style=basic")681 .arg("--reference=sleep 2.0")682 .arg("sleep 1.0")679- .arg("sleep 3.0")680- .assert()681- .success();682-683- insta::assert_snapshot!(stdout(&output), @r"683+ .arg("sleep 3.0"), @r"684+ success: true685+ exit_code: 0686+ ----- stdout -----687 Benchmark 1: sleep 2.0688 Time (mean ± σ): 2.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]689 Range (min … max): 2.000 s … 2.000 s 10 runs690 8 unmodified lines699 Summary700 sleep 2.0 ran701 2.00 ± 0.00 times slower than sleep 1.0702 1.50 ± 0.00 times faster than sleep 3.0703+704+ ----- stderr -----705 ");706 }707 708 #[test]709 fn shows_reference_name() {705- let output = hyperfine_debug()710+ let _settings = snapshot_settings().bind_to_scope();711+ assert_cmd_snapshot!(hyperfine_debug()712 .arg("--style=basic")713 .arg("--reference=sleep 2.0")714 .arg("--reference-name=refabc123")715 .arg("sleep 1.0")710- .arg("sleep 3.0")711- .assert()712- .success();713-714- insta::assert_snapshot!(stdout(&output), @r"716+ .arg("sleep 3.0"), @r"717+ success: true718+ exit_code: 0719+ ----- stdout -----720 Benchmark 1: refabc123721 Time (mean ± σ): 2.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]722 Range (min … max): 2.000 s … 2.000 s 10 runs723 8 unmodified lines732 Summary733 refabc123 ran734 2.00 ± 0.00 times slower than sleep 1.0735 1.50 ± 0.00 times faster than sleep 3.0736+737+ ----- stderr -----738 ");739 }740 741 #[test]742 fn performs_all_benchmarks_in_parameter_scan() {736- let output = hyperfine_debug()743+ let _settings = snapshot_settings().bind_to_scope();744+ assert_cmd_snapshot!(hyperfine_debug()745 .arg("--style=basic")746 .arg("--parameter-scan")747 .arg("time")748 .arg("30")749 .arg("45")750 .arg("--parameter-step-size")751 .arg("5")744- .arg("sleep {time}")745- .assert()746- .success();747-748- insta::assert_snapshot!(stdout(&output), @r"752+ .arg("sleep {time}"), @r"753+ success: true754+ exit_code: 0755+ ----- stdout -----756 Benchmark 1: sleep 30757 Time (mean ± σ): 30.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]758 Range (min … max): 30.000 s … 30.000 s 10 runs759 13 unmodified lines773 sleep 30 ran774 1.17 ± 0.00 times faster than sleep 35775 1.33 ± 0.00 times faster than sleep 40776 1.50 ± 0.00 times faster than sleep 45777+778+ ----- stderr -----779 ");780 }781 782 #[test]783 fn performs_reference_and_all_benchmarks_in_parameter_scan() {775- let output = hyperfine_debug()784+ let _settings = snapshot_settings().bind_to_scope();785+ assert_cmd_snapshot!(hyperfine_debug()786 .arg("--style=basic")787 .arg("--reference=sleep 25")788 .arg("--parameter-scan")789 .arg("time")790 .arg("30")791 .arg("45")792 .arg("--parameter-step-size")793 .arg("5")784- .arg("sleep {time}")785- .assert()786- .success();787-788- insta::assert_snapshot!(stdout(&output), @r"794+ .arg("sleep {time}"), @r"795+ success: true796+ exit_code: 0797+ ----- stdout -----798 Benchmark 1: sleep 25799 Time (mean ± σ): 25.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]800 Range (min … max): 25.000 s … 25.000 s 10 runs801 18 unmodified lines820 1.20 ± 0.00 times faster than sleep 30821 1.40 ± 0.00 times faster than sleep 35822 1.60 ± 0.00 times faster than sleep 40823 1.80 ± 0.00 times faster than sleep 45824+825+ ----- stderr -----826 ");827 }828 829 #[test]830 fn intermediate_results_are_not_exported_to_stdout() {820- let output = hyperfine_debug()831+ let _settings = snapshot_settings().bind_to_scope();832+ assert_cmd_snapshot!(hyperfine_debug()833 .arg("--style=none") // To only see the Markdown export on stdout834 .arg("--export-markdown")835 .arg("-")836 .arg("sleep 1")825- .arg("sleep 2")826- .assert()827- .success();837+ .arg("sleep 2"), @r"838+ success: true839+ exit_code: 0840+ ----- stdout -----841 829- insta::assert_snapshot!(stdout(&output), @r"830-842 | Command | Mean [s] | Min [s] | Max [s] | Relative |843 |:---|---:|---:|---:|---:|844 | `sleep 1` | 1.000 ± 0.000 | 1.000 | 1.000 | 1.00 |845 | `sleep 2` | 2.000 ± 0.000 | 2.000 | 2.000 | 2.00 ± 0.00 |846+847+848+ ----- stderr -----849 ");850 }851 852 #[test]34 unmodified lines887 }888 889 #[test]890 fn speed_comparison_sort_order() {891+ let _settings = snapshot_settings().bind_to_scope();892 insta::allow_duplicates! {893 for sort_order in ["auto", "mean-time"] {879- let output = hyperfine_debug()894+ assert_cmd_snapshot!(hyperfine_debug()895 .arg("--style=basic")896 .arg("sleep 2")897 .arg("sleep 1")883- .arg(format!("--sort={sort_order}"))884- .assert()885- .success();886-887- insta::assert_snapshot!(stdout(&output), @r"898+ .arg(format!("--sort={sort_order}")), @r"899+ success: true900+ exit_code: 0901+ ----- stdout -----902 Benchmark 1: sleep 2903 Time (mean ± σ): 2.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]904 Range (min … max): 2.000 s … 2.000 s 10 runs905 3 unmodified lines909 910 Summary911 sleep 1 ran912 2.00 ± 0.00 times faster than sleep 2913+914+ ----- stderr -----915 ");916 }917 }918 903- let output = hyperfine_debug()919+ assert_cmd_snapshot!(hyperfine_debug()920 .arg("--style=basic")921 .arg("sleep 2")922 .arg("sleep 1")907- .arg("--sort=command")908- .assert()909- .success();910-911- insta::assert_snapshot!(stdout(&output), @r"923+ .arg("--sort=command"), @r"924+ success: true925+ exit_code: 0926+ ----- stdout -----927 Benchmark 1: sleep 2928 Time (mean ± σ): 2.000 s ± 0.000 s [User: 0.000 s, System: 0.000 s]929 Range (min … max): 2.000 s … 2.000 s 10 runs930 3 unmodified lines934 935 Relative speed comparison936 2.00 ± 0.00 sleep 2937 1.00 sleep 1938+939+ ----- stderr -----940 ");941 }942 943 #[cfg(windows)]16 unmodified linesmodified:Cargo.lock+32 added
410 unmodified lines411 "colored",412 "csv",413 "indicatif",414 "insta",415+ "insta-cmd",416 "libc",417 "nix",418 "once_cell",419 "predicates",38 unmodified lines458 checksum = "86f0f8fee8c926415c58d6ae43a08523a26faccb2323f5e6b644fe7dd4ef6b82"459 dependencies = [460 "console 0.16.6",461 "once_cell",462+ "regex",463 "serde",464 "similar",465+ "strip-ansi-escapes",466 "tempfile",467 ]468 469 [[package]]470+name = "insta-cmd"471+version = "0.7.0"472+source = "registry+https://github.com/rust-lang/crates.io-index"473+checksum = "bffdf4af1db390cf0401535d7c1303cd079a074d28d8473b026fdb6559c41403"474+dependencies = [475+ "insta",476+ "serde",477+ "serde_json",478+]479+480+[[package]]481 name = "instant"482 version = "0.1.13"483 source = "registry+https://github.com/rust-lang/crates.io-index"484 checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"553 unmodified lines1038 "rand 0.6.5",1039 ]1040 1041 [[package]]1042+name = "strip-ansi-escapes"1043+version = "0.2.1"1044+source = "registry+https://github.com/rust-lang/crates.io-index"1045+checksum = "2a8f8038e7e7969abb3f1b7c2a811225e9296da208539e0f79c5251d6cac0025"1046+dependencies = [1047+ "vte",1048+]1049+1050+[[package]]1051 name = "strsim"1052 version = "0.11.1"1053 source = "registry+https://github.com/rust-lang/crates.io-index"1054 checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"123 unmodified lines1178 source = "registry+https://github.com/rust-lang/crates.io-index"1179 checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"1180 1181 [[package]]1182+name = "vte"1183+version = "0.14.1"1184+source = "registry+https://github.com/rust-lang/crates.io-index"1185+checksum = "231fdcd7ef3037e8330d8e17e61011a2c244126acc0a982f4040ac3f9f0bc077"1186+dependencies = [1187+ "memchr",1188+]1189+1190+[[package]]1191 name = "wait-timeout"1192 version = "0.2.1"1193 source = "registry+https://github.com/rust-lang/crates.io-index"1194 checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11"215 unmodified linesmodified:Cargo.toml+2 added-1 removed
62 unmodified lines636465666767686970717211 unmodified lines62 unmodified lines
[dev-dependencies]approx = "0.5"assert_cmd = "2.2"insta = { version = "1.48", features = ["yaml"] }insta = { version = "1.48", features = ["yaml", "filters"] }insta-cmd = "0.7"predicates = "3.1"tempfile = "3.27"
[profile.dev.package]11 unmodified lines