1+ using AutoFixture ;
2+ using AutoFixture . AutoNSubstitute ;
3+ using AutoFixture . Xunit2 ;
4+ using NSubstitute ;
5+ using NSubstitute . Extensions ;
6+ using Shouldly ;
7+ using System . Threading . Tasks ;
8+ using vs_commitizen . Infrastructure ;
9+ using vs_commitizen . Settings ;
10+ using vs_commitizen . Tests . TestAttributes ;
11+ using vs_commitizen . ViewModels ;
12+ using Xunit ;
13+
14+ namespace vs_commitizen . Tests
15+ {
16+ public class OpenGenerateLocalConfigViewModelTests
17+ {
18+ OpenGenerateLocalConfigViewModel getSut ( Fixture fixture , ConfigFileProvider configFileProvider , IFileAccessor fileAccessor , string configPath , bool fileExists )
19+ {
20+ configFileProvider . Configure ( ) . TryGetLocalConfigAsync ( ) . Returns ( configPath ) ;
21+ fileAccessor . Exists ( Arg . Any < string > ( ) ) . ReturnsForAnyArgs ( fileExists ) ;
22+
23+ IoC . Container . EjectAllInstancesOf < IConfigFileProvider > ( ) ;
24+ IoC . Container . Inject < IConfigFileProvider > ( configFileProvider ) ;
25+
26+ IoC . Container . EjectAllInstancesOf < IFileAccessor > ( ) ;
27+ IoC . Container . Inject < IFileAccessor > ( fileAccessor ) ;
28+
29+ return fixture . Create < OpenGenerateLocalConfigViewModel > ( ) ;
30+ }
31+
32+ [ Theory ]
33+ [ InlineTestConventions ( true , "path to config" ) ]
34+ [ InlineTestConventions ( false , null ) ]
35+ public async Task Command_Is_Enabled_Based_On_Solution_Loaded (
36+ bool solutionLoaded ,
37+ string configPath ,
38+ [ Frozen ] IFileAccessor fileAccessor ,
39+ [ Frozen ] [ Substitute ] ConfigFileProvider configFileProvider ,
40+ Fixture fixture
41+ )
42+ {
43+ var sut = getSut ( fixture , configFileProvider , fileAccessor , configPath , solutionLoaded ) ;
44+
45+ var expectedEnabledState = solutionLoaded ;
46+ ( await sut . IsCommandEnabledAsync ( ) ) . ShouldBe ( expectedEnabledState ) ;
47+ }
48+
49+ [ Theory ]
50+ [ InlineTestConventions ( true , "path to config" ) ]
51+ public async Task Execute_With_Existing_Config_Opens_The_File (
52+ bool solutionLoaded ,
53+ string configPath ,
54+ [ Frozen ] IFileAccessor fileAccessor ,
55+ [ Frozen ] [ Substitute ] IPopupManager popupManager ,
56+ [ Frozen ] [ Substitute ] ConfigFileProvider configFileProvider ,
57+ Fixture fixture
58+ )
59+ {
60+ // Arrange
61+ var sut = getSut ( fixture , configFileProvider , fileAccessor , configPath , solutionLoaded ) ;
62+ var called = false ;
63+
64+ // Act
65+ await sut . ExecuteAsync ( s =>
66+ {
67+ called = true ;
68+ return Task . CompletedTask ;
69+ } ) ;
70+
71+ // Assert
72+ called . ShouldBeTrue ( ) ;
73+ popupManager . DidNotReceiveWithAnyArgs ( ) . Confirm ( Arg . Any < string > ( ) , Arg . Any < string > ( ) ) ;
74+ }
75+
76+ [ Theory ]
77+ [ InlineTestConventions ( false , "path to config" ) ]
78+ public async Task Execute_With_NonExisting_Config_Asks_To_Create_It (
79+ bool solutionLoaded ,
80+ string configPath ,
81+ [ Frozen ] IFileAccessor fileAccessor ,
82+ [ Frozen ] [ Substitute ] IPopupManager popupManager ,
83+ [ Frozen ] [ Substitute ] ConfigFileProvider configFileProvider ,
84+ Fixture fixture
85+ )
86+ {
87+ // Arrange
88+ IoC . Container . Inject ( popupManager ) ;
89+ var sut = getSut ( fixture , configFileProvider , fileAccessor , configPath , solutionLoaded ) ;
90+ var called = false ;
91+
92+ // Act
93+ await sut . ExecuteAsync ( s =>
94+ {
95+ called = true ;
96+ return Task . CompletedTask ;
97+ } ) ;
98+
99+ // Assert
100+ called . ShouldBeFalse ( ) ;
101+ popupManager . Received ( ) . Confirm ( Arg . Any < string > ( ) , Arg . Any < string > ( ) ) ;
102+ }
103+
104+ [ Theory ]
105+ [ InlineTestConventions ( false , "path to config" , true ) ]
106+ [ InlineTestConventions ( false , "path to config" , false ) ]
107+ public async Task Response_To_Popup_Should_Open_File (
108+ bool solutionLoaded ,
109+ string configPath ,
110+ bool userWantsToCreateFile ,
111+ [ Frozen ] IFileAccessor fileAccessor ,
112+ [ Frozen ] [ Substitute ] IPopupManager popupManager ,
113+ [ Frozen ] [ Substitute ] ConfigFileProvider configFileProvider ,
114+ Fixture fixture
115+ )
116+ {
117+ // Arrange
118+ popupManager . Configure ( ) . Confirm ( Arg . Any < string > ( ) , Arg . Any < string > ( ) ) . Returns ( userWantsToCreateFile ) ;
119+ IoC . Container . Inject ( popupManager ) ;
120+ var sut = getSut ( fixture , configFileProvider , fileAccessor , configPath , solutionLoaded ) ;
121+ var called = false ;
122+
123+ // Act
124+ await sut . ExecuteAsync ( s =>
125+ {
126+ called = true ;
127+ return Task . CompletedTask ;
128+ } ) ;
129+
130+ // Assert
131+ var expectedResult = userWantsToCreateFile ;
132+ called . ShouldBe ( expectedResult ) ;
133+ popupManager . Received ( ) . Confirm ( Arg . Any < string > ( ) , Arg . Any < string > ( ) ) ;
134+ }
135+ }
136+ }
0 commit comments