Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saul committed Dec 10, 2019
1 parent 4310a2d commit fea3905
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/NerdBank.GitVersioning.Tests/FilterPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class FilterPathTests
[InlineData(":!/absolutepath.txt", "foo", "absolutepath.txt")]
[InlineData(":!\\absolutepath.txt", "foo", "absolutepath.txt")]
[InlineData("../bar/relativepath.txt", "foo", "bar/relativepath.txt")]
[InlineData("/", "foo", "")]
[InlineData("/absolute/file.txt", "foo", "absolute/file.txt")]
[InlineData(":/", "foo", "")]
[InlineData(":/absolutepath.txt", "foo", "absolutepath.txt")]
[InlineData(":/bar/absolutepath.txt", "foo", "bar/absolutepath.txt")]
Expand Down
88 changes: 85 additions & 3 deletions src/NerdBank.GitVersioning.Tests/GitExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,15 @@ public void GetVersionHeight_IntroducingFiltersIncrementsHeight()
Assert.Equal(2, this.Repo.GetVersionHeight("some-sub-dir"));
}

[Fact]
public void GetVersionHeight_IncludeFilter()
[Theory]
[InlineData("./")]
[InlineData("../some-sub-dir")]
[InlineData("/some-sub-dir")]
[InlineData(":/some-sub-dir")]
public void GetVersionHeight_IncludeFilter(string includeFilter)
{
var versionData = VersionOptions.FromVersion(new Version("1.2"));
versionData.PathFilters = new[] { "./" };
versionData.PathFilters = new[] { includeFilter };
this.WriteVersionFile(versionData, "some-sub-dir");
Assert.Equal(1, this.Repo.GetVersionHeight("some-sub-dir"));

Expand Down Expand Up @@ -189,6 +193,84 @@ public void GetVersionHeight_IncludeExcludeFilter()
Assert.Equal(2, this.Repo.GetVersionHeight("some-sub-dir"));
}

[Theory]
[InlineData(":^/excluded-dir")]
[InlineData(":^../excluded-dir")]
public void GetVersionHeight_AddingExcludeDoesNotLowerHeight(string excludePathFilter)
{
var versionData = VersionOptions.FromVersion(new Version("1.2"));
this.WriteVersionFile(versionData, "some-sub-dir");
Assert.Equal(1, this.Repo.GetVersionHeight("some-sub-dir"));

// Commit a file which will later be ignored
var ignoredFilePath = Path.Combine(this.RepoPath, "excluded-dir", "ignore.txt");
Directory.CreateDirectory(Path.GetDirectoryName(ignoredFilePath));
File.WriteAllText(ignoredFilePath, "hello");
Commands.Stage(this.Repo, ignoredFilePath);
this.Repo.Commit("Add file which will later be excluded", this.Signer, this.Signer);
Assert.Equal(2, this.Repo.GetVersionHeight("some-sub-dir"));

versionData.PathFilters = new[] { excludePathFilter };
this.WriteVersionFile(versionData, "some-sub-dir");
Assert.Equal(3, this.Repo.GetVersionHeight("some-sub-dir"));

// Committing a change to an ignored file does not increment the version height
File.WriteAllText(ignoredFilePath, "changed");
Commands.Stage(this.Repo, ignoredFilePath);
this.Repo.Commit("Change now excluded file", this.Signer, this.Signer);
Assert.Equal(3, this.Repo.GetVersionHeight("some-sub-dir"));
}

