Skip to content

Snapbox updated the same inline snapshots shared between different tests  #345

Closed
@weihanglo

Description

@weihanglo

When an inline snapshot is shared between different #[test], it could be updated multiple times, causing the final updated snapshot being wrong.

Here is a reproducible case using Cargo -Zscript:

---
[dependencies]
snapbox = "=0.6.10"
---

pub fn shared_snapshots() {
    snapbox::assert_data_eq!("Bad hobbits!", snapbox::str![]);
    snapbox::assert_data_eq!("My precious!\n", snapbox::str![]);
}

#[test]
fn frodo_baggins() {
    shared_snapshots();
}
#[test]
fn samwise_gamgee() {
    shared_snapshots();
}
#[test]
fn peregrin_took() {
    shared_snapshots();
}
#[test]
fn meriadoc_brandybuck() {
    shared_snapshots();
}

You can found snapbox has overwritten the expected string multiple times:

$ SNAPSHOTS=overwrite cargo +nightly -Zscript test --manifest-path lib.rs
...
...

Fixing: 
---- expected: /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs:8:48
++++ actual:   In-memory
        1 + My precious!

Fixing: 
---- expected: /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs:8:48
++++ actual:   In-memory
        1 + My precious!

Fixing: 
---- expected: /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs:8:48
++++ actual:   In-memory
        1 + My precious!

Fixing: 
---- expected: /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs:8:48
++++ actual:   In-memory
        1 + My precious!

test frodo_baggins ... ok
test meriadoc_brandybuck ... ok
test samwise_gamgee ... ok
test peregrin_took ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

$ cat /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs
pub fn shared_snapshots() {
    snapbox::assert_data_eq!("Bad hobbits!", snapbox::str!["Bad hobbits!""Bad hobbits!""Bad hobbits!""Bad hobbits!"]);
    snapbox::assert_data_eq!("My precious!\n", snapbox::str![[r#"
My precious!

"#][r#"
My precious!

"#][r#"
My precious!

"#][r#"
My precious!

"#]]);
}

#[test]
fn frodo_baggins() {
    shared_snapshots();
}
#[test]
fn samwise_gamgee() {
    shared_snapshots();
}
#[test]
fn peregrin_took() {
    shared_snapshots();
}
#[test]
fn meriadoc_brandybuck() {
    shared_snapshots();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-snapboxArea: snapbox packagebugNot as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions