Skip to content

Commit 0dda984

Browse files
committed
Add GitVcs tests
1 parent c119237 commit 0dda984

File tree

5 files changed

+128
-31
lines changed

5 files changed

+128
-31
lines changed

test/GitVcsTest.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

test/Vcs/Git/GitVcsTest.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.IO;
3+
using Skarp.Version.Cli.Vcs.Git;
4+
using Xunit;
5+
6+
namespace Skarp.Version.Cli.Test.Vcs.Git
7+
{
8+
public class GitVcsTest : IClassFixture<GitVcsFixture>
9+
{
10+
private readonly GitVcsFixture _fixture;
11+
12+
13+
public GitVcsTest(GitVcsFixture fixture)
14+
{
15+
_fixture = fixture;
16+
}
17+
18+
[Fact]
19+
public void ReturnsProperToolname()
20+
{
21+
Assert.Equal("git", _fixture.Vcs.ToolName());
22+
}
23+
24+
[Fact]
25+
public void DetectingGitOnMachineWorks()
26+
{
27+
Assert.True(_fixture.Vcs.IsVcsToolPresent(_fixture.AbsolutePathToGitTestDir));
28+
}
29+
30+
[Fact]
31+
public void IsRepositoryCleanWorks()
32+
{
33+
Assert.True(_fixture.Vcs.IsRepositoryClean(_fixture.AbsolutePathToGitTestDir));
34+
}
35+
36+
[Fact]
37+
public void CanCommit()
38+
{
39+
// arrange
40+
var commitMessage = Guid.NewGuid().ToString("N");
41+
var fileToCommit = "dotnet-version.dll";
42+
File.Copy(fileToCommit, Path.Combine(_fixture.GitTestDir, fileToCommit));
43+
44+
// act
45+
_fixture.Vcs.Commit(fileToCommit, commitMessage, _fixture.AbsolutePathToGitTestDir);
46+
47+
// assert
48+
49+
// grep the git-log for messages containing our guid message
50+
var (
51+
exitCode,
52+
stdOut,
53+
_
54+
) = GitVcs.LaunchGitWithArgsInner($"log --grep={commitMessage}", 1000,
55+
_fixture.AbsolutePathToGitTestDir);
56+
Assert.Equal(0, exitCode);
57+
Assert.Contains(commitMessage, stdOut);
58+
}
59+
60+
[Fact]
61+
public void CanCreateTags()
62+
{
63+
var tagToMake = Guid.NewGuid().ToString("N");
64+
65+
_fixture.Vcs.Tag(tagToMake, _fixture.AbsolutePathToGitTestDir);
66+
67+
var (exitCode, stdOut, _) =
68+
GitVcs.LaunchGitWithArgsInner(
69+
"tag -l"
70+
, 1000,
71+
_fixture.AbsolutePathToGitTestDir
72+
);
73+
74+
Assert.Equal(0, exitCode);
75+
Assert.Contains(tagToMake, stdOut);
76+
}
77+
}
78+
}

test/Vcs/Git/GitVcsTestFixture.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.IO;
3+
using System.IO.Compression;
4+
using Skarp.Version.Cli.Vcs.Git;
5+
6+
namespace Skarp.Version.Cli.Test.Vcs.Git
7+
{
8+
public class GitVcsFixture : IDisposable
9+
{
10+
public readonly string GitTestDir;
11+
public readonly GitVcs Vcs;
12+
public readonly string AbsolutePathToGitTestDir;
13+
14+
public GitVcsFixture()
15+
{
16+
GitTestDir = "./target-git-dir";
17+
AbsolutePathToGitTestDir = Path.Combine(
18+
Directory.GetCurrentDirectory(),
19+
GitTestDir
20+
);
21+
DirectoryDelete(GitTestDir, recursive: true);
22+
23+
ZipFile.ExtractToDirectory("./target-git.zip", "./");
24+
Vcs = new GitVcs();
25+
}
26+
27+
28+
private void DirectoryDelete(string dir, bool recursive)
29+
{
30+
try
31+
{
32+
Directory.Delete(dir, recursive);
33+
}
34+
// we don't want to fail at all if deleting the dir fails
35+
catch (Exception ex)
36+
{
37+
}
38+
}
39+
40+
public void Dispose()
41+
{
42+
DirectoryDelete(GitTestDir, recursive: true);
43+
}
44+
}
45+
}

test/dotnet-version-test.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@
2222
<ItemGroup>
2323
<ProjectReference Include="..\src\dotnet-version.csproj" />
2424
</ItemGroup>
25+
<ItemGroup>
26+
<None Update="target-git.zip">
27+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
28+
</None>
29+
</ItemGroup>
2530
</Project>

test/target-git.zip

18.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)