github.com/verrazzano/verrazzano@v1.7.1/tools/vz/pkg/internal/util/files/text_test.go (about) 1 // Copyright (c) 2021, 2024, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 package files 4 5 import ( 6 "fmt" 7 "os" 8 "regexp" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/verrazzano/verrazzano/tools/vz/pkg/internal/util/log" 13 "go.uber.org/zap" 14 ) 15 16 // TestSearchFilesGood Tests that we can find the expected set of files with a matching expression 17 // GIVEN a call to SearchFiles 18 // WHEN with a valid rootDirectory, list of files, and regular expression 19 // THEN search matches will be returned 20 func TestSearchFilesGood(t *testing.T) { 21 logger := log.GetDebugEnabledLogger() 22 rootDirectory := "../../test" 23 myFiles, err := GetMatchingFileNames(logger, rootDirectory, regexp.MustCompile(".*")) 24 assert.Nil(t, err) 25 assert.NotNil(t, myFiles) 26 assert.True(t, len(myFiles) > 0) 27 myMatches, err := SearchFiles(logger, rootDirectory, myFiles, regexp.MustCompile("ghcr.io/.*/rancher"), nil) 28 assert.Nil(t, err) 29 assert.NotNil(t, myMatches) 30 assert.True(t, len(myMatches) > 0) 31 for _, match := range myMatches { 32 assert.True(t, len(checkMatch(logger, match)) == 0) 33 } 34 } 35 36 // TestFindFilesAndSearchGood Tests that we can find the expected set of files with a matching expression 37 // GIVEN a call to FindFilesAndSearch 38 // WHEN with a valid rootDirectory, list of files, and regular expression 39 // THEN search matches will be returned 40 func TestFindFilesAndSearchGood(t *testing.T) { 41 logger := log.GetDebugEnabledLogger() 42 myMatches, err := FindFilesAndSearch(logger, "../../test", regexp.MustCompile(".*"), regexp.MustCompile("ghcr.io/.*/rancher"), nil) 43 assert.Nil(t, err) 44 assert.NotNil(t, myMatches) 45 assert.True(t, len(myMatches) > 0) 46 for _, match := range myMatches { 47 assert.True(t, len(checkMatch(logger, match)) == 0) 48 } 49 } 50 51 // TestBadExpressions Tests that we fail correctly when given bad search expressions 52 // GIVEN a call to helpers 53 // WHEN with invalid regular expression 54 // THEN error will be returned 55 func TestBadExpressions(t *testing.T) { 56 logger := log.GetDebugEnabledLogger() 57 _, err := FindFilesAndSearch(logger, "../../test", nil, regexp.MustCompile("ghcr.io/.*/rancher"), nil) 58 assert.NotNil(t, err) 59 _, err = FindFilesAndSearch(logger, "../../test", regexp.MustCompile(".*"), nil, nil) 60 assert.NotNil(t, err) 61 _, err = GetMatchingFileNames(logger, "../../test", nil) 62 assert.NotNil(t, err) 63 myFiles := []string{"test file"} 64 _, err = SearchFiles(logger, "../../test", myFiles, nil, nil) 65 assert.NotNil(t, err) 66 } 67 68 func checkMatch(logger *zap.SugaredLogger, match TextMatch) string { 69 logger.Debugf("Matched file: %s", match.FileName) 70 logger.Debugf("Matched line: %d", match.FileLine) 71 logger.Debugf("Matched text: %s", len(match.MatchedText)) 72 failText := "" 73 stat, err := os.Stat(match.FileName) 74 if err != nil { 75 logger.Errorf("Stat failed for matched file: %s", match.FileName, err) 76 failText = fmt.Sprintf("Stat failed for matched file: %s", match.FileName) 77 } else if stat.IsDir() { 78 failText = fmt.Sprintf("Matched file was a directory: %s", match.FileName) 79 } else if match.FileLine <= 0 { 80 failText = fmt.Sprintf("Matched linenumber %d was invalid for: %s", match.FileLine, match.FileName) 81 } else if len(match.MatchedText) == 0 { 82 failText = fmt.Sprintf("Matched text was empty for linenumber %d was invalid for: %s", match.FileLine, match.FileName) 83 } 84 if len(failText) > 0 { 85 logger.Error(failText) 86 } 87 return failText 88 } 89 90 // TODO: Add more test cases (more result validation, time ranges, more expression variants, negative cases, etc...)