-
Notifications
You must be signed in to change notification settings - Fork 1
/
files.go
55 lines (49 loc) · 1.61 KB
/
files.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
package tests
import (
"io/ioutil"
"strings"
"testing"
"github.com/jenkins-x/jx/pkg/util"
"github.com/stretchr/testify/assert"
)
// AssertFileContains asserts that a given file exists and contains the given text
func AssertFileContains(t *testing.T, fileName string, containsText string) {
if AssertFileExists(t, fileName) {
data, err := ioutil.ReadFile(fileName)
assert.NoError(t, err, "Failed to read file %s", fileName)
if err == nil {
text := string(data)
assert.True(t, strings.Index(text, containsText) >= 0, "The file %s does not contain text: %s", fileName, containsText)
}
}
}
// AssertFileExists asserts that the given file exists
func AssertFileExists(t *testing.T, fileName string) bool {
exists, err := util.FileExists(fileName)
assert.NoError(t, err, "Failed checking if file exists %s", fileName)
assert.True(t, exists, "File %s should exist", fileName)
if exists {
Debugf("File %s exists\n", fileName)
}
return exists
}
// AssertFileDoesNotExist asserts that the given file does not exist
func AssertFileDoesNotExist(t *testing.T, fileName string) bool {
exists, err := util.FileExists(fileName)
assert.NoError(t, err, "Failed checking if file exists %s", fileName)
assert.False(t, exists, "File %s should not exist", fileName)
if exists {
Debugf("File %s does not exist\n", fileName)
}
return exists
}
// AssertFilesExist asserts that the list of file paths either exist or don't exist
func AssertFilesExist(t *testing.T, expected bool, paths ...string) {
for _, path := range paths {
if expected {
AssertFileExists(t, path)
} else {
AssertFileDoesNotExist(t, path)
}
}
}