Skip to content

Commit

Permalink
Merge pull request americanexpress#32 from aschaef19/main
Browse files Browse the repository at this point in the history
  • Loading branch information
grinish21 authored Oct 5, 2021
2 parents 40b3ff2 + f549dc3 commit 259f6f5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
18 changes: 10 additions & 8 deletions pkg/file/fileUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func MultipartToScanFiles(files []*multipart.FileHeader, cfg cfgreader.Earlybird
fileNameWithPathPrefix = pathSeparator + fileNameWithPathPrefix
}
//Skip file with extensions Earlybird ignores
if isIgnoredFile(fileNameWithPathPrefix) {
if isIgnoredFile(fileNameWithPathPrefix, cfg.SearchDir) {
continue
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func GetGitFiles(fileType string, cfg *cfgreader.EarlybirdConfig) (fileContext C

fileList = parseGitFiles(output, cfg.VerboseEnabled, cfg.MaxFileSize)
compressList, fileList = separateCompressedAndUncompressed(fileList)
compressList, fileContext.CompressPaths, err = GetCompressedFiles(compressList) //Get the files within our compressed list
compressList, fileContext.CompressPaths, err = GetCompressedFiles(compressList, cfg.SearchDir) //Get the files within our compressed list
if err != nil {
return fileContext, err
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func GetFiles(searchDir, ignoreFile string, verbose bool, maxFileSize int64) (fi
if err != nil {
log.Println("Error reading directory: ", err)
}
if !isIgnoredFile(path) {
if !isIgnoredFile(path, searchDir) {
// Ignore the path if it's a directory
pathIsDirectory, isDirErr := isDirectory(path)
if !pathIsDirectory {
Expand Down Expand Up @@ -224,7 +224,7 @@ func GetFiles(searchDir, ignoreFile string, verbose bool, maxFileSize int64) (fi

var compressList []scan.File
compressList, fileList = separateCompressedAndUncompressed(fileList)
compressList, fileContext.CompressPaths, err = GetCompressedFiles(compressList) //Get the files within our compressed list
compressList, fileContext.CompressPaths, err = GetCompressedFiles(compressList, searchDir) //Get the files within our compressed list
if err != nil {
return fileContext, err
}
Expand Down Expand Up @@ -360,9 +360,11 @@ func getIgnorePatterns(filePath, ignoreFile string, verbose bool) (ignorePattern
}

// If the file matches a pattern in one of the ignore files, return true
func isIgnoredFile(fileName string) bool {
func isIgnoredFile(fileName string, fileRoot string) bool {
// ignore root directory when checking ignore matching
trimmedName := strings.Replace(fileName, fileRoot, "", 1)
for _, pattern := range ignorePatterns {
if wildcard.PatternMatch(fileName, pattern) {
if wildcard.PatternMatch(trimmedName, pattern) {
return true
}
}
Expand Down Expand Up @@ -417,7 +419,7 @@ func separateCompressedAndUncompressed(files []scan.File) (compressed, uncompres
}

//GetCompressedFiles provides all the files contained within compressed files
func GetCompressedFiles(files []scan.File) (newfiles []scan.File, compresspaths []string, err error) {
func GetCompressedFiles(files []scan.File, rootPath string) (newfiles []scan.File, compresspaths []string, err error) {
//check if file list contains compressed files, if so, scan their contents
for _, file := range files {
//Unpack and append to file list
Expand All @@ -431,7 +433,7 @@ func GetCompressedFiles(files []scan.File) (newfiles []scan.File, compresspaths
return newfiles, compresspaths, err
}
for _, subfile := range filenames {
if !isIgnoredFile(subfile) && !scan.CompressPattern.MatchString(subfile) {
if !isIgnoredFile(subfile, rootPath) && !scan.CompressPattern.MatchString(subfile) {
var curFile scan.File
curFile.Path = file.Path + "/" + filepath.Base(subfile)
curFile.Name = subfile //Build view file name in format: file.zip/contents/file
Expand Down
14 changes: 12 additions & 2 deletions pkg/file/fileUtil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func Test_isIgnoredFile(t *testing.T) {
}
type args struct {
fileName string
rootPath string
}
tests := []struct {
name string
Expand All @@ -144,10 +145,18 @@ func Test_isIgnoredFile(t *testing.T) {
},
want: true,
},
{
name: "Check if file is ignored",
args: args{
fileName: "/test.js",
rootPath: "src/node_modules",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isIgnoredFile(tt.args.fileName); got != tt.want {
if got := isIgnoredFile(tt.args.fileName, tt.args.rootPath); got != tt.want {
t.Errorf("isIgnoredFile() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -272,6 +281,7 @@ func TestExists(t *testing.T) {
func TestGetCompressedFiles(t *testing.T) {
type args struct {
files []scan.File
rootPath string
}
tests := []struct {
name string
Expand All @@ -291,7 +301,7 @@ func TestGetCompressedFiles(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotNewfiles, gotCompresspaths, err := GetCompressedFiles(tt.args.files)
gotNewfiles, gotCompresspaths, err := GetCompressedFiles(tt.args.files, tt.args.rootPath)
if err != nil {
t.Errorf("GetCompressedFiles() err = %v", err)
}
Expand Down

0 comments on commit 259f6f5

Please sign in to comment.