Skip to content

Commit 689a1d3

Browse files
committed
docs(test/clitools): add docs for Assert
1 parent a88c6f9 commit 689a1d3

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/test/clitools.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,61 @@ pub struct Config {
6565
pub test_root_dir: PathBuf,
6666
}
6767

68+
/// Helper type to simplify assertions of a command's output.
69+
///
70+
/// Typically, an [`Assert`] instance is created by calling the
71+
/// [`Config::expect()`] method (or its variations) which will run the given
72+
/// command in the test environment and wrap its output in the instance.
73+
/// Then, that output can be compared against the expected one.
74+
///
75+
/// # Snapshot-Based Testing
76+
///
77+
/// Currently, rustup utilizes [`snapbox`] (a snapshot-based testing library)
78+
/// for most comparisons, where the corresponding [`Assert`] method would
79+
/// accept an `expected` argument conforming [`IntoData`], which is also
80+
/// called a "snapshot".
81+
///
82+
/// Most of our tests use [inline][`snapbox::data::Inline`] snapshots created
83+
/// with [`snapbox::str`], but it is also possible to use snapshots from
84+
/// another file with [`snapbox::file`].
85+
///
86+
/// # Creating or Updating a Snapshot
87+
///
88+
/// There is no need to write out a snapshot by hand. Instead, create an empty
89+
/// snapshot (e.g. `snapbox::str![[""]]`), and run the test in question with the
90+
/// environment variable `SNAPSHOTS=overwrite` set. Normally, the snapshot will
91+
/// then be populated automatically with the right value (modulo redaction,
92+
/// which will be discussed in a later section).
93+
///
94+
/// The same environment variable is also used to update a snapshot with no extra
95+
/// steps required.
96+
///
97+
/// # Redacting Snapshots
98+
///
99+
/// To defend against leaking of environment-specific information that might
100+
/// break the tests on a different machine, a set of redaction rules (also known
101+
/// as "filters") is used to sanitize the output before sending to comparison.
102+
///
103+
/// Rustup extends the list of `snapbox`'s default filters (see
104+
/// [`assert_data_eq`]'s documentation for more info) with a list of
105+
/// rustup-specific values, including:
106+
/// - `[CURRENT_VERSION]`: The current rustup version.
107+
/// - `[HOST_TRIPLE]`: The return value of [`this_host_triple()`].
108+
/// - `[CROSS_ARCH_I]`: The value of [`CROSS_ARCH1`].
109+
/// - `[CROSS_ARCH_II]`: The value of [`CROSS_ARCH2`].
110+
/// - `[MULTI_ARCH_I]`: The value of [`MULTI_ARCH1`].
111+
///
112+
/// When updating snapshots, the filters are automatically applied.
113+
/// To redact some remaining part of the snapshot, you can use the
114+
/// [`Assert::extend_redactions`] method to introduce new filters.
68115
#[derive(Clone)]
69116
pub struct Assert {
70117
output: SanitizedOutput,
71118
redactions: Redactions,
72119
}
73120

74121
impl Assert {
75-
/// Creates a new [`Assert`] object with the given command [`SanitizedOutput`].
122+
/// Creates a new [`Assert`] object with the given [`SanitizedOutput`].
76123
pub fn new(output: SanitizedOutput) -> Self {
77124
let mut redactions = Redactions::new();
78125
redactions

0 commit comments

Comments
 (0)