Fix host tests clean-up of test artifact parent directories (#82573)
authorElinor Fung <elfung@microsoft.com>
Fri, 24 Feb 2023 22:36:11 +0000 (14:36 -0800)
committerGitHub <noreply@github.com>
Fri, 24 Feb 2023 22:36:11 +0000 (14:36 -0800)
src/installer/tests/HostActivation.Tests/DependencyResolution/DependencyResolutionBase.cs
src/installer/tests/HostActivation.Tests/SDKLookup.cs
src/installer/tests/TestUtils/SharedFramework.cs
src/installer/tests/TestUtils/TestApp.cs
src/installer/tests/TestUtils/TestArtifact.cs

index 5823e3a..d9c5296 100644 (file)
@@ -23,7 +23,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.DependencyResolution
             }
 
             public SharedTestStateBase()
-                : base(GetBaseDir("dependencyResolution"), "dependencyResolution")
+                : base(GetBaseDir("dependencyResolution"))
             {
                 BuiltDotnetPath = Path.Combine(TestArtifactsPath, "sharedFrameworkPublish");
                 RepoDirectories = new RepoDirectoriesProvider(builtDotnet: BuiltDotnetPath);
index 7e4a5b5..e28cf2b 100644 (file)
@@ -22,8 +22,8 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation
         {
             SharedState = sharedState;
 
-            string exeDotNetPath = SharedFramework.CalculateUniqueTestDirectory(sharedState.BaseDir);
-            ExecutableDotNetBuilder = new DotNetBuilder(exeDotNetPath, sharedState.BuiltDotNet.BinPath, "exe");
+            string exeDotNetPath = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(sharedState.BaseDir, "exe"));
+            ExecutableDotNetBuilder = new DotNetBuilder(exeDotNetPath, sharedState.BuiltDotNet.BinPath, null);
             ExecutableDotNet = ExecutableDotNetBuilder
                 .AddMicrosoftNETCoreAppFrameworkMockHostPolicy("9999.0.0")
                 .Build();
index 927389f..53b8ee3 100644 (file)
@@ -15,9 +15,8 @@ namespace Microsoft.DotNet.CoreSetup.Test
     {
         private static readonly Mutex id_mutex = new Mutex();
 
-        // MultilevelDirectory is %TEST_ARTIFACTS%\dotnetMultilevelSharedFxLookup\id.
-        // We must locate the first non existing id.
-        public static string CalculateUniqueTestDirectory(string baseDir)
+        // Locate the first non-existent directory of the form <basePath>-<count>
+        public static string CalculateUniqueTestDirectory(string basePath)
         {
             id_mutex.WaitOne();
 
@@ -26,7 +25,7 @@ namespace Microsoft.DotNet.CoreSetup.Test
 
             do
             {
-                dir = Path.Combine(baseDir, count.ToString());
+                dir = $"{basePath}-{count}";
                 count++;
             } while (Directory.Exists(dir));
 
index 69c29d0..2ad61ac 100644 (file)
@@ -25,7 +25,7 @@ namespace Microsoft.DotNet.CoreSetup.Test
             LoadAssets();
         }
 
-        public TestApp(TestApp source)
+        private TestApp(TestApp source)
             : base(source)
         {
             AssemblyName = source.AssemblyName;
@@ -34,8 +34,11 @@ namespace Microsoft.DotNet.CoreSetup.Test
 
         public static TestApp CreateEmpty(string name)
         {
-            string location = GetNewTestArtifactPath(name);
-            return new TestApp(location);
+            var (location, parentPath) = GetNewTestArtifactPath(name);
+            return new TestApp(location)
+            {
+                DirectoryToDelete = parentPath
+            };
         }
 
         public TestApp Copy()
index e1ca674..c900bba 100644 (file)
@@ -31,18 +31,21 @@ namespace Microsoft.DotNet.CoreSetup.Test
         public string Location { get; }
         public string Name { get; }
 
+        protected string DirectoryToDelete { get; init; }
+
         private readonly List<TestArtifact> _copies = new List<TestArtifact>();
 
-        public TestArtifact(string location, string? name = null)
+        public TestArtifact(string location)
         {
             Location = location;
-            Name = name ?? Path.GetFileName(Location);
+            Name = Path.GetFileName(Location);
+            DirectoryToDelete = Location;
         }
 
         protected TestArtifact(TestArtifact source)
         {
             Name = source.Name;
-            Location = GetNewTestArtifactPath(Name);
+            (Location, DirectoryToDelete) = GetNewTestArtifactPath(source.Name);
 
             CopyRecursive(source.Location, Location, overwrite: true);
 
@@ -56,16 +59,15 @@ namespace Microsoft.DotNet.CoreSetup.Test
 
         public virtual void Dispose()
         {
-            if (!PreserveTestRuns() && Directory.Exists(Location))
+            if (!PreserveTestRuns() && Directory.Exists(DirectoryToDelete))
             {
                 try
                 {
-                    Directory.Delete(Location, true);
+                    Directory.Delete(DirectoryToDelete, true);
+                    Debug.Assert(!Directory.Exists(DirectoryToDelete));
 
                     // Delete lock file last
-                    Debug.Assert(!Directory.Exists(Location));
-                    var lockPath = Directory.GetParent(Location) + ".lock";
-                    File.Delete(lockPath);
+                    File.Delete($"{DirectoryToDelete}.lock");
                 } catch (Exception e)
                 {
                     Console.WriteLine("delete failed" + e);
@@ -80,7 +82,7 @@ namespace Microsoft.DotNet.CoreSetup.Test
             _copies.Clear();
         }
 
-        protected static string GetNewTestArtifactPath(string artifactName)
+        protected static (string, string) GetNewTestArtifactPath(string artifactName)
         {
             Exception? lastException = null;
             for (int i = 0; i < 10; i++)
@@ -100,7 +102,7 @@ namespace Microsoft.DotNet.CoreSetup.Test
                     continue;
                 }
                 Directory.CreateDirectory(artifactPath);
-                return artifactPath;
+                return (artifactPath, parentPath);
             }
             Debug.Assert(lastException != null);
             throw lastException;