File tree Expand file tree Collapse file tree
test/Nerdbank.GitVersioning.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -83,6 +83,13 @@ protected virtual void Dispose(bool disposing)
8383 {
8484 this . LibGit2Repository ? . Dispose ( ) ;
8585 this . Context ? . Dispose ( ) ;
86+
87+ // LibGit2Sharp may still hold native file handles even after Dispose().
88+ // Force a GC cycle to release any lingering native handles before
89+ // attempting to delete the temporary directories.
90+ GC . Collect ( ) ;
91+ GC . WaitForPendingFinalizers ( ) ;
92+
8693 foreach ( string dir in this . repoDirectories )
8794 {
8895 try
Original file line number Diff line number Diff line change @@ -29,14 +29,19 @@ internal static void DeleteDirectory(string path)
2929 {
3030 Directory . Delete ( path , true ) ;
3131 }
32- catch ( UnauthorizedAccessException )
32+ catch ( Exception ex ) when ( ex is UnauthorizedAccessException or IOException )
3333 {
3434 // Unknown why this fails so often.
3535 // Somehow making commits with libgit2sharp locks files
3636 // such that we can't delete them (but Windows Explorer can).
37+ // Redirect stderr so that rd errors don't bleed into the test host's
38+ // stderr, which can cause the test platform to misinterpret file-lock
39+ // messages as a crash (non-zero exit + stderr output).
3740 var psi = new ProcessStartInfo ( "cmd.exe" , $ "/c rd /s /q \" { path } \" ") ;
3841 psi . WorkingDirectory = Path . GetTempPath ( ) ;
3942 psi . WindowStyle = ProcessWindowStyle . Hidden ;
43+ psi . RedirectStandardError = true ;
44+ psi . UseShellExecute = false ;
4045 var process = Process . Start ( psi ) ;
4146 process . WaitForExit ( ) ;
4247 }
You can’t perform that action at this time.
0 commit comments