github.com/jenkins-x/jx/v2@v2.1.155/pkg/tests/files.go (about) 1 package tests 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strconv" 9 "strings" 10 "testing" 11 12 "github.com/pkg/errors" 13 14 "github.com/stretchr/testify/require" 15 16 "github.com/jenkins-x/jx/v2/pkg/util" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 // AssertFileContains asserts that a given file exists and contains the given text 21 func AssertFileContains(t *testing.T, fileName string, containsText string) { 22 if AssertFileExists(t, fileName) { 23 data, err := ioutil.ReadFile(fileName) 24 assert.NoError(t, err, "Failed to read file %s", fileName) 25 if err == nil { 26 text := string(data) 27 assert.True(t, strings.Index(text, containsText) >= 0, "The file %s does not contain text: %s", fileName, containsText) 28 } 29 } 30 } 31 32 // AssertFileDoesNotContain asserts that a given file exists and does not contain the given text 33 func AssertFileDoesNotContain(t *testing.T, fileName string, containsText string) { 34 if AssertFileExists(t, fileName) { 35 data, err := ioutil.ReadFile(fileName) 36 assert.NoError(t, err, "Failed to read file %s", fileName) 37 if err == nil { 38 text := string(data) 39 idx := strings.Index(text, containsText) 40 line := "" 41 if idx > 0 { 42 lines := strings.Split(text, "\n") 43 for i, l := range lines { 44 if strings.Index(l, containsText) >= 0 { 45 line = "line " + strconv.Itoa(i+1) + " = " + l 46 break 47 } 48 } 49 } 50 assert.True(t, idx < 0, "The file %s should not contain text: %s as %s", fileName, containsText, line) 51 } 52 } 53 } 54 55 // AssertFileExists asserts that the given file exists 56 func AssertFileExists(t *testing.T, fileName string) bool { 57 exists, err := util.FileExists(fileName) 58 assert.NoError(t, err, "Failed checking if file exists %s", fileName) 59 assert.True(t, exists, "File %s should exist", fileName) 60 if exists { 61 Debugf("File %s exists\n", fileName) 62 } 63 return exists 64 } 65 66 // AssertFileDoesNotExist asserts that the given file does not exist 67 func AssertFileDoesNotExist(t *testing.T, fileName string) bool { 68 exists, err := util.FileExists(fileName) 69 assert.NoError(t, err, "Failed checking if file exists %s", fileName) 70 assert.False(t, exists, "File %s should not exist", fileName) 71 if exists { 72 Debugf("File %s does not exist\n", fileName) 73 } 74 return exists 75 } 76 77 // AssertFilesExist asserts that the list of file paths either exist or don't exist 78 func AssertFilesExist(t *testing.T, expected bool, paths ...string) { 79 for _, path := range paths { 80 if expected { 81 AssertFileExists(t, path) 82 } else { 83 AssertFileDoesNotExist(t, path) 84 } 85 } 86 } 87 88 // AssertDirsExist asserts that the list of directory paths either exist or don't exist 89 func AssertDirsExist(t *testing.T, expected bool, paths ...string) { 90 for _, path := range paths { 91 if expected { 92 AssertDirExists(t, path) 93 } else { 94 AssertDirDoesNotExist(t, path) 95 } 96 } 97 } 98 99 // AssertDirExists asserts that the given directory exists 100 func AssertDirExists(t *testing.T, dir string) bool { 101 exists, err := util.DirExists(dir) 102 assert.NoError(t, err, "Failed checking if directory exists %s", dir) 103 assert.True(t, exists, "directory %s should exist", dir) 104 return exists 105 } 106 107 // AssertDirDoesNotExist asserts that the given directory does not exist 108 func AssertDirDoesNotExist(t *testing.T, dir string) bool { 109 exists, err := util.DirExists(dir) 110 assert.NoError(t, err, "failed checking if directory exists %s", dir) 111 assert.False(t, exists, "directory %s should not exist", dir) 112 return exists 113 } 114 115 func AssertEqualFileText(t *testing.T, expectedFile string, actualFile string) error { 116 expectedText, err := AssertLoadFileText(t, expectedFile) 117 if err != nil { 118 return err 119 } 120 actualText, err := AssertLoadFileText(t, actualFile) 121 if err != nil { 122 return err 123 } 124 assert.Equal(t, expectedText, actualText, "comparing text content of files %s and %s", expectedFile, actualFile) 125 return nil 126 } 127 128 // AssertLoadFileText asserts that the given file name can be loaded and returns the string contents 129 func AssertLoadFileText(t *testing.T, fileName string) (string, error) { 130 if !AssertFileExists(t, fileName) { 131 return "", fmt.Errorf("File %s does not exist", fileName) 132 } 133 data, err := ioutil.ReadFile(fileName) 134 assert.NoError(t, err, "Failed loading data for file: %s", fileName) 135 if err != nil { 136 return "", err 137 } 138 return string(data), nil 139 } 140 141 // AssertTextFileContentsEqual asserts that both the expected and actual files can be loaded as text 142 // and that their contents are identical 143 func AssertTextFileContentsEqual(t *testing.T, expectedFile string, actualFile string) { 144 assert.NotEqual(t, expectedFile, actualFile, "should be given different file names") 145 146 expected, err := AssertLoadFileText(t, expectedFile) 147 require.NoError(t, err) 148 actual, err := AssertLoadFileText(t, actualFile) 149 require.NoError(t, err) 150 151 assert.Equal(t, expected, actual, "contents of expected file %s and actual file %s", expectedFile, actualFile) 152 } 153 154 // AssertDirContentsEqual walks two directory structures and validates that the same files exist (by name) and that they have the same content 155 func AssertDirContentsEqual(t *testing.T, expectedDir string, actualDir string) { 156 actualFiles := make(map[string]string, 0) 157 expectedFiles := make(map[string]string, 0) 158 err := filepath.Walk(actualDir, func(path string, info os.FileInfo, err error) error { 159 relativePath, err := filepath.Rel(actualDir, path) 160 if err != nil { 161 return errors.WithStack(err) 162 } 163 actualFiles[relativePath] = path 164 return nil 165 }) 166 assert.NoError(t, err) 167 err = filepath.Walk(expectedDir, func(path string, info os.FileInfo, err error) error { 168 relativePath, err := filepath.Rel(expectedDir, path) 169 if err != nil { 170 return errors.WithStack(err) 171 } 172 expectedFiles[relativePath] = path 173 return nil 174 }) 175 assert.Len(t, actualFiles, len(expectedFiles)) 176 for relativePath, path := range expectedFiles { 177 actualFile, ok := actualFiles[relativePath] 178 assert.True(t, ok, "%s not present", relativePath) 179 info, err := os.Stat(actualFile) 180 assert.NoError(t, err) 181 if !info.IsDir() { 182 AssertTextFileContentsEqual(t, path, actualFile) 183 } 184 } 185 }