Move execution order tests to separate file
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 ce7087b · Browse files · 3 files·+252 added−245 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+252 added−245 removed
- tests:+252 added−245 removed
Provenance
- Commit
c751a77- Tree
4403421- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
ce7087b
3 files changed+252 added−245 removed
added:tests/common.rs+13 added
1+use std::process::Command;2+3+use assert_cmd::cargo::CommandCargoExt;4+5+pub fn hyperfine_raw_command() -> Command {6+ let mut cmd = Command::cargo_bin("hyperfine").unwrap();7+ cmd.current_dir("tests/");8+ cmd9+}10+11+pub fn hyperfine() -> assert_cmd::Command {12+ assert_cmd::Command::from_std(hyperfine_raw_command())13+}added:tests/execution_order_tests.rs+237 added
1+use std::{fs::File, io::Read, path::PathBuf};2+3+use tempfile::{tempdir, TempDir};4+5+mod common;6+use common::hyperfine;7+8+struct ExecutionOrderTest {9+ cmd: assert_cmd::Command,10+ expected_content: String,11+ logfile_path: PathBuf,12+ #[allow(dead_code)]13+ tempdir: TempDir,14+}15+16+impl ExecutionOrderTest {17+ fn new() -> Self {18+ let tempdir = tempdir().unwrap();19+ let logfile_path = tempdir.path().join("output.log");20+21+ ExecutionOrderTest {22+ cmd: hyperfine(),23+ expected_content: String::new(),24+ logfile_path,25+ tempdir,26+ }27+ }28+29+ fn arg<S: AsRef<str>>(&mut self, arg: S) -> &mut Self {30+ self.cmd.arg(arg.as_ref());31+ self32+ }33+34+ fn get_command(&self, output: &str) -> String {35+ format!(36+ "echo {output} >> {path}",37+ output = output,38+ path = self.logfile_path.to_string_lossy()39+ )40+ }41+42+ fn command(&mut self, output: &str) -> &mut Self {43+ self.arg(self.get_command(output));44+ self45+ }46+47+ fn setup(&mut self, output: &str) -> &mut Self {48+ self.arg("--setup");49+ self.command(output)50+ }51+52+ fn prepare(&mut self, output: &str) -> &mut Self {53+ self.arg("--prepare");54+ self.command(output)55+ }56+57+ fn cleanup(&mut self, output: &str) -> &mut Self {58+ self.arg("--cleanup");59+ self.command(output)60+ }61+62+ fn expect_output(&mut self, output: &str) -> &mut Self {63+ self.expected_content.push_str(output);64+65+ #[cfg(windows)]66+ {67+ self.expected_content.push_str(" \r");68+ }69+70+ self.expected_content.push('\n');71+ self72+ }73+74+ fn run(&mut self) {75+ self.cmd.assert().success();76+77+ let mut f = File::open(&self.logfile_path).unwrap();78+ let mut content = String::new();79+ f.read_to_string(&mut content).unwrap();80+81+ assert_eq!(content, self.expected_content);82+ }83+}84+85+impl Default for ExecutionOrderTest {86+ fn default() -> Self {87+ Self::new()88+ }89+}90+91+#[test]92+fn benchmarks_are_executed_sequentially_one() {93+ ExecutionOrderTest::new()94+ .arg("--runs=1")95+ .command("command 1")96+ .command("command 2")97+ .expect_output("command 1")98+ .expect_output("command 2")99+ .run();100+}101+102+#[test]103+fn benchmarks_are_executed_sequentially() {104+ ExecutionOrderTest::new()105+ .arg("--runs=2")106+ .command("command 1")107+ .command("command 2")108+ .expect_output("command 1")109+ .expect_output("command 1")110+ .expect_output("command 2")111+ .expect_output("command 2")112+ .run();113+}114+115+#[test]116+fn warmup_runs_are_executed_before_benchmarking_runs() {117+ ExecutionOrderTest::new()118+ .arg("--runs=2")119+ .arg("--warmup=3")120+ .command("command 1")121+ .expect_output("command 1")122+ .expect_output("command 1")123+ .expect_output("command 1")124+ .expect_output("command 1")125+ .expect_output("command 1")126+ .run();127+}128+129+#[test]130+fn setup_commands_are_executed_before_each_series_of_timing_runs() {131+ ExecutionOrderTest::new()132+ .arg("--runs=2")133+ .setup("setup")134+ .command("command 1")135+ .command("command 2")136+ .expect_output("setup")137+ .expect_output("command 1")138+ .expect_output("command 1")139+ .expect_output("setup")140+ .expect_output("command 2")141+ .expect_output("command 2")142+ .run();143+}144+145+#[test]146+fn prepare_commands_are_executed_before_each_timing_run() {147+ ExecutionOrderTest::new()148+ .arg("--runs=2")149+ .prepare("prepare")150+ .command("command 1")151+ .command("command 2")152+ .expect_output("prepare")153+ .expect_output("command 1")154+ .expect_output("prepare")155+ .expect_output("command 1")156+ .expect_output("prepare")157+ .expect_output("command 2")158+ .expect_output("prepare")159+ .expect_output("command 2")160+ .run();161+}162+163+#[test]164+fn cleanup_commands_are_executed_once_after_each_benchmark() {165+ ExecutionOrderTest::new()166+ .arg("--runs=2")167+ .cleanup("cleanup")168+ .command("command 1")169+ .command("command 2")170+ .expect_output("command 1")171+ .expect_output("command 1")172+ .expect_output("cleanup")173+ .expect_output("command 2")174+ .expect_output("command 2")175+ .expect_output("cleanup")176+ .run();177+}178+179+#[test]180+fn setup_prepare_cleanup_combined() {181+ ExecutionOrderTest::new()182+ .arg("--runs=2")183+ .setup("make")184+ .prepare("make testclean")185+ .command("make test")186+ .cleanup("make clean")187+ .expect_output("make")188+ .expect_output("make testclean")189+ .expect_output("make test")190+ .expect_output("make testclean")191+ .expect_output("make test")192+ .expect_output("make clean")193+ .run();194+}195+196+#[test]197+fn single_parameter_value() {198+ ExecutionOrderTest::new()199+ .arg("--runs=2")200+ .arg("--parameter-list")201+ .arg("number")202+ .arg("1,2,3")203+ .command("command {number}")204+ .expect_output("command 1")205+ .expect_output("command 1")206+ .expect_output("command 2")207+ .expect_output("command 2")208+ .expect_output("command 3")209+ .expect_output("command 3")210+ .run();211+}212+213+#[test]214+fn multiple_parameter_values() {215+ ExecutionOrderTest::new()216+ .arg("--runs=2")217+ .arg("--parameter-list")218+ .arg("number")219+ .arg("1,2,3")220+ .arg("--parameter-list")221+ .arg("letter")222+ .arg("a,b")223+ .command("command {number} {letter}")224+ .expect_output("command 1 a")225+ .expect_output("command 1 a")226+ .expect_output("command 2 a")227+ .expect_output("command 2 a")228+ .expect_output("command 3 a")229+ .expect_output("command 3 a")230+ .expect_output("command 1 b")231+ .expect_output("command 1 b")232+ .expect_output("command 2 b")233+ .expect_output("command 2 b")234+ .expect_output("command 3 b")235+ .expect_output("command 3 b")236+ .run();237+}modified:tests/integration_tests.rs+2 added-245 removed
1-use std::{fs::File, io::Read, path::PathBuf, process::Command};1+mod common;2+use common::hyperfine;3 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-4 #[test]5 fn hyperfine_runs_successfully() {6 hyperfine()7 .arg("--runs=2")9 unmodified lines17 .arg("echo dummy benchmark")18 .assert()19 .success();20 }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 setup(&mut self, output: &str) -> &mut Self {74- self.arg("--setup");75- self.command(output)76- }77-78- fn prepare(&mut self, output: &str) -> &mut Self {79- self.arg("--prepare");80- self.command(output)81- }82-83- fn cleanup(&mut self, output: &str) -> &mut Self {84- self.arg("--cleanup");85- self.command(output)86- }87-88- fn expect_output(&mut self, output: &str) -> &mut Self {89- self.expected_content.push_str(output);90-91- #[cfg(windows)]92- {93- self.expected_content.push_str(" \r");94- }95-96- self.expected_content.push('\n');97- self98- }99-100- fn run(&mut self) {101- self.cmd.assert().success();102-103- let mut f = File::open(&self.logfile_path).unwrap();104- let mut content = String::new();105- f.read_to_string(&mut content).unwrap();106-107- assert_eq!(content, self.expected_content);108- }109-}110-111-impl Default for ExecutionOrderTest {112- fn default() -> Self {113- Self::new()114- }115-}116-117-#[test]118-fn benchmarks_are_executed_sequentially_one() {119- ExecutionOrderTest::new()120- .arg("--runs=1")121- .command("command 1")122- .command("command 2")123- .expect_output("command 1")124- .expect_output("command 2")125- .run();126-}127-128-#[test]129-fn benchmarks_are_executed_sequentially() {130- ExecutionOrderTest::new()131- .arg("--runs=2")132- .command("command 1")133- .command("command 2")134- .expect_output("command 1")135- .expect_output("command 1")136- .expect_output("command 2")137- .expect_output("command 2")138- .run();139-}140-141-#[test]142-fn warmup_runs_are_executed_before_benchmarking_runs() {143- ExecutionOrderTest::new()144- .arg("--runs=2")145- .arg("--warmup=3")146- .command("command 1")147- .expect_output("command 1")148- .expect_output("command 1")149- .expect_output("command 1")150- .expect_output("command 1")151- .expect_output("command 1")152- .run();153-}154-155-#[test]156-fn setup_commands_are_executed_before_each_series_of_timing_runs() {157- ExecutionOrderTest::new()158- .arg("--runs=2")159- .setup("setup")160- .command("command 1")161- .command("command 2")162- .expect_output("setup")163- .expect_output("command 1")164- .expect_output("command 1")165- .expect_output("setup")166- .expect_output("command 2")167- .expect_output("command 2")168- .run();169-}170-171-#[test]172-fn prepare_commands_are_executed_before_each_timing_run() {173- ExecutionOrderTest::new()174- .arg("--runs=2")175- .prepare("prepare")176- .command("command 1")177- .command("command 2")178- .expect_output("prepare")179- .expect_output("command 1")180- .expect_output("prepare")181- .expect_output("command 1")182- .expect_output("prepare")183- .expect_output("command 2")184- .expect_output("prepare")185- .expect_output("command 2")186- .run();187-}188-189-#[test]190-fn cleanup_commands_are_executed_once_after_each_benchmark() {191- ExecutionOrderTest::new()192- .arg("--runs=2")193- .cleanup("cleanup")194- .command("command 1")195- .command("command 2")196- .expect_output("command 1")197- .expect_output("command 1")198- .expect_output("cleanup")199- .expect_output("command 2")200- .expect_output("command 2")201- .expect_output("cleanup")202- .run();203-}204-205-#[test]206-fn setup_prepare_cleanup_combined() {207- ExecutionOrderTest::new()208- .arg("--runs=2")209- .setup("make")210- .prepare("make testclean")211- .command("make test")212- .cleanup("make clean")213- .expect_output("make")214- .expect_output("make testclean")215- .expect_output("make test")216- .expect_output("make testclean")217- .expect_output("make test")218- .expect_output("make clean")219- .run();220-}221-222-#[test]223-fn single_parameter_value() {224- ExecutionOrderTest::new()225- .arg("--runs=2")226- .arg("--parameter-list")227- .arg("number")228- .arg("1,2,3")229- .command("command {number}")230- .expect_output("command 1")231- .expect_output("command 1")232- .expect_output("command 2")233- .expect_output("command 2")234- .expect_output("command 3")235- .expect_output("command 3")236- .run();237-}238-239-#[test]240-fn multiple_parameter_values() {241- ExecutionOrderTest::new()242- .arg("--runs=2")243- .arg("--parameter-list")244- .arg("number")245- .arg("1,2,3")246- .arg("--parameter-list")247- .arg("letter")248- .arg("a,b")249- .command("command {number} {letter}")250- .expect_output("command 1 a")251- .expect_output("command 1 a")252- .expect_output("command 2 a")253- .expect_output("command 2 a")254- .expect_output("command 3 a")255- .expect_output("command 3 a")256- .expect_output("command 1 b")257- .expect_output("command 1 b")258- .expect_output("command 2 b")259- .expect_output("command 2 b")260- .expect_output("command 3 b")261- .expect_output("command 3 b")262- .run();263-}