Skip to content

Commit 317f0f5

Browse files
committed
Auto merge of #7395 - ehuss:fix-timings-test, r=alexcrichton
Fix -Ztimings with doc tests. `cargo test -Ztimings` would crash if you have any doc tests. This is because the `Doctest` mode unit doesn't generate any artifacts, so a map lookup was failing. This also adds a basic test just to ensure it works.
2 parents 9f7a68f + 8fadd2b commit 317f0f5

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,7 @@ fn substitute_macros(input: &str) -> String {
16321632
("[RUNNING]", " Running"),
16331633
("[COMPILING]", " Compiling"),
16341634
("[CHECKING]", " Checking"),
1635+
("[COMPLETED]", " Completed"),
16351636
("[CREATED]", " Created"),
16361637
("[FINISHED]", " Finished"),
16371638
("[ERROR]", "error:"),

src/cargo/core/compiler/timings.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,15 +444,19 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
444444
"todo"
445445
}
446446
.to_string();
447+
448+
// These filter on the unlocked units because not all unlocked
449+
// units are actually "built". For example, Doctest mode units
450+
// don't actually generate artifacts.
447451
let unlocked_units: Vec<usize> = ut
448452
.unlocked_units
449453
.iter()
450-
.map(|unit| unit_map[unit])
454+
.filter_map(|unit| unit_map.get(unit).copied())
451455
.collect();
452456
let unlocked_rmeta_units: Vec<usize> = ut
453457
.unlocked_rmeta_units
454458
.iter()
455-
.map(|unit| unit_map[unit])
459+
.filter_map(|unit| unit_map.get(unit).copied())
456460
.collect();
457461
UnitData {
458462
i,

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ mod shell_quoting;
9090
mod small_fd_limits;
9191
mod standard_lib;
9292
mod test;
93+
mod timings;
9394
mod tool_paths;
9495
mod update;
9596
mod vendor;

tests/testsuite/timings.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use cargo_test_support::registry::Package;
2+
use cargo_test_support::{is_nightly, project};
3+
4+
#[cargo_test]
5+
fn timings_works() {
6+
if !is_nightly() {
7+
// Requires --json flag, remove this check in 1.38.
8+
return;
9+
}
10+
11+
Package::new("dep", "0.1.0").publish();
12+
13+
let p = project()
14+
.file(
15+
"Cargo.toml",
16+
r#"
17+
[package]
18+
name = "foo"
19+
version = "0.1.0"
20+
21+
[dependencies]
22+
dep = "0.1"
23+
"#,
24+
)
25+
.file("src/lib.rs", "")
26+
.file("src/main.rs", "fn main() {}")
27+
.file("tests/t1.rs", "")
28+
.file("examples/ex1.rs", "fn main() {}")
29+
.build();
30+
31+
p.cargo("build --all-targets -Ztimings")
32+
.masquerade_as_nightly_cargo()
33+
.with_stderr_unordered(
34+
"\
35+
[UPDATING] [..]
36+
[DOWNLOADING] crates ...
37+
[DOWNLOADED] dep v0.1.0 [..]
38+
[COMPILING] dep v0.1.0
39+
[COMPILING] foo v0.1.0 [..]
40+
[COMPLETED] dep v0.1.0 in [..]s
41+
[COMPLETED] foo v0.1.0 in [..]s
42+
[COMPLETED] foo v0.1.0 bin \"foo\" in [..]s
43+
[COMPLETED] foo v0.1.0 example \"ex1\" in [..]s
44+
[COMPLETED] foo v0.1.0 lib (test) in [..]s
45+
[COMPLETED] foo v0.1.0 bin \"foo\" (test) in [..]s
46+
[COMPLETED] foo v0.1.0 test \"t1\" (test) in [..]s
47+
[FINISHED] [..]
48+
Timing report saved to [..]/foo/cargo-timing-[..].html
49+
",
50+
)
51+
.run();
52+
53+
p.cargo("clean").run();
54+
55+
p.cargo("test -Ztimings")
56+
.masquerade_as_nightly_cargo()
57+
.run();
58+
59+
p.cargo("clean").run();
60+
61+
p.cargo("check -Ztimings")
62+
.masquerade_as_nightly_cargo()
63+
.run();
64+
65+
p.cargo("clean").run();
66+
67+
p.cargo("doc -Ztimings").masquerade_as_nightly_cargo().run();
68+
}

0 commit comments

Comments
 (0)