-
Notifications
You must be signed in to change notification settings - Fork 119
/
GitIgnoreTests.cs
185 lines (150 loc) · 7.2 KB
/
GitIgnoreTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Text;
using TestUtilities;
using Xunit;
namespace Microsoft.Build.Tasks.Git.UnitTests
{
public class GitIgnoreTests
{
[Theory]
[InlineData("\t", "\t", GitIgnore.PatternFlags.None)]
[InlineData("\v", "\v", GitIgnore.PatternFlags.None)]
[InlineData("\f", "\f", GitIgnore.PatternFlags.None)]
[InlineData("\\ ", " ", GitIgnore.PatternFlags.None)]
[InlineData(" #", " #", GitIgnore.PatternFlags.None)]
[InlineData("!x ", "x", GitIgnore.PatternFlags.Negative)]
[InlineData("!x/", "x", GitIgnore.PatternFlags.Negative | GitIgnore.PatternFlags.DirectoryPattern)]
[InlineData("!/x", "x", GitIgnore.PatternFlags.Negative | GitIgnore.PatternFlags.FullPath)]
[InlineData("x/", "x", GitIgnore.PatternFlags.DirectoryPattern)]
[InlineData("/x", "x", GitIgnore.PatternFlags.FullPath)]
[InlineData("//x//", "/x/", GitIgnore.PatternFlags.DirectoryPattern | GitIgnore.PatternFlags.FullPath)]
[InlineData("\\", "\\", GitIgnore.PatternFlags.None)]
[InlineData("\\x", "x", GitIgnore.PatternFlags.None)]
[InlineData("x\\", "x\\", GitIgnore.PatternFlags.None)]
[InlineData("\\\\", "\\", GitIgnore.PatternFlags.None)]
[InlineData("\\abc\\xy\\z", "abcxyz", GitIgnore.PatternFlags.None)]
internal void TryParsePattern(string line, string glob, GitIgnore.PatternFlags flags)
{
Assert.True(GitIgnore.TryParsePattern(line, new StringBuilder(), out var actualGlob, out var actualFlags));
Assert.Equal(glob, actualGlob);
Assert.Equal(flags, actualFlags);
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(" ")]
[InlineData("#")]
[InlineData("!")]
[InlineData("/")]
[InlineData("//")]
[InlineData("!/")]
[InlineData("!//")]
public void TryParsePattern_None(string line)
{
Assert.False(GitIgnore.TryParsePattern(line, new StringBuilder(), out _, out _));
}
[Fact]
public void IsIgnored_CaseSensitive()
{
using var temp = new TempRoot();
var root = temp.CreateDirectory();
var workingDir = root.CreateDirectory("Repo");
// root
// A (.gitignore)
// B
// C (.gitignore)
// D1, D2, D3
var dirA = workingDir.CreateDirectory("A");
var dirB = dirA.CreateDirectory("B");
var dirC = dirB.CreateDirectory("C");
dirC.CreateDirectory("D1");
dirC.CreateDirectory("D2");
dirC.CreateDirectory("D3");
dirA.CreateFile(".gitignore").WriteAllText(@"
!z.txt
*.txt
!u.txt
!v.txt
!.git
b/
D3/
Bar/**/*.xyz
v.txt
");
dirC.CreateFile(".gitignore").WriteAllText(@"
!a.txt
D2
D1/c.cs
/*.c
");
var ignore = new GitIgnore(root: null, PathUtils.ToPosixDirectoryPath(workingDir.Path), ignoreCase: false);
// outside of the working directory:
Assert.Null(ignore.IsPathIgnored(root.Path));
Assert.Null(ignore.IsPathIgnored(workingDir.Path.ToUpperInvariant()));
// special case:
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, ".git")));
Assert.False(ignore.IsPathIgnored(workingDir.Path));
Assert.False(ignore.IsPathIgnored(workingDir.Path + Path.DirectorySeparatorChar));
Assert.False(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "X")));
// matches "*.txt"
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "b.txt")));
// matches "!a.txt"
Assert.False(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "a.txt")));
// matches "*.txt", "!z.txt" is ignored
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "z.txt")));
// matches "*.txt", overriden by "!u.txt"
Assert.False(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "u.txt")));
// matches "*.txt", overriden by "!v.txt", which is overriden by "v.txt"
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "v.txt")));
// matches directory name "D2"
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D2", "E", "a.txt")));
// does not match "b/" (treated as a file path)
Assert.False(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "b")));
// matches "b/" (treated as a directory path)
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "b") + Path.DirectorySeparatorChar));
// matches "D3/" (existing directory path)
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D3")));
// matches "D1/c.cs"
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "c.cs")));
// matches "Bar/**/*.xyz"
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "Bar", "Baz", "Goo", ".xyz")));
// matches "/*.c"
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "x.c")));
// does not match "/*.c"
Assert.False(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "B", "C", "D1", "x.c")));
}
[Fact]
public void IsIgnored_IgnoreCase()
{
using var temp = new TempRoot();
var root = temp.CreateDirectory();
var workingDir = root.CreateDirectory("Repo");
// root
// A (.gitignore)
// diR
var dirA = workingDir.CreateDirectory("A");
dirA.CreateDirectory("diR");
dirA.CreateFile(".gitignore").WriteAllText(@"
*.txt
!a.TXT
dir/
");
var ignore = new GitIgnore(root: null, PathUtils.ToPosixDirectoryPath(workingDir.Path), ignoreCase: true);
// outside of the working directory:
Assert.Null(ignore.IsPathIgnored(root.Path.ToUpperInvariant()));
// special case:
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, ".GIT")));
// matches "*.txt"
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "b.TXT")));
// matches "!a.TXT"
Assert.False(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "a.txt")));
// matches directory name "dir/"
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "DIr", "a.txt")));
// matches "dir/" (treated as a directory path)
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "DiR") + Path.DirectorySeparatorChar));
// matches "dir/" (existing directory path)
Assert.True(ignore.IsPathIgnored(Path.Combine(workingDir.Path, "A", "DIR")));
}
}
}