forked from strangelove-ventures/interchaintest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempdir_test.go
104 lines (80 loc) · 2.18 KB
/
tempdir_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
package interchaintest_test
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/mocktesting"
)
func TestTempDir_Cleanup(t *testing.T) {
origKeep := interchaintest.KeepingTempDirOnFailure()
defer func() {
interchaintest.KeepTempDirOnFailure(origKeep)
}()
t.Run("keep=true", func(t *testing.T) {
interchaintest.KeepTempDirOnFailure(true)
t.Run("test passed", func(t *testing.T) {
mt := mocktesting.NewT("t")
dir := interchaintest.TempDir(mt)
require.DirExists(t, dir)
mt.RunCleanups()
require.NoDirExists(t, dir)
require.Empty(t, mt.Logs)
})
t.Run("test failed", func(t *testing.T) {
mt := mocktesting.NewT("t")
dir := interchaintest.TempDir(mt)
require.DirExists(t, dir)
defer func() { _ = os.RemoveAll(dir) }()
mt.Fail()
mt.RunCleanups()
// Directory still exists after cleanups.
require.DirExists(t, dir)
// And the last log message mentions the directory.
require.NotEmpty(t, mt.Logs)
require.Contains(t, mt.Logs[len(mt.Logs)-1], dir)
})
})
t.Run("keep=false", func(t *testing.T) {
interchaintest.KeepTempDirOnFailure(false)
for name, failed := range map[string]bool{
"test passed": false,
"test failed": true,
} {
t.Run(name, func(t *testing.T) {
mt := mocktesting.NewT("t")
dir := interchaintest.TempDir(mt)
require.DirExists(t, dir)
if failed {
mt.Fail()
}
mt.RunCleanups()
require.NoDirExists(t, dir)
require.Empty(t, mt.Logs)
})
}
})
}
func TestTempDir_Naming(t *testing.T) {
const testNamePrefix = "TestTempDir_Naming"
tmpRoot := os.TempDir()
for name, expDir := range map[string]string{
"A": "A",
"Foo_Bar": "Foo_Bar",
"1/2 full": "12_full",
"/..": "..", // Gets prefix appended, so will not traverse upwards.
"\x00\xFF": "",
} {
wantDir := filepath.Join(tmpRoot, testNamePrefix+expDir)
t.Run(name, func(t *testing.T) {
dir := interchaintest.TempDir(t)
require.Truef(
t,
strings.HasPrefix(dir, wantDir),
"directory %s should have started with %s", dir, wantDir,
)
})
}
}