-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAddCommandTests.cs
More file actions
112 lines (92 loc) · 3.7 KB
/
Copy pathAddCommandTests.cs
File metadata and controls
112 lines (92 loc) · 3.7 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
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using SolarWinds.Changesets.Commands.Add;
using SolarWinds.Changesets.Commands.Init;
using SolarWinds.Changesets.Infrastructure;
using SolarWinds.Changesets.Shared;
using Spectre.Console.Cli;
using Spectre.Console.Cli.Testing;
using Spectre.Console.Testing;
namespace SolarWinds.Changesets.Tests.Add;
[TestFixture]
internal sealed class AddCommandTests
{
private readonly Mock<IProjectFileNamesLocator> _projectFileNameLocator = new();
private TypeRegistrar _typeRegistrar = null!;
private CommandAppTester _app = null!;
private ChangesetsRepository _changesetsRepository = null!;
[SetUp]
public void SetUp()
{
CleanUp();
ServiceCollection serviceCollection = new();
serviceCollection.AddSingleton<IConfigurationService, ConfigurationService>();
serviceCollection.AddSingleton(_projectFileNameLocator.Object);
_changesetsRepository = new ChangesetsRepository();
serviceCollection.AddSingleton<IChangesetsRepository>(_changesetsRepository);
_typeRegistrar = new(serviceCollection);
_app = new(_typeRegistrar);
_app.Configure(config =>
{
config
.SetApplicationName("changeset")
.SetExceptionHandler(ExceptionHandler.Handle);
});
}
[TearDown]
public void TearDown()
{
CleanUp();
}
[Test]
public async Task AddCommand_HappyPath_ChangesetIsCreated()
{
// Arrange
_projectFileNameLocator.Setup(x => x.GetProjectFileNames(It.IsAny<string>())).Returns(["A", "B"]);
CommandAppTester initApp = new(_typeRegistrar);
initApp.SetDefaultCommand<InitChangesetCommand>();
await initApp.RunAsync();
_app.Console.Interactive();
// MultiSelectionPrompt - Select 2nd project 'B'
_app.Console.Input.PushKey(ConsoleKey.DownArrow);
_app.Console.Input.PushKey(ConsoleKey.Spacebar);
_app.Console.Input.PushKey(ConsoleKey.Enter);
// SelectionPrompt - Select BumpType 'Minor'
_app.Console.Input.PushKey(ConsoleKey.DownArrow);
_app.Console.Input.PushKey(ConsoleKey.Enter);
// TextPrompt - Describe changes
string expectedDescription = "Test example description";
_app.Console.Input.PushTextWithEnter(expectedDescription);
// Act
_app.SetDefaultCommand<AddChangesetCommand>();
CommandAppResult result = await _app.RunAsync();
// Assert
result.ExitCode.Should().Be(ResultCodes.Success, result.Output);
result.Output.Should().MatchRegex(@"Changeset file '[a-z]+\.md' for your branch has been created\.\s*$");
ChangesetFile[] changesetFiles = await _changesetsRepository.GetChangesetsAsync(Constants.ChangesetDirectoryFullPath);
changesetFiles.Should().HaveCount(1);
ChangesetFile changesetFile = changesetFiles[0];
changesetFile.ChangedModuleNames.Should().HaveCount(1);
changesetFile.ChangedModuleNames.Should().Contain("B");
changesetFile.BumpType.Should().Be(BumpType.Minor);
changesetFile.Description.Should().Be(expectedDescription);
}
[Test]
public void AddCommand_NotInitialized_ReturnsErrorCodeWithMessage()
{
// Arrange
_app.SetDefaultCommand<AddChangesetCommand>();
// Act
CommandAppResult result = _app.Run();
// Assert
result.ExitCode.Should().Be(ResultCodes.NotInitialized, because: result.Output);
}
private static void CleanUp()
{
if (Directory.Exists(Constants.ChangesetDirectoryName))
{
Directory.Delete(Constants.ChangesetDirectoryName, true);
}
}
}