Skip to content

Commit 4bafaf6

Browse files
second-franktw-owen-nelson
authored andcommitted
use existing path matching logic when filtering allowed patterns on file configs (#414)
1 parent 68191ff commit 4bafaf6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

talismanrc/talismanrc.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,14 @@ func combineFileIgnores(exsiting, incoming []FileIgnoreConfig) []FileIgnoreConfi
9393

9494
// RemoveAllowedPatterns removes globally- and per-file allowed patterns from an Addition
9595
func (tRC *TalismanRC) RemoveAllowedPatterns(addition gitrepo.Addition) string {
96-
additionPathAsString := string(addition.Path)
9796
// Processing global allowed patterns
9897
for _, pattern := range tRC.AllowedPatterns {
9998
addition.Data = pattern.ReplaceAll(addition.Data, []byte(""))
10099
}
101100

102101
// Processing allowed patterns based on file path
103102
for _, ignoreConfig := range tRC.FileIgnoreConfig {
104-
if ignoreConfig.GetFileName() == additionPathAsString {
103+
if addition.Matches(ignoreConfig.GetFileName()) {
105104
for _, pattern := range ignoreConfig.GetAllowedPatterns() {
106105
addition.Data = pattern.ReplaceAll(addition.Data, []byte(""))
107106
}

talismanrc/talismanrc_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ func TestShouldFilterAllowedPatternsFromAdditionBasedOnFileConfig(t *testing.T)
6363
assert.Equal(t, fileContentFiltered2, fileContent)
6464
}
6565

66+
func TestShouldFilterAllowedPatternsFromAdditionBasedOnFileConfigWithWildcards(t *testing.T) {
67+
const hexContent string = "68656C6C6F20776F726C6421"
68+
const fileContent string = "Prefix content" + hexContent
69+
gitRepoAddition1 := testAdditionWithData("foo/file1.yml", []byte(fileContent))
70+
gitRepoAddition2 := testAdditionWithData("foo/file2.yml", []byte(fileContent))
71+
talismanrc := createTalismanRCWithFileIgnores("foo/*.yml", "somedetector", []string{hexContent})
72+
73+
fileContentFiltered1 := talismanrc.RemoveAllowedPatterns(gitRepoAddition1)
74+
fileContentFiltered2 := talismanrc.RemoveAllowedPatterns(gitRepoAddition2)
75+
76+
assert.Equal(t, fileContentFiltered1, "Prefix content")
77+
assert.Equal(t, fileContentFiltered2, "Prefix content")
78+
}
79+
6680
func TestDirectoryPatterns(t *testing.T) {
6781
assertAccepts("foo/", "", "bar", t)
6882
assertAccepts("foo/", "", "foo", t)

0 commit comments

Comments
 (0)