Skip to content
Myr

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

3 files+252 added−245 removed

added:tests/common.rs+13 added
1use std::process::Command;23use assert_cmd::cargo::CommandCargoExt;45pub fn hyperfine_raw_command() -> Command {6 let mut cmd = Command::cargo_bin("hyperfine").unwrap();7 cmd.current_dir("tests/");8 cmd9}1011pub fn hyperfine() -> assert_cmd::Command {12 assert_cmd::Command::from_std(hyperfine_raw_command())13}
added:tests/execution_order_tests.rs+237 added
1use std::{fs::File, io::Read, path::PathBuf};23use tempfile::{tempdir, TempDir};45mod common;6use common::hyperfine;78struct ExecutionOrderTest {9 cmd: assert_cmd::Command,10 expected_content: String,11 logfile_path: PathBuf,12 #[allow(dead_code)]13 tempdir: TempDir,14}1516impl ExecutionOrderTest {17 fn new() -> Self {18 let tempdir = tempdir().unwrap();19 let logfile_path = tempdir.path().join("output.log");2021 ExecutionOrderTest {22 cmd: hyperfine(),23 expected_content: String::new(),24 logfile_path,25 tempdir,26 }27 }2829 fn arg<S: AsRef<str>>(&mut self, arg: S) -> &mut Self {30 self.cmd.arg(arg.as_ref());31 self32 }3334 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 }4142 fn command(&mut self, output: &str) -> &mut Self {43 self.arg(self.get_command(output));44 self45 }4647 fn setup(&mut self, output: &str) -> &mut Self {48 self.arg("--setup");49 self.command(output)50 }5152 fn prepare(&mut self, output: &str) -> &mut Self {53 self.arg("--prepare");54 self.command(output)55 }5657 fn cleanup(&mut self, output: &str) -> &mut Self {58 self.arg("--cleanup");59 self.command(output)60 }6162 fn expect_output(&mut self, output: &str) -> &mut Self {63 self.expected_content.push_str(output);6465 #[cfg(windows)]66 {67 self.expected_content.push_str(" \r");68 }6970 self.expected_content.push('\n');71 self72 }7374 fn run(&mut self) {75 self.cmd.assert().success();7677 let mut f = File::open(&self.logfile_path).unwrap();78 let mut content = String::new();79 f.read_to_string(&mut content).unwrap();8081 assert_eq!(content, self.expected_content);82 }83}8485impl Default for ExecutionOrderTest {86 fn default() -> Self {87 Self::new()88 }89}9091#[test]92fn 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}101102#[test]103fn 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}114115#[test]116fn 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}128129#[test]130fn 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}144145#[test]146fn 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}162163#[test]164fn 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}178179#[test]180fn 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}195196#[test]197fn 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}212213#[test]214fn 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
1use std::{fs::File, io::Read, path::PathBuf, process::Command};1mod common;2use common::hyperfine;33use 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}154#[test]5fn hyperfine_runs_successfully() {6 hyperfine()7 .arg("--runs=2")
9 unmodified lines
17 .arg("echo dummy benchmark")18 .assert()19 .success();20}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 setup(&mut self, output: &str) -> &mut Self {74 self.arg("--setup");75 self.command(output)76 }7778 fn prepare(&mut self, output: &str) -> &mut Self {79 self.arg("--prepare");80 self.command(output)81 }8283 fn cleanup(&mut self, output: &str) -> &mut Self {84 self.arg("--cleanup");85 self.command(output)86 }8788 fn expect_output(&mut self, output: &str) -> &mut Self {89 self.expected_content.push_str(output);9091 #[cfg(windows)]92 {93 self.expected_content.push_str(" \r");94 }9596 self.expected_content.push('\n');97 self98 }99100 fn run(&mut self) {101 self.cmd.assert().success();102103 let mut f = File::open(&self.logfile_path).unwrap();104 let mut content = String::new();105 f.read_to_string(&mut content).unwrap();106107 assert_eq!(content, self.expected_content);108 }109}110111impl Default for ExecutionOrderTest {112 fn default() -> Self {113 Self::new()114 }115}116117#[test]118fn 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}127128#[test]129fn 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}140141#[test]142fn 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}154155#[test]156fn 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}170171#[test]172fn 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}188189#[test]190fn 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}204205#[test]206fn 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}221222#[test]223fn 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}238239#[test]240fn 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}