Giter VIP home page Giter VIP logo

cupaloy's People

Contributors

aeneasr avatar bradleyjkemp avatar byang223 avatar dependabot-preview[bot] avatar gilbsgilbs avatar rudbast avatar smitt04 avatar wgoodall01 avatar zepatrik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cupaloy's Issues

Add negative testing of SnapshotT

Add some tests for when SnapshotT is called on a changed value and ensure that this would fail the test.

Slightly non-trivial because we need to mock testing.T in order to check that this would fail the user's test but without failing our test but should be pretty simple with something like github.com/matryer/moq

Snapshot filename special character scrub needed - semicolon

A recent commit jf-tech/omniparser@1b4997b introduced a snapshot filename containing ;. While itself is fine, i.e. running tests is all good, we ran into issues when the module being imported by 3rd party, hitting an error:

extensions/omniv21/fileformat/flatfile/fixedlength/.snapshots/TestReadAndMatchHeaderFooterBasedEnvelope-empty_buf;_single_line_header-footer_match: malformed file path "extensions/omniv21/fileformat/flatfile/fixedlength/.snapshots/TestReadAndMatchHeaderFooterBasedEnvelope-empty_buf;_single_line_header-footer_match": invalid char ';'

Turns out ; isn't allowed in module filepath: https://cs.opensource.google/go/x/mod/+/master:module/module.go;l=285;drc=02c991387e35f9e4c0a9b7ce137717055f8d716d

Your library already does many of the special character replacement during snapshot file creation. Wondering if ; semicolon can be added?

Cheers!

Allow one-shot configuration options

Allow something of the form:

cupaloy.WithOpt(cupaloy.ErrorMessage("blah")).SnapshotT(t, result)

Where the given functional option only applies to that invocation. This makes using options less verbose when you only need to use them once and makes Snapshotter structs more useful because they can be temporarily modified.

Clarify WithOptions semantics

Currently docs suggest that WithOptions modifies the original whereas the actual semantics are that the original is unchanged and returns a clone with the options applied.

Single vs double spaced output

I am getting inconsistent test failures. The difference is sometimes there are single spaces in the snapshot of a variable, and sometimes there are double spaces. Any ideas on why or how to fix? Thanks.

From go.sum

github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y=
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible/go.mod h1:Au1Xw1sgaJ5iSFktEhYsS0dbQiS1B0/XMXl+42y9Ilk=
...
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

Allow custom shouldUpdate function

We are currently tied to using an environment variable to control snapshot updating. Some people are used to using cli flags so would be nice to be able to support this.

Dependabot can't resolve your Go dependency files

Dependabot can't resolve your Go dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:


If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Add SnapshotMultiT function

I would like to have multiple snapshots from tests cases in table driven tests. For that, I would need a combination of SnapshotMulti and SnapshotT. Would it make sense to have an additional SnapshotTMulti or SnapshotMultiT functions?

var testCases = map[string][]string{
    "TestCaseOne": []string{......},
    "AnotherTestCase": []string{......},
    ....
}

for testName, args := range testCases {
        t.Run(testName, func(t *testing.T) {
                myMock := MyMock{
                         MockFunc: function (input string) {
                                cupaloy.SnapshotTMulti(t, "input", input)
                         },
                }
                result := functionUnderTest(myMock, args...)
                cupaloy.SnapshotTMulti(t, "result", result)
        })
}

