소스 검색

Fix missing parent info case in MatchesUsingParentResults

Unfortunately, this check was missing in the original version. It could
cause a positive match to be overwritten by checking parent dirs.

Signed-off-by: Aaron Lehmann <alehmann@netflix.com>
Aaron Lehmann 3 년 전
부모
커밋
55da5245de
2개의 변경된 파일25개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 1
      pkg/fileutils/fileutils.go
  2. 24 0
      pkg/fileutils/fileutils_test.go

+ 1 - 1
pkg/fileutils/fileutils.go

@@ -251,7 +251,7 @@ func (pm *PatternMatcher) MatchesUsingParentResults(file string, parentMatchInfo
 			// If the zero value of MatchInfo was passed in, we don't have
 			// If the zero value of MatchInfo was passed in, we don't have
 			// any information about the parent dir's match results, and we
 			// any information about the parent dir's match results, and we
 			// apply the same logic as MatchesOrParentMatches.
 			// apply the same logic as MatchesOrParentMatches.
-			if len(parentMatched) == 0 {
+			if !match && len(parentMatched) == 0 {
 				if parentPath := filepath.Dir(file); parentPath != "." {
 				if parentPath := filepath.Dir(file); parentPath != "." {
 					parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
 					parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
 					// Check to see if the pattern matches one of our parent dirs.
 					// Check to see if the pattern matches one of our parent dirs.

+ 24 - 0
pkg/fileutils/fileutils_test.go

@@ -465,6 +465,30 @@ func TestMatches(t *testing.T) {
 			check(pm, test.text, test.pass, desc)
 			check(pm, test.text, test.pass, desc)
 		}
 		}
 	})
 	})
+
+	t.Run("MatchesUsingParentResultsNoContext", func(t *testing.T) {
+		check := func(pm *PatternMatcher, text string, pass bool, desc string) {
+			res, _, _ := pm.MatchesUsingParentResults(text, MatchInfo{})
+			assert.Check(t, is.Equal(pass, res), desc)
+		}
+
+		for _, test := range tests {
+			desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
+			pm, err := NewPatternMatcher([]string{test.pattern})
+			assert.NilError(t, err, desc)
+
+			check(pm, test.text, test.pass, desc)
+		}
+
+		for _, test := range multiPatternTests {
+			desc := fmt.Sprintf("pattern=%q text=%q", test.patterns, test.text)
+			pm, err := NewPatternMatcher(test.patterns)
+			assert.NilError(t, err, desc)
+
+			check(pm, test.text, test.pass, desc)
+		}
+	})
+
 }
 }
 
 
 func TestCleanPatterns(t *testing.T) {
 func TestCleanPatterns(t *testing.T) {