Skip to content

Commit b833eb3

Browse files
committed
Add Tests for Orphan recognition and the new Filtering structure
1 parent bc48adb commit b833eb3

File tree

5 files changed

+221
-1
lines changed

5 files changed

+221
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Moq;
7+
using NUnit.Framework;
8+
using Octokit;
9+
using ReleaseCleaner.Filtering;
10+
using ReleaseCleaner.Invocation;
11+
12+
namespace ReleaseCleaner
13+
{
14+
[TestFixture]
15+
[Category("Core Logic")]
16+
[Category("Github Interaction")]
17+
public class CleanerTests
18+
{
19+
private Cleaner cut;
20+
private Mock<IGitHubClient> clientMock = new Mock<IGitHubClient>();
21+
22+
[SetUp]
23+
public void SetUp() {
24+
clientMock.Reset();
25+
cut = new Cleaner(clientMock.Object);
26+
}
27+
28+
[Test]
29+
public void GivenSimpleArguments_FindReleases_RetrievesTagsAndReleases()
30+
{
31+
const string repoOwner = "foo";
32+
const string repoName = "bar";
33+
var repoMock = new Mock<IRepositoriesClient>();
34+
clientMock.Setup(client => client.Repository).Returns(repoMock.Object).Verifiable();
35+
var releasesMock = new Mock<IReleasesClient>();
36+
repoMock.SetupGet(repo => repo.Release).Returns(releasesMock.Object).Verifiable();
37+
releasesMock.Setup(releases => releases.GetAll(repoOwner, repoName))
38+
.Returns(Task.FromResult<IReadOnlyList<Release>>(new List<Release>()))
39+
.Verifiable();
40+
41+
var argBuilder = new ArgumentsBuilder();
42+
argBuilder.AddMatcher(".*");
43+
argBuilder.SetOwner(repoOwner);
44+
argBuilder.SetProject(repoName);
45+
46+
var results = cut.FindReleases(argBuilder.Build(), new ConstantValueFilter(true));
47+
48+
Mock.VerifyAll(clientMock, repoMock, releasesMock);
49+
Assert.AreEqual(0, results.Count);
50+
}
51+
52+
}
53+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using NUnit.Framework;
2+
using Octokit;
3+
using ReleaseCleaner.Invocation;
4+
using Moq;
5+
6+
namespace ReleaseCleaner.Filtering
7+
{
8+
[TestFixture]
9+
[Category("Core Logic")]
10+
[Category("Filtering")]
11+
public class BasicFilterTests
12+
{
13+
[TestCase(true, true)]
14+
[TestCase(true, false)]
15+
[TestCase(false, true)]
16+
[TestCase(false, false)]
17+
public void CheckAndFilter(bool a, bool b)
18+
{
19+
var filter = new AndFilter(new []{new ConstantValueFilter(a), new ConstantValueFilter(b)});
20+
Assert.AreEqual(a && b, filter.Matches(null));
21+
}
22+
23+
[TestCase(true, true)]
24+
[TestCase(true, false)]
25+
[TestCase(false, true)]
26+
[TestCase(false, false)]
27+
public void CheckOrFilter(bool a, bool b)
28+
{
29+
var filter = new OrFilter(new []{new ConstantValueFilter(a), new ConstantValueFilter(b)});
30+
Assert.AreEqual(a || b, filter.Matches(null));
31+
}
32+
33+
[TestCase(true, true, false)]
34+
[TestCase(true, false, true)]
35+
[TestCase(false, true, true)]
36+
[TestCase(false, false, false)]
37+
public void ConditionalInversionFilter(bool invert, bool wrappedResult, bool expected)
38+
{
39+
var builder = ArgumentsMockData.BaseBuilder();
40+
if (invert)
41+
builder.Inverted();
42+
43+
var filter = new ConditionalInversion(new ConstantValueFilter(wrappedResult));
44+
filter.Prepare(builder.Build());
45+
Assert.AreEqual(expected, filter.Matches(null));
46+
}
47+
}
48+
49+
50+
class ConstantValueFilter : IReleasePredicate
51+
{
52+
private readonly bool value;
53+
public ConstantValueFilter(bool value) {
54+
this.value = value;
55+
}
56+
public bool Matches(Release releaseInfo)
57+
{
58+
return value;
59+
}
60+
61+
public void Prepare(Arguments arguments)
62+
{
63+
// NO-OP
64+
}
65+
}
66+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Moq;
4+
using NUnit.Framework;
5+
using Octokit;
6+
using ReleaseCleaner.Invocation;
7+
8+
namespace ReleaseCleaner.Filtering
9+
{
10+
[TestFixture]
11+
[Category("Core Logic")]
12+
[Category("Filtering")]
13+
public class OrphanMatcherTests
14+
{
15+
private readonly Mock<IGitHubClient> clientMock = new Mock<IGitHubClient>();
16+
private OrphanMatcher cut;
17+
18+
[SetUp]
19+
public void TestSetup()
20+
{
21+
clientMock.Reset();
22+
cut = new OrphanMatcher(clientMock.Object);
23+
}
24+
25+
[Test]
26+
public void NoOrphanFlag_DoesNotIncurRequest()
27+
{
28+
var args = ArgumentsMockData.BaseBuilder();
29+
// OrphanOnly defaults to false
30+
cut.Prepare(args.Build());
31+
var result = cut.Matches(null);
32+
Assert.IsTrue(result);
33+
clientMock.VerifyNoOtherCalls();
34+
}
35+
36+
[Test]
37+
public void PrepareWithOrphanFlag_HitsApi()
38+
{
39+
var args = ArgumentsMockData.BaseBuilder();
40+
args.Orphans();
41+
42+
var repositoryMock = new Mock<IRepositoriesClient>();
43+
clientMock.SetupGet(client => client.Repository).Returns(repositoryMock.Object);
44+
repositoryMock.Setup(repository => repository.GetAllTags(ArgumentsMockData.Owner, ArgumentsMockData.Project))
45+
.Returns(Task.FromResult<IReadOnlyList<RepositoryTag>>(new List<RepositoryTag>()))
46+
.Verifiable();
47+
cut.Prepare(args.Build());
48+
49+
Mock.VerifyAll(clientMock, repositoryMock);
50+
clientMock.VerifyNoOtherCalls();
51+
repositoryMock.VerifyNoOtherCalls();
52+
}
53+
54+
[TestCase(true, "exists", false, Description = "Draft and existing Tag is not orphan")]
55+
[TestCase(true, "missing", true, Description = "Draft and missing Tag is an orphan")]
56+
[TestCase(false, "exists", false, Description = "NonDraft and existing Tag is not orphan")]
57+
[TestCase(false, "missing", false, Description = "NonDraft and missing Tag is not orphan")]
58+
public void OrphaningLogic(bool draftStatus, string tagName, bool expected)
59+
{
60+
var release = new Release(
61+
null, null, null, null, 0, null,
62+
tagName: tagName,
63+
null, null, null,
64+
draft: draftStatus, false, default, null, default, null, null, new List<ReleaseAsset>()
65+
);
66+
67+
var args = ArgumentsMockData.BaseBuilder();
68+
args.Orphans();
69+
70+
var repositoryMock = new Mock<IRepositoriesClient>();
71+
clientMock.SetupGet(client => client.Repository).Returns(repositoryMock.Object);
72+
repositoryMock.Setup(repository => repository.GetAllTags(ArgumentsMockData.Owner, ArgumentsMockData.Project))
73+
.Returns(Task.FromResult<IReadOnlyList<RepositoryTag>>(new List<RepositoryTag>(new [] { new RepositoryTag("exists", default, default, default, default)})))
74+
.Verifiable();
75+
cut.Prepare(args.Build());
76+
77+
Assert.AreEqual(expected, cut.Matches(release));
78+
79+
Mock.VerifyAll(clientMock, repositoryMock);
80+
clientMock.VerifyNoOtherCalls();
81+
repositoryMock.VerifyNoOtherCalls();
82+
}
83+
}
84+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace ReleaseCleaner.Invocation
2+
{
3+
static class ArgumentsMockData
4+
{
5+
public const string Owner = "foo";
6+
public const string Project = "bar";
7+
8+
public static ArgumentsBuilder BaseBuilder()
9+
{
10+
var builder = new ArgumentsBuilder();
11+
builder.AddMatcher(".*");
12+
builder.SetOwner(Owner);
13+
builder.SetProject(Project);
14+
return builder;
15+
}
16+
}
17+
}

ReleaseCleanerTests/ReleaseCleanerTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
5+
<LangVersion>7.2</LangVersion>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

0 commit comments

Comments
 (0)