You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### What does this PR try to resolve?
The goal is to make filesystem layout tests easier to understand and
review.
The `build-dir` test's have a [helper
fns](https://github.com/rust-lang/cargo/blob/master/tests/testsuite/build_dir.rs#L720)
that have imperative checks. While this works, its not easy to see what
its validating at a glance.
My thought is to reuse the snapbox infrastructure we already have for
the cargo ui tests.
The prototype in this PR generates a file tree (like the unix tree
command) and uses snapbox to compare against a snapshot. This makes the
layout tests much easier to read and failures are much more obvious what
what went wrong.
### Implementation Details
* The `LayoutTree` struct handles comparison logic (as opposed to using
snapbox directly)
* This avoids the issues initial implementation (ordering and platform
specific files, see full details below)
* If the comparison fails, 2 human readable tree's are generated and
snapbox is used to display the diffs.
* Note: These diffs are in-memory thus, do not support
`SNAPSHOTS=overwrite`. This is arguably good as often the overwrite
would be incorrect as the snapshots are platform specific.
* Snapshots would support conditional files based on the target os /
platform env
* Files/directories may be suffixed with
`[target_platform=<target-platform>]`
* Multiple platforms may be specified separated by comma
<details><summary>initial implementation issues</summary>
However there are some problems with the current implementation that
limit the usefulness of it.
1. Different platforms have different files which cause some tests to
fail
* Examples
* lib prefix/suffixes based on platform
* `dSYM` on macos
* One idea I have would be to have a cfg suffix after file name in the
tree like so
* `└── foo-[HASH].dSYM [cfg(target_os = "macos")]`
* This would also require rethinking the "tree lines" (`└`, `├`, `│`) to
handle optional files.
2. When dealing with build scripts there are multiple
`target/<profile>/build/pkg-[HASH]` directories. The hash changes the
order of the directories when generating the tree making snapshots
inconsistent.
* We have redactions to handle replacing the hash with a placeholder
`[HASH]`, but this does not help with the order.
</details>
---
Fun fact: These changes were written and tested at [Rust Forge
2025](https://rustforgeconf.com/) in New Zealand. 🇳🇿
🥳
Copy file name to clipboardExpand all lines: src/doc/contrib/src/tests/writing.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,16 @@ test.
64
64
- See [`support::compare`] for an explanation of the string pattern matching.
65
65
Patterns are used to make it easier to match against the expected output.
66
66
67
+
#### Filesystem layout testing
68
+
69
+
Tests often to need to verify Cargo created/removed files.
70
+
The `CargoPathExt` trait (implemented by `Path` and `PathBuf`) provides a `assert_dir_layout()` to verify the files in a directory (including nested directories).
71
+
This takes a snapshot of file paths for the given directory and asserts that all files are present and no new files have been created.
72
+
This function also takes a list of patterns to ignore from the snapshot to make working with platform specific files easier.
73
+
74
+
Note: You will commonly need to call `unordered()` before passing your snapshot to deal with platform differences like binaries having `.exe` on Windows.
75
+
`assert_build_dir_layout` is a more specialized version of `assert_dir_layout()` that is automatically unordered and ignores common platform specific files designed for the Cargo build cache.
76
+
67
77
#### Testing Nightly Features
68
78
69
79
If you are testing a Cargo feature that only works on "nightly" Cargo, then
0 commit comments