Code Monkey home page Code Monkey logo

goldie's People

Contributors

adamhicks avatar almalki avatar danawoodman avatar derekbassett avatar eripa avatar gtrevg avatar jakdept avatar ltpquang avatar mightyguava avatar nervo avatar novas0x2a avatar sebdah avatar takashabe avatar xorcare 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

goldie's Issues

what about a `mkdir` flag?

Love this.

One minor annoyance - when I add a completely new test, I have to create a directory within my fixtures directory by hand ex:

In this example, FixtureDir is testdata/fixtures, and in this case I just added a table test called TestThumbnailJPG with subtests TestThumbnail-jpg-0 etc. So, then when i go test -update it still fails:

--- FAIL: TestThumbnailJPG (2.88s)
    --- FAIL: TestThumbnailJPG/TestThumbnail-jpg-0- (0.10s)
    	goldie.go:56: open testdata/fixtures/TestThumbnailJPG/TestThumbnail-jpg-0-.golden: no such file or directory
    --- FAIL: TestThumbnailJPG/TestThumbnail-jpg-1- (0.08s)
    	goldie.go:56: open testdata/fixtures/TestThumbnailJPG/TestThumbnail-jpg-1-.golden: no such file or directory
    --- FAIL: TestThumbnailJPG/TestThumbnail-jpg-2- (0.09s)
    	goldie.go:56: open testdata/fixtures/TestThumbnailJPG/TestThumbnail-jpg-2-.golden: no such file or directory

The -update flag fails because the directory for that test does not exist. So, I mkdir then it goes well.

It'd be nice to have a -mkdir flag that assumes -update, or maybe -update mkdir as an option command to the -update flag or something - so when you run that, it automatically creates the required folders. I'd only see a use for that flag when you've changed FixtureDir or when you've written a new test - with a new test name - so I think I'd rather it not happen every time you -update.

-update flag doing nothing

I have a test file like:

// +build e2e

package e2e

import (
	"testing"

	"github.com/sebdah/goldie/v2"
)

func TestFoo(t *testing.T) {
	g := goldie.New(t)
	out := []byte("some-text")
	g.Assert(t, "example", out)
}

And I am running:

go test -tags=e2e -update -clean ./...

And the output is:

?   	github.com/myrepo/myproj	[no test files]

I would expect it to create the golden file but it does nothing. If I remove the -update flag the the test runs but fails because of no golden file.

Manually creating the golden file does work but I cannot get the -update flag to create the file.

I am using v2.

Any ideas what I am doing wrong?

Reduce duplication of testing.T argument

It would be nice if the next version used a syntax similar to stretchr/testify. Since t is passed in to New() we shouldn't need to pass it in again for each assertion.

See this example from stretchr/testify:

package yours

import (
  "testing"
  "github.com/stretchr/testify/assert"
)

func TestSomething(t *testing.T) {
  assert := assert.New(t)

  // assert equality
  assert.Equal(123, 123, "they should be equal")

  // assert inequality
  assert.NotEqual(123, 456, "they should not be equal")

  // assert for nil (good for errors)
  assert.Nil(object)

  // assert for not nil (good when you expect something)
  if assert.NotNil(object) {

    // now we know that object isn't nil, we are safe to make
    // further assertions without causing any errors
    assert.Equal("Something", object.Value)
  }
}

For goldie it would look like this:

func TestExample(t *testing.T) {
    recorder := httptest.NewRecorder()

    req, err := http.NewRequest("GET", "/example", nil)
    assert.Nil(t, err)

    handler := http.HandlerFunc(ExampleHandler)
    handler.ServeHTTP()

    g := goldie.New(t)
    g.Assert("example", recorder.Body.Bytes()) // notice lack of `t` arg here.
}

If there is interest, I would be happy to make a PR.

Helper for XML

Given encoding.json and encoding.xml are both standard packages it would be nice to have the same support for XML as for JSON.
It is a simple addition to provide the helper method, but does break any other Tester API implementations.

Update pattern for templated golden files

First, thanks for building this package!

I'm trying to leverage the goldie.AssertWithTemplate feature along with the pattern of using the -update flag when the file doesn't exist.

In regards to the template golden file, I would expect the process to be something like:

  • Write unit test
  • Run go test and see it fails with the use -update flag message
  • Run go test -update and see the example.golden file gets created
  • Replace Golden in the generated file with {{ .Type }}
  • Run go test and see everything passes
  • Write some table driven tests that iterate through many different types

