forked from MarvinJWendt/testza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot_test.go
111 lines (86 loc) 路 3.47 KB
/
snapshot_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package testza_test
import (
"io/ioutil"
"math/rand"
"os"
"testing"
"time"
"github.com/MarvinJWendt/testza"
"github.com/MarvinJWendt/testza/internal"
"github.com/davecgh/go-spew/spew"
)
type snapshotObjectType struct {
Name string
Username string
Birthday time.Time
}
var snapshotObject = snapshotObjectType{
Name: "Marvin Wendt",
Username: "MarvinJWendt",
Birthday: time.Date(2001, time.January, 24, 0, 0, 0, 0, time.UTC),
}
var snapshotName = "TestSnapshotCreate_file_content"
var snapshotString = `hello world`
var snapshotNameString = "TestSnapshotCreate_file_content_string"
func TestSnapshotCreate_file_content(t *testing.T) {
err := testza.SnapshotCreate(snapshotName, snapshotObject)
testza.AssertNoError(t, err)
snapshotContent, err := ioutil.ReadFile(internal.GetCurrentScriptDirectory() + "/testdata/snapshots/" + t.Name() + ".testza")
testza.AssertNoError(t, err)
testza.AssertEqual(t, spew.Sdump(snapshotObject), string(snapshotContent))
}
func TestSnapshotCreate_file_content_string(t *testing.T) {
err := testza.SnapshotCreate(snapshotNameString, snapshotString)
testza.AssertNoError(t, err)
snapshotContent, err := ioutil.ReadFile(internal.GetCurrentScriptDirectory() + "/testdata/snapshots/" + t.Name() + ".testza")
testza.AssertNoError(t, err)
testza.AssertEqual(t, spew.Sdump(snapshotString), string(snapshotContent))
}
func TestSnapshotValidate(t *testing.T) {
err := testza.SnapshotValidate(t, snapshotName, snapshotObject)
testza.AssertNoError(t, err)
err = testza.SnapshotValidate(t, snapshotNameString, snapshotString)
testza.AssertNoError(t, err)
}
func TestSnapshotValidate_fails(t *testing.T) {
modifiedSnapshotObject := snapshotObject
modifiedSnapshotObject.Username = "NotMarvinJWendt"
testza.AssertTestFails(t, func(t testza.TestingPackageWithFailFunctions) {
err := testza.SnapshotValidate(t, snapshotName, modifiedSnapshotObject)
testza.AssertNoError(t, err)
})
testza.AssertTestFails(t, func(t testza.TestingPackageWithFailFunctions) {
err := testza.SnapshotValidate(t, snapshotNameString, `foo bar`)
testza.AssertNoError(t, err)
})
}
func TestSnapshotCreateOrValidate(t *testing.T) {
err := testza.SnapshotCreateOrValidate(t, t.Name(), snapshotObject)
testza.AssertNoError(t, err)
}
func TestSnapshotCreateOrValidate_create_random(t *testing.T) {
name := t.Name() + testza.FuzzInputStringGenerateRandom(1, rand.Intn(10))[0]
err := testza.SnapshotCreateOrValidate(t, name, snapshotObject)
testza.AssertNoError(t, err)
err = os.Remove(internal.GetCurrentScriptDirectory() + "/testdata/snapshots/" + name + ".testza")
testza.AssertNoError(t, err)
}
func TestSnapshotCreateOrValidate_invalid_name(t *testing.T) {
err := testza.SnapshotCreateOrValidate(t, string(rune(0))+"><", snapshotObject)
testza.AssertNotNil(t, err)
}
func TestSnapshotCreateOrValidate_nested_test_name(t *testing.T) {
snapshotNestedDirName := t.Name()
snapshotName := snapshotNestedDirName + "/nested_name"
snapshotFullPath := internal.GetCurrentScriptDirectory() + "/testdata/snapshots/" + snapshotNestedDirName
// ensure snapshot does not exist - remove snapshot and empty parent directory
if _, err := os.Stat(snapshotFullPath); err == nil {
_ = os.RemoveAll(snapshotFullPath)
}
err := testza.SnapshotCreateOrValidate(t, snapshotName, "snapshot-data")
// try to clean up before the assert so we leave the working copy clean
if _, err := os.Stat(snapshotFullPath); err == nil {
_ = os.RemoveAll(snapshotFullPath)
}
testza.AssertNoError(t, err)
}