-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(config): error if user doesn't want to init config * ensure same config after init * test: ensure ioc is reset before all tests
- Loading branch information
Showing
7 changed files
with
208 additions
and
33 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
vs-commitizen.Tests/OpenGenerateLocalConfigViewModelTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using AutoFixture; | ||
using AutoFixture.AutoNSubstitute; | ||
using AutoFixture.Xunit2; | ||
using NSubstitute; | ||
using NSubstitute.Extensions; | ||
using Shouldly; | ||
using System.Threading.Tasks; | ||
using vs_commitizen.Infrastructure; | ||
using vs_commitizen.Settings; | ||
using vs_commitizen.Tests.TestAttributes; | ||
using vs_commitizen.ViewModels; | ||
using Xunit; | ||
|
||
namespace vs_commitizen.Tests | ||
{ | ||
public class OpenGenerateLocalConfigViewModelTests | ||
{ | ||
OpenGenerateLocalConfigViewModel getSut(Fixture fixture, ConfigFileProvider configFileProvider, IFileAccessor fileAccessor, string configPath, bool fileExists) | ||
{ | ||
configFileProvider.Configure().TryGetLocalConfigAsync().Returns(configPath); | ||
fileAccessor.Exists(Arg.Any<string>()).ReturnsForAnyArgs(fileExists); | ||
|
||
IoC.Container.EjectAllInstancesOf<IConfigFileProvider>(); | ||
IoC.Container.Inject<IConfigFileProvider>(configFileProvider); | ||
|
||
IoC.Container.EjectAllInstancesOf<IFileAccessor>(); | ||
IoC.Container.Inject<IFileAccessor>(fileAccessor); | ||
|
||
return fixture.Create<OpenGenerateLocalConfigViewModel>(); | ||
} | ||
|
||
[Theory] | ||
[InlineTestConventions(true, "path to config")] | ||
[InlineTestConventions(false, null)] | ||
public async Task Command_Is_Enabled_Based_On_Solution_Loaded( | ||
bool solutionLoaded, | ||
string configPath, | ||
[Frozen]IFileAccessor fileAccessor, | ||
[Frozen][Substitute] ConfigFileProvider configFileProvider, | ||
Fixture fixture | ||
) | ||
{ | ||
var sut = getSut(fixture, configFileProvider, fileAccessor, configPath, solutionLoaded); | ||
|
||
var expectedEnabledState = solutionLoaded; | ||
(await sut.IsCommandEnabledAsync()).ShouldBe(expectedEnabledState); | ||
} | ||
|
||
[Theory] | ||
[InlineTestConventions(true, "path to config")] | ||
public async Task Execute_With_Existing_Config_Opens_The_File( | ||
bool solutionLoaded, | ||
string configPath, | ||
[Frozen]IFileAccessor fileAccessor, | ||
[Frozen][Substitute] IPopupManager popupManager, | ||
[Frozen][Substitute] ConfigFileProvider configFileProvider, | ||
Fixture fixture | ||
) | ||
{ | ||
// Arrange | ||
var sut = getSut(fixture, configFileProvider, fileAccessor, configPath, solutionLoaded); | ||
var called = false; | ||
|
||
// Act | ||
await sut.ExecuteAsync(s => | ||
{ | ||
called = true; | ||
return Task.CompletedTask; | ||
}); | ||
|
||
// Assert | ||
called.ShouldBeTrue(); | ||
popupManager.DidNotReceiveWithAnyArgs().Confirm(Arg.Any<string>(), Arg.Any<string>()); | ||
} | ||
|
||
[Theory] | ||
[InlineTestConventions(false, "path to config")] | ||
public async Task Execute_With_NonExisting_Config_Asks_To_Create_It( | ||
bool solutionLoaded, | ||
string configPath, | ||
[Frozen]IFileAccessor fileAccessor, | ||
[Frozen][Substitute] IPopupManager popupManager, | ||
[Frozen][Substitute] ConfigFileProvider configFileProvider, | ||
Fixture fixture | ||
) | ||
{ | ||
// Arrange | ||
IoC.Container.Inject(popupManager); | ||
var sut = getSut(fixture, configFileProvider, fileAccessor, configPath, solutionLoaded); | ||
var called = false; | ||
|
||
// Act | ||
await sut.ExecuteAsync(s => | ||
{ | ||
called = true; | ||
return Task.CompletedTask; | ||
}); | ||
|
||
// Assert | ||
called.ShouldBeFalse(); | ||
popupManager.Received().Confirm(Arg.Any<string>(), Arg.Any<string>()); | ||
} | ||
|
||
[Theory] | ||
[InlineTestConventions(false, "path to config", true)] | ||
[InlineTestConventions(false, "path to config", false)] | ||
public async Task Response_To_Popup_Should_Open_File( | ||
bool solutionLoaded, | ||
string configPath, | ||
bool userWantsToCreateFile, | ||
[Frozen]IFileAccessor fileAccessor, | ||
[Frozen][Substitute] IPopupManager popupManager, | ||
[Frozen][Substitute] ConfigFileProvider configFileProvider, | ||
Fixture fixture | ||
) | ||
{ | ||
// Arrange | ||
popupManager.Configure().Confirm(Arg.Any<string>(), Arg.Any<string>()).Returns(userWantsToCreateFile); | ||
IoC.Container.Inject(popupManager); | ||
var sut = getSut(fixture, configFileProvider, fileAccessor, configPath, solutionLoaded); | ||
var called = false; | ||
|
||
// Act | ||
await sut.ExecuteAsync(s => | ||
{ | ||
called = true; | ||
return Task.CompletedTask; | ||
}); | ||
|
||
// Assert | ||
var expectedResult = userWantsToCreateFile; | ||
called.ShouldBe(expectedResult); | ||
popupManager.Received().Confirm(Arg.Any<string>(), Arg.Any<string>()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
vs-commitizen/ViewModels/OpenGenerateLocalConfigViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using vs_commitizen.Infrastructure; | ||
using vs_commitizen.Settings; | ||
|
||
namespace vs_commitizen.ViewModels | ||
{ | ||
public class OpenGenerateLocalConfigViewModel | ||
{ | ||
private readonly IConfigFileProvider configFileProvider; | ||
private readonly IFileAccessor fileAccessor; | ||
private readonly IPopupManager popupManager; | ||
|
||
public OpenGenerateLocalConfigViewModel() | ||
{ | ||
configFileProvider = IoC.GetInstance<IConfigFileProvider>(); | ||
fileAccessor = IoC.GetInstance<IFileAccessor>(); | ||
popupManager = IoC.GetInstance<IPopupManager>(); | ||
|
||
} | ||
|
||
public async Task<bool> IsCommandEnabledAsync() | ||
{ | ||
var localConfigPath = await this.configFileProvider.TryGetLocalConfigAsync(); | ||
|
||
return !string.IsNullOrWhiteSpace(localConfigPath); | ||
} | ||
|
||
public async Task ExecuteAsync(Func<string, Task> openFunc) | ||
{ | ||
try | ||
{ | ||
var localConfigPath = await this.configFileProvider.TryGetLocalConfigAsync(); | ||
if (string.IsNullOrWhiteSpace(localConfigPath)) return; | ||
|
||
if (fileAccessor.Exists(localConfigPath)) | ||
{ | ||
await openFunc(localConfigPath); | ||
} | ||
else | ||
{ | ||
if (popupManager.Confirm("There is no local configuration file yet, do you want to create it for this repository ?", "Init local config file")) | ||
{ | ||
fileAccessor.CopyFile(ConfigFileProvider.ConfigPathUserProfile, localConfigPath); | ||
await openFunc(localConfigPath); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
popupManager.Show(ex.ToString(), "An error occured"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters