├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── test.yml ├── .gitignore ├── LICENSE ├── Readme.md ├── fixture.go ├── go.mod └── go.sum /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: tj -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Prerequisites 2 | 3 | * [ ] I searched to see if the issue already exists. 4 | 5 | ## Description 6 | 7 | Describe the bug or feature. 8 | 9 | ## Steps to Reproduce 10 | 11 | Describe the steps required to reproduce the issue if applicable. 12 | 13 | ## Slack 14 | 15 | Join us on Slack https://chat.apex.sh/ 16 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please open an issue and discuss changes before spending time on them, unless the change is trivial or an issue already exists. 2 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | name: Tests 3 | jobs: 4 | test: 5 | strategy: 6 | matrix: 7 | go-version: [1.14.x] 8 | platform: [ubuntu-latest, macos-latest, windows-latest] 9 | runs-on: ${{ matrix.platform }} 10 | steps: 11 | - name: Install Go 12 | uses: actions/setup-go@v1 13 | with: 14 | go-version: ${{ matrix.go-version }} 15 | - name: Checkout code 16 | uses: actions/checkout@v1 17 | - name: Test 18 | run: go test ./... -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .envrc 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2020 TJ Holowaychuk tj@tjholowaychuk.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Fixture 2 | 3 | Package fixture provides test assertions using test fixtures with nice line diffs, and an -update flag for bootstrapping & updating fixtures. 4 | 5 | ## Example 6 | 7 | ```go 8 | func TestGenerate(t *testing.T) { 9 | // ... pretend we generate some client code here for TypeScript 10 | var act bytes.Buffer 11 | err = tsclient.Generate(&act) 12 | assert.NoError(t, err, "generating") 13 | 14 | // assert the contents of act.Bytes() to the test fixture ./testdata/client.ts 15 | fixture.Assert(t, "client.ts", act.Bytes()) 16 | } 17 | ``` 18 | 19 | --- 20 | 21 | [![GoDoc](https://godoc.org/github.com/tj/{PROJECT}?status.svg)](https://godoc.org/github.com/tj/{PROJECT}) 22 | ![](https://img.shields.io/badge/license-MIT-blue.svg) 23 | ![](https://img.shields.io/badge/status-stable-green.svg) 24 | ![](https://github.com/tj/{PROJECT}/workflows/Tests/badge.svg) 25 | 26 | ## Sponsors 27 | 28 | This project is sponsored by [CTO.ai](https://cto.ai/), making it easy for development teams to create and share workflow automations without leaving the command line. 29 | 30 | [![](https://apex-software.imgix.net/github/sponsors/cto.png)](https://cto.ai/) 31 | 32 | And my [GitHub sponsors](https://github.com/sponsors/tj): 33 | 34 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/0) 35 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/1) 36 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/2) 37 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/3) 38 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/4) 39 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/5) 40 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/6) 41 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/7) 42 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/8) 43 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/9) 44 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/10) 45 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/11) 46 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/12) 47 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/13) 48 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/14) 49 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/15) 50 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/16) 51 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/17) 52 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/18) 53 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/19) 54 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/20) 55 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/21) 56 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/22) 57 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/23) 58 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/24) 59 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/25) 60 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/26) 61 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/27) 62 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/28) 63 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/29) 64 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/30) 65 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/31) 66 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/32) 67 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/33) 68 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/34) 69 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/35) 70 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/36) 71 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/37) 72 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/38) 73 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/39) 74 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/40) 75 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/41) 76 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/42) 77 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/43) 78 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/44) 79 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/45) 80 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/46) 81 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/47) 82 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/48) 83 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/49) 84 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/50) 85 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/51) 86 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/52) 87 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/53) 88 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/54) 89 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/55) 90 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/56) 91 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/57) 92 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/58) 93 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/59) 94 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/60) 95 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/61) 96 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/62) 97 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/63) 98 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/64) 99 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/65) 100 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/66) 101 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/67) 102 | [](https://sponsors-api-u2fftug6kq-uc.a.run.app/sponsor/profile/68) 103 | -------------------------------------------------------------------------------- /fixture.go: -------------------------------------------------------------------------------- 1 | // Package fixture provides test assertions using test fixtures with nice line diffs, and an -update flag for updating fixtures. 2 | package fixture 3 | 4 | import ( 5 | "flag" 6 | "io/ioutil" 7 | "path/filepath" 8 | "testing" 9 | 10 | "github.com/shibukawa/cdiff" 11 | ) 12 | 13 | // update flag. 14 | var update = flag.Bool("update", false, "Update test fixtures.") 15 | 16 | // Read a test fixture from the "testdata" directory. 17 | func Read(t testing.TB, name string) []byte { 18 | t.Helper() 19 | path := filepath.Join("testdata", name) 20 | b, err := ioutil.ReadFile(path) 21 | if err != nil { 22 | t.Fatalf("error reading fixture %q: %s", name, err) 23 | } 24 | return b 25 | } 26 | 27 | // Write a test fixture to the "testdata" directory. 28 | func Write(t testing.TB, name string, b []byte) { 29 | t.Helper() 30 | path := filepath.Join("testdata", name) 31 | err := ioutil.WriteFile(path, b, 0755) 32 | if err != nil { 33 | t.Fatalf("error writing fixture %q: %s", path, err) 34 | } 35 | } 36 | 37 | // Assert that the contents of fixture name matches the expected output. 38 | func Assert(t testing.TB, name string, expected []byte) { 39 | // update fixtures 40 | if *update { 41 | t.Logf("updating test fixture %q", name) 42 | Write(t, name, expected) 43 | } 44 | 45 | // read fixture 46 | actual := Read(t, name) 47 | 48 | // assert 49 | act := string(actual) 50 | exp := string(expected) 51 | if act != exp { 52 | result := cdiff.Diff(exp, act, cdiff.LineByLine) 53 | t.Fatalf("Result does not match %q:\n%s", name, result.UnifiedWithGooKitColor("Expected", "Actual", 5, cdiff.GooKitColorTheme)) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tj/go-fixture 2 | 3 | go 1.14 4 | 5 | require github.com/shibukawa/cdiff v0.1.3 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= 2 | github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= 3 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 4 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/gookit/color v1.2.0 h1:lHA77Kuyi5JpBnA9ESvwkY+nanLjRZ0mHbWQXRYk2Lk= 6 | github.com/gookit/color v1.2.0/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg= 7 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 8 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 9 | github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= 10 | github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= 11 | github.com/shibukawa/cdiff v0.1.3 h1:0ren00CxjQKvP0IqS1aVDZ/eFIcLXNZ9cmru22t6CTU= 12 | github.com/shibukawa/cdiff v0.1.3/go.mod h1:7ewfFiaynzVpGSV03BbT2IsthIWQRPG2ejUVs9AWkCA= 13 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 14 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 15 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 16 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 17 | gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= 18 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 19 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 20 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 21 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 22 | --------------------------------------------------------------------------------