Skip to content

Commit 40e3921

Browse files
kboomGrzegorz Gurgul
and
Grzegorz Gurgul
authored
Add tests (#5)
Co-authored-by: Grzegorz Gurgul <[email protected]>
1 parent fcfa55e commit 40e3921

33 files changed

+933
-42
lines changed

.git-hooks/post-merge

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
echo "Updating submodules"
4+
git submodule update --init
5+
git submodule foreach git pull origin main
6+
git submodule foreach git checkout main

.github/workflows/build.yml

+37-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: build
1+
name: Build
22

33
on:
44
push:
@@ -12,30 +12,60 @@ env:
1212
jobs:
1313
build:
1414
runs-on: windows-2022
15+
permissions:
16+
checks: write
1517
defaults:
1618
run:
1719
working-directory: ${{ github.workspace }}
1820

1921
steps:
20-
- uses: actions/checkout@v2
22+
- uses: actions/checkout@v3
2123
with:
24+
submodules: 'true'
2225
fetch-depth: 0
2326

24-
- name: Add MSBuild to PATH
27+
- name: Track main branches in submodules
28+
run: |
29+
git submodule foreach git checkout main
30+
31+
- uses: actions/cache@v2
32+
with:
33+
path: ~/.nuget/packages
34+
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.config') }}
35+
restore-keys: |
36+
${{ runner.os }}-nuget-
37+
38+
- name: Setup MSBuild
2539
uses: microsoft/setup-msbuild@v1
2640
with:
2741
vs-version: '[17.0, ]'
2842

43+
- name: Setup NuGet
44+
uses: NuGet/[email protected]
45+
2946
- name: Setup VSTest
3047
uses: darenm/Setup-VSTest@v1
3148

3249
- name: Restore NuGet packages
3350
run: nuget restore ${{env.SOLUTION_FILE_PATH}}
3451

3552
- name: Build
36-
run: |
37-
msbuild ${{env.SOLUTION_FILE_PATH}} /p:configuration=Release /p:DeployExtension=false /p:ZipPackageCompressionLevel=normal /v:m
53+
run: msbuild ${{env.SOLUTION_FILE_PATH}} /p:configuration=Release /p:DeployExtension=false /p:ZipPackageCompressionLevel=normal /v:m
3854
shell: powershell
3955

40-
- name: Tests
41-
run: vstest.console.exe **\*.Tests.dll /TestCaseFilter:"FullyQualifiedName!=Xunit.Instances.VisualStudio&integration!=true" #exclude integration tests and the psuedo-tests that launch a VS instance
56+
- name: Run vstests
57+
uses: microsoft/[email protected]
58+
with:
59+
testAssembly: GitTreeFilter.Core.Tests.dll
60+
searchFolder: ${{ github.workspace }}\GitTreeFilter.Core.Tests\bin\Release\net472\
61+
runInParallel: true
62+
otherConsoleOptions: /Logger:trx
63+
64+
- name: Test Report
65+
uses: dorny/test-reporter@v1
66+
if: always()
67+
with:
68+
name: Test results
69+
path: "TestResults/*.trx"
70+
reporter: dotnet-trx
71+
fail-on-error: true

.github/workflows/release.yml

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: release
1+
name: Release
22

33
on:
44
push:
@@ -16,17 +16,24 @@ permissions:
1616
jobs:
1717
build-test-and-release-vs22:
1818
runs-on: windows-2022
19+
permissions:
20+
checks: write
1921
defaults:
2022
run:
2123
working-directory: ${{ github.workspace }}
2224
env:
2325
VsixManifestPath: .\GitTreeFilter\source.extension.vsixmanifest
2426

2527
steps:
26-
- uses: actions/checkout@v2
28+
- uses: actions/checkout@v3
2729
with:
30+
submodules: 'true'
2831
fetch-depth: 0
2932

33+
- name: Track main branches in submodules
34+
run: |
35+
git submodule foreach git checkout main
36+
3037
- uses: actions/cache@v2
3138
with:
3239
path: ~/.nuget/packages
@@ -42,6 +49,9 @@ jobs:
4249
- name: Setup VSTest
4350
uses: darenm/Setup-VSTest@v1
4451

52+
- name: Setup NuGet
53+
uses: NuGet/[email protected]
54+
4555
- name: Restore NuGet packages
4656
run: nuget restore ${{env.SOLUTION_FILE_PATH}}
4757

@@ -60,8 +70,22 @@ jobs:
6070
msbuild ${{env.SOLUTION_FILE_PATH}} /p:configuration=Release /p:DeployExtension=false /p:ZipPackageCompressionLevel=normal /v:m
6171
shell: powershell
6272

63-
- name: Tests
64-
run: vstest.console.exe **\*.Tests.dll
73+
- name: Run vstests
74+
uses: microsoft/[email protected]
75+
with:
76+
testAssembly: GitTreeFilter.Core.Tests.dll
77+
searchFolder: ${{ github.workspace }}\GitTreeFilter.Core.Tests\bin\Release\net472\
78+
runInParallel: true
79+
otherConsoleOptions: /Logger:trx
80+
81+
- name: Test Report
82+
uses: dorny/test-reporter@v1
83+
if: always()
84+
with:
85+
name: Test results
86+
path: "TestResults/*.trx"
87+
reporter: dotnet-trx
88+
fail-on-error: true
6589

6690
- name: Set up Git actions user
6791
uses: fregante/setup-git-user@v1

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,4 @@ MigrationBackup/
360360
.ionide/
361361

362362
# Fody - auto-generated XML schema
363-
FodyWeavers.xsd
363+
FodyWeavers.xsd

.gitmodules

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[submodule "TestRepository"]
2+
path = TestResources/TestRepository
3+
url = https://github.com/kboom/GitTreeFilter-4-VisualStudio-Test-Repository
4+
update = rebase
5+
branch = main

Docs/demo.gif

2.28 MB
Loading
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Linq;
2+
using FluentAssertions;
3+
using FluentAssertions.Execution;
4+
using GitTreeFilter.Core.Tests.DataSource;
5+
using GitTreeFilter.Core.Tests.Repositories;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace GitTreeFilter.Core.Tests
9+
{
10+
[TestClass]
11+
[TestCategory("SolutionRepository.Branches")]
12+
public class BranchesTest : SolutionRepositoryTest
13+
{
14+
[DataTestMethod]
15+
[AllRepositoriesData]
16+
public void IsCorrect(ITestRepository testRepository)
17+
{
18+
// given
19+
var solutionRepository = CreateSolutionRepository(testRepository);
20+
21+
// when
22+
var branches = solutionRepository.Branches;
23+
24+
// then
25+
branches.Should()
26+
.NotContainNulls()
27+
.And
28+
.HaveCountGreaterThan(0)
29+
.And
30+
.AllSatisfy(x =>
31+
{
32+
x.Reference.Should().NotBeNull();
33+
x.FriendlyName.Should().NotBeNullOrEmpty();
34+
x.Reference.Should().NotBeNull();
35+
x.Reference.Sha.Should().NotBeNullOrEmpty();
36+
x.Reference.ShortMessage.Should().NotBeNullOrEmpty();
37+
});
38+
}
39+
40+
[DataTestMethod]
41+
[AllRepositoriesData]
42+
public void ResolvesToCorrectBranches(ITestRepository testRepository)
43+
{
44+
// given
45+
var solutionRepository = CreateSolutionRepository(testRepository);
46+
47+
// when
48+
var branches = solutionRepository.Branches;
49+
50+
// then
51+
using (new AssertionScope())
52+
{
53+
branches.Select(b => b.FriendlyName)
54+
.Should()
55+
.Equal(testRepository.Branches.Select(x => x.FriendlyName));
56+
57+
branches.Select(b => b.Reference.Sha)
58+
.Should()
59+
.Equal(testRepository.Branches.Select(x => x.Reference.Sha));
60+
61+
branches.Select(b => b.Reference.ShortMessage)
62+
.Should()
63+
.Equal(testRepository.Branches.Select(x => x.Reference.ShortMessage));
64+
}
65+
}
66+
}
67+
}
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Collections.Immutable;
2+
using System.Linq;
3+
using FluentAssertions;
4+
using FluentAssertions.Execution;
5+
using GitTreeFilter.Core.Exceptions;
6+
using GitTreeFilter.Core.Models;
7+
using GitTreeFilter.Core.Tests.DataSource;
8+
using GitTreeFilter.Core.Tests.Repositories;
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
11+
namespace GitTreeFilter.Core.Tests
12+
{
13+
[TestClass]
14+
[TestCategory("SolutionRepository.Changeset")]
15+
public class ChangesetTest : SolutionRepositoryTest
16+
{
17+
[TestMethod]
18+
public void ThrowsNothingToCompareExceptionIfReferenceNotSet()
19+
{
20+
// given
21+
var solutionRepository = CreateSolutionRepository(TestRepositories.First);
22+
solutionRepository.GitReference = null;
23+
24+
// when & then
25+
solutionRepository.Invoking(x => x.Changeset)
26+
.Should()
27+
.Throw<NothingToCompareException>()
28+
.WithMessage("Git reference was null");
29+
}
30+
31+
[TestMethod]
32+
public void ThrowsRepositoryTargetNotFoundExceptionIfReferenceSetToMissingObject()
33+
{
34+
// given
35+
var solutionRepository = CreateSolutionRepository(TestRepositories.First);
36+
const string missingSha = "9eabf5b536662000f79978c4d1b6e4eff5c8d785";
37+
solutionRepository.GitReference = new GitCommit(new GitCommitObject(missingSha, "any description"));
38+
39+
// when & then
40+
solutionRepository.Invoking(x => x.Changeset)
41+
.Should()
42+
.Throw<RepositoryTargetNotFoundException>()
43+
.WithMessage("Selected reference does not exist in repository");
44+
}
45+
46+
[DataTestMethod]
47+
[AllRepositoriesData]
48+
public void ReturnsEmptyChangesetIfTargetEqualsHead(ITestRepository testRepository)
49+
{
50+
// given
51+
var solutionRepository = CreateSolutionRepository(testRepository);
52+
solutionRepository.GitReference = testRepository.Head;
53+
54+
// when
55+
var changeset = solutionRepository.Changeset;
56+
57+
// then
58+
using(new AssertionScope())
59+
{
60+
changeset.Should().NotBeNull();
61+
changeset.Should().BeEmpty();
62+
}
63+
}
64+
65+
[DataTestMethod]
66+
[FirstRepositoryChangesetDataSource]
67+
public void FirstRepositoryChangeset(ITestRepository testRepository, GitReference<GitCommitObject> gitReference, IImmutableSet<string> changedFilesPaths)
68+
{
69+
// given
70+
var solutionRepository = CreateSolutionRepository(testRepository);
71+
solutionRepository.GitReference = gitReference;
72+
73+
// when
74+
var changeset = solutionRepository.Changeset;
75+
76+
// then
77+
using (new AssertionScope())
78+
{
79+
changeset.Should().NotBeNull();
80+
changeset.Select(x => x.AbsoluteFilePath).Should().BeEquivalentTo(changedFilesPaths);
81+
}
82+
}
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Immutable;
4+
using System.Globalization;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Reflection;
8+
using GitTreeFilter.Core.Models;
9+
using GitTreeFilter.Core.Tests.Repositories;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
12+
namespace GitTreeFilter.Core.Tests.DataSource
13+
{
14+
[AttributeUsage(AttributeTargets.Method)]
15+
internal abstract class AbstractChangesetDataSource : Attribute, ITestDataSource
16+
{
17+
public IEnumerable<object[]> GetData(MethodInfo methodInfo)
18+
{
19+
foreach (var descriptor in GetDescriptors())
20+
{
21+
var repository = descriptor.Repository;
22+
var basePathForFiles = repository.Solution.SolutionPath;
23+
var files = descriptor.FilesInChangeset
24+
.Select(x => Path.Combine(basePathForFiles, x))
25+
.ToImmutableHashSet();
26+
27+
yield return new object[] { repository, descriptor.Reference, files };
28+
}
29+
}
30+
31+
public abstract IReadOnlyList<ChangesetDescriptor> GetDescriptors();
32+
33+
public string GetDisplayName(MethodInfo methodInfo, object[] data)
34+
{
35+
if (data != null)
36+
{
37+
var repositoryName = (data[0] as ITestRepository).Name;
38+
var targetName = (data[1] as GitReference<GitCommitObject>).FriendlyName;
39+
var targetType = data[1].GetType();
40+
41+
return string.Format(CultureInfo.CurrentCulture, "{0} ({1} reference {2} {3})", methodInfo.Name, repositoryName, targetName, targetType);
42+
}
43+
44+
return null;
45+
}
46+
}
47+
48+
internal class ChangesetDescriptor
49+
{
50+
public ITestRepository Repository { get; set; }
51+
52+
public GitReference<GitCommitObject> Reference { get; set; }
53+
54+
public IReadOnlyList<string> FilesInChangeset { get; set; }
55+
}
56+
}

0 commit comments

Comments
 (0)