Skip to content

Commit d502bbe

Browse files
committed
feat(test-support): extract log access helper function
1 parent 1d34efe commit d502bbe

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,41 @@ pub fn cargo_home() -> PathBuf {
119119
home().join(".cargo")
120120
}
121121

122+
/// Path to the current test's `$CARGO_LOG`
123+
///
124+
/// ex: `$CARGO_TARGET_TMPDIR/cit/t0/home/.cargo/log`
125+
pub fn log_dir() -> PathBuf {
126+
cargo_home().join("log")
127+
}
128+
129+
/// Path to the current test's `$CARGO_LOG` file
130+
///
131+
/// ex: `$CARGO_TARGET_TMPDIR/cit/t0/home/.cargo/log/<id>.jsonl`
132+
///
133+
/// This also asserts the number of log files is exactly the same as `idx + 1`.
134+
pub fn log_file(idx: usize) -> PathBuf {
135+
let log_dir = log_dir();
136+
137+
let entries = std::fs::read_dir(&log_dir).unwrap();
138+
let mut log_files: Vec<_> = entries
139+
.filter_map(Result::ok)
140+
.filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl"))
141+
.collect();
142+
143+
// Sort them to get chronological order
144+
log_files.sort_unstable_by(|a, b| a.file_name().to_str().cmp(&b.file_name().to_str()));
145+
146+
assert_eq!(
147+
idx + 1,
148+
log_files.len(),
149+
"unexpected number of log files: {}, expected {}",
150+
log_files.len(),
151+
idx + 1
152+
);
153+
154+
log_files[idx].path()
155+
}
156+
122157
/// Common path and file operations
123158
pub trait CargoPathExt {
124159
fn to_url(&self) -> url::Url;

tests/testsuite/build_analysis.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::prelude::*;
44

55
use cargo_test_support::basic_manifest;
66
use cargo_test_support::compare::assert_e2e;
7-
use cargo_test_support::paths;
7+
use cargo_test_support::paths::log_file;
88
use cargo_test_support::project;
99
use cargo_test_support::str;
1010

@@ -509,27 +509,6 @@ fn log_rebuild_reason_no_rebuild() {
509509
);
510510
}
511511

512-
/// This also asserts the number of log files is exactly the same as `idx + 1`.
513512
fn get_log(idx: usize) -> String {
514-
let cargo_home = paths::cargo_home();
515-
let log_dir = cargo_home.join("log");
516-
517-
let entries = std::fs::read_dir(&log_dir).unwrap();
518-
let mut log_files: Vec<_> = entries
519-
.filter_map(Result::ok)
520-
.filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("jsonl"))
521-
.collect();
522-
523-
// Sort them to get chronological order
524-
log_files.sort_unstable_by(|a, b| a.file_name().to_str().cmp(&b.file_name().to_str()));
525-
526-
assert_eq!(
527-
idx + 1,
528-
log_files.len(),
529-
"unexpected number of log files: {}, expected {}",
530-
log_files.len(),
531-
idx + 1
532-
);
533-
534-
std::fs::read_to_string(log_files[idx].path()).unwrap()
513+
std::fs::read_to_string(log_file(idx)).unwrap()
535514
}

0 commit comments

Comments
 (0)