Skip to content

Commit 7f09e82

Browse files
authored
Merge pull request #465 from saul/451-fix
pathFilters paths should be relative to the version.json
2 parents 5e894bb + bf8ba86 commit 7f09e82

13 files changed

+530
-207
lines changed

doc/pathFilters.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ Path filters take on a variety of formats, and can specify paths relative to the
5050

5151
Multiple path filters may also be specified. The order is irrelevant. After a path matches any non-exclude path filter, it will be run through all exclude path filter. If it matches, the path is ignored.
5252

53-
| Path filter | Description |
54-
| ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
55-
| `file-here.txt`<br>`./quux.txt`<br>`./sub-dir/foo.txt`<br>`../subdir/inclusion.txt` | File will be included. Path is relative to the `version.json` file. |
56-
| `sub-dir`<br>`../sub-dir` | Directory will be included. Path is relative to the `version.json` file. |
57-
| `:/dir/file.txt` | File will be included. Path is absolute (i.e., relative to the root of the repository). |
58-
| `:!bar.txt`<br>`:^../foo/baz.txt` | File will be excluded. Path is relative to the `version.json` file. `:!` and `:^` prefixes are synonymous. |
59-
| `:!/root-file.txt` | File will be excluded. Path is absolute (i.e., relative to the root of the repository). |
53+
| Path filter | Description |
54+
| ---------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
55+
| `./quux.txt`<br>`file-here.txt`<br>`sub-dir/foo.txt`<br>`../sibling/inclusion.txt` | File will be included. Path is relative to the `version.json` file. |
56+
| `./`<br>`sub-dir`<br>`../sibling` | Directory will be included. Path is relative to the `version.json` file. |
57+
| `/root-file.txt`<br>`:/dir/file.txt` | File will be included. Path is absolute (i.e., relative to the root of the repository). |
58+
| `:!bar.txt`<br>`:^../foo/baz.txt` | File will be excluded. Path is relative to the `version.json` file. `:!` and `:^` prefixes are synonymous. |
59+
| `:!/root-file.txt` | File will be excluded. Path is absolute (i.e., relative to the root of the repository). |

src/NerdBank.GitVersioning.Tests/FilterPathTests.cs

+36-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.InteropServices;
23
using Nerdbank.GitVersioning;
34
using Xunit;
45

@@ -7,6 +8,9 @@ public class FilterPathTests
78
[Theory]
89
[InlineData("./", "foo", "foo")]
910
[InlineData("../relative-dir", "foo", "relative-dir")]
11+
[InlineData("relative-dir", "some/dir/../zany", "some/zany/relative-dir")]
12+
[InlineData("relative-dir", "some/dir/..", "some/relative-dir")]
13+
[InlineData("relative-dir", "some/../subdir", "subdir/relative-dir")]
1014
[InlineData("../../some/dir/here", "foo/multi/wow", "foo/some/dir/here")]
1115
[InlineData("relativepath.txt", "foo", "foo/relativepath.txt")]
1216
[InlineData("./relativepath.txt", "foo", "foo/relativepath.txt")]
@@ -41,8 +45,8 @@ public void CanBeParsedToRepoRelativePath(string pathSpec, string relativeTo, st
4145
[InlineData(":^/absolute.txt", "foo", "absolute.txt")]
4246
public void PathsCanBeExcluded(string pathSpec, string relativeTo, string repoRelativePath)
4347
{
44-
Assert.True(new FilterPath(pathSpec, relativeTo, true).Excludes(repoRelativePath));
45-
Assert.True(new FilterPath(pathSpec, relativeTo, false).Excludes(repoRelativePath));
48+
Assert.True(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, true));
49+
Assert.True(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, false));
4650
}
4751

4852
[Theory]
@@ -58,8 +62,8 @@ public void PathsCanBeExcluded(string pathSpec, string relativeTo, string repoRe
5862
[InlineData("relativepath.txt", "foo", "foo/relativepath.txt")]
5963
public void NonMatchingPathsAreNotExcluded(string pathSpec, string relativeTo, string repoRelativePath)
6064
{
61-
Assert.False(new FilterPath(pathSpec, relativeTo, true).Excludes(repoRelativePath));
62-
Assert.False(new FilterPath(pathSpec, relativeTo, false).Excludes(repoRelativePath));
65+
Assert.False(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, true));
66+
Assert.False(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, false));
6367
}
6468

6569
[Theory]
@@ -75,7 +79,7 @@ public void NonMatchingPathsAreNotExcluded(string pathSpec, string relativeTo, s
7579
[InlineData(":^/absOLUte.txt", "foo", "Absolute.TXT")]
7680
public void PathsCanBeExcludedCaseInsensitive(string pathSpec, string relativeTo, string repoRelativePath)
7781
{
78-
Assert.True(new FilterPath(pathSpec, relativeTo, true).Excludes(repoRelativePath));
82+
Assert.True(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, true));
7983
}
8084

8185
[Theory]
@@ -91,7 +95,7 @@ public void PathsCanBeExcludedCaseInsensitive(string pathSpec, string relativeTo
9195
[InlineData(":^/absOLUte.txt", "foo", "Absolute.TXT")]
9296
public void NonMatchingPathsAreNotExcludedCaseSensitive(string pathSpec, string relativeTo, string repoRelativePath)
9397
{
94-
Assert.False(new FilterPath(pathSpec, relativeTo, false).Excludes(repoRelativePath));
98+
Assert.False(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, false));
9599
}
96100

97101
[Fact]
@@ -103,4 +107,29 @@ public void InvalidPathspecsThrow()
103107
Assert.Throws<FormatException>(() => new FilterPath("../foo.txt", ""));
104108
Assert.Throws<FormatException>(() => new FilterPath(".././a/../../foo.txt", "foo"));
105109
}
106-
}
110+
111+
[Theory]
112+
[InlineData(":/abc/def", "", "/abc/def")]
113+
[InlineData(":/abc/def", ".", "/abc/def")]
114+
[InlineData("abc", ".", "./abc")]
115+
[InlineData(".", ".", "./")]
116+
[InlineData("./", ".", "./")]
117+
[InlineData("./", "", "./")]
118+
[InlineData("abc/def", ".", "./abc/def")]
119+
[InlineData("abc/def", "./foo", "./abc/def")]
120+
[InlineData("../Directory.Build.props", "./foo", "../Directory.Build.props")]
121+
[InlineData(":!/Directory.Build.props", "./foo", ":!/Directory.Build.props")]
122+
[InlineData(":!relative.txt", "./foo", ":!relative.txt")]
123+
public void ToPathSpec(string pathSpec, string relativeTo, string expectedPathSpec)
124+
{
125+
Assert.Equal(expectedPathSpec, new FilterPath(pathSpec, relativeTo).ToPathSpec(relativeTo));
126+
}
127+
128+
[Theory]
129+
[InlineData("foo/bar", "foo", "./bar")]
130+
[InlineData("foo/bar", "FOO", "./bar")]
131+
public void ToPathSpecTest(string pathSpec, string relativeTo, string expectedPathSpec)
132+
{
133+
Assert.Equal(expectedPathSpec, new FilterPath(pathSpec, ".").ToPathSpec(relativeTo));
134+
}
135+
}

0 commit comments

Comments
 (0)