Fix Black compatible snapshot deletion (#5646)

This commit is contained in:
Micha Reiser 2023-07-10 15:00:18 +02:00 committed by GitHub
parent bd8f65814c
commit 089a671adb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -32,18 +32,20 @@ fn black_compatibility() {
// already perfectly captures the expected output.
// The following code mimics insta's logic generating the snapshot name for a test.
let workspace_path = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let snapshot_name = insta::_function_name!()
.strip_prefix(&format!("{}::", module_path!()))
.unwrap();
let module_path = module_path!().replace("::", "__");
let mut components = input_path.components().rev();
let file_name = components.next().unwrap();
let test_suite = components.next().unwrap();
let snapshot_name = format!(
"black_compatibility@{}__{}.snap",
test_suite.as_os_str().to_string_lossy(),
file_name.as_os_str().to_string_lossy()
);
let snapshot_path = Path::new(&workspace_path)
.join("src/snapshots")
.join(format!(
"{module_path}__{}.snap",
snapshot_name.replace(&['/', '\\'][..], "__")
));
.join("tests/snapshots")
.join(snapshot_name);
if snapshot_path.exists() && snapshot_path.is_file() {
// SAFETY: This is a convenience feature. That's why we don't want to abort
// when deleting a no longer needed snapshot fails.