diff options
| author | Jubilee <46493976+workingjubilee@users.noreply.github.com> | 2021-10-07 20:26:12 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-07 20:26:12 -0700 |
| commit | 37f17bca7ccca0b393a58c87bb87bac18f371f61 (patch) | |
| tree | 23c2653b0ede7c0e6fd0b49687219a138ab13796 /library/test/src/formatters | |
| parent | 6c17601a2e6fa55e5d2ec7284359bee931c0c61a (diff) | |
| parent | ecf474152350664f1227421eeb278b2e8185cd07 (diff) | |
Rollup merge of #89082 - smoelius:master, r=kennytm
Implement #85440 (Random test ordering)
This PR adds `--shuffle` and `--shuffle-seed` options to `libtest`. The options are similar to the [`-shuffle` option](https://github.com/golang/go/blob/c894b442d1e5e150ad33fa3ce13dbfab1c037b3a/src/testing/testing.go#L1482-L1499) that was recently added to Go.
Here are the relevant parts of the help message:
```
--shuffle Run tests in random order
--shuffle-seed SEED
Run tests in random order; seed the random number
generator with SEED
...
By default, the tests are run in alphabetical order. Use --shuffle or set
RUST_TEST_SHUFFLE to run the tests in random order. Pass the generated
"shuffle seed" to --shuffle-seed (or set RUST_TEST_SHUFFLE_SEED) to run the
tests in the same order again. Note that --shuffle and --shuffle-seed do not
affect whether the tests are run in parallel.
```
Is an RFC needed for this?
Diffstat (limited to 'library/test/src/formatters')
| -rw-r--r-- | library/test/src/formatters/json.rs | 11 | ||||
| -rw-r--r-- | library/test/src/formatters/junit.rs | 6 | ||||
| -rw-r--r-- | library/test/src/formatters/mod.rs | 2 | ||||
| -rw-r--r-- | library/test/src/formatters/pretty.rs | 9 | ||||
| -rw-r--r-- | library/test/src/formatters/terse.rs | 9 |
5 files changed, 28 insertions, 9 deletions
diff --git a/library/test/src/formatters/json.rs b/library/test/src/formatters/json.rs index 57b6d1a0202..424d3ef7b41 100644 --- a/library/test/src/formatters/json.rs +++ b/library/test/src/formatters/json.rs @@ -60,10 +60,15 @@ impl<T: Write> JsonFormatter<T> { } impl<T: Write> OutputFormatter for JsonFormatter<T> { - fn write_run_start(&mut self, test_count: usize) -> io::Result<()> { + fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> { + let shuffle_seed_json = if let Some(shuffle_seed) = shuffle_seed { + format!(r#", "shuffle_seed": {}"#, shuffle_seed) + } else { + String::new() + }; self.writeln_message(&*format!( - r#"{{ "type": "suite", "event": "started", "test_count": {} }}"#, - test_count + r#"{{ "type": "suite", "event": "started", "test_count": {}{} }}"#, + test_count, shuffle_seed_json )) } diff --git a/library/test/src/formatters/junit.rs b/library/test/src/formatters/junit.rs index 04057906d1c..e2aebee916d 100644 --- a/library/test/src/formatters/junit.rs +++ b/library/test/src/formatters/junit.rs @@ -27,7 +27,11 @@ impl<T: Write> JunitFormatter<T> { } impl<T: Write> OutputFormatter for JunitFormatter<T> { - fn write_run_start(&mut self, _test_count: usize) -> io::Result<()> { + fn write_run_start( + &mut self, + _test_count: usize, + _shuffle_seed: Option<u64>, + ) -> io::Result<()> { // We write xml header on run start self.out.write_all(b"\n")?; self.write_message("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") diff --git a/library/test/src/formatters/mod.rs b/library/test/src/formatters/mod.rs index 2e03581b3af..cb80859759f 100644 --- a/library/test/src/formatters/mod.rs +++ b/library/test/src/formatters/mod.rs @@ -18,7 +18,7 @@ pub(crate) use self::pretty::PrettyFormatter; pub(crate) use self::terse::TerseFormatter; pub(crate) trait OutputFormatter { - fn write_run_start(&mut self, test_count: usize) -> io::Result<()>; + fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()>; fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()>; fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()>; fn write_result( diff --git a/library/test/src/formatters/pretty.rs b/library/test/src/formatters/pretty.rs index 9cad71e30bd..4a03b4b9147 100644 --- a/library/test/src/formatters/pretty.rs +++ b/library/test/src/formatters/pretty.rs @@ -181,9 +181,14 @@ impl<T: Write> PrettyFormatter<T> { } impl<T: Write> OutputFormatter for PrettyFormatter<T> { - fn write_run_start(&mut self, test_count: usize) -> io::Result<()> { + fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> { let noun = if test_count != 1 { "tests" } else { "test" }; - self.write_plain(&format!("\nrunning {} {}\n", test_count, noun)) + let shuffle_seed_msg = if let Some(shuffle_seed) = shuffle_seed { + format!(" (shuffle seed: {})", shuffle_seed) + } else { + String::new() + }; + self.write_plain(&format!("\nrunning {} {}{}\n", test_count, noun, shuffle_seed_msg)) } fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> { diff --git a/library/test/src/formatters/terse.rs b/library/test/src/formatters/terse.rs index 0c8215c5dac..1f2c410cd96 100644 --- a/library/test/src/formatters/terse.rs +++ b/library/test/src/formatters/terse.rs @@ -170,10 +170,15 @@ impl<T: Write> TerseFormatter<T> { } impl<T: Write> OutputFormatter for TerseFormatter<T> { - fn write_run_start(&mut self, test_count: usize) -> io::Result<()> { + fn write_run_start(&mut self, test_count: usize, shuffle_seed: Option<u64>) -> io::Result<()> { self.total_test_count = test_count; let noun = if test_count != 1 { "tests" } else { "test" }; - self.write_plain(&format!("\nrunning {} {}\n", test_count, noun)) + let shuffle_seed_msg = if let Some(shuffle_seed) = shuffle_seed { + format!(" (shuffle seed: {})", shuffle_seed) + } else { + String::new() + }; + self.write_plain(&format!("\nrunning {} {}{}\n", test_count, noun, shuffle_seed_msg)) } fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> { |
