From 1b2c33ad0172220ee7b31eaba1243bc38f925914 Mon Sep 17 00:00:00 2001 From: Georgios Kampitakis <50364739+gkampitakis@users.noreply.github.com> Date: Tue, 5 Sep 2023 18:04:11 +0100 Subject: [PATCH] fix: increase MaxTokenSize to handle large snapshot lines (#76) * fix: increase MaxTokenSize to handle large snapshot lines * chore: bump go to 1.21.0 --- .github/workflows/go.yml | 4 ++-- go.mod | 7 ++----- go.sum | 4 ++-- snaps/clean.go | 7 +++++-- snaps/snapshot.go | 12 ++++++++++-- snaps/utils.go | 10 ++++++++++ 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a68daba..e3afe18 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -21,7 +21,7 @@ jobs: steps: - uses: actions/setup-go@v3 with: - go-version: 1.20.x + go-version: 1.21.x - uses: actions/checkout@v3 - name: golangci-lint uses: golangci/golangci-lint-action@v3 @@ -42,6 +42,6 @@ jobs: - name: Setup go uses: actions/setup-go@v3 with: - go-version: 1.20.x + go-version: 1.21.x - name: Run Tests run: make test-verbose diff --git a/go.mod b/go.mod index 94f2405..2276bf7 100644 --- a/go.mod +++ b/go.mod @@ -1,15 +1,12 @@ module github.com/gkampitakis/go-snaps -go 1.20 +go 1.21.0 require ( github.com/gkampitakis/ciinfo v0.2.5 github.com/gkampitakis/go-diff v1.3.2 github.com/kr/pretty v0.3.1 -) - -require ( - github.com/tidwall/gjson v1.14.4 + github.com/tidwall/gjson v1.16.0 github.com/tidwall/pretty v1.2.1 github.com/tidwall/sjson v1.2.5 ) diff --git a/go.sum b/go.sum index 42fb6e5..c9aa061 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f 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.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= -github.com/tidwall/gjson v1.14.4/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/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= diff --git a/snaps/clean.go b/snaps/clean.go index b7a6f9f..9c7d685 100644 --- a/snaps/clean.go +++ b/snaps/clean.go @@ -1,7 +1,6 @@ package snaps import ( - "bufio" "bytes" "flag" "fmt" @@ -157,7 +156,7 @@ func examineSnaps( var hasDiffs bool registeredTests := occurrences(registry[snapPath]) - s := bufio.NewScanner(f) + s := snapshotScanner(f) for s.Scan() { b := s.Bytes() @@ -192,6 +191,10 @@ func examineSnaps( } } + if err := s.Err(); err != nil { + return nil, err + } + if !hasDiffs || !shouldUpdate { continue } diff --git a/snaps/snapshot.go b/snaps/snapshot.go index 3dd35bd..a6b7d2d 100644 --- a/snaps/snapshot.go +++ b/snaps/snapshot.go @@ -116,7 +116,7 @@ func getPrevSnapshot(testID, snapPath string) (string, int, error) { lineNumber := 1 tid := []byte(testID) - s := bufio.NewScanner(bytes.NewReader(f)) + s := snapshotScanner(bytes.NewReader(f)) for s.Scan() { l := s.Bytes() if !bytes.Equal(l, tid) { @@ -136,6 +136,10 @@ func getPrevSnapshot(testID, snapPath string) (string, int, error) { } } + if err := s.Err(); err != nil { + return "", -1, err + } + return "", -1, errSnapNotFound } @@ -172,7 +176,7 @@ func updateSnapshot(testID, snapshot, snapPath string) error { updatedSnapFile.Grow(int(i.Size())) } - s := bufio.NewScanner(f) + s := snapshotScanner(f) for s.Scan() { b := s.Bytes() updatedSnapFile.Write(b) @@ -190,6 +194,10 @@ func updateSnapshot(testID, snapshot, snapPath string) error { updatedSnapFile.WriteByte('\n') } + if err := s.Err(); err != nil { + return err + } + return overwriteFile(f, updatedSnapFile.Bytes()) } diff --git a/snaps/utils.go b/snaps/utils.go index 727fb1a..52d0d32 100644 --- a/snaps/utils.go +++ b/snaps/utils.go @@ -1,7 +1,10 @@ package snaps import ( + "bufio" "errors" + "io" + "math" "os" "path/filepath" "runtime" @@ -103,6 +106,13 @@ func baseCaller(skip int) string { } } +// snapshotScanner returns a new *bufio.Scanner with a `MaxScanTokenSize == math.MaxInt` to read from r. +func snapshotScanner(r io.Reader) *bufio.Scanner { + s := bufio.NewScanner(r) + s.Buffer([]byte{}, math.MaxInt) + return s +} + // 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.