-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathGitTests.cs
More file actions
148 lines (127 loc) · 5.03 KB
/
Copy pathGitTests.cs
File metadata and controls
148 lines (127 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using CliFx.Exceptions;
using CliFx.Infrastructure;
using FluentAssertions;
using Husky.Services;
using Husky.Services.Contracts;
using Husky.Stdout;
using NSubstitute;
using Xunit;
namespace HuskyTest.Services
{
public class GitTests
{
private readonly FakeInMemoryConsole _console;
private readonly ICliWrap _cliWrap;
public GitTests()
{
_console = new FakeInMemoryConsole();
LoggerEx.logger = new Logger(_console);
// sub
_cliWrap = Substitute.For<ICliWrap>();
}
[Fact]
public async Task GetStagedFiles_WhenGitReturnExitCodeDifferentThanZero_ThrowCommandException()
{
// Arrange
var git = new Git(_cliWrap);
var now = DateTime.UtcNow;
_cliWrap.ExecBufferedAsync(Arg.Any<string>(), Arg.Any<string>()).Returns(Task.FromResult(new CliWrap.Buffered.BufferedCommandResult(1, now, now, string.Empty, string.Empty)));
// Act
Func<Task> act = async () => await git.GetStagedFilesAsync();
// Assert
await act.Should()
.ThrowAsync<CommandException>()
.WithMessage("Could not find the staged files");
}
[Fact]
public async Task GetHuskyPath_WhenGitCommandFails_ThrowsCommandExceptionWithInstallHint()
{
// Arrange
var git = new Git(_cliWrap);
var now = DateTime.UtcNow;
_cliWrap.ExecBufferedAsync("git", "config --get core.hooksPath")
.Returns(Task.FromResult(new CliWrap.Buffered.BufferedCommandResult(1, now, now, string.Empty, string.Empty)));
// Act
Func<Task> act = async () => await git.GetHuskyPathAsync();
// Assert
await act.Should()
.ThrowAsync<CommandException>()
.WithMessage("*dotnet husky install*");
}
[Fact]
public async Task GetStagedFiles_Return_StagedFiles()
{
// Arrange
var git = new Git(_cliWrap);
var now = DateTime.UtcNow;
var gitOutput = $"myfile.cs{Environment.NewLine}mysecondfile.cs{Environment.NewLine}src\\mythirdfile.cs";
_cliWrap.ExecBufferedAsync("git", "diff --staged --name-only --no-ext-diff --diff-filter=AM").Returns(Task.FromResult(new CliWrap.Buffered.BufferedCommandResult(0, now, now, gitOutput, string.Empty)));
// Act
var stagedFiles = await git.GetStagedFilesAsync();
// Assert
stagedFiles.Should()
.NotBeEmpty()
.And
.HaveCount(3)
.And
.Contain(new List<string>
{
"myfile.cs",
"mysecondfile.cs",
"src\\mythirdfile.cs"
});
}
[Fact]
public async Task GetStagedFiles_WithSpacesInPath_Return_StagedFiles()
{
// Arrange
var git = new Git(_cliWrap);
var now = DateTime.UtcNow;
var gitOutput = $"src/Private Assemblies/MyController.cs{Environment.NewLine}My Project/Test.cs{Environment.NewLine}file with spaces.cs";
_cliWrap.ExecBufferedAsync("git", "diff --staged --name-only --no-ext-diff --diff-filter=AM").Returns(Task.FromResult(new CliWrap.Buffered.BufferedCommandResult(0, now, now, gitOutput, string.Empty)));
// Act
var stagedFiles = await git.GetStagedFilesAsync();
// Assert
stagedFiles.Should()
.NotBeEmpty()
.And
.HaveCount(3)
.And
.Contain(new List<string>
{
"src/Private Assemblies/MyController.cs",
"My Project/Test.cs",
"file with spaces.cs"
});
}
[Fact]
public async Task ExecAsync_WithArrayArgs_CallsCliWrapWithArrayArgs()
{
// Arrange
var git = new Git(_cliWrap);
var now = DateTime.UtcNow;
var args = new[] { "add", "src/Private Assemblies/Test.cs", "My Project/File.cs" };
_cliWrap.ExecDirectAsync("git", Arg.Is<IEnumerable<string>>(a => a.SequenceEqual(args)))
.Returns(Task.FromResult(new CliWrap.CommandResult(0, now, now)));
// Act
await git.ExecAsync(args);
// Assert
await _cliWrap.Received(1).ExecDirectAsync("git", Arg.Is<IEnumerable<string>>(a => a.SequenceEqual(args)));
}
[Fact]
public async Task ExecBufferedAsync_WithArrayArgs_CallsCliWrapWithArrayArgs()
{
// Arrange
var git = new Git(_cliWrap);
var now = DateTime.UtcNow;
var args = new[] { "hash-object", "-w", "src/Private Assemblies/temp.cs" };
_cliWrap.ExecBufferedAsync("git", Arg.Is<IEnumerable<string>>(a => a.SequenceEqual(args)))
.Returns(Task.FromResult(new CliWrap.Buffered.BufferedCommandResult(0, now, now, "abc123", string.Empty)));
// Act
var result = await git.ExecBufferedAsync(args);
// Assert
await _cliWrap.Received(1).ExecBufferedAsync("git", Arg.Is<IEnumerable<string>>(a => a.SequenceEqual(args)));
result.StandardOutput.Should().Be("abc123");
}
}
}