At that point, everything seems good. Then we

  • Create some more unit tests in that package
  • Need to generate the initial golden files for those tests, so we run go test -update
  • Run go test to validate
  • Tests fail because the templated example.golden file is now overwritten with a single value and doesn't retain the initial {{ .Type }} that we put in there.

I can imagine that it would be relatively simple to go back and change the one overwritten .Type variable in this case. It is a bit more complex, though, if you had a bunch of variables throughout a large .golden file.

Do you have any thoughts on how to preserve the original template variables while doing an -update? One idea is that on -update, the template code could be smart enough to replace all scalar values in the data passed in with {{ .Dot.Path.To.Key }}. That way, when .Type writes itself out to the golden file, it'll end up writing itself out as {{ .Type }} once again, thus preserving the template variables while updating all the other content of the golden file.

Thanks!

test failed with Json data

testdata/foo.golden

{
    "foo": 1,
    "bar": 3
}

foo_test.go

package main

import (
	"testing"

	"github.com/sebdah/goldie/v2"
)

func TestFoo(t *testing.T) {
	g := goldie.New(t)

	g.AssertJson(t, "something", `{"foo": 1, "bar": 2}`)
}

test failed

=== RUN   TestFoo
    foo_test.go:12: Result did not match the golden fixture. Diff is below:

        --- Expected
        +++ Actual
        @@ -1,5 +1 @@
        -{
        -    "foo": 1,
        -    "bar": 3
        -}
        -
        +"{\"foo\": 1, \"bar\": 2}"

--- FAIL: TestFoo (0.00s)

Clean testdata/fixture dir before generate new goldie file

Should we clean the output directory before running go test -update? Since every time I update the test cases that changes the test cases' names, the old goldie files stay untouch, and I need to delete the files manually to keep the dir clean and clear.

.Contains()-type functionality?

Would it be possible to have a .Contains() method that would check just part of the golden file? I have a golden file where some parts will change (like generic help text from a CLI) and all I really want to check for is "success" or similar.

I know that means its not really using the golden file but it would make the test less fragile.

Thoughts?

Assertion Failure Should List Name

Something like this:

g.AssertJson(t, "test_case_23.json", jsonData)

Will result in:

Result did not match the golden fixture. Diff is below:
        
        --- Expected
        +++ Actual
        @@ -1 +1,12 @@
        -[]
        +[
        +  {
        +    "Min": {
        +      "X": 7,
        +      "Y": 3
        +    },
        +    "Max": {
        +      "X": 28,
        +      "Y": 50
        +    }
        +  }
        +]

For table driven tests, you can't tell which one failed. I propose adding the name field to the assert failure logging.

feature request: output as diff view

I'm regularly using this tool for regression tests.
While using I found a typical use case that's not yet perfectly covered.
I'm often pretty printing structs to json, e.g. to regression test the output of an adapter.
It regularly happens that I have to split the output into two files and use an external tool to diff view the expected value vs. the actual value to quickly identify the change.

I'd like to add to your api another method/option to output as diff view.
Do you like to add this feature or are you open to pull requests?

Template fixtures break non-text fixtures

It appears that in commit f0e441682ff73cf164ffd4f0cb53978f0f29a7ff when template support was added, everything now uses template support. Also, template support breaks if you use an image, or other non-text file for your fixture. text/template.Parse() cannot parse non text and thus fails that function before getting to bytes.Equal().

Minimal changes I think are to move https://github.com/sebdah/goldie/blob/master/goldie.go#L123 up above the Template code, return nil if match, and go onto the template work if not exact match? But then probably also add a PNG image template + test for a regression test?

Cannot go get v2 via modules

Hi, I'm trying to install goldie to project with modules. And error about incompatible version appears. Could you help me get around this problem?

> go get github.com/sebdah/[email protected]
go: finding github.com/sebdah/goldie v2.0.0
go get github.com/sebdah/[email protected]: github.com/sebdah/[email protected]: invalid version: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2

go version go1.13.4 darwin/amd64

Change FixtureDir to `testdata`

By default, this package uses the directory FixtureDir = "fixtures" , but in Go common practice of using the name testdata I would like to see that you have in the package by default used the dir name testdata

Examples in standard library
https://github.com/golang/go/tree/master/src/cmd/gofmt/testdata
https://github.com/golang/go/tree/master/src/encoding/json/testdata

P. S. For backward compatibility, you need to share version tags for v1.0.0 and v2.0.0 to current users of the library have not received problems.

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.