Skip to content

Commit

Permalink
fix: add natural sort
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis committed Nov 3, 2023
1 parent 1d3f605 commit 9398840
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/gkampitakis/ciinfo v0.3.0
github.com/gkampitakis/go-diff v1.3.2
github.com/kr/pretty v0.3.1
github.com/maruel/natural v1.1.0
github.com/tidwall/gjson v1.17.0
github.com/tidwall/pretty v1.2.1
github.com/tidwall/sjson v1.2.5
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/gkampitakis/ciinfo v0.2.5 h1:K0mac90lGguc1conc46l0YEsB7/nioWCqSnJp/6z8Eo=
github.com/gkampitakis/ciinfo v0.2.5/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
github.com/gkampitakis/ciinfo v0.3.0 h1:gWZlOC2+RYYttL0hBqcoQhM7h1qNkVqvRCV1fOvpAv8=
github.com/gkampitakis/ciinfo v0.3.0/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZdC4M=
Expand All @@ -9,13 +7,13 @@ 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=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/maruel/natural v1.1.0 h1:2z1NgP/Vae+gYrtC0VuvrTJ6U35OuyUqDdfluLqMWuQ=
github.com/maruel/natural v1.1.0/go.mod h1:eFVhYCcUOfZFxXoDZam8Ktya72wa79fNC3lc/leA0DQ=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
Expand Down
36 changes: 25 additions & 11 deletions snaps/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"

"github.com/gkampitakis/go-snaps/internal/colors"
"github.com/maruel/natural"
)

