Skip to content
Myr

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

3 files+360 added−0 removed

added:tests/integration_tests.rs+208 added
1use std::{fs::File, io::Read, path::PathBuf, process::Command};23use assert_cmd::cargo::CommandCargoExt;4use tempfile::{tempdir, TempDir};56fn hyperfine_raw_command() -> Command {7 let mut cmd = Command::cargo_bin("hyperfine").unwrap();8 cmd.current_dir("tests/");9 cmd10}1112fn hyperfine() -> assert_cmd::Command {13 assert_cmd::Command::from_std(hyperfine_raw_command())14}1516#[test]17fn hyperfine_runs_successfully() {18 hyperfine()19 .arg("--runs=2")20 .arg("echo 'dummy benchmark'")21 .assert()22 .success();23}2425#[test]26fn at_least_two_runs_are_required() {27 hyperfine()28 .arg("--runs=1")29 .arg("echo 'dummy benchmark'")30 .assert()31 .failure();32}3334struct ExecutionOrderTest {35 cmd: assert_cmd::Command,36 expected_content: String,37 logfile_path: PathBuf,38 #[allow(dead_code)]39 tempdir: TempDir,40}4142impl ExecutionOrderTest {43 fn new() -> Self {44 let tempdir = tempdir().unwrap();45 let logfile_path = tempdir.path().join("output.log");4647 ExecutionOrderTest {48 cmd: hyperfine(),49 expected_content: String::new(),50 logfile_path,51 tempdir,52 }53 }5455 fn arg<S: AsRef<str>>(&mut self, arg: S) -> &mut Self {56 self.cmd.arg(arg.as_ref());57 self58 }5960 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 }6768 fn command(&mut self, output: &str) -> &mut Self {69 self.arg(self.get_command(output));70 self71 }7273 fn prepare(&mut self, output: &str) -> &mut Self {74 self.arg("--prepare");75 self.command(output)76 }7778 fn cleanup(&mut self, output: &str) -> &mut Self {79 self.arg("--cleanup");80 self.command(output)81 }8283 fn expect_output(&mut self, output: &str) -> &mut Self {84 self.expected_content.push_str(output);85 self.expected_content.push('\n');86 self87 }8889 fn run(&mut self) {90 self.cmd.assert().success();9192 let mut f = File::open(&self.logfile_path).unwrap();93 let mut content = String::new();94 f.read_to_string(&mut content).unwrap();9596 assert_eq!(content, self.expected_content);97 }98}99100impl Default for ExecutionOrderTest {101 fn default() -> Self {102 Self::new()103 }104}105106#[test]107fn 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}118119#[test]120fn 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}132133#[test]134fn 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}150151#[test]152fn 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}166167#[test]168fn 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}183184#[test]185fn 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.3version = 345[[package]]6name = "aho-corasick"7version = "0.7.18"8source = "registry+https://github.com/rust-lang/crates.io-index"9checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"10dependencies = [11 "memchr",12]1314[[package]]15name = "ansi_term"16version = "0.11.0"17source = "registry+https://github.com/rust-lang/crates.io-index"18checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
16 unmodified lines
35source = "registry+https://github.com/rust-lang/crates.io-index"36checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"3738[[package]]39name = "assert_cmd"40version = "1.0.8"41source = "registry+https://github.com/rust-lang/crates.io-index"42checksum = "c98233c6673d8601ab23e77eb38f999c51100d46c5703b17288c57fddf3a1ffe"43dependencies = [44 "bstr",45 "doc-comment",46 "predicates",47 "predicates-core",48 "predicates-tree",49 "wait-timeout",50]5152[[package]]53name = "atty"54version = "0.2.14"55source = "registry+https://github.com/rust-lang/crates.io-index"56checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
116 unmodified lines
173 "memchr",174]175176[[package]]177name = "difflib"178version = "0.4.0"179source = "registry+https://github.com/rust-lang/crates.io-index"180checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"181182[[package]]183name = "doc-comment"184version = "0.3.3"185source = "registry+https://github.com/rust-lang/crates.io-index"186checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"187188[[package]]189name = "either"190version = "1.6.1"191source = "registry+https://github.com/rust-lang/crates.io-index"192checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"193194[[package]]195name = "encode_unicode"196version = "0.3.6"197source = "registry+https://github.com/rust-lang/crates.io-index"198checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"199200[[package]]201name = "float-cmp"202version = "0.9.0"203source = "registry+https://github.com/rust-lang/crates.io-index"204checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"205dependencies = [206 "num-traits",207]208209[[package]]210name = "fuchsia-cprng"211version = "0.1.1"212source = "registry+https://github.com/rust-lang/crates.io-index"213checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
22 unmodified lines
236name = "hyperfine"237version = "1.11.0"238dependencies = [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 lines
268 "regex",269]270271[[package]]272name = "itertools"273version = "0.10.1"274source = "registry+https://github.com/rust-lang/crates.io-index"275checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"276dependencies = [277 "either",278]279280[[package]]281name = "itoa"282version = "0.4.7"283source = "registry+https://github.com/rust-lang/crates.io-index"284checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
16 unmodified lines
301source = "registry+https://github.com/rust-lang/crates.io-index"302checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"303304[[package]]305name = "normalize-line-endings"306version = "0.3.0"307source = "registry+https://github.com/rust-lang/crates.io-index"308checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be"309310[[package]]311name = "num"312version = "0.2.1"313source = "registry+https://github.com/rust-lang/crates.io-index"314checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36"
81 unmodified lines
396source = "registry+https://github.com/rust-lang/crates.io-index"397checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"398399[[package]]400name = "predicates"401version = "2.0.2"402source = "registry+https://github.com/rust-lang/crates.io-index"403checksum = "c143348f141cc87aab5b950021bac6145d0e5ae754b0591de23244cee42c9308"404dependencies = [405 "difflib",406 "float-cmp",407 "itertools",408 "normalize-line-endings",409 "predicates-core",410 "regex",411]412413[[package]]414name = "predicates-core"415version = "1.0.2"416source = "registry+https://github.com/rust-lang/crates.io-index"417checksum = "57e35a3326b75e49aa85f5dc6ec15b41108cf5aee58eabb1f274dd18b73c2451"418419[[package]]420name = "predicates-tree"421version = "1.0.3"422source = "registry+https://github.com/rust-lang/crates.io-index"423checksum = "d7dd0fd014130206c9352efbdc92be592751b2b9274dff685348341082c6ea3d"424dependencies = [425 "predicates-core",426 "treeline",427]428429[[package]]430name = "proc-macro2"431version = "1.0.26"432source = "registry+https://github.com/rust-lang/crates.io-index"433checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec"
165 unmodified lines
599 "rand_core 0.3.1",600]601602[[package]]603name = "redox_syscall"604version = "0.2.10"605source = "registry+https://github.com/rust-lang/crates.io-index"606checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"607dependencies = [608 "bitflags",609]610611[[package]]612name = "regex"613version = "1.5.4"614source = "registry+https://github.com/rust-lang/crates.io-index"615checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"616dependencies = [617 "aho-corasick",618 "memchr",619 "regex-syntax",620]621622[[package]]
11 unmodified lines
634source = "registry+https://github.com/rust-lang/crates.io-index"635checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"636637[[package]]638name = "remove_dir_all"639version = "0.5.3"640source = "registry+https://github.com/rust-lang/crates.io-index"641checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"642dependencies = [643 "winapi",644]645646[[package]]647name = "rust_decimal"648version = "1.15.0"649source = "registry+https://github.com/rust-lang/crates.io-index"650checksum = "c5446d1cf2dfe2d6367c8b27f2082bdf011e60e76fa1fcd140047f535156d6e7"
67 unmodified lines
718 "unicode-xid",719]720721[[package]]722name = "tempfile"723version = "3.2.0"724source = "registry+https://github.com/rust-lang/crates.io-index"725checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"726dependencies = [727 "cfg-if",728 "libc",729 "rand 0.8.4",730 "redox_syscall",731 "remove_dir_all",732 "winapi",733]734735[[package]]736name = "term_size"737version = "0.3.2"738source = "registry+https://github.com/rust-lang/crates.io-index"739checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9"
22 unmodified lines
762 "unicode-width",763]764765[[package]]766name = "treeline"767version = "0.1.0"768source = "registry+https://github.com/rust-lang/crates.io-index"769checksum = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41"770771[[package]]772name = "unicode-width"773version = "0.1.8"774source = "registry+https://github.com/rust-lang/crates.io-index"775checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
16 unmodified lines
792source = "registry+https://github.com/rust-lang/crates.io-index"793checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"794795[[package]]796name = "wait-timeout"797version = "0.2.0"798source = "registry+https://github.com/rust-lang/crates.io-index"799checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"800dependencies = [801 "libc",802]803804[[package]]805name = "wasi"806version = "0.10.2+wasi-snapshot-preview1"807source = "registry+https://github.com/rust-lang/crates.io-index"808checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
22 unmodified lines
modified:Cargo.toml+3 added