Skip to content

Commit

Permalink
Merge pull request #28 from ivancea/#27/regex-filters
Browse files Browse the repository at this point in the history
[#27] Added regex filters
  • Loading branch information
ivancea authored Apr 29, 2022
2 parents 3961e7f + b4e0105 commit 6a97a95
Show file tree
Hide file tree
Showing 34 changed files with 791 additions and 315 deletions.
12 changes: 12 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-stryker": {
"version": "0.22.3",
"commands": [
"dotnet-stryker"
]
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

coverage.*
clones
data

Expand Down
34 changes: 34 additions & 0 deletions Backend.Tests/Backend.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<CodeAnalysisRuleSet>StyleCop.ruleset</CodeAnalysisRuleSet>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CollectCoverage>true</CollectCoverage>
<CoverletOutputFormat>lcov</CoverletOutputFormat>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="3.1.2" />
<PackageReference Include="fluentassertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Backend\Backend.csproj" />
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions Backend.Tests/Services/BasicUserValidationServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading.Tasks;
using GitMonitor.Configurations;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GitMonitor.Services
{
[TestClass]
public class BasicUserValidationServiceTest
{
[TestMethod]
public async Task IsValidAsync_Anonymous()
{
var options = new ApplicationOptions
{
Username = null,
Password = null,
};

var service = new BasicUserValidationService(Options.Create(options));

Assert.IsTrue(await service.IsValidAsync("username", "password"));
}

[TestMethod]
public async Task IsValidAsync_Credentials()
{
var options = new ApplicationOptions
{
Username = "username",
Password = "password",
};

var service = new BasicUserValidationService(Options.Create(options));

Assert.IsTrue(await service.IsValidAsync("username", "password"));
Assert.IsFalse(await service.IsValidAsync("wrong_username", "password"));
Assert.IsFalse(await service.IsValidAsync("username", "wrong_password"));
Assert.IsFalse(await service.IsValidAsync("wrong_username", "wrong_password"));
}
}
}
109 changes: 109 additions & 0 deletions Backend.Tests/Services/ChangesTrackers/BranchesTrackerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System.Collections.Generic;
using FluentAssertions;
using GitMonitor.Objects.Changes;
using LibGit2Sharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace GitMonitor.Services.ChangesTrackers
{
[TestClass]
public class BranchesTrackerTest
{
public BranchesTrackerTest()
{
Branches = new List<Branch>();

var branchCollection = new Mock<BranchCollection>();

branchCollection.Setup(b => b.GetEnumerator()).Returns(() => Branches.GetEnumerator());

Repository = Mock.Of<IRepository>(r => r.Branches == branchCollection.Object);

Changes = new List<Change>();
}

private List<Branch> Branches { get; }

private IRepository Repository { get; }

private List<Change> Changes { get; }

[TestCleanup]
public void Cleanup()
{
Branches.Clear();
Changes.Clear();
}

[TestMethod]
public void AddedBranch()
{
Branches.Add(MockBranch("original", "1"));

using (var tracker = new BranchesTracker(Repository, Changes))
{
Branches.Clear();
Branches.Add(MockBranch("original", "1"));
Branches.Add(MockBranch("new", "1"));
}

Changes.Should().HaveCount(1)
.And.SatisfyRespectively(c =>
{
c.ObjectType.Should().Be(ChangeObjectType.Branch);
c.Type.Should().Be(ChangeType.Created);
c.ObjectName.Should().Be("new");
c.Should().BeOfType<BranchChange>()
.Which.TargetCommit.Should().Be("1");
});
}

[TestMethod]
public void RemovedBranch()
{
Branches.Add(MockBranch("original", "1"));

using (var tracker = new BranchesTracker(Repository, Changes))
{
Branches.Clear();
}

Assert.AreEqual(1, Changes.Count);
Assert.AreEqual(ChangeObjectType.Branch, Changes[0].ObjectType);
Assert.AreEqual(ChangeType.Deleted, Changes[0].Type);
Assert.AreEqual("original", Changes[0].ObjectName);
Assert.IsInstanceOfType(Changes[0], typeof(BranchChange));
Assert.AreEqual("1", ((BranchChange)Changes[0]).TargetCommit);
}

[TestMethod]
public void UpdatedBranch()
{
Branches.Add(MockBranch("original", "1"));

using (var tracker = new BranchesTracker(Repository, Changes))
{
Branches.Clear();
Branches.Add(MockBranch("original", "2"));
}

Assert.AreEqual(1, Changes.Count);
Assert.AreEqual(ChangeObjectType.Branch, Changes[0].ObjectType);
Assert.AreEqual(ChangeType.Updated, Changes[0].Type);
Assert.AreEqual("original", Changes[0].ObjectName);
Assert.IsInstanceOfType(Changes[0], typeof(BranchChange));
Assert.AreEqual("2", ((BranchChange)Changes[0]).TargetCommit);
}

private Branch MockBranch(string name, string sha)
{
return Mock.Of<Branch>(b =>
b.IsRemote == true &&
b.RemoteName == "origin" &&
b.CanonicalName == name &&
b.FriendlyName == "origin/" + name &&
b.Tip == Mock.Of<Commit>(c => c.Sha == sha));
}
}
}
95 changes: 95 additions & 0 deletions Backend.Tests/Services/ChangesTrackers/CommitsTrackerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using GitMonitor.Objects.Changes;
using LibGit2Sharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace GitMonitor.Services.ChangesTrackers
{
[TestClass]
public class CommitsTrackerTest
{
public CommitsTrackerTest()
{
Commits = new List<Commit>();

var commitCollection = new Mock<IQueryableCommitLog>();

commitCollection.Setup(c => c.GetEnumerator()).Returns(() => Commits.GetEnumerator());
commitCollection.Setup(c => c.QueryBy(It.IsAny<CommitFilter>())).Returns(() => commitCollection.Object);

Repository = Mock.Of<IRepository>(r =>
r.Commits == commitCollection.Object &&
r.Refs == new Mock<ReferenceCollection>().Object);

Changes = new List<Change>();
}

private List<Commit> Commits { get; }

private IRepository Repository { get; }

private List<Change> Changes { get; }

[TestCleanup]
public void Cleanup()
{
Commits.Clear();
Changes.Clear();
}

[TestMethod]
public void AddedCommit()
{
Commits.Add(MockCommit("Message1", "1", "U", "E"));

using (var tracker = new CommitsTracker(Repository, Changes))
{
Commits.Clear();
Commits.Add(MockCommit("Message1", "1", "U", "E"));
Commits.Add(MockCommit("Message2", "2", "User", "test@example.com"));
}

Assert.AreEqual(1, Changes.Count);
Assert.AreEqual(ChangeObjectType.Commit, Changes[0].ObjectType);
Assert.AreEqual(ChangeType.Created, Changes[0].Type);
Assert.AreEqual("Message2", Changes[0].ObjectName);
Assert.IsInstanceOfType(Changes[0], typeof(CommitChange));
Assert.AreEqual("2", ((CommitChange)Changes[0]).Hash);
Assert.AreEqual("Message2", ((CommitChange)Changes[0]).Message);
Assert.AreEqual("User", ((CommitChange)Changes[0]).User?.Name);
Assert.AreEqual("test@example.com", ((CommitChange)Changes[0]).User?.Email);
}

[TestMethod]
public void RemovedCommit()
{
Commits.Add(MockCommit("Message1", "1", "User", "test@example.com"));

using (var tracker = new CommitsTracker(Repository, Changes))
{
Commits.Clear();
}

Assert.AreEqual(1, Changes.Count);
Assert.AreEqual(ChangeObjectType.Commit, Changes[0].ObjectType);
Assert.AreEqual(ChangeType.Deleted, Changes[0].Type);
Assert.AreEqual("Message1", Changes[0].ObjectName);
Assert.IsInstanceOfType(Changes[0], typeof(CommitChange));
Assert.AreEqual("1", ((CommitChange)Changes[0]).Hash);
Assert.AreEqual("Message1", ((CommitChange)Changes[0]).Message);
Assert.AreEqual("User", ((CommitChange)Changes[0]).User?.Name);
Assert.AreEqual("test@example.com", ((CommitChange)Changes[0]).User?.Email);
}

private Commit MockCommit(string message, string sha, string user, string email)
{
return Mock.Of<Commit>(c =>
c.Message == message &&
c.MessageShort == message &&
c.Sha == sha &&
c.Committer == new Signature(user, email, DateTimeOffset.FromUnixTimeSeconds(1)));
}
}
}
Loading

0 comments on commit 6a97a95

Please sign in to comment.