| 1 |
// Copyright 2019 The go-git authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
// |
| 15 |
// This file was originally part of https://github.com/go-git/go-git/blob/main/plumbing/format/gitattributes |
| 16 |
// and has been modified to provide tests for a simplified matcher that works with a single pattern. |
| 17 |
// |
| 18 |
// This file was originally part of https://github.com/go-git/go-git/blob/main/plumbing/format/gitattributes |
| 19 |
// and has been modified to be dependency free. |
| 20 |
|
| 21 |
package gitignore |
| 22 |
|
| 23 |
import ( |
| 24 |
"testing" |
| 25 |
) |
| 26 |
|
| 27 |
func TestMatcher_Match(t *testing.T) { |
| 28 |
pattern := ParsePattern("**/middle/v[uo]l?ano", nil) |
| 29 |
matcher := NewMatcher(pattern) |
| 30 |
|
| 31 |
if !matcher.Match([]string{"head", "middle", "vulkano"}, false) { |
| 32 |
t.Error("Expected to match 'head/middle/vulkano'") |
| 33 |
} |
| 34 |
|
| 35 |
if matcher.Match([]string{"head", "middle", "other"}, false) { |
| 36 |
t.Error("Expected not to match 'head/middle/other'") |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestMatcher_Exclude(t *testing.T) { |
| 41 |
pattern := ParsePattern("!volcano", nil) |
| 42 |
matcher := NewMatcher(pattern) |
| 43 |
|
| 44 |
// Include patterns return false |
| 45 |
if matcher.Match([]string{"volcano"}, false) { |
| 46 |
t.Error("Include patterns should return false") |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Test that demonstrates how to handle exclusion patterns with Matcher |
| 51 |
func TestMatcher_ExcludeHandling(t *testing.T) { |
| 52 |
// For exclusion patterns, Matcher will return false |
| 53 |
// because exclusion means "don't exclude" which is effectively "include" |
| 54 |
excludePattern := ParsePattern("!volcano", nil) |
| 55 |
matcher := NewMatcher(excludePattern) |
| 56 |
|
| 57 |
// This returns false because it's an inclusion pattern |
| 58 |
if matcher.Match([]string{"volcano"}, false) { |
| 59 |
t.Error("Include patterns should return false") |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Test the "exclude everything except" example from git documentation |
| 64 |
// Note: This is a simplified version that tests individual patterns |
| 65 |
func TestMatcher_EverythingExceptExample(t *testing.T) { |
| 66 |
// Test /* pattern (exclude everything) |
| 67 |
pattern1 := ParsePattern("/*", nil) |
| 68 |
matcher1 := NewMatcher(pattern1) |
| 69 |
|
| 70 |
if !matcher1.Match([]string{"foo"}, true) { // Should match and exclude |
| 71 |
t.Error("Expected to match 'foo'") |
| 72 |
} |
| 73 |
|
| 74 |
if !matcher1.Match([]string{"baz"}, false) { // Should match and exclude |
| 75 |
t.Error("Expected to match 'baz'") |
| 76 |
} |
| 77 |
|
| 78 |
// Test !/foo pattern (but don't exclude foo) |
| 79 |
pattern2 := ParsePattern("!/foo", nil) |
| 80 |
matcher2 := NewMatcher(pattern2) |
| 81 |
|
| 82 |
if matcher2.Match([]string{"foo"}, true) { // Should match but include (not exclude) |
| 83 |
t.Error("Include patterns should return false") |
| 84 |
} |
| 85 |
|
| 86 |
// Test /foo/* pattern (exclude files in foo directory) |
| 87 |
pattern3 := ParsePattern("/foo/*", nil) |
| 88 |
matcher3 := NewMatcher(pattern3) |
| 89 |
|
| 90 |
if !matcher3.Match([]string{"foo", "bar"}, false) { // Should match and exclude |
| 91 |
t.Error("Expected to match 'foo/bar'") |
| 92 |
} |
| 93 |
|
| 94 |
// Test !/foo/bar pattern (but don't exclude foo/bar) |
| 95 |
pattern4 := ParsePattern("!/foo/bar", nil) |
| 96 |
matcher4 := NewMatcher(pattern4) |
| 97 |
|
| 98 |
if matcher4.Match([]string{"foo", "bar"}, false) { // Should match but include (not exclude) |
| 99 |
t.Error("Include patterns should return false") |
| 100 |
} |
| 101 |
} |