@@ -201,6 +201,92 @@ public void PreprocessCommand_WithExistingDirectory_DoesNotFail()
201201 }
202202 }
203203
204+ [ Theory ]
205+ [ InlineData ( "!homedir C:\\ my\\ home\\ dir" , "!homedir C:\\ my\\ home\\ dir" ) ]
206+ [ InlineData ( "!homedir \" C:\\ my\\ home\\ dir\" " , "!homedir \" C:\\ my\\ home\\ dir\" " ) ]
207+ [ InlineData ( "!homedir /mnt/c/my/home/dir" , "!homedir C:\\ my\\ home\\ dir" ) ]
208+ [ InlineData ( "!HOMEDIR C:\\ test" , "!HOMEDIR C:\\ test" ) ]
209+ public void PreprocessCommand_Homedir_ConvertsWslPaths ( string input , string expected )
210+ {
211+ // Arrange
212+ string result = string . Empty ;
213+
214+ try
215+ {
216+ // Act
217+ result = m_CommandPreprocessor . PreprocessCommand ( input ) ;
218+
219+ // Assert
220+ Assert . Equal ( expected , result ) ;
221+ }
222+ finally
223+ {
224+ // Cleanup - extract path from result for cleanup
225+ CleanupHomedirPath ( result ) ;
226+ }
227+ }
228+
229+ [ Fact ]
230+ public void PreprocessCommand_Homedir_CreatesDirectoryIfNotExists ( )
231+ {
232+ // Arrange
233+ var tempDir = Path . Combine ( Path . GetTempPath ( ) , "test_homedir_" + Guid . NewGuid ( ) . ToString ( "N" ) [ ..8 ] ) ;
234+ var input = $ "!homedir { tempDir } ";
235+
236+ try
237+ {
238+ // Ensure directory doesn't exist initially
239+ if ( Directory . Exists ( tempDir ) )
240+ {
241+ Directory . Delete ( tempDir , true ) ;
242+ }
243+
244+ // Act
245+ var result = m_CommandPreprocessor . PreprocessCommand ( input ) ;
246+
247+ // Assert
248+ Assert . Equal ( $ "!homedir { tempDir } ", result ) ;
249+ Assert . True ( Directory . Exists ( tempDir ) , "Directory should be created automatically" ) ;
250+ }
251+ finally
252+ {
253+ // Cleanup
254+ if ( Directory . Exists ( tempDir ) )
255+ {
256+ Directory . Delete ( tempDir , true ) ;
257+ }
258+ }
259+ }
260+
261+ [ Fact ]
262+ public void PreprocessCommand_Homedir_WithExistingDirectory_DoesNotFail ( )
263+ {
264+ // Arrange
265+ var tempDir = Path . Combine ( Path . GetTempPath ( ) , "test_homedir_existing_" + Guid . NewGuid ( ) . ToString ( "N" ) [ ..8 ] ) ;
266+ var input = $ "!homedir \" { tempDir } \" ";
267+
268+ try
269+ {
270+ // Create directory first
271+ Directory . CreateDirectory ( tempDir ) ;
272+
273+ // Act
274+ var result = m_CommandPreprocessor . PreprocessCommand ( input ) ;
275+
276+ // Assert
277+ Assert . Equal ( $ "!homedir \" { tempDir } \" ", result ) ;
278+ Assert . True ( Directory . Exists ( tempDir ) , "Directory should still exist" ) ;
279+ }
280+ finally
281+ {
282+ // Cleanup
283+ if ( Directory . Exists ( tempDir ) )
284+ {
285+ Directory . Delete ( tempDir , true ) ;
286+ }
287+ }
288+ }
289+
204290 /// <summary>
205291 /// Best-effort cleanup for any directories that may be created by CommandPreprocessor during tests.
206292 /// Parses .srcpath arguments and removes non-srv, non-UNC directories.
@@ -234,5 +320,35 @@ internal static void CleanupCreatedDirectoriesFromSrcpath(string command)
234320 }
235321 }
236322 }
323+
324+ /// <summary>
325+ /// Best-effort cleanup for any directories that may be created by CommandPreprocessor during !homedir tests.
326+ /// Parses !homedir argument and removes the directory.
327+ /// </summary>
328+ internal static void CleanupHomedirPath ( string command )
329+ {
330+ if ( string . IsNullOrWhiteSpace ( command ) ) return ;
331+ if ( ! command . StartsWith ( "!homedir" , StringComparison . OrdinalIgnoreCase ) ) return ;
332+
333+ // Extract argument after !homedir
334+ var firstSpace = command . IndexOf ( ' ' ) ;
335+ if ( firstSpace < 0 || firstSpace + 1 >= command . Length ) return ;
336+ var path = command [ ( firstSpace + 1 ) ..] . Trim ( ) . Trim ( '"' ) . Trim ( '\' ' ) ;
337+
338+ // Skip UNC paths
339+ if ( path . StartsWith ( "\\ \\ " ) ) return ;
340+
341+ try
342+ {
343+ if ( Directory . Exists ( path ) )
344+ {
345+ Directory . Delete ( path , true ) ;
346+ }
347+ }
348+ catch
349+ {
350+ // Ignore cleanup errors in tests
351+ }
352+ }
237353 }
238354}
0 commit comments