-
Notifications
You must be signed in to change notification settings - Fork 205
/
helpers.go
74 lines (65 loc) · 2.12 KB
/
helpers.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
package tests
import (
"crypto/md5"
"os"
"path/filepath"
"fmt"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// RequireErrorContains fails the test when the error doesn't contain
// AssertFilePermissionsEqual checks that the file permission bits are equal for the specified file.
func AssertFilePermissionsEqual(t *testing.T, path string, want os.FileMode, got os.FileMode) bool {
want = want.Perm()
got = got.Perm()
return assert.Equal(t, want, got|want,
"incorrect file permissions on %s. want: %o, got %o", path, want, got)
}
// AssertDirectoryPermissionsEqual checks that all files in the specified path match the desired
// file mode. Uses a glob pattern to match.
func AssertDirectoryPermissionsEqual(t *testing.T, path string, mode os.FileMode) bool {
files, err := filepath.Glob(path)
if os.IsNotExist(err) {
return true
}
require.NoError(t, err)
for _, file := range files {
info, err := os.Stat(file)
require.NoError(t, err)
if info.IsDir() {
continue
}
if !AssertFilePermissionsEqual(t, path, mode, info.Mode()) {
return false
}
}
return true
}
// the specified substring. This is less fragile than using require.EqualError
func RequireErrorContains(t *testing.T, err error, substring string, msgAndArgs ...interface{}) {
require.Error(t, err)
require.Contains(t, err.Error(), substring, msgAndArgs...)
}
// GenerateDatabaseName comes up with a valid mongodb database name from a Go test name.
func GenerateDatabaseName(testName string) string {
reg, err := regexp.Compile(`[^a-zA-Z0-9_]+`)
if err != nil {
panic(err)
}
safeTestName := reg.ReplaceAllString(testName, "_")
if len(safeTestName) > 50 {
safeTestName = fmt.Sprintf("%x", md5.Sum([]byte(safeTestName)))
}
return fmt.Sprintf("porter_%s", safeTestName)
}
// This is the same as require.Contains but it prints the output string without
// newlines escaped so that it's easier to read.
func RequireOutputContains(t *testing.T, output string, substring string) {
ok := assert.Contains(t, output, substring)
if !ok {
t.Errorf("%s\ndoes not contain\n%s", output, substring)
t.FailNow()
}
}