[Fact]
public void GetVersionHeight_IncludeRoot()
{
var versionData = VersionOptions.FromVersion(new Version("1.2"));
versionData.PathFilters = new[] { ":/" };
this.WriteVersionFile(versionData, "some-sub-dir");
Assert.Equal(1, this.Repo.GetVersionHeight("some-sub-dir"));

// Expect commit outside of project tree to affect version height
var otherFilePath = Path.Combine(this.RepoPath, "my-file.txt");
File.WriteAllText(otherFilePath, "hello");
Commands.Stage(this.Repo, otherFilePath);
this.Repo.Commit("Add other file outside of project root", this.Signer, this.Signer);
Assert.Equal(2, this.Repo.GetVersionHeight("some-sub-dir"));

// Expect commit inside project tree to affect version height
var containedFilePath = Path.Combine(this.RepoPath, "some-sub-dir", "another-file.txt");
File.WriteAllText(containedFilePath, "hello");
Commands.Stage(this.Repo, containedFilePath);
this.Repo.Commit("Add file within project root", this.Signer, this.Signer);
Assert.Equal(3, this.Repo.GetVersionHeight("some-sub-dir"));
}

[Fact]
public void GetVersionHeight_IncludeRootExcludeSome()
{
var versionData = VersionOptions.FromVersion(new Version("1.2"));
versionData.PathFilters = new[] { ":/", ":^/excluded-dir" };
this.WriteVersionFile(versionData, "some-sub-dir");
Assert.Equal(1, this.Repo.GetVersionHeight("some-sub-dir"));

// Expect commit in an excluded directory to not affect version height
var ignoredFilePath = Path.Combine(this.RepoPath, "excluded-dir", "my-file.txt");
Directory.CreateDirectory(Path.GetDirectoryName(ignoredFilePath));
File.WriteAllText(ignoredFilePath, "hello");
Commands.Stage(this.Repo, ignoredFilePath);
this.Repo.Commit("Add other file to excluded directory", this.Signer, this.Signer);
Assert.Equal(1, this.Repo.GetVersionHeight("some-sub-dir"));

// Expect commit within another directory to affect version height
var otherFilePath = Path.Combine(this.RepoPath, "another-dir", "another-file.txt");
Directory.CreateDirectory(Path.GetDirectoryName(otherFilePath));
File.WriteAllText(otherFilePath, "hello");
Commands.Stage(this.Repo, otherFilePath);
this.Repo.Commit("Add file within project root", this.Signer, this.Signer);
Assert.Equal(2, this.Repo.GetVersionHeight("some-sub-dir"));
}

// TODO: test excluding root

[Theory]
[InlineData("2.2-alpha", "2.2-rc", false)]
[InlineData("2.2-rc", "2.2", false)]
Expand Down
5 changes: 5 additions & 0 deletions src/NerdBank.GitVersioning/FilterPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class FilterPath
/// </summary>
public string RepoRelativePath { get; }

/// <summary>
/// True if this <see cref="FilterPath"/> represents the root of the repository.
/// </summary>
public bool IsRoot => this.RepoRelativePath == "";

/// <summary>
/// Parses a pathspec-like string into a root-relative path.
/// </summary>
Expand Down
14 changes: 11 additions & 3 deletions src/NerdBank.GitVersioning/GitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -732,17 +732,25 @@ private static int GetCommitHeight(Commit commit, string repoRelativeProjectDire

height = 1;

if (includePaths != null && excludePaths != null)
if (includePaths != null)
{
// If there are no include paths, or any of the include
// paths refer to the root of the repository, then do not
// filter the diff at all.
var diffInclude =
includePaths.Count == 0 || pathFilters.Any(filter => filter.IsRoot)
? null
: includePaths;

// If the diff between this commit and any of its parents
// does not touch a path that we care about, don't bump the
// height.
var relevantCommit =
commit.Parents.Any()
? commit.Parents.Any(parent => ContainsRelevantChanges(commit.GetRepository().Diff
.Compare<TreeChanges>(parent.Tree, commit.Tree, includePaths)))
.Compare<TreeChanges>(parent.Tree, commit.Tree, diffInclude)))
: ContainsRelevantChanges(commit.GetRepository().Diff
.Compare<TreeChanges>(null, commit.Tree, includePaths));
.Compare<TreeChanges>(null, commit.Tree, diffInclude));

if (!relevantCommit)
{
Expand Down

0 comments on commit fea3905

Please sign in to comment.