Skip to content

Commit 4fc6c6b

Browse files
committed
Add GitVcs tests
1 parent 138af8c commit 4fc6c6b

File tree

5 files changed

+142
-31
lines changed

5 files changed

+142
-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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.IO.Compression;
5+
using System.Threading;
6+
using Skarp.Version.Cli.Vcs.Git;
7+
8+
namespace Skarp.Version.Cli.Test.Vcs.Git
9+
{
10+
public class GitVcsFixture : IDisposable
11+
{
12+
public readonly string GitTestDir;
13+
public readonly GitVcs Vcs;
14+
public readonly string AbsolutePathToGitTestDir;
15+
16+
public GitVcsFixture()
17+
{
18+
GitTestDir = "./target-git-dir";
19+
AbsolutePathToGitTestDir = Path.Combine(
20+
Directory.GetCurrentDirectory(),
21+
GitTestDir
22+
);
23+
DirectoryDelete(GitTestDir, recursive: true);
24+
25+
ZipFile.ExtractToDirectory("./target-git.zip", "./");
26+
Vcs = new GitVcs();
27+
28+
var (_, stdOut, _) =
29+
GitVcs.LaunchGitWithArgsInner(
30+
"config user.email",
31+
1000,
32+
AbsolutePathToGitTestDir
33+
);
34+
if (string.IsNullOrWhiteSpace(stdOut))
35+
{
36+
GitVcs.LaunchGitWithArgs("config user.email [email protected]");
37+
GitVcs.LaunchGitWithArgs("config user.name Nicklas Laine Overgaard");
38+
}
39+
}
40+
41+
42+
private void DirectoryDelete(string dir, bool recursive)
43+
{
44+
try
45+
{
46+
Directory.Delete(dir, recursive);
47+
}
48+
// we don't want to fail at all if deleting the dir fails
49+
catch (Exception ex)
50+
{
51+
}
52+
}
53+
54+
public void Dispose()
55+
{
56+
DirectoryDelete(GitTestDir, recursive: true);
57+
}
58+
}
59+
}

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)