Skip to content

Commit

Permalink
fix: add support for partial updates (#59)
Browse files Browse the repository at this point in the history
* fix: support updating only specific snapshots

also it adds support for only cleaning obsolete snaps and files

* fix: update deps
  • Loading branch information
gkampitakis authored May 15, 2023
1 parent 72d515c commit 6c41cd3
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.19.x
go-version: 1.20.x
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand All @@ -37,7 +37,7 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
go: ['1.16.x', '1.17.x', '1.18.x', '1.19.x']
go: ['1.16.x', '1.17.x', '1.18.x', '1.19.x', '1.20.x']
steps:
- name: Set git to use LF
run: |
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/gkampitakis/go-snaps
go 1.16

require (
github.com/gkampitakis/ciinfo v0.2.2
github.com/gkampitakis/go-diff v1.3.0
github.com/gkampitakis/ciinfo v0.2.4
github.com/gkampitakis/go-diff v1.3.2
github.com/kr/pretty v0.3.1
)

Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/gkampitakis/ciinfo v0.2.1 h1:LyUKWarjkDRPO6qnoNcj/+pA+Hc2oh4BBEg9Cwn0JeA=
github.com/gkampitakis/ciinfo v0.2.1/go.mod h1:bVaOGziPqf8PoeYZxatq1HmCsJUmv191hLnFboYxd9Y=
github.com/gkampitakis/ciinfo v0.2.2 h1:CjsrlahWwk65bIRfNz3sjEQ81GKWKC1c/48UB0ABpSU=
github.com/gkampitakis/ciinfo v0.2.2/go.mod h1:bVaOGziPqf8PoeYZxatq1HmCsJUmv191hLnFboYxd9Y=
github.com/gkampitakis/go-diff v1.3.0 h1:Szdbo5w73LSQ9sQ02h+NSSf2ZlW/E8naJCI1ZzQtWgE=
github.com/gkampitakis/go-diff v1.3.0/go.mod h1:QUJDQRA0JkEX0d7tgDaBHzJv9IH6k6e91TByC+9/RFk=
github.com/gkampitakis/ciinfo v0.2.4 h1:Ip1hf4K7ISRuVlDrheuhaeffg1VOhlyeFGaQ/vTxrtE=
github.com/gkampitakis/ciinfo v0.2.4/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZdC4M=
github.com/gkampitakis/go-diff v1.3.2/go.mod h1:LLgOrpqleQe26cte8s36HTWcTmMEur6OPYerdAAS9tk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
6 changes: 3 additions & 3 deletions snaps/__snapshots__/clean_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
1 snapshot file obsolete
 ↳ • test0.snap

To remove it, re-run tests with `UPDATE_SNAPS=true go test ./...`
To remove it, re-run tests with `UPDATE_SNAPS=clean go test ./...`

---

Expand All @@ -20,7 +20,7 @@ To remove it, re-run tests with `UPDATE_SNAPS=true go test ./...`
 ↳ • TestMock/should_pass - 1
 ↳ • TestMock/should_pass - 2

To remove them, re-run tests with `UPDATE_SNAPS=true go test ./...`
To remove them, re-run tests with `UPDATE_SNAPS=clean go test ./...`

---

Expand Down Expand Up @@ -83,6 +83,6 @@ To remove them, re-run tests with `UPDATE_SNAPS=true go test ./...`
1 snapshot test obsolete
 ↳ • TestMock/should_pass - 1

To remove them, re-run tests with `UPDATE_SNAPS=true go test ./...`
To remove them, re-run tests with `UPDATE_SNAPS=clean go test ./...`

---
8 changes: 4 additions & 4 deletions snaps/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func Clean(t *testing.M) {
_ = t
runOnly := flag.Lookup("test.run").Value.String()

obsoleteFiles, usedFiles := examineFiles(testsRegistry.values, runOnly, shouldUpdate)
obsoleteTests, err := examineSnaps(testsRegistry.values, usedFiles, runOnly, shouldUpdate)
obsoleteFiles, usedFiles := examineFiles(testsRegistry.values, runOnly, shouldClean)
obsoleteTests, err := examineSnaps(testsRegistry.values, usedFiles, runOnly, shouldClean)
if err != nil {
fmt.Println(err)
return
Expand All @@ -75,7 +75,7 @@ func Clean(t *testing.M) {
obsoleteTests,
len(skippedTests.values),
testEvents.items,
shouldUpdate); s != "" {
shouldClean); s != "" {
fmt.Print(s)
}
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func summary(
&s,
colors.Dim,
fmt.Sprintf(
"\nTo remove %s, re-run tests with `UPDATE_SNAPS=true go test ./...`\n",
"\nTo remove %s, re-run tests with `UPDATE_SNAPS=clean go test ./...`\n",
it,
),
)
Expand Down
4 changes: 4 additions & 0 deletions snaps/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ func singlelineDiff(s *strings.Builder, expected, received string) (int, int) {
}

func prettyDiff(expected, received string) string {
if expected == received {
return ""
}

if shouldPrintHighlights(expected, received) {
var diff strings.Builder
if i, d := singlelineDiff(&diff, expected, received); i != -1 {
Expand Down
2 changes: 1 addition & 1 deletion snaps/matchJSON.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func MatchJSON(t testingT, input interface{}, matchers ...match.JSONMatcher) {
return
}

if !shouldUpdate {
if !shouldUpdateSingle(t.Name()) {
handleError(t, diff)
return
}
Expand Down
2 changes: 1 addition & 1 deletion snaps/matchSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func MatchSnapshot(t testingT, values ...interface{}) {
return
}

if !shouldUpdate {
if !shouldUpdateSingle(t.Name()) {
handleError(t, diff)
return
}
Expand Down
16 changes: 8 additions & 8 deletions snaps/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
var (
errSnapNotFound = errors.New("snapshot not found")
isCI = ciinfo.IsCI
shouldUpdate = getEnvBool("UPDATE_SNAPS", false) && !isCI
envVar = os.Getenv("UPDATE_SNAPS")
shouldUpdate = envVar == "true" && !isCI
shouldClean = shouldUpdate || envVar == "clean" && !isCI
)

const (
Expand Down Expand Up @@ -129,11 +131,9 @@ func isTest(name, prefix string) bool {
return !unicode.IsLower(r)
}

func getEnvBool(variable string, fallback bool) bool {
e, exists := os.LookupEnv(variable)
if !exists {
return fallback
}

return e == "true"
// shouldUpdateSingle returns if a single should be updated or not
//
// it depends on the general should update or if the given name on `UPDATE_SNAPS` matches the current test.
func shouldUpdateSingle(tName string) bool {
return shouldUpdate || (!isCI && envVar != "" && strings.HasPrefix(tName, envVar))
}
46 changes: 27 additions & 19 deletions snaps/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,40 @@ func TestBaseCaller(t *testing.T) {
})
}

func TestEnv(t *testing.T) {
t.Run("should return true if env var is 'true'", func(t *testing.T) {
test.SetEnv(t, "MOCK_ENV", "true")
func TestShouldUpdateSingle(t *testing.T) {
setup := func() {
shouldUpdate = false
isCI = false
envVar = ""
}

t.Run("should be true if shouldUpdate", func(t *testing.T) {
setup()

shouldUpdate = true
test.True(t, shouldUpdateSingle(""))
})

res := getEnvBool("MOCK_ENV", false)
t.Run("should be true if specific test provided from envVar", func(t *testing.T) {
setup()
envVar = "mock-test/"
shouldUpdate = false

if !res {
t.Error("getEnvBool should return true")
}
test.True(t, shouldUpdateSingle("mock-test/should_pass - 1"))
test.False(t, shouldUpdateSingle("mock-test-2/should_pass - 1"))
})

t.Run("should return false", func(t *testing.T) {
test.SetEnv(t, "MOCK_ENV", "")

res := getEnvBool("MOCK_ENV", true)
t.Run("should be false if running on CI", func(t *testing.T) {
setup()
envVar = "mock-test/"
isCI = true

if res {
t.Error("getEnvBool should return false")
}
test.False(t, shouldUpdateSingle("mock-test/should_pass - 1"))
})

t.Run("should return fallback value for non existing env var", func(t *testing.T) {
res := getEnvBool("MISSING_ENV", true)
t.Run("should be false if not envVar provided", func(t *testing.T) {
setup()

if !res {
t.Error("getEnvBool should return false")
}
test.False(t, shouldUpdateSingle("mock-test"))
})
}

0 comments on commit 6c41cd3

Please sign in to comment.