Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(update): add update by environment variable #1

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

nkxxll
Copy link

@nkxxll nkxxll commented Sep 9, 2024

Added the second option to update a snapshot test with an environment variable very inspired by the original snapshot testing library from TigerBeetle (see tigerbeetle/tigerbeetle/blob/main/src/testing/snaptest.zig#L109-L112).

This option was, tested but a unit test is kind of useless in this case. The test included the update of the CustomStruct structure like shown in the code snippet below (first snippet before update, second commands, third after update):

test "update env var" {
    const oh = OhSnap{};
    const foobar = CustomStruct{ .foo = 42, .bar = 23 };
    try oh.snap(
        @src(),
        \\
        ,
    ).expectEqual(foobar);
}
# fail on test "update env var"
zig build test
# command to update
SNAP_UPDATE=1 zig build test
# "update env var" test passes
zig build test
test "update env var" {
    const oh = OhSnap{};
    const foobar = CustomStruct{ .foo = 42, .bar = 23 };
    try oh.snap(
        @src(),
        \\ohsnap.CustomStruct
        \\  .foo: u64 = 42
        \\  .bar: u66 = 23
        ,
    ).expectEqual(foobar);
}

PS: I know the code is a little bit repetitive if you have an idea how make it look nicer I'm open for changes.

PPS: I know this is out of the blue. I looked at snapshot testing, then the blog entry from TigerBeetle, then thought I could outsource the snap lib from, them then saw your project and missed this feature while using it hope you like it. Like the idea of this project very much ❤️!

Added the second option to update a snapshot test with an environment
variable very inspired by the original snapshot testing library from
TigerBeetle. This option was, tested but a unit test is kind of useless
in this case. The test included the update of the `CustomStruct`
structure like shown in the code snippet below (first snippet before
update, second after update):

```zig
test "update env var" {
    const oh = OhSnap{};
    const foobar = CustomStruct{ .foo = 42, .bar = 23 };
    try oh.snap(
        @src(),
        \\
        ,
    ).expectEqual(foobar);
}
```

```bash
 # fail on test "update env var"
zig build test
 # command to update
SNAP_UPDATE=1 zig build test
 # "update env var" test passes
zig build test
```

```zig
test "update env var" {
    const oh = OhSnap{};
    const foobar = CustomStruct{ .foo = 42, .bar = 23 };
    try oh.snap(
        @src(),
        \\ohsnap.CustomStruct
        \\  .foo: u64 = 42
        \\  .bar: u66 = 23
        ,
    ).expectEqual(foobar);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant