Skip to content

Commit 0082636

Browse files
author
roeil
committed
test
1 parent b8077d2 commit 0082636

File tree

5 files changed

+165
-12
lines changed

5 files changed

+165
-12
lines changed

src/GitVersion.Core.Tests/Extensions/GitRepositoryTestingExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public static ICommit CreateMockCommit()
3232
return commit;
3333
}
3434

35+
public static ICommit CreateMockCommit(List<string> diffPaths)
36+
{
37+
var commit = CreateMockCommit();
38+
commit.DiffPaths.Returns(diffPaths);
39+
return commit;
40+
}
41+
3542
public static IBranch CreateMockBranch(string name, params ICommit[] commits)
3643
{
3744
var branch = Substitute.For<IBranch>();

src/GitVersion.Core.Tests/IntegrationTests/IgnoreCommitScenarios.cs

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ public void GivenTrunkBasedWorkflowWithIgnoreConfigurationBeforeCommitWithTagThe
135135
fixture.ApplyTag("1.0.0");
136136
fixture.MakeACommit("D");
137137

138+
var before = commitC.Committer.When.AddSeconds(1);
138139
var configuration = TrunkBasedConfigurationBuilder.New
139-
.WithIgnoreConfiguration(new IgnoreConfiguration { Before = commitC.Committer.When })
140+
.WithIgnoreConfiguration(new IgnoreConfiguration { Before = before })
140141
.Build();
141142

142143
// ✅ succeeds as expected
@@ -285,8 +286,9 @@ public void GivenGitHubFlowWorkflowWithIgnoreConfigurationBeforeCommitWithTagThe
285286
fixture.ApplyTag("1.0.0");
286287
fixture.MakeACommit("D");
287288

289+
var before = commitC.Committer.When.AddSeconds(1);
288290
var configuration = GitHubFlowConfigurationBuilder.New
289-
.WithIgnoreConfiguration(new IgnoreConfiguration { Before = commitC.Committer.When })
291+
.WithIgnoreConfiguration(new IgnoreConfiguration { Before = before })
290292
.Build();
291293

292294
// ✅ succeeds as expected
@@ -331,4 +333,104 @@ public void GivenGitHubFlowWorkflowWithCommitParameterBThenTagShouldBeConsidered
331333
// ✅ succeeds as expected
332334
fixture.AssertFullSemver(semanticVersion, configuration, commitId: commitA.Sha);
333335
}
336+
337+
[Test]
338+
public void GivenTrunkBasedWorkflowWithIgnoreConfigurationForPathThenVersionShouldBeCorrect()
339+
{
340+
using var fixture = new EmptyRepositoryFixture();
341+
342+
var commitA = fixture.Repository.MakeACommit("A");
343+
var commitB = fixture.Repository.MakeACommit("B");
344+
fixture.MakeACommit("C");
345+
fixture.MakeACommit("D");
346+
347+
var ignoredPath = fixture.Repository.Diff.Compare<LibGit2Sharp.TreeChanges>(commitA.Tree, commitB.Tree).Select(element => element.Path).First();
348+
349+
var configuration = TrunkBasedConfigurationBuilder.New
350+
.WithIgnoreConfiguration(new IgnoreConfiguration { Paths = { ignoredPath } })
351+
.Build();
352+
353+
// commitB should be ignored, so version should be as if B didn't exist
354+
fixture.AssertFullSemver("0.0.3", configuration);
355+
}
356+
357+
[Test]
358+
public void GivenTrunkBasedWorkflowWithIgnoreConfigurationForPathAndCommitParameterCThenVersionShouldBeCorrect()
359+
{
360+
using var fixture = new EmptyRepositoryFixture();
361+
362+
var commitA = fixture.Repository.MakeACommit("A");
363+
fixture.MakeACommit("B");
364+
var commitC = fixture.Repository.MakeACommit("C");
365+
fixture.MakeACommit("D");
366+
367+
var ignoredPath = fixture.Repository.Diff.Compare<LibGit2Sharp.TreeChanges>(commitA.Tree, commitC.Tree).Select(element => element.Path).First();
368+
369+
var configuration = TrunkBasedConfigurationBuilder.New
370+
.WithIgnoreConfiguration(new IgnoreConfiguration { Paths = { ignoredPath } })
371+
.Build();
372+
373+
// commitC should be ignored, so version should be as if C didn't exist
374+
fixture.AssertFullSemver("0.0.2", configuration, commitId: commitC.Sha);
375+
}
376+
377+
[Test]
378+
public void GivenGitHubFlowWorkflowWithIgnoreConfigurationForPathThenVersionShouldBeCorrect()
379+
{
380+
using var fixture = new EmptyRepositoryFixture();
381+
382+
var commitA = fixture.Repository.MakeACommit("A");
383+
var commitB = fixture.Repository.MakeACommit("B");
384+
fixture.MakeACommit("C");
385+
fixture.MakeACommit("D");
386+
387+
var ignoredPath = fixture.Repository.Diff.Compare<LibGit2Sharp.TreeChanges>(commitA.Tree, commitB.Tree).Select(element => element.Path).First();
388+
389+
var configuration = GitHubFlowConfigurationBuilder.New
390+
.WithIgnoreConfiguration(new IgnoreConfiguration { Paths = { ignoredPath } })
391+
.Build();
392+
393+
// commitB should be ignored, so version should be as if B didn't exist
394+
fixture.AssertFullSemver("0.0.1-3", configuration);
395+
}
396+
397+
[Test]
398+
public void GivenTrunkBasedWorkflowWithIgnoreConfigurationForTaggedCommitPathThenTagShouldBeIgnored()
399+
{
400+
using var fixture = new EmptyRepositoryFixture();
401+
402+
var commitA = fixture.Repository.MakeACommit("A");
403+
var commitB = fixture.Repository.MakeACommit("B");
404+
fixture.ApplyTag("1.0.0");
405+
fixture.MakeACommit("C");
406+
407+
var ignoredPath = fixture.Repository.Diff.Compare<LibGit2Sharp.TreeChanges>(commitA.Tree, commitB.Tree).Select(element => element.Path).First();
408+
409+
var configuration = TrunkBasedConfigurationBuilder.New
410+
.WithIgnoreConfiguration(new IgnoreConfiguration { Paths = { ignoredPath } })
411+
.Build();
412+
413+
// commitB should be ignored, so version should be as if B didn't exist
414+
fixture.AssertFullSemver("0.0.2", configuration);
415+
}
416+
417+
[Test]
418+
public void GivenGitHubFlowWorkflowWithIgnoreConfigurationForTaggedCommitPathThenTagShouldBeIgnored()
419+
{
420+
using var fixture = new EmptyRepositoryFixture();
421+
422+
var commitA = fixture.Repository.MakeACommit("A");
423+
var commitB = fixture.Repository.MakeACommit("B");
424+
fixture.ApplyTag("1.0.0");
425+
fixture.MakeACommit("C");
426+
427+
var ignoredPath = fixture.Repository.Diff.Compare<LibGit2Sharp.TreeChanges>(commitA.Tree, commitB.Tree).Select(element => element.Path).First();
428+
429+
var configuration = GitHubFlowConfigurationBuilder.New
430+
.WithIgnoreConfiguration(new IgnoreConfiguration { Paths = { ignoredPath } })
431+
.Build();
432+
433+
// commitB should be ignored, so version should be as if B didn't exist
434+
fixture.AssertFullSemver("0.0.1-2", configuration);
435+
}
334436
}

src/GitVersion.Core.Tests/VersionCalculation/PathFilterTests.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,55 @@ namespace GitVersion.Core.Tests;
77
public class PathFilterTests : TestBase
88
{
99
[Test]
10-
public void VerifyNullGuard() => Should.Throw<ArgumentNullException>(() => new PathFilter(null!));
10+
public void VerifyNullGuard()
11+
{
12+
var sut = new PathFilter([]);
13+
14+
Should.Throw<ArgumentNullException>(() => sut.Exclude((IBaseVersion)null!, out _));
15+
}
16+
17+
[Test]
18+
public void WhenPathMatchShouldExcludeWithReason()
19+
{
20+
var commit = GitRepositoryTestingExtensions.CreateMockCommit(["/path"]);
21+
BaseVersion version = new("dummy", new SemanticVersion(1), commit);
22+
var sut = new PathFilter(commit.DiffPaths);
23+
24+
sut.Exclude(version, out var reason).ShouldBeTrue();
25+
reason.ShouldNotBeNullOrWhiteSpace();
26+
}
27+
28+
[Test]
29+
public void WhenPathMismatchShouldNotExclude()
30+
{
31+
var commit = GitRepositoryTestingExtensions.CreateMockCommit(["/path"]);
32+
BaseVersion version = new("dummy", new SemanticVersion(1), commit);
33+
var sut = new PathFilter(["/another_path"]);
34+
35+
sut.Exclude(version, out var reason).ShouldBeFalse();
36+
reason.ShouldBeNull();
37+
}
38+
39+
[Test]
40+
public void WhenCommitSourceStartsWithCommitTagShouldNotBeExcluded()
41+
{
42+
var commit = GitRepositoryTestingExtensions.CreateMockCommit(["/path"]);
43+
BaseVersion version = new("Git tag: v1.0.0", new SemanticVersion(1), commit);
44+
var sut = new PathFilter(commit.DiffPaths);
45+
46+
var result = sut.Exclude(version, out var reason);
47+
48+
result.ShouldBeFalse();
49+
reason.ShouldBeNull();
50+
}
51+
52+
[Test]
53+
public void ExcludeShouldAcceptVersionWithNullCommit()
54+
{
55+
BaseVersion version = new("dummy", new SemanticVersion(1));
56+
var sut = new PathFilter(["/path"]);
57+
58+
sut.Exclude(version, out var reason).ShouldBeFalse();
59+
reason.ShouldBeNull();
60+
}
1161
}

src/GitVersion.Core/VersionCalculation/PathFilter.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ namespace GitVersion.VersionCalculation;
77

88
internal class PathFilter(IReadOnlyList<string> paths) : IVersionFilter
99
{
10-
private readonly List<Regex> pathsRegexes = [.. paths.Select(path => new Regex(path, RegexOptions.IgnoreCase | RegexOptions.Compiled))];
10+
private readonly List<Regex> pathsRegexes = [.. paths.Select(path => new Regex(path, RegexOptions.Compiled))];
1111
private readonly ConcurrentDictionary<string, bool> pathMatchCache = [];
1212

1313
public bool Exclude(IBaseVersion baseVersion, [NotNullWhen(true)] out string? reason)
1414
{
1515
ArgumentNullException.ThrowIfNull(baseVersion);
16-
17-
reason = null;
18-
if (baseVersion.Source.StartsWith("Fallback") || baseVersion.Source.StartsWith("Git tag") || baseVersion.Source.StartsWith("NextVersion")) return false;
19-
2016
return Exclude(baseVersion.BaseVersionSource, out reason);
2117
}
2218

@@ -26,9 +22,7 @@ public bool Exclude(ICommit? commit, [NotNullWhen(true)] out string? reason)
2622

2723
if (commit != null)
2824
{
29-
var patchPaths = commit.DiffPaths;
30-
31-
foreach (var path in patchPaths)
25+
foreach (var path in commit.DiffPaths)
3226
{
3327
if (!pathMatchCache.TryGetValue(path, out var isMatch))
3428
{

src/GitVersion.LibGit2Sharp/Git/Commit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ public IReadOnlyList<string> DiffPaths
4444
public override int GetHashCode() => equalityHelper.GetHashCode(this);
4545
public override string ToString() => $"'{Id.ToString(7)}' - {this.innerCommit.MessageShort}";
4646
public static implicit operator LibGit2Sharp.Commit(Commit d) => d.innerCommit;
47-
private TreeChanges CommitChanges => new TreeChanges(this.repoDiff.Compare<LibGit2Sharp.TreeChanges>(this.innerCommit.Tree, this.innerCommit.Parents.FirstOrDefault()?.Tree));
47+
private TreeChanges CommitChanges => new(this.repoDiff.Compare<LibGit2Sharp.TreeChanges>(this.innerCommit.Tree, this.innerCommit.Parents.FirstOrDefault()?.Tree));
4848
}

0 commit comments

Comments
 (0)