Skip to content

Commit

Permalink
Merge pull request mercedes-benz#1868 from VirtuslabRnD/feature-1867-…
Browse files Browse the repository at this point in the history
…use-pattern-matching-library

Use library for glob patterns
  • Loading branch information
sven-dmlr authored Feb 3, 2023
2 parents dd108dd + 14361da commit b2435f3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func Test_prepareScan_binary_upload_respects_exclude_patterns(t *testing.T) {
"files": [ "sechub-cli-tmptest/test.bin" ],
"folders": [ "sechub-cli-tmptest/binaries/" ]
},
"excludes": [ "**/ignore/**", "*.bin" ]
"excludes": [ "**/ignore/**", "**/*.bin" ]
}
]
}
Expand Down
28 changes: 3 additions & 25 deletions sechub-cli/src/mercedes-benz.com/sechub/util/filepathmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
package util

import (
"fmt"
"github.com/bmatcuk/doublestar/v4"
"path/filepath"
"regexp"
"strings"
)

Expand All @@ -16,29 +15,8 @@ import (
// - "a1b.txt"
//
func FilePathMatch(path string, pattern string) (result bool) {

// Let's turn the ant style pattern into a regexp:
doublestarPatterns := strings.Split(pattern, "**/")

for i, subElement := range doublestarPatterns {
// esacpe . with backslash and convert * to .*
doublestarPatterns[i] = strings.Replace(subElement, ".", "\\.", -1)
doublestarPatterns[i] = strings.Replace(doublestarPatterns[i], "*", ".*", -1)
}
regexpPattern := "^" + strings.Join(doublestarPatterns, ".*/") + "$"

// add a './' in front of the path except it starts with a '/'
// so e.g. "**/.git/**" will also match the current working directory and not only subdirectories
if !strings.HasPrefix(path, "/") && strings.Contains(pattern, "/") {
path = "./" + path
}

matched, err := regexp.MatchString(regexpPattern, path)
if err != nil {
LogError(fmt.Sprintln("Error evaluating filepath matches:", err, matched))
}

return matched
match, _ := doublestar.PathMatch(pattern, path)
return match
}

// ConvertBackslashPath - converts a path containing windows separators to unix ones
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func Test_Simple_filename_and_pattern_with_asterisk_but_more_matches(t *testing.
AssertFalse(FilePathMatch("a2.txt", "a1.txt"), t)
AssertFalse(FilePathMatch("a2.txt", "a*1.txt"), t)
AssertFalse(FilePathMatch("a2.txt", "a1*.txt"), t)

AssertFalse(FilePathMatch("folder/0123/file1.txt", "f*0*.txt"), t)
}

func TestXYZ_When_NO_double_asterisk_and_path_is_same_it_matches(t *testing.T) {
Expand Down Expand Up @@ -81,6 +83,34 @@ func Test_When_multiple_double_asterisk_on_inside_path_is_accepted_when_filename
AssertFalse(FilePathMatch("/home/gargamel/schlumpfine/testfolder/a.txt", "/home/**/schlaubi/**/a.txt"), t)
}

//func Test_Path_and_simple_filename_and_pattern_with_asterisk_at_start_and_prefix_matches(t *testing.T) {
// AssertTrue(FilePathMatch("/home/albert/a.txt","/home/albert/*.txt"), t)
//}
func Test_Matches_all_files_in_CVS_directories_that_can_be_located_anywhere_in_the_directory_tree(t *testing.T) {
AssertTrue(FilePathMatch("CVS/Repository*", "**/CVS/*"), t)
AssertTrue(FilePathMatch("org/apache/CVS/Entries", "**/CVS/*"), t)
AssertTrue(FilePathMatch("org/apache/jakarta/tools/ant/CVS/Entries", "**/CVS/*"), t)

/* but not when the foo/bar/ part does not match */
AssertFalse(FilePathMatch("org/apache/CVS/foo/bar/Entries", "**/CVS/*"), t)
}

func Test_Matches_all_files_in_the_org_apache_jakarta_directory_tree(t *testing.T) {
AssertTrue(FilePathMatch("org/apache/jakarta/tools/ant/docs/index.html", "org/apache/jakarta/**"), t)
AssertTrue(FilePathMatch("org/apache/jakarta/test.xml", "org/apache/jakarta/**"), t)

/* but not when the jakarta/ part is missing */
AssertFalse(FilePathMatch("org/apache/xyz.java", "org/apache/jakarta/**"), t)
}

func Test_Matches_all_files_in_CVS_directories_that_are_located_anywhere_in_the_directory_tree_under_org_apache(t *testing.T) {
AssertTrue(FilePathMatch("org/apache/CVS/Entries", "org/apache/**/CVS/*"), t)
AssertTrue(FilePathMatch("org/apache/jakarta/tools/ant/CVS/Entries", "org/apache/**/CVS/*"), t)

/* but not when the foo/bar/ part does not match */
AssertFalse(FilePathMatch("org/apache/xyz.java", "org/apache/**/CVS/*"), t)
}

func Test_Matches_all_files_that_have_a_test_element_in_their_path_including_test_as_a_filename(t *testing.T) {
AssertTrue(FilePathMatch("org/test/Entries", "**/test/**"), t)
AssertTrue(FilePathMatch("org/test", "**/test/**"), t)
AssertTrue(FilePathMatch("test/Entries", "**/test/**"), t)
AssertTrue(FilePathMatch("test", "**/test/**"), t)
}
4 changes: 2 additions & 2 deletions sechub-cli/src/mercedes-benz.com/sechub/util/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestTarFileCanBeCreated_with_exclude_patterns_applied(t *testing.T) {
TarWriter: tarWriter,
PrefixInTar: "",
Folders: []string{dirname1, dirname2},
Excludes: []string{"**/sub3/**", "*.txt"},
Excludes: []string{"**/sub3/**", "**/*.txt"},
}

/* execute */
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestTarFileContainsRelativeFoldersOutsideCurrent(t *testing.T) {
TarWriter: tarWriter,
PrefixInTar: "",
Folders: []string{RelativeTmpTestDir},
Excludes: []string{"*.tar"}, // exclude our own tar
Excludes: []string{"**/*.tar"}, // exclude our own tar
}

/* execute */
Expand Down

0 comments on commit b2435f3

Please sign in to comment.