Fix ProfileOptimization test
authorDan Moseley <danmose@microsoft.com>
Thu, 5 May 2022 23:01:10 +0000 (17:01 -0600)
committerDan Moseley <danmose@microsoft.com>
Thu, 5 May 2022 23:52:00 +0000 (17:52 -0600)
src/libraries/System.Runtime.Extensions/tests/System/Runtime/ProfileOptimization.cs

index 530661c..64eb27b 100644 (file)
@@ -16,23 +16,37 @@ namespace System.Runtime.Tests
         [ActiveIssue("https://github.com/dotnet/runtime/issues/31853", TestRuntimes.Mono)]
         public void ProfileOptimization_CheckFileExists(bool stopProfile)
         {
-            string profileFile = GetTestFileName();
+            // Don't put this file into the normal per-test directory, because
+            // when RemoteExecutor.Invoke returns, it disposes this class and deletes
+            // that directory. Instead just put it in temp and delete it manually after.
+            string profileFile = Path.Combine(Path.GetTempPath(), GetTestFileName());
 
-            RemoteExecutor.Invoke((_profileFile, _stopProfile) =>
+            try
             {
-                // Perform the test work
-                ProfileOptimization.SetProfileRoot(Path.GetDirectoryName(_profileFile));
-                ProfileOptimization.StartProfile(Path.GetFileName(_profileFile));
-
-                if (bool.Parse(_stopProfile))
+                RemoteExecutor.Invoke((_profileFile, _stopProfile) =>
                 {
-                    ProfileOptimization.StartProfile(null);
-                    CheckProfileFileExists(_profileFile);
-                }
+                    // Perform the test work
+                    ProfileOptimization.SetProfileRoot(Path.GetDirectoryName(_profileFile));
+                    ProfileOptimization.StartProfile(Path.GetFileName(_profileFile));
 
-            }, profileFile, stopProfile.ToString()).Dispose();
+                    if (bool.Parse(_stopProfile))
+                    {
+                        ProfileOptimization.StartProfile(null);
+                        CheckProfileFileExists(_profileFile);
+                    }
 
-            CheckProfileFileExists(profileFile);
+                }, profileFile, stopProfile.ToString()).Dispose();
+
+                CheckProfileFileExists(profileFile);
+            }
+            finally
+            {
+                try
+                {
+                    File.Delete(profileFile);
+                }
+                catch {}
+            }
         }
 
         static void CheckProfileFileExists(string profileFile)