Skip to content

Commit 0d7239c

Browse files
committed
test(build-analysis): helper to get log file
1 parent 4452c2f commit 0d7239c

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

tests/testsuite/build_analysis.rs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ fn one_logfile_per_invocation() {
3434
.file("src/lib.rs", "")
3535
.build();
3636

37-
let cargo_home = paths::cargo_home();
38-
let log_dir = cargo_home.join("log");
39-
4037
// First invocation
4138
p.cargo("check -Zbuild-analysis")
4239
.env("CARGO_BUILD_ANALYSIS_ENABLED", "true")
@@ -48,14 +45,7 @@ fn one_logfile_per_invocation() {
4845
"#]])
4946
.run();
5047

51-
assert!(log_dir.exists());
52-
let entries = std::fs::read_dir(&log_dir).unwrap();
53-
let log_files: Vec<_> = entries
54-
.filter_map(Result::ok)
55-
.filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl"))
56-
.collect();
57-
58-
assert_eq!(log_files.len(), 1);
48+
let _ = get_log(0);
5949

6050
// Second invocation
6151
p.cargo("check -Zbuild-analysis")
@@ -67,13 +57,7 @@ fn one_logfile_per_invocation() {
6757
"#]])
6858
.run();
6959

70-
let entries = std::fs::read_dir(&log_dir).unwrap();
71-
let log_files: Vec<_> = entries
72-
.filter_map(Result::ok)
73-
.filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl"))
74-
.collect();
75-
76-
assert_eq!(log_files.len(), 2);
60+
let _ = get_log(1);
7761
}
7862

7963
#[cargo_test]
@@ -88,20 +72,8 @@ fn log_msg_build_started() {
8872
.masquerade_as_nightly_cargo(&["build-analysis"])
8973
.run();
9074

91-
let cargo_home = paths::cargo_home();
92-
let log_dir = cargo_home.join("log");
93-
assert!(log_dir.exists());
94-
95-
let entries = std::fs::read_dir(&log_dir).unwrap();
96-
let log_file = entries
97-
.filter_map(Result::ok)
98-
.find(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl"))
99-
.unwrap();
100-
101-
let content = std::fs::read_to_string(log_file.path()).unwrap();
102-
10375
assert_e2e().eq(
104-
&content,
76+
&get_log(0),
10577
str![[r#"
10678
[
10779
{
@@ -150,20 +122,8 @@ fn log_msg_timing_info() {
150122
.masquerade_as_nightly_cargo(&["build-analysis"])
151123
.run();
152124

153-
let cargo_home = paths::cargo_home();
154-
let log_dir = cargo_home.join("log");
155-
assert!(log_dir.exists());
156-
157-
let entries = std::fs::read_dir(&log_dir).unwrap();
158-
let log_file = entries
159-
.filter_map(Result::ok)
160-
.find(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl"))
161-
.unwrap();
162-
163-
let content = std::fs::read_to_string(log_file.path()).unwrap();
164-
165125
assert_e2e().eq(
166-
&content,
126+
&get_log(0),
167127
str![[r#"
168128
[
169129
{
@@ -196,3 +156,28 @@ fn log_msg_timing_info() {
196156
.against_jsonlines(),
197157
);
198158
}
159+
160+
/// This also asserts the number of log files is exactly the same as `idx + 1`.
161+
fn get_log(idx: usize) -> String {
162+
let cargo_home = paths::cargo_home();
163+
let log_dir = cargo_home.join("log");
164+
165+
let entries = std::fs::read_dir(&log_dir).unwrap();
166+
let mut log_files: Vec<_> = entries
167+
.filter_map(Result::ok)
168+
.filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl"))
169+
.collect();
170+
171+
// Sort them to get chronological order
172+
log_files.sort_unstable_by(|a, b| a.file_name().to_str().cmp(&b.file_name().to_str()));
173+
174+
assert_eq!(
175+
idx + 1,
176+
log_files.len(),
177+
"unexpected number of log files: {}, expected {}",
178+
log_files.len(),
179+
idx + 1
180+
);
181+
182+
std::fs::read_to_string(log_files[idx].path()).unwrap()
183+
}

0 commit comments

Comments
 (0)