Skip to content

Add canonicalise paths to snapshots #423

@Danil42Russia

Description

@Danil42Russia

Our project has its own wrappers for loading and comparing the programme output with a snapshot. It looks something like this:

fn assert_snapshot_matches(&self, snapshot_test_path: &str) -> &Self {
    let assertion = assertion();
    let normalized = self.get_normalized_stdout();

    let mut snapshot_path = std::env::current_dir()?;
    snapshot_path.push(snapshot_test_path);

    let expected = Data::read_from(&snapshot_path, None);
    assertion.eq(normalized, expected);
    self
}

However, sometimes the symbol for transitioning to the parent directory .. may appear in the paths to the snapshot. Because of this, when submitting tests, you may see the following

---- expected: tests/integration/../integration/snapshots/snapshot_name.stdout.txt
++++ actual:   In-memory

How about adding Path::canonicalize to the call, where you save the snapshot?

pub fn path(path: impl Into<std::path::PathBuf>) -> Self {
Self {
inner: DataSourceInner::Path(path.into()),
}
}

But in Windows, truncating the current directory may break due to UNC paths :(

pub(crate) fn display_relpath(path: impl AsRef<std::path::Path>) -> String {
let path = path.as_ref();
let relpath = if let Ok(cwd) = std::env::current_dir() {
match path.strip_prefix(cwd) {
Ok(path) => path,
Err(_) => path,
}
} else {
path
};
relpath.display().to_string()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions