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 535802b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 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
29 changes: 22 additions & 7 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 @@ -88,6 +89,7 @@ func Clean(t *testing.M, opts ...CleanOpts) {
usedFiles,
runOnly,
shouldClean,
isCI,
opt.Sort,
)
if err != nil {
Expand Down Expand Up @@ -182,7 +184,8 @@ func examineSnaps(
registry map[string]map[string]int,
used []string,
runOnly string,
shouldUpdate bool,
update,
isCI,
sort bool,
) ([]string, error) {
obsoleteTests := []string{}
Expand Down Expand Up @@ -237,11 +240,11 @@ func examineSnaps(
return nil, err
}

isSorted := slices.IsSorted(testIDs)
shouldSort := !isCI && sort && !slices.IsSortedFunc(testIDs, naturalSort)
shouldUpdate := !isCI && 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 +254,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 +403,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
}
48 changes: 43 additions & 5 deletions snaps/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestExamineSnaps(t *testing.T) {
}
shouldUpdate := false

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

test.Equal(t, []string{}, obsolete)
test.NoError(t, err)
Expand All @@ -171,7 +171,7 @@ func TestExamineSnaps(t *testing.T) {
// Removing the test entirely
delete(tests[used[1]], "TestDir2_2/TestSimple")

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

Expand Down Expand Up @@ -199,7 +199,7 @@ func TestExamineSnaps(t *testing.T) {
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, false, false)
content1 := test.GetFileContent(t, used[0])
content2 := test.GetFileContent(t, used[1])

Expand Down Expand Up @@ -256,7 +256,7 @@ string hello world 2 2 1
filepath.FromSlash(dir2 + "/test2.snap"),
}

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

test.NoError(t, err)
test.Equal(t, 0, len(obsolete))
Expand All @@ -268,6 +268,44 @@ string hello world 2 2 1
test.Equal(t, string(expectedMockSnap2), content2)
})

t.Run("should not sort or update in case of running on CI", func(t *testing.T) {
mockSnap1 := loadMockSnap(t, "mock-snap-sort-1")
mockSnap2 := loadMockSnap(t, "mock-snap-sort-2")
tests, dir1, dir2 := setupTempExamineFiles(
t,
mockSnap1,
mockSnap2,
)
used := []string{
filepath.FromSlash(dir1 + "/test1.snap"),
filepath.FromSlash(dir2 + "/test2.snap"),
}
shouldUpdate, isCI, sort := true, true, 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, isCI, sort)
test.Equal(t, []string{
"TestDir1_3/TestSimple - 1",
"TestDir1_3/TestSimple - 2",
"TestDir2_1/TestSimple - 1",
"TestDir2_1/TestSimple - 2",
"TestDir2_1/TestSimple - 3",
},
obsolete,
)
test.NoError(t, err)

content1 := test.GetFileContent(t, filepath.FromSlash(dir1+"/test1.snap"))
content2 := test.GetFileContent(t, filepath.FromSlash(dir2+"/test2.snap"))

// Content of snaps is not changed
test.Equal(t, string(mockSnap1), content1)
test.Equal(t, string(mockSnap2), content2)
})

t.Run(
"should not update file if snaps are already sorted and shouldUpdate=false",
func(t *testing.T) {
Expand All @@ -287,7 +325,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, "", false, false, true)

test.NoError(t, err)
test.Equal(t, []string{
Expand Down

0 comments on commit 535802b

Please sign in to comment.