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