Skip to content

Commit b82cadf

Browse files
authored
Remove more duplication in show_settings tests (#19664)
## Summary A follow up from #19654. Now with an even larger total diff. This PR does three things (let me know if you'd rather it be 3 stacked PRs). Firstly, it reduces the `diff_uv_snapshot` and `capture_uv_snapshot` context down to the regular 3. It keeps the 10 override for existing users. This leads to a loss of ~200 lines in the existing cases. I think the new context size is perfectly appropriate for these tests. Secondly, it converts all the applicable show_settings tests to the new approach. And, lastly, it removes the need for every test to have a big initial snapshot to diff against, by quietly generating a relevant baseline and then diffing against that. Because we'd be losing some test cases, it reproduces these baselines as some dedicated tests instead. ## Review Guide The PR is split into 10 commits which are intended to aid in reviewing it. 1. Reduce diff snapshot context: This makes the necessary adjustments to reduce the size of the diff context, and amends the existing converted tests from the previous PR. 2. Refactor uv diff snapshot helpers: This is just a small change to de-duplicate some of the logic from the previous PR and set up for a `capture_uv_snapshot` that does not assert. This is needed for quietly generating baseline snapshots. 3. Remove snapshot contents: This commit deletes the snapshot contents for all snapshots which will subsequently need updating. This commit is solely here to make it easier to follow the following commits. The intent was that this would be just deletions, and it _is_ just deletions, except the default git diff algorithm doesn't realise this. You need `--patience` or `--histogram` to see that this is the case. 4. Use baselines and diff snapshots: This commit converts all remaining `show_settings` tests to using diff snapshots _and_ introduces the baselines to remove their snapshots. The previously affected tests from the previous PR are _not_ touched here (intentionally). 5. Use intra-test diffs: This commit develops on the previous by adding diffs between outputs within test cases to further reduce duplication and clarify/highlight dependent test-cases within a test function. The idea being that the diff is happening against a more relevant command to further minimise the snapshot sizes. 6. Populate diff snapshots: This commit is the counterpart to commit 3. It re-populates the changed snapshots. This is, again, to aid in review if going commit-by-commit. Commits 4 and 5 have some minor adjustments that are beyond their stated purpose in order to allow this commit to be 100% added lines only. 7. Improve existing diff snapshots: This is a cleanup pass on the existing diffs (from the previous PR) to upgrade them to using the baseline snapshots. The change should be just that. 8. Clarify existing diff snapshot comparisons: I've improved (I hope) on the existing comments from the previous PR for cases where intra-test diffs are used. I've also added missing comments. The goal is to make it clear why a specific previous snapshot is being diffed against so that the following diff snapshot combined with any existing normal "what is this snapshot supposed to tell me" comments makes sense to anyone who may encounter it in the future. 9. Add baseline tests: This introduces the now missing test cases for the baseline cases which the other tests use for their diffs. 10. Populate baseline snapshots: Again, in order to keep commit 9 easier to review, the snapshot population was split off. ## Test Plan Baseline tests added, otherwise N/A.
1 parent d3cd632 commit b82cadf

3 files changed

Lines changed: 2410 additions & 8761 deletions

File tree

‎crates/uv-test/src/lib.rs‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ impl TestContext {
19071907
);
19081908
assert!(status.success(), "{snapshot}");
19091909
let new_lock = fs_err::read_to_string(&lock_path).unwrap();
1910-
diff_snapshot(&old_lock, &new_lock)
1910+
diff_snapshot(&old_lock, &new_lock, 10)
19111911
}
19121912

19131913
/// Read a file in the temporary directory
@@ -1965,14 +1965,14 @@ impl TestContext {
19651965

19661966
/// Creates a "unified" diff between the two line-oriented strings suitable
19671967
/// for snapshotting.
1968-
pub fn diff_snapshot(old: &str, new: &str) -> String {
1968+
pub fn diff_snapshot(old: &str, new: &str, context_radius: usize) -> String {
19691969
static TRIM_TRAILING_WHITESPACE: std::sync::LazyLock<Regex> =
19701970
std::sync::LazyLock::new(|| Regex::new(r"(?m)^\s+$").unwrap());
19711971

19721972
let diff = similar::TextDiff::from_lines(old, new);
19731973
let unified = diff
19741974
.unified_diff()
1975-
.context_radius(10)
1975+
.context_radius(context_radius)
19761976
.header("old", "new")
19771977
.to_string();
19781978
// Not totally clear why, but some lines end up containing only
@@ -1989,29 +1989,27 @@ pub fn diff_snapshot(old: &str, new: &str) -> String {
19891989
#[macro_export]
19901990
macro_rules! diff_uv_snapshot {
19911991
($filters:expr, $old:expr, $spawnable:expr, @$snapshot:literal) => {{
1992-
let (new, _) = $crate::run_and_format(
1993-
$spawnable,
1994-
&$filters,
1995-
$crate::function_name!(),
1996-
Some($crate::WindowsFilters::Platform),
1997-
None,
1998-
);
1999-
::insta::assert_snapshot!($crate::diff_snapshot($old, &new), @$snapshot);
1992+
let new = $crate::capture_uv_snapshot!($filters, $spawnable);
1993+
::insta::assert_snapshot!($crate::diff_snapshot($old, &new, 3), @$snapshot);
20001994
new
20011995
}};
20021996
}
20031997

2004-
/// Assert and capture a snapshot of a command's output.
1998+
/// Capture a command's output, optionally asserting it against a snapshot.
20051999
#[macro_export]
20062000
macro_rules! capture_uv_snapshot {
2007-
($filters:expr, $spawnable:expr, @$snapshot:literal) => {{
2001+
($filters:expr, $spawnable:expr) => {{
20082002
let (snapshot, _) = $crate::run_and_format(
20092003
$spawnable,
20102004
&$filters,
20112005
$crate::function_name!(),
20122006
Some($crate::WindowsFilters::Platform),
20132007
None,
20142008
);
2009+
snapshot
2010+
}};
2011+
($filters:expr, $spawnable:expr, @$snapshot:literal) => {{
2012+
let snapshot = $crate::capture_uv_snapshot!($filters, $spawnable);
20152013
::insta::assert_snapshot!(snapshot, @$snapshot);
20162014
snapshot
20172015
}};

0 commit comments

Comments
 (0)