Add simple integration tests
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 9b328ff · Browse files · 3 files·+360 added−0 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+360 added−0 removed
- tests:+208 added−0 removed
- other:+152 added−0 removed
Provenance
- Commit
e71ff55- Tree
182aaa9- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
9b328ff
3 files changed+360 added−0 removed
added:tests/integration_tests.rs+208 added
1+use std::{fs::File, io::Read, path::PathBuf, process::Command};2+3+use assert_cmd::cargo::CommandCargoExt;4+use tempfile::{tempdir, TempDir};5+6+fn hyperfine_raw_command() -> Command {7+ let mut cmd = Command::cargo_bin("hyperfine").unwrap();8+ cmd.current_dir("tests/");9+ cmd10+}11+12+fn hyperfine() -> assert_cmd::Command {13+ assert_cmd::Command::from_std(hyperfine_raw_command())14+}15+16+#[test]17+fn hyperfine_runs_successfully() {18+ hyperfine()19+ .arg("--runs=2")20+ .arg("echo 'dummy benchmark'")21+ .assert()22+ .success();23+}24+25+#[test]26+fn at_least_two_runs_are_required() {27+ hyperfine()28+ .arg("--runs=1")29+ .arg("echo 'dummy benchmark'")30+ .assert()31+ .failure();32+}33+34+struct ExecutionOrderTest {35+ cmd: assert_cmd::Command,36+ expected_content: String,37+ logfile_path: PathBuf,38+ #[allow(dead_code)]39+ tempdir: TempDir,40+}41+42+impl ExecutionOrderTest {43+ fn new() -> Self {44+ let tempdir = tempdir().unwrap();45+ let logfile_path = tempdir.path().join("output.log");46+47+ ExecutionOrderTest {48+ cmd: hyperfine(),49+ expected_content: String::new(),50+ logfile_path,51+ tempdir,52+ }53+ }54+55+ fn arg<S: AsRef<str>>(&mut self, arg: S) -> &mut Self {56+ self.cmd.arg(arg.as_ref());57+ self58+ }59+60+ fn get_command(&self, output: &str) -> String {61+ format!(62+ "echo '{output}' >> {path}",63+ output = output,64+ path = self.logfile_path.to_string_lossy()65+ )66+ }67+68+ fn command(&mut self, output: &str) -> &mut Self {69+ self.arg(self.get_command(output));70+ self71+ }72+73+ fn prepare(&mut self, output: &str) -> &mut Self {74+ self.arg("--prepare");75+ self.command(output)76+ }77+78+ fn cleanup(&mut self, output: &str) -> &mut Self {79+ self.arg("--cleanup");80+ self.command(output)81+ }82+83+ fn expect_output(&mut self, output: &str) -> &mut Self {84+ self.expected_content.push_str(output);85+ self.expected_content.push('\n');86+ self87+ }88+89+ fn run(&mut self) {90+ self.cmd.assert().success();91+92+ let mut f = File::open(&self.logfile_path).unwrap();93+ let mut content = String::new();94+ f.read_to_string(&mut content).unwrap();95+96+ assert_eq!(content, self.expected_content);97+ }98+}99+100+impl Default for ExecutionOrderTest {101+ fn default() -> Self {102+ Self::new()103+ }104+}105+106+#[test]107+fn benchmarks_are_executed_sequentially() {108+ ExecutionOrderTest::new()109+ .arg("--runs=2")110+ .command("command 1")111+ .command("command 2")112+ .expect_output("command 1")113+ .expect_output("command 1")114+ .expect_output("command 2")115+ .expect_output("command 2")116+ .run();117+}118+119+#[test]120+fn warmup_runs_are_executed_before_benchmarking_runs() {121+ ExecutionOrderTest::new()122+ .arg("--runs=2")123+ .arg("--warmup=3")124+ .command("command 1")125+ .expect_output("command 1")126+ .expect_output("command 1")127+ .expect_output("command 1")128+ .expect_output("command 1")129+ .expect_output("command 1")130+ .run();131+}132+133+#[test]134+fn prepare_commands_are_executed_before_each_timing_run() {135+ ExecutionOrderTest::new()136+ .arg("--runs=2")137+ .prepare("prepare")138+ .command("command 1")139+ .command("command 2")140+ .expect_output("prepare")141+ .expect_output("command 1")142+ .expect_output("prepare")143+ .expect_output("command 1")144+ .expect_output("prepare")145+ .expect_output("command 2")146+ .expect_output("prepare")147+ .expect_output("command 2")148+ .run();149+}150+151+#[test]152+fn cleanup_commands_are_executed_once_after_each_benchmark() {153+ ExecutionOrderTest::new()154+ .arg("--runs=2")155+ .cleanup("cleanup")156+ .command("command 1")157+ .command("command 2")158+ .expect_output("command 1")159+ .expect_output("command 1")160+ .expect_output("cleanup")161+ .expect_output("command 2")162+ .expect_output("command 2")163+ .expect_output("cleanup")164+ .run();165+}166+167+#[test]168+fn single_parameter_value() {169+ ExecutionOrderTest::new()170+ .arg("--runs=2")171+ .arg("--parameter-list")172+ .arg("number")173+ .arg("1,2,3")174+ .command("command {number}")175+ .expect_output("command 1")176+ .expect_output("command 1")177+ .expect_output("command 2")178+ .expect_output("command 2")179+ .expect_output("command 3")180+ .expect_output("command 3")181+ .run();182+}183+184+#[test]185+fn multiple_parameter_values() {186+ ExecutionOrderTest::new()187+ .arg("--runs=2")188+ .arg("--parameter-list")189+ .arg("number")190+ .arg("1,2,3")191+ .arg("--parameter-list")192+ .arg("letter")193+ .arg("a,b")194+ .command("command {number} {letter}")195+ .expect_output("command 1 a")196+ .expect_output("command 1 a")197+ .expect_output("command 2 a")198+ .expect_output("command 2 a")199+ .expect_output("command 3 a")200+ .expect_output("command 3 a")201+ .expect_output("command 1 b")202+ .expect_output("command 1 b")203+ .expect_output("command 2 b")204+ .expect_output("command 2 b")205+ .expect_output("command 3 b")206+ .expect_output("command 3 b")207+ .run();208+}modified:Cargo.lock+149 added
1 # This file is automatically @generated by Cargo.2 # It is not intended for manual editing.3+version = 34+5 [[package]]6+name = "aho-corasick"7+version = "0.7.18"8+source = "registry+https://github.com/rust-lang/crates.io-index"9+checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"10+dependencies = [11+ "memchr",12+]13+14+[[package]]15 name = "ansi_term"16 version = "0.11.0"17 source = "registry+https://github.com/rust-lang/crates.io-index"18 checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"16 unmodified lines35 source = "registry+https://github.com/rust-lang/crates.io-index"36 checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"37 38 [[package]]39+name = "assert_cmd"40+version = "1.0.8"41+source = "registry+https://github.com/rust-lang/crates.io-index"42+checksum = "c98233c6673d8601ab23e77eb38f999c51100d46c5703b17288c57fddf3a1ffe"43+dependencies = [44+ "bstr",45+ "doc-comment",46+ "predicates",47+ "predicates-core",48+ "predicates-tree",49+ "wait-timeout",50+]51+52+[[package]]53 name = "atty"54 version = "0.2.14"55 source = "registry+https://github.com/rust-lang/crates.io-index"56 checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"116 unmodified lines173 "memchr",174 ]175 176 [[package]]177+name = "difflib"178+version = "0.4.0"179+source = "registry+https://github.com/rust-lang/crates.io-index"180+checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"181+182+[[package]]183+name = "doc-comment"184+version = "0.3.3"185+source = "registry+https://github.com/rust-lang/crates.io-index"186+checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"187+188+[[package]]189+name = "either"190+version = "1.6.1"191+source = "registry+https://github.com/rust-lang/crates.io-index"192+checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"193+194+[[package]]195 name = "encode_unicode"196 version = "0.3.6"197 source = "registry+https://github.com/rust-lang/crates.io-index"198 checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"199 200 [[package]]201+name = "float-cmp"202+version = "0.9.0"203+source = "registry+https://github.com/rust-lang/crates.io-index"204+checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"205+dependencies = [206+ "num-traits",207+]208+209+[[package]]210 name = "fuchsia-cprng"211 version = "0.1.1"212 source = "registry+https://github.com/rust-lang/crates.io-index"213 checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"22 unmodified lines236 name = "hyperfine"237 version = "1.11.0"238 dependencies = [239 "approx",240+ "assert_cmd",241 "atty",242 "cfg-if",243 "clap",244 "colored",245 "csv",246 "indicatif",247 "libc",248+ "predicates",249 "rand 0.8.4",250 "rust_decimal",251 "serde",252 "serde_json",253 "statistical",254+ "tempfile",255 "version_check",256 "winapi",257 ]258 9 unmodified lines268 "regex",269 ]270 271 [[package]]272+name = "itertools"273+version = "0.10.1"274+source = "registry+https://github.com/rust-lang/crates.io-index"275+checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"276+dependencies = [277+ "either",278+]279+280+[[package]]281 name = "itoa"282 version = "0.4.7"283 source = "registry+https://github.com/rust-lang/crates.io-index"284 checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"16 unmodified lines301 source = "registry+https://github.com/rust-lang/crates.io-index"302 checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"303 304 [[package]]305+name = "normalize-line-endings"306+version = "0.3.0"307+source = "registry+https://github.com/rust-lang/crates.io-index"308+checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"309+310+[[package]]311 name = "num"312 version = "0.2.1"313 source = "registry+https://github.com/rust-lang/crates.io-index"314 checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36"81 unmodified lines396 source = "registry+https://github.com/rust-lang/crates.io-index"397 checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"398 399 [[package]]400+name = "predicates"401+version = "2.0.2"402+source = "registry+https://github.com/rust-lang/crates.io-index"403+checksum = "c143348f141cc87aab5b950021bac6145d0e5ae754b0591de23244cee42c9308"404+dependencies = [405+ "difflib",406+ "float-cmp",407+ "itertools",408+ "normalize-line-endings",409+ "predicates-core",410+ "regex",411+]412+413+[[package]]414+name = "predicates-core"415+version = "1.0.2"416+source = "registry+https://github.com/rust-lang/crates.io-index"417+checksum = "57e35a3326b75e49aa85f5dc6ec15b41108cf5aee58eabb1f274dd18b73c2451"418+419+[[package]]420+name = "predicates-tree"421+version = "1.0.3"422+source = "registry+https://github.com/rust-lang/crates.io-index"423+checksum = "d7dd0fd014130206c9352efbdc92be592751b2b9274dff685348341082c6ea3d"424+dependencies = [425+ "predicates-core",426+ "treeline",427+]428+429+[[package]]430 name = "proc-macro2"431 version = "1.0.26"432 source = "registry+https://github.com/rust-lang/crates.io-index"433 checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec"165 unmodified lines599 "rand_core 0.3.1",600 ]601 602 [[package]]603+name = "redox_syscall"604+version = "0.2.10"605+source = "registry+https://github.com/rust-lang/crates.io-index"606+checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"607+dependencies = [608+ "bitflags",609+]610+611+[[package]]612 name = "regex"613 version = "1.5.4"614 source = "registry+https://github.com/rust-lang/crates.io-index"615 checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"616 dependencies = [617+ "aho-corasick",618+ "memchr",619 "regex-syntax",620 ]621 622 [[package]]11 unmodified lines634 source = "registry+https://github.com/rust-lang/crates.io-index"635 checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"636 637 [[package]]638+name = "remove_dir_all"639+version = "0.5.3"640+source = "registry+https://github.com/rust-lang/crates.io-index"641+checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"642+dependencies = [643+ "winapi",644+]645+646+[[package]]647 name = "rust_decimal"648 version = "1.15.0"649 source = "registry+https://github.com/rust-lang/crates.io-index"650 checksum = "c5446d1cf2dfe2d6367c8b27f2082bdf011e60e76fa1fcd140047f535156d6e7"67 unmodified lines718 "unicode-xid",719 ]720 721 [[package]]722+name = "tempfile"723+version = "3.2.0"724+source = "registry+https://github.com/rust-lang/crates.io-index"725+checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"726+dependencies = [727+ "cfg-if",728+ "libc",729+ "rand 0.8.4",730+ "redox_syscall",731+ "remove_dir_all",732+ "winapi",733+]734+735+[[package]]736 name = "term_size"737 version = "0.3.2"738 source = "registry+https://github.com/rust-lang/crates.io-index"739 checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9"22 unmodified lines762 "unicode-width",763 ]764 765 [[package]]766+name = "treeline"767+version = "0.1.0"768+source = "registry+https://github.com/rust-lang/crates.io-index"769+checksum = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41"770+771+[[package]]772 name = "unicode-width"773 version = "0.1.8"774 source = "registry+https://github.com/rust-lang/crates.io-index"775 checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"16 unmodified lines792 source = "registry+https://github.com/rust-lang/crates.io-index"793 checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"794 795 [[package]]796+name = "wait-timeout"797+version = "0.2.0"798+source = "registry+https://github.com/rust-lang/crates.io-index"799+checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"800+dependencies = [801+ "libc",802+]803+804+[[package]]805 name = "wasi"806 version = "0.10.2+wasi-snapshot-preview1"807 source = "registry+https://github.com/rust-lang/crates.io-index"808 checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"22 unmodified linesmodified:Cargo.toml+3 added
34 unmodified lines35363738394041424344454 unmodified lines34 unmodified linesfeatures = ["suggestions", "color", "wrap_help"]
[dev-dependencies]approx = "0.5"assert_cmd = "1.0.8"predicates = "2.0.1"tempfile = "3.2.0"
[build-dependencies]clap = "2"version_check = "0.9"4 unmodified lines