Skip to content

Commit

Permalink
pathFilters paths should be relative to the version.json
Browse files Browse the repository at this point in the history
Fixes dotnet#451

Path were incorrectly relative to the project directory.
  • Loading branch information
saul committed Apr 12, 2020
1 parent 07423ab commit 5a62b8a
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 164 deletions.
14 changes: 7 additions & 7 deletions doc/pathFilters.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ Path filters take on a variety of formats, and can specify paths relative to the

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.

| Path filter | Description |
| ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| `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. |
| `sub-dir`<br>`../sub-dir` | Directory will be included. Path is relative to the `version.json` file. |
| `:/dir/file.txt` | File will be included. Path is absolute (i.e., relative to the root of the repository). |
| `:!bar.txt`<br>`:^../foo/baz.txt` | File will be excluded. Path is relative to the `version.json` file. `:!` and `:^` prefixes are synonymous. |
| `:!/root-file.txt` | File will be excluded. Path is absolute (i.e., relative to the root of the repository). |
| Path filter | Description |
| ---------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| `./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. |
| `./`<br>`sub-dir`<br>`../sibling` | Directory will be included. Path is relative to the `version.json` file. |
| `/root-file.txt`<br>`:/dir/file.txt` | File will be included. Path is absolute (i.e., relative to the root of the repository). |
| `:!bar.txt`<br>`:^../foo/baz.txt` | File will be excluded. Path is relative to the `version.json` file. `:!` and `:^` prefixes are synonymous. |
| `:!/root-file.txt` | File will be excluded. Path is absolute (i.e., relative to the root of the repository). |
43 changes: 36 additions & 7 deletions src/NerdBank.GitVersioning.Tests/FilterPathTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;
using Nerdbank.GitVersioning;
using Xunit;

Expand All @@ -7,6 +8,9 @@ public class FilterPathTests
[Theory]
[InlineData("./", "foo", "foo")]
[InlineData("../relative-dir", "foo", "relative-dir")]
[InlineData("relative-dir", "some/dir/../zany", "some/zany/relative-dir")]
[InlineData("relative-dir", "some/dir/..", "some/relative-dir")]
[InlineData("relative-dir", "some/../subdir", "subdir/relative-dir")]
[InlineData("../../some/dir/here", "foo/multi/wow", "foo/some/dir/here")]
[InlineData("relativepath.txt", "foo", "foo/relativepath.txt")]
[InlineData("./relativepath.txt", "foo", "foo/relativepath.txt")]
Expand Down Expand Up @@ -41,8 +45,8 @@ public void CanBeParsedToRepoRelativePath(string pathSpec, string relativeTo, st
[InlineData(":^/absolute.txt", "foo", "absolute.txt")]
public void PathsCanBeExcluded(string pathSpec, string relativeTo, string repoRelativePath)
{
Assert.True(new FilterPath(pathSpec, relativeTo, true).Excludes(repoRelativePath));
Assert.True(new FilterPath(pathSpec, relativeTo, false).Excludes(repoRelativePath));
Assert.True(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, true));
Assert.True(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, false));
}

[Theory]
Expand All @@ -58,8 +62,8 @@ public void PathsCanBeExcluded(string pathSpec, string relativeTo, string repoRe
[InlineData("relativepath.txt", "foo", "foo/relativepath.txt")]
public void NonMatchingPathsAreNotExcluded(string pathSpec, string relativeTo, string repoRelativePath)
{
Assert.False(new FilterPath(pathSpec, relativeTo, true).Excludes(repoRelativePath));
Assert.False(new FilterPath(pathSpec, relativeTo, false).Excludes(repoRelativePath));
Assert.False(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, true));
Assert.False(new FilterPath(pathSpec, relativeTo).Excludes(repoRelativePath, false));
}

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

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

[Fact]
Expand All @@ -103,4 +107,29 @@ public void InvalidPathspecsThrow()
Assert.Throws<FormatException>(() => new FilterPath("../foo.txt", ""));
Assert.Throws<FormatException>(() => new FilterPath(".././a/../../foo.txt", "foo"));
}
}

[Theory]
[InlineData(":/abc/def", "", "/abc/def")]
[InlineData(":/abc/def", ".", "/abc/def")]
[InlineData("abc", ".", "./abc")]
[InlineData(".", ".", "./")]
[InlineData("./", ".", "./")]
[InlineData("./", "", "./")]
[InlineData("abc/def", ".", "./abc/def")]
[InlineData("abc/def", "./foo", "./abc/def")]
[InlineData("../Directory.Build.props", "./foo", "../Directory.Build.props")]
[InlineData(":!/Directory.Build.props", "./foo", ":!/Directory.Build.props")]
[InlineData(":!relative.txt", "./foo", ":!relative.txt")]
public void ToPathSpec(string pathSpec, string relativeTo, string expectedPathSpec)
{
Assert.Equal(expectedPathSpec, new FilterPath(pathSpec, relativeTo).ToPathSpec(relativeTo));
}

[Theory]
[InlineData("foo/bar", "foo", "./bar")]
[InlineData("foo/bar", "FOO", "./bar")]
public void ToPathSpecTest(string pathSpec, string relativeTo, string expectedPathSpec)
{
Assert.Equal(expectedPathSpec, new FilterPath(pathSpec, ".").ToPathSpec(relativeTo));
}
}

0 comments on commit 5a62b8a

Please sign in to comment.