// Matches [ Test... - number ] testIDs
Expand Down Expand Up @@ -82,13 +83,13 @@ func Clean(t *testing.M, opts ...CleanOpts) {
_ = t
runOnly := flag.Lookup("test.run").Value.String()

obsoleteFiles, usedFiles := examineFiles(testsRegistry.values, runOnly, shouldClean)
obsoleteFiles, usedFiles := examineFiles(testsRegistry.values, runOnly, shouldClean && !isCI)
obsoleteTests, err := examineSnaps(
testsRegistry.values,
usedFiles,
runOnly,
shouldClean,
opt.Sort,
shouldClean && !isCI,
opt.Sort && !isCI,
)
if err != nil {
fmt.Println(err)
Expand All @@ -100,7 +101,8 @@ func Clean(t *testing.M, opts ...CleanOpts) {
obsoleteTests,
len(skippedTests.values),
testEvents.items,
shouldClean); s != "" {
shouldClean && !isCI,
); s != "" {
fmt.Print(s)
}
}
Expand Down Expand Up @@ -182,7 +184,7 @@ func examineSnaps(
registry map[string]map[string]int,
used []string,
runOnly string,
shouldUpdate bool,
update,
sort bool,
) ([]string, error) {
obsoleteTests := []string{}
Expand Down Expand Up @@ -237,11 +239,11 @@ func examineSnaps(
return nil, err
}

isSorted := slices.IsSorted(testIDs)
shouldSort := sort && !slices.IsSortedFunc(testIDs, naturalSort)
shouldUpdate := update && hasDiffs

// If there are no diffs or we don't want to update the snaps
// and if we don't want to sort the snaps or they are already sorted we skip
if (!hasDiffs || !shouldUpdate) && (!sort || isSorted) {
// if we don't have to "write" anything on the snap we skip
if !shouldUpdate && !shouldSort {
f.Close()

clear(tests)
Expand All @@ -251,8 +253,9 @@ func examineSnaps(
continue
}

if sort && !isSorted {
slices.Sort(testIDs)
if shouldSort {
// sort testIDs
slices.SortFunc(testIDs, naturalSort)
}

if err := overwriteFile(f, nil); err != nil {
Expand Down Expand Up @@ -399,3 +402,14 @@ func occurrences(tests map[string]int) set {

return result
}

// naturalSort is a function that can be used to sort strings in natural order
func naturalSort(a, b string) int {
if a == b {
return 0
}
if natural.Less(a, b) {
return -1
}
return 1
}
18 changes: 10 additions & 8 deletions snaps/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestExamineFiles(t *testing.T) {

func TestExamineSnaps(t *testing.T) {
t.Run("should report no obsolete snapshots", func(t *testing.T) {
shouldUpdate, sort := false, false
tests, dir1, dir2 := setupTempExamineFiles(
t,
loadMockSnap(t, "mock-snap-1"),
Expand All @@ -148,30 +149,29 @@ func TestExamineSnaps(t *testing.T) {
filepath.FromSlash(dir1 + "/test1.snap"),
filepath.FromSlash(dir2 + "/test2.snap"),
}
shouldUpdate := false

obsolete, err := examineSnaps(tests, used, "", shouldUpdate, false)
obsolete, err := examineSnaps(tests, used, "", shouldUpdate, sort)

test.Equal(t, []string{}, obsolete)
test.NoError(t, err)
})

t.Run("should report two obsolete snapshots and not change content", func(t *testing.T) {
shouldUpdate, sort := false, false
mockSnap1 := loadMockSnap(t, "mock-snap-1")
mockSnap2 := loadMockSnap(t, "mock-snap-2")
tests, dir1, dir2 := setupTempExamineFiles(t, mockSnap1, mockSnap2)
used := []string{
filepath.FromSlash(dir1 + "/test1.snap"),
filepath.FromSlash(dir2 + "/test2.snap"),
}
shouldUpdate := false

// Reducing test occurrence to 1 meaning the second test was removed ( testid - 2 )
tests[used[0]]["TestDir1_3/TestSimple"] = 1
// Removing the test entirely
delete(tests[used[1]], "TestDir2_2/TestSimple")

obsolete, err := examineSnaps(tests, used, "", shouldUpdate, false)
obsolete, err := examineSnaps(tests, used, "", shouldUpdate, sort)
content1 := test.GetFileContent(t, used[0])
content2 := test.GetFileContent(t, used[1])

Expand All @@ -184,6 +184,7 @@ func TestExamineSnaps(t *testing.T) {
})

t.Run("should update the obsolete snap files", func(t *testing.T) {
shouldUpdate, sort := true, false
tests, dir1, dir2 := setupTempExamineFiles(
t,
loadMockSnap(t, "mock-snap-1"),
Expand All @@ -193,13 +194,12 @@ func TestExamineSnaps(t *testing.T) {
filepath.FromSlash(dir1 + "/test1.snap"),
filepath.FromSlash(dir2 + "/test2.snap"),
}
shouldUpdate := true

// removing tests from the map means those tests are no longer used
delete(tests[used[0]], "TestDir1_3/TestSimple")
delete(tests[used[1]], "TestDir2_1/TestSimple")

obsolete, err := examineSnaps(tests, used, "", shouldUpdate, false)
obsolete, err := examineSnaps(tests, used, "", shouldUpdate, sort)
content1 := test.GetFileContent(t, used[0])
content2 := test.GetFileContent(t, used[1])

Expand Down Expand Up @@ -242,6 +242,7 @@ string hello world 2 2 1
})

t.Run("should sort all tests", func(t *testing.T) {
shouldUpdate, sort := false, true
mockSnap1 := loadMockSnap(t, "mock-snap-sort-1")
mockSnap2 := loadMockSnap(t, "mock-snap-sort-2")
expectedMockSnap1 := loadMockSnap(t, "mock-snap-sort-1-sorted")
Expand All @@ -256,7 +257,7 @@ string hello world 2 2 1
filepath.FromSlash(dir2 + "/test2.snap"),
}

obsolete, err := examineSnaps(tests, used, "", false, true)
obsolete, err := examineSnaps(tests, used, "", shouldUpdate, sort)

test.NoError(t, err)
test.Equal(t, 0, len(obsolete))
Expand All @@ -271,6 +272,7 @@ string hello world 2 2 1
t.Run(
"should not update file if snaps are already sorted and shouldUpdate=false",
func(t *testing.T) {
shouldUpdate, sort := false, true
mockSnap1 := loadMockSnap(t, "mock-snap-sort-1-sorted")
mockSnap2 := loadMockSnap(t, "mock-snap-sort-2-sorted")
tests, dir1, dir2 := setupTempExamineFiles(
Expand All @@ -287,7 +289,7 @@ string hello world 2 2 1
delete(tests[used[0]], "TestDir1_3/TestSimple")
delete(tests[used[1]], "TestDir2_1/TestSimple")

obsolete, err := examineSnaps(tests, used, "", false, true)
obsolete, err := examineSnaps(tests, used, "", shouldUpdate, sort)

test.NoError(t, err)
test.Equal(t, []string{
Expand Down
2 changes: 1 addition & 1 deletion snaps/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
errSnapNotFound = errors.New("snapshot not found")
isCI = ciinfo.IsCI
updateVAR = os.Getenv("UPDATE_SNAPS")
shouldClean = (updateVAR == "true" || updateVAR == "clean") && !isCI
shouldClean = updateVAR == "true" || updateVAR == "clean"
defaultConfig = config{
snapsDir: "__snapshots__",
}
Expand Down

0 comments on commit 9398840

Please sign in to comment.