Skip to content

Commit

Permalink
Merge pull request #20 from tri-adam/goldie
Browse files Browse the repository at this point in the history
Use sebdah/goldie for Golden File Testing
  • Loading branch information
tri-adam authored Jun 14, 2021
2 parents da34172 + d7d59f7 commit 69d9879
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 80 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/satori/go.uuid v1.2.1-0.20180404165556-75cca531ea76
github.com/sebdah/goldie/v2 v2.5.3
github.com/spf13/cobra v1.1.3
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
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=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand Down Expand Up @@ -125,6 +126,7 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand All @@ -143,6 +145,10 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/satori/go.uuid v1.2.1-0.20180404165556-75cca531ea76 h1:ofyVTM1w4iyKwaQIlRR6Ip06mXXx5Cnz7a4mTGYq1hE=
github.com/satori/go.uuid v1.2.1-0.20180404165556-75cca531ea76/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y=
github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
Expand All @@ -161,6 +167,7 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down
8 changes: 4 additions & 4 deletions pkg/integrity/clearsign_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, Sylabs Inc. All rights reserved.
// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
Expand All @@ -15,6 +15,7 @@ import (
"strings"
"testing"

"github.com/sebdah/goldie/v2"
"golang.org/x/crypto/openpgp"
pgperrors "golang.org/x/crypto/openpgp/errors"
"golang.org/x/crypto/openpgp/packet"
Expand Down Expand Up @@ -63,9 +64,8 @@ func TestSignAndEncodeJSON(t *testing.T) {
}

if err == nil {
if err := verifyGolden(t.Name(), &b); err != nil {
t.Fatalf("failed to verify golden: %v", err)
}
g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b.Bytes())
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/integrity/digest_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, Sylabs Inc. All rights reserved.
// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
Expand All @@ -16,6 +16,7 @@ import (
"strings"
"testing"

"github.com/sebdah/goldie/v2"
"github.com/sylabs/sif/pkg/sif"
)

Expand Down Expand Up @@ -162,9 +163,8 @@ func TestDigest_MarshalJSON(t *testing.T) {
}

if err == nil {
if err := verifyGolden(t.Name(), &b); err != nil {
t.Fatalf("failed to verify golden: %v", err)
}
g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b.Bytes())
}
})
}
Expand Down
54 changes: 1 addition & 53 deletions pkg/integrity/main_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
// Copyright (c) 2020, Sylabs Inc. All rights reserved.
// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.

package integrity

import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"

"golang.org/x/crypto/openpgp"
)

var update = flag.Bool("update", false, "update .golden files")

// fixedTime returns a fixed time value, useful for ensuring tests are deterministic.
func fixedTime() time.Time {
return time.Unix(1504657553, 0)
Expand Down Expand Up @@ -83,48 +76,3 @@ func getTestEntity(t *testing.T) *openpgp.Entity {
}
return el[0]
}

// goldenPath returns the path of the golden file corresponding to name.
func goldenPath(name string) string {
// Replace test name separator with OS-specific path separator.
name = path.Join(strings.Split(name, "/")...)
return path.Join("testdata", name) + ".golden"
}

// updateGolden writes b to a golden file associated with name.
func updateGolden(name string, b []byte) error {
p := goldenPath(name)
if err := os.MkdirAll(path.Dir(p), 0755); err != nil {
return err
}
return ioutil.WriteFile(p, b, 0644) // nolint:gosec
}

// verifyGolden compares b to the contents golden file associated with name.
func verifyGolden(name string, r io.Reader) error {
b, err := ioutil.ReadAll(r)
if err != nil {
return err
}

if *update {
if err := updateGolden(name, b); err != nil {
return err
}
}
g, err := ioutil.ReadFile(goldenPath(name))
if err != nil {
return err
}

if !bytes.Equal(b, g) {
return errors.New("output does not match golden file")
}
return nil
}

func TestMain(m *testing.M) {
flag.Parse()

os.Exit(m.Run())
}
28 changes: 12 additions & 16 deletions pkg/integrity/metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, Sylabs Inc. All rights reserved.
// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
Expand All @@ -18,6 +18,7 @@ import (
"testing"

uuid "github.com/satori/go.uuid"
"github.com/sebdah/goldie/v2"
"github.com/sylabs/sif/pkg/sif"
)

Expand Down Expand Up @@ -98,9 +99,8 @@ func TestWriteHeader(t *testing.T) {
t.Fatal(err)
}

if err := verifyGolden(t.Name(), &b); err != nil {
t.Errorf("failed to verify golden: %v", err)
}
g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b.Bytes())
})
}
}
Expand Down Expand Up @@ -199,9 +199,8 @@ func TestWriteDescriptor(t *testing.T) {
t.Fatal(err)
}

if err := verifyGolden(t.Name(), &b); err != nil {
t.Errorf("failed to verify golden: %v", err)
}
g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b.Bytes())
})
}
}
Expand Down Expand Up @@ -246,9 +245,8 @@ func TestGetHeaderMetadata(t *testing.T) {
t.Fatal(err)
}

if err := verifyGolden(t.Name(), &b); err != nil {
t.Errorf("failed to verify golden: %v", err)
}
g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b.Bytes())
}
})
}
Expand Down Expand Up @@ -293,9 +291,8 @@ func TestGetObjectMetadata(t *testing.T) {
t.Fatal(err)
}

if err := verifyGolden(t.Name(), &b); err != nil {
t.Errorf("failed to verify golden: %v", err)
}
g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b.Bytes())
}
})
}
Expand Down Expand Up @@ -350,9 +347,8 @@ func TestGetImageMetadata(t *testing.T) {
t.Fatal(err)
}

if err := verifyGolden(t.Name(), &b); err != nil {
t.Errorf("failed to verify golden: %v", err)
}
g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b.Bytes())
}
})
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/integrity/sign_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, Sylabs Inc. All rights reserved.
// Copyright (c) 2020-2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
Expand All @@ -8,11 +8,13 @@ package integrity
import (
"crypto"
"errors"
"io"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/sebdah/goldie/v2"
"github.com/sylabs/sif/pkg/sif"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
Expand Down Expand Up @@ -430,9 +432,12 @@ func TestGroupSigner_SignWithEntity(t *testing.T) {
t.Errorf("got link %v, want %v", got, want)
}

if err := verifyGolden(t.Name(), di.Fp); err != nil {
t.Errorf("failed to verify golden: %v", err)
b := make([]byte, di.Size)
if _, err := io.ReadFull(di.Fp, b); err != nil {
t.Fatal(err)
}
g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b)
}
})
}
Expand Down

0 comments on commit 69d9879

Please sign in to comment.