Another possibility would be that the *testing.T can be passed into the constructor of Cupaloy (This is how https://github.com/matryer/is works):

var testCases = map[string][]string{
    "TestCaseOne": []string{......},
    "AnotherTestCase": []string{......},
    ....
}

for testName, args := range testCases {
        t.Run(testName, func(t *testing.T) {
                cupaloy := cupaloy.New(cupaloy.T(t))

                myMock := MyMock{
                         MockFunc: function (input string) {
                                cupaloy.SnapshotMulti("input", input)
                         },
                }
                result := functionUnderTest(myMock, args...)
                cupaloy.SnapshotMulti("result", result)
        })
}

Fix broken Sourcegraph badge

Sourcegraph unexpectedly removed the used-by badge feature replacing it with a dummy value.
Find a replacement or remove the badge from the readme.

Use a backwards compatible snapshot format

Currently we use a raw text snapshot format. If we moved to something like JSON then we could version this to allow for breaking changes to the snapshot format without breaking everyone's tests.

Allow customising error messages

The built in error message is very opaque (necessary because it has to be generic) but users can likely provide a more helpful friendly message customised to their tests (or e.g. when the diff just isn't useful). Therefore an option to customise this message would be useful.

This would only really be usable once #28 is implemented.

Make obvious that SnapshotT is the preferred method

Snapshot and SnapshotMulti are not deprecated but for most cases they are the wrong choice (too easy to forget to check the return value and hence tests always pass).
They do have their uses but it should be made clear in the docs that SnapshotT should be used with preference.

Sort map keys when spewing

I'm trying to snapshot a struct whose one of its fields is a map. Sometimes the test passes, at other times it doesn't. Go maps are randomly sorted by default. However, go-spew has a SortKeys configuration that solves this problem, but it doesn't seem to be used here.

I'm sorry if I'm missing something here, I didn't read the source code so I'm guessing it's a bug and which bug it is, but I might have missed some docs.

Thanks nonetheless!

Fix Sourcegraph badge

The badge endpoint takes a long time to respond and so cloudflare returns a 500, this then seems to get cached by GitHub as a broken/missing image.

can't install

docs here https://pkg.go.dev/github.com/pyros2097/cupaloy#section-readme
say use

go get -u github.com/bradleyjkemp/cupaloy

README on here doesn't even mention how to install. Not even sure if same package.

I get

go get: module github.com/bradleykemp/cupaloy: git ls-remote -q origin in /Users/kevin/.go/pkg/mod/cache/vcs/80d9f6d7d4a4bdada3cd1ba5dfc1c3543d1449b3cb2ccd046ae294400d51c447: exit status 128:
	fatal: could not read Username for 'https://github.com': terminal prompts disabled
Confirm the import path was entered correctly.

No other package in my current project is having this problem.

However, I got a clue from pkg.go.dev docs

go get github.com/pyros2097/cupaloy

which worked.

Allow ignoring contents of specific structure fields

Some struct fields might be known and intended to not remain constant between test runs. It would then be required to ignore these fields (perhaps just by overwriting with a constant value)
Arguably tests should be deterministic though.

Support for JSON Snapshots

Currently the diffing tool used by Cupaloy treats strings as just a sequence of characters. Which is reasonable, but limited.

In particular, it would be fantastic if there were a way to tell it to treat the two strings as JSON. Which would mean:

  • Whitespace is ignored
  • Object keys are unordered

That way, any changes to the output that are changes to the raw string but are not meaningful changes to the JSON structure are ignored and will not cause the tests to fail.

In particular, I've had an issue where VSCode auto-formats the JSON within a snapshot file when I'm making changes to it, which then means that everything breaks. Often it actually means it's quicker to delete the file and regenerate it rather than just fixing up one value that's changed because of a code change.

Use of internal package not allowed

After installing this package using govendor running the tests it fails with the error

package command-line-arguments (test)
        imports github.com/bradleyjkemp/cupaloy
        imports github.com/bradleyjkemp/cupaloy/v2/internal: use of internal package github.com/bradleyjkemp/cupaloy/internal not allowed
FAIL    command-line-arguments [setup failed]

Don't create snapshots in a failed test

While writing a bunch of tests, I noticed that Cupaloy will still write snapshots on the first invocation of a test, even if that test has already failed. For instance:

func TestShouldNotWriteSnapshot(t *testing.T) {
	t.Fail()
	cupaloy.SnapshotT(t, "bad snapshot")
}

will still log:

--- FAIL: TestShouldNotWriteSnapshot (0.00s)
	board_test.go:199: snapshot created for test TestShouldNotWriteSnapshot

and write a snapshot to .snapshots/TestShouldNotWriteSnapshot.

I'm not sure if this is the intended behavior or not, but it seems like a bad idea to write snapshots for a test that is known to have failed. Maybe we should check t.Failed() inside cupaloy.SnapshotT?

Do not spew results using Stringer methods?

This can hide significant portions of the result being spewed and unnecessarily ties snapshots to the (designed for users) String() implementation.

This should just involve setting DisableMethods: true in the Spew config but we need to check that this isn't going to affect any known users (https://github.com/search?l=Go&o=desc&q="bradleyjkemp%2Fcupaloy"&s=indexed&type=Code) i.e. that no one is currently snapshotting types implementing the Stringer interface

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.