@@ -50,8 +50,72 @@ public void Cleanup()
5050 }
5151
5252 [ TestMethod ]
53- public void Run_WhenVersionIsNull_ShouldReturnError ( )
53+ public void Run_WhenVersionIsNull_AndNoNodeSwapFile_ShouldReturnError ( )
5454 {
55+ _mockFileSystem . FileExists ( Arg . Any < string > ( ) ) . Returns ( false ) ;
56+
57+ var useCommand = new UseCommand ( _globalContext , _mockNodeJs , _mockProcessElevation , _mockConsoleWriter , _mockFileSystem )
58+ {
59+ Version = null ,
60+ } ;
61+
62+ var result = useCommand . Run ( ) ;
63+
64+ result . ShouldBe ( 1 ) ;
65+ _mockConsoleWriter . Received ( 1 ) . WriteErrorLine ( "Missing version argument. Either provide a version or create a .nodeswap file." ) ;
66+ }
67+
68+ [ TestMethod ]
69+ public void Run_WhenVersionIsNull_AndNodeSwapFileExists_ShouldUseVersionFromFile ( )
70+ {
71+ var version = new Version ( 18 , 17 , 0 ) ;
72+ var installedVersions = new List < NodeJsVersion >
73+ {
74+ new ( ) { Version = version , Path = $ "/fake/path/node-v{ version } ", IsActive = false } ,
75+ } ;
76+
77+ _mockFileSystem . FileExists ( Arg . Is < string > ( path => path . EndsWith ( ".nodeswap" ) ) ) . Returns ( true ) ;
78+ _mockFileSystem . ReadAllText ( Arg . Is < string > ( path => path . EndsWith ( ".nodeswap" ) ) ) . Returns ( "18.17.0" ) ;
79+ _mockNodeJs . GetInstalledVersions ( ) . Returns ( installedVersions ) ;
80+ _mockProcessElevation . IsAdministrator ( ) . Returns ( true ) ;
81+ _mockFileSystem . CreateSymbolicLink ( Arg . Any < string > ( ) , Arg . Any < string > ( ) , true ) . Returns ( true ) ;
82+
83+ var useCommand = new UseCommand ( _globalContext , _mockNodeJs , _mockProcessElevation , _mockConsoleWriter , _mockFileSystem )
84+ {
85+ Version = null ,
86+ } ;
87+
88+ var result = useCommand . Run ( ) ;
89+
90+ result . ShouldBe ( 0 ) ;
91+ _mockConsoleWriter . Received ( 1 ) . WriteLine ( "Using Node.js version from .nodeswap: 18.17.0" ) ;
92+ _mockConsoleWriter . Received ( 1 ) . WriteLine ( "Done" ) ;
93+ }
94+
95+ [ TestMethod ]
96+ public void Run_WhenVersionIsNull_AndNodeSwapFileIsEmpty_ShouldReturnError ( )
97+ {
98+ _mockFileSystem . FileExists ( Arg . Is < string > ( path => path . EndsWith ( ".nodeswap" ) ) ) . Returns ( true ) ;
99+ _mockFileSystem . ReadAllText ( Arg . Is < string > ( path => path . EndsWith ( ".nodeswap" ) ) ) . Returns ( " " ) ;
100+
101+ var useCommand = new UseCommand ( _globalContext , _mockNodeJs , _mockProcessElevation , _mockConsoleWriter , _mockFileSystem )
102+ {
103+ Version = null ,
104+ } ;
105+
106+ var result = useCommand . Run ( ) ;
107+
108+ result . ShouldBe ( 1 ) ;
109+ _mockConsoleWriter . Received ( 1 ) . WriteErrorLine ( "The .nodeswap file is empty" ) ;
110+ }
111+
112+ [ TestMethod ]
113+ public void Run_WhenVersionIsNull_AndNodeSwapFileReadFails_ShouldReturnError ( )
114+ {
115+ _mockFileSystem . FileExists ( Arg . Is < string > ( path => path . EndsWith ( ".nodeswap" ) ) ) . Returns ( true ) ;
116+ _mockFileSystem . When ( x => x . ReadAllText ( Arg . Is < string > ( path => path . EndsWith ( ".nodeswap" ) ) ) )
117+ . Do ( x => throw new IOException ( "File access denied" ) ) ;
118+
55119 var useCommand = new UseCommand ( _globalContext , _mockNodeJs , _mockProcessElevation , _mockConsoleWriter , _mockFileSystem )
56120 {
57121 Version = null ,
@@ -60,7 +124,7 @@ public void Run_WhenVersionIsNull_ShouldReturnError()
60124 var result = useCommand . Run ( ) ;
61125
62126 result . ShouldBe ( 1 ) ;
63- _mockConsoleWriter . Received ( 1 ) . WriteErrorLine ( "Missing version argument " ) ;
127+ _mockConsoleWriter . Received ( 1 ) . WriteErrorLine ( "Error reading .nodeswap: File access denied " ) ;
64128 }
65129
66130 [ TestMethod ]
@@ -141,6 +205,7 @@ public void Run_WhenAdministratorAndVersionInstalled_ShouldSwitchSuccessfully()
141205 new ( ) { Version = version , Path = $ "/fake/path/node-v{ version } ", IsActive = false } ,
142206 } ;
143207 _mockNodeJs . GetInstalledVersions ( ) . Returns ( installedVersions ) ;
208+ _mockNodeJs . GetActiveVersion ( ) . Returns ( ( Version ) null ) ; // No active version
144209 _mockProcessElevation . IsAdministrator ( ) . Returns ( true ) ;
145210 _mockFileSystem . DirectoryExists ( _globalContext . SymlinkPath ) . Returns ( false ) ;
146211 _mockFileSystem . CreateSymbolicLink ( _globalContext . SymlinkPath , $ "/fake/path/node-v{ version } ", true ) . Returns ( true ) ;
@@ -167,6 +232,7 @@ public void Run_WhenSymlinkCreationFails_ShouldReturnError()
167232 new ( ) { Version = version , Path = $ "/fake/path/node-v{ version } ", IsActive = false } ,
168233 } ;
169234 _mockNodeJs . GetInstalledVersions ( ) . Returns ( installedVersions ) ;
235+ _mockNodeJs . GetActiveVersion ( ) . Returns ( ( Version ) null ) ; // No active version
170236 _mockProcessElevation . IsAdministrator ( ) . Returns ( true ) ;
171237 _mockFileSystem . DirectoryExists ( _globalContext . SymlinkPath ) . Returns ( false ) ;
172238 _mockFileSystem . CreateSymbolicLink ( _globalContext . SymlinkPath , $ "/fake/path/node-v{ version } ", true ) . Returns ( false ) ;
@@ -214,6 +280,7 @@ public void Run_WhenExistingSymlinkExists_ShouldDeleteFirst()
214280 new ( ) { Version = version , Path = $ "/fake/path/node-v{ version } ", IsActive = false } ,
215281 } ;
216282 _mockNodeJs . GetInstalledVersions ( ) . Returns ( installedVersions ) ;
283+ _mockNodeJs . GetActiveVersion ( ) . Returns ( ( Version ) null ) ; // No active version
217284 _mockProcessElevation . IsAdministrator ( ) . Returns ( true ) ;
218285 _mockFileSystem . DirectoryExists ( _globalContext . SymlinkPath ) . Returns ( true ) ;
219286 _mockFileSystem . CreateSymbolicLink ( _globalContext . SymlinkPath , $ "/fake/path/node-v{ version } ", true ) . Returns ( true ) ;
@@ -229,4 +296,33 @@ public void Run_WhenExistingSymlinkExists_ShouldDeleteFirst()
229296 _mockFileSystem . Received ( 1 ) . DeleteDirectory ( _globalContext . SymlinkPath , true ) ;
230297 _mockFileSystem . Received ( 1 ) . CreateSymbolicLink ( _globalContext . SymlinkPath , $ "/fake/path/node-v{ version } ", true ) ;
231298 }
299+
300+ [ TestMethod ]
301+ public void Run_WhenAlreadyUsingRequestedVersion_ShouldReturnEarlyWithoutChanges ( )
302+ {
303+ var version = new Version ( 18 , 17 , 0 ) ;
304+ var installedVersions = new List < NodeJsVersion >
305+ {
306+ new ( ) { Version = version , Path = $ "/fake/path/node-v{ version } ", IsActive = false } ,
307+ } ;
308+ _mockNodeJs . GetInstalledVersions ( ) . Returns ( installedVersions ) ;
309+ _mockNodeJs . GetActiveVersion ( ) . Returns ( version ) ; // Already using this version
310+ _mockProcessElevation . IsAdministrator ( ) . Returns ( true ) ;
311+
312+ var useCommand = new UseCommand ( _globalContext , _mockNodeJs , _mockProcessElevation , _mockConsoleWriter , _mockFileSystem )
313+ {
314+ Version = version . ToString ( ) ,
315+ } ;
316+
317+ var result = useCommand . Run ( ) ;
318+
319+ result . ShouldBe ( 0 ) ;
320+ _mockConsoleWriter . Received ( 1 ) . WriteLine ( $ "Already using Node.js version { version } ") ;
321+
322+ // Should not perform any file operations
323+ _mockFileSystem . DidNotReceive ( ) . WriteAllText ( _globalContext . PreviousVersionTrackerFilePath , Arg . Any < string > ( ) ) ;
324+ _mockFileSystem . DidNotReceive ( ) . DeleteDirectory ( Arg . Any < string > ( ) , Arg . Any < bool > ( ) ) ;
325+ _mockFileSystem . DidNotReceive ( ) . CreateSymbolicLink ( Arg . Any < string > ( ) , Arg . Any < string > ( ) , Arg . Any < bool > ( ) ) ;
326+ _mockFileSystem . DidNotReceive ( ) . WriteAllText ( _globalContext . ActiveVersionTrackerFilePath , Arg . Any < string > ( ) ) ;
327+ }
232328}
0 commit comments