-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.out | ||
*.test | ||
tmp/ | ||
tmp/ | ||
.snapshots/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Name": "b", | ||
"Value": 1 | ||
} | ||
{ | ||
"Name": "a", | ||
"Value": "ok" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package got | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type snapshots struct { | ||
list *sync.Map | ||
file io.ReadWriter | ||
} | ||
|
||
type snapshot struct { | ||
Name string | ||
Value interface{} | ||
} | ||
|
||
// snapshotsFilePath returns the path of the snapshot file for current test. | ||
func (g G) snapshotsFilePath() string { | ||
return filepath.Join(".snapshots", escapeFileName(g.Name())+".txt") | ||
} | ||
|
||
func (g G) loadSnapshots() { | ||
p := g.snapshotsFilePath() | ||
|
||
g.snapshots.list = &sync.Map{} | ||
|
||
if g.PathExists(p) { | ||
f, err := os.OpenFile(p, os.O_RDWR, 0755) | ||
g.E(err) | ||
g.snapshots.file = f | ||
} else { | ||
return | ||
} | ||
|
||
dec := json.NewDecoder(g.snapshots.file) | ||
|
||
for { | ||
var data snapshot | ||
err := dec.Decode(&data) | ||
if err == io.EOF { | ||
return | ||
} | ||
g.E(err) | ||
g.snapshots.list.Store(data.Name, data.Value) | ||
} | ||
} | ||
|
||
// Snapshot asserts that x equals the snapshot with the specified name, name should be unique. | ||
// It can only compare JSON serializable types. | ||
// It will create a new snapshot if the name is not found. | ||
// The snapshot will be saved to ".snapshots/{TEST_NAME}" beside the test file, | ||
// TEST_NAME is the current test name. | ||
// To update the snapshot, just delete the corresponding file. | ||
func (g G) Snapshot(name string, x interface{}) { | ||
g.Helper() | ||
|
||
if y, ok := g.snapshots.list.Load(name); ok { | ||
g.Eq(x, y) | ||
return | ||
} | ||
|
||
g.snapshots.list.Store(name, x) | ||
|
||
g.Cleanup(func() { | ||
if g.snapshots.file == nil { | ||
p := g.snapshotsFilePath() | ||
|
||
err := os.MkdirAll(filepath.Dir(p), 0755) | ||
g.E(err) | ||
|
||
f, err := os.Create(p) | ||
g.E(err) | ||
g.snapshots.file = f | ||
} | ||
|
||
enc := json.NewEncoder(g.snapshots.file) | ||
enc.SetIndent("", " ") | ||
g.E(enc.Encode(snapshot{name, x})) | ||
}) | ||
} | ||
|
||
func escapeFileName(fileName string) string { | ||
// Define the invalid characters for both Windows and Unix | ||
invalidChars := `< > : " / \ | ? *` | ||
|
||
// Replace the invalid characters with an underscore | ||
regex := "[" + regexp.QuoteMeta(invalidChars) + "]" | ||
escapedFileName := regexp.MustCompile(regex).ReplaceAllString(fileName, "_") | ||
|
||
// Remove any leading or trailing spaces or dots | ||
escapedFileName = strings.Trim(escapedFileName, " .") | ||
|
||
// Remove consecutive dots | ||
escapedFileName = regexp.MustCompile(`\.{2,}`).ReplaceAllString(escapedFileName, ".") | ||
|
||
return escapedFileName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package got_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/ysmood/got" | ||
) | ||
|
||
func TestSnapshots(t *testing.T) { | ||
m := &mock{t: t, name: t.Name()} | ||
g := got.T(m) | ||
|
||
g.Snapshot("a", "ok") | ||
g.Snapshot("b", 1) | ||
|
||
g.Snapshot("a", "no") | ||
m.check(`"no" ⦗not ==⦘ "ok"`) | ||
} | ||
|
||
func TestSnapshotsCreate(t *testing.T) { | ||
err := os.RemoveAll(filepath.Join(".snapshots", "TestSnapshotsCreate.txt")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
g := got.T(t) | ||
|
||
g.Snapshot("a", "ok") | ||
} |