Switch tests for --depsfile to mocked components (#83435)
authorElinor Fung <elfung@microsoft.com>
Wed, 15 Mar 2023 18:21:09 +0000 (11:21 -0700)
committerGitHub <noreply@github.com>
Wed, 15 Mar 2023 18:21:09 +0000 (11:21 -0700)
src/installer/tests/HostActivation.Tests/DependencyResolution/DependencyResolutionBase.cs
src/installer/tests/HostActivation.Tests/DependencyResolution/DepsFile.cs [new file with mode: 0644]
src/installer/tests/HostActivation.Tests/DependencyResolution/PerAssemblyVersionResolutionMultipleFrameworks.cs
src/installer/tests/HostActivation.Tests/DependencyResolution/RidAssetResolution.cs
src/installer/tests/HostActivation.Tests/PortableAppActivation.cs
src/installer/tests/TestUtils/Constants.cs

index 14d1e1e..87b750e 100644 (file)
@@ -12,10 +12,6 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.DependencyResolution
 
         public abstract class SharedTestStateBase : TestArtifact
         {
-            protected string BuiltDotnetPath { get; }
-
-            public RepoDirectoriesProvider RepoDirectories { get; }
-
             private static string GetBaseDir(string name)
             {
                 string baseDir = Path.Combine(TestArtifactsPath, name);
@@ -25,20 +21,18 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.DependencyResolution
             public SharedTestStateBase()
                 : base(GetBaseDir("dependencyResolution"))
             {
-                BuiltDotnetPath = Path.Combine(TestArtifactsPath, "sharedFrameworkPublish");
-                RepoDirectories = new RepoDirectoriesProvider(builtDotnet: BuiltDotnetPath);
             }
 
             public DotNetBuilder DotNet(string name)
             {
-                return new DotNetBuilder(Location, BuiltDotnetPath, name);
+                return new DotNetBuilder(Location, RepoDirectoriesProvider.Default.BuiltDotnet, name);
             }
 
-            public TestApp CreateFrameworkReferenceApp(string fxName, string fxVersion)
+            public TestApp CreateFrameworkReferenceApp(string fxName, string fxVersion, Action<NetCoreAppBuilder> customizer = null)
             {
                 // Prepare the app mock - we're not going to run anything really, so we just need the basic files
                 TestApp testApp = CreateTestApp(Location, "FrameworkReferenceApp");
-                testApp.PopulateFrameworkDependent(fxName, fxVersion);
+                testApp.PopulateFrameworkDependent(fxName, fxVersion, customizer);
                 return testApp;
             }
 
diff --git a/src/installer/tests/HostActivation.Tests/DependencyResolution/DepsFile.cs b/src/installer/tests/HostActivation.Tests/DependencyResolution/DepsFile.cs
new file mode 100644 (file)
index 0000000..9f811c2
--- /dev/null
@@ -0,0 +1,77 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using System.IO;
+using Microsoft.DotNet.Cli.Build;
+using Microsoft.DotNet.Cli.Build.Framework;
+using Xunit;
+
+namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.DependencyResolution
+{
+    public class DepsFile : DependencyResolutionBase, IClassFixture<DepsFile.SharedTestState>
+    {
+        private readonly SharedTestState sharedState;
+
+        public DepsFile(SharedTestState sharedState)
+        {
+            this.sharedState = sharedState;
+        }
+
+        [Fact]
+        public void NoDepsJson()
+        {
+            // Without .deps.json, all assemblies in the app's directory are added to the TPA
+            // and the app's directory is added to the native library search path
+            TestApp app = sharedState.FrameworkReferenceApp;
+            sharedState.DotNetWithNetCoreApp.Exec(app.AppDll)
+                .EnableTracingAndCaptureOutputs()
+                .Execute()
+                .Should().Pass()
+                .And.HaveResolvedAssembly(Path.Combine(app.Location, $"{SharedTestState.DependencyName}.dll"))
+                .And.HaveResolvedNativeLibraryPath(app.Location);
+        }
+
+        [Fact]
+        public void SeparateDepsJson()
+        {
+            // For framework-dependent apps, the probing directories are:
+            // - The directory where the .deps.json is
+            // - Any framework directory
+            // Dependency should resolve relative to the .deps.json directory without checking for file existence
+            string dependencyPath = Path.Combine(Path.GetDirectoryName(sharedState.DepsJsonPath), $"{SharedTestState.DependencyName}.dll");
+            sharedState.DotNetWithNetCoreApp.Exec("exec", Constants.DepsFile.CommandLineArgument, sharedState.DepsJsonPath, sharedState.FrameworkReferenceApp.AppDll)
+                .EnableTracingAndCaptureOutputs()
+                .Execute()
+                .Should().Pass()
+                .And.HaveResolvedAssembly(dependencyPath);
+        }
+
+        public class SharedTestState : DependencyResolutionBase.SharedTestStateBase
+        {
+            public DotNetCli DotNetWithNetCoreApp { get; }
+
+            public TestApp FrameworkReferenceApp { get; }
+
+            public const string DependencyName = "Dependency";
+
+            public string DepsJsonPath { get; }
+
+            public SharedTestState()
+            {
+                DotNetWithNetCoreApp = DotNet("WithNetCoreApp")
+                    .AddMicrosoftNETCoreAppFrameworkMockCoreClr(RepoDirectoriesProvider.Default.MicrosoftNETCoreAppVersion)
+                    .Build();
+
+                FrameworkReferenceApp = CreateFrameworkReferenceApp(MicrosoftNETCoreApp, RepoDirectoriesProvider.Default.MicrosoftNETCoreAppVersion, b => b
+                    .WithProject(DependencyName, "1.0.0", p => p
+                        .WithAssemblyGroup(null, g => g.WithAsset($"{DependencyName}.dll"))));
+
+                var depsDir = Path.Combine(Location, "deps");
+                Directory.CreateDirectory(depsDir);
+                DepsJsonPath = Path.Combine(depsDir, Path.GetFileName(FrameworkReferenceApp.DepsJson));
+                File.Move(FrameworkReferenceApp.DepsJson, DepsJsonPath);
+            }
+        }
+    }
+}
index 92ed54c..4ea71e8 100644 (file)
@@ -1,4 +1,4 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System;
@@ -104,7 +104,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.DependencyResolution
                     HighWare,
                     "1.1.1",
                     runtimeConfig => runtimeConfig.WithFramework(MicrosoftNETCoreApp, "4.0.0"),
-                    path => NetCoreAppBuilder.ForNETCoreApp(HighWare, RepoDirectories.TargetRID)
+                    path => NetCoreAppBuilder.ForNETCoreApp(HighWare, RepoDirectoriesProvider.Default.TargetRID)
                         .WithProject(HighWare, "1.1.1", p => p
                             .WithAssemblyGroup(null, g => g
                             .WithAsset(TestAssemblyWithNoVersions + ".dll")
index 27c6e09..f86d0d2 100644 (file)
@@ -228,7 +228,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.DependencyResolution
             protected void UseFallbacksFromBuiltDotNet(NetCoreAppBuilder builder)
             {
                 IReadOnlyList<RuntimeFallbacks> fallbacks;
-                string depsJson = Path.Combine(new DotNetCli(BuiltDotnetPath).GreatestVersionSharedFxPath, $"{Constants.MicrosoftNETCoreApp}.deps.json");
+                string depsJson = Path.Combine(new DotNetCli(RepoDirectoriesProvider.Default.BuiltDotnet).GreatestVersionSharedFxPath, $"{Constants.MicrosoftNETCoreApp}.deps.json");
                 using (FileStream fileStream = File.Open(depsJson, FileMode.Open))
                 using (DependencyContextJsonReader reader = new DependencyContextJsonReader())
                 {
index 17b3f06..953d75c 100644 (file)
@@ -84,25 +84,6 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation
 
         // https://github.com/dotnet/runtime/issues/3654
         [Fact(Skip = "The 3.0 SDK copies NuGet references to the output by default now for executable projects, so this no longer fails.")]
-        public void Muxer_Exec_activation_of_Build_Output_Portable_DLL_with_DepsJson_Local_and_RuntimeConfig_Remote_Without_AdditionalProbingPath_Fails()
-        {
-            var fixture = sharedTestState.PortableAppFixture_Built
-                .Copy();
-
-            var runtimeConfig = MoveRuntimeConfigToSubdirectory(fixture);
-
-            var dotnet = fixture.BuiltDotnet;
-            var appDll = fixture.TestProject.AppDll;
-
-            dotnet.Exec("exec", "--runtimeconfig", runtimeConfig, appDll)
-                .CaptureStdErr()
-                .CaptureStdOut()
-                .Execute(expectedToFail: true)
-                .Should().Fail();
-        }
-
-        // https://github.com/dotnet/runtime/issues/3654
-        [Fact(Skip = "The 3.0 SDK copies NuGet references to the output by default now for executable projects, so this no longer fails.")]
         public void Muxer_Exec_activation_of_Build_Output_Portable_DLL_with_DepsJson_Local_and_RuntimeConfig_Remote_With_AdditionalProbingPath_Succeeds()
         {
             var fixture = sharedTestState.PortableAppFixture_Built
@@ -157,34 +138,6 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation
         }
 
         [Fact]
-        public void Muxer_Exec_activation_of_Build_Output_Portable_DLL_with_DepsJson_Remote_and_RuntimeConfig_Local_Succeeds()
-        {
-            var fixture = sharedTestState.PortableAppFixture_Built
-                .Copy();
-
-            // Move the .deps.json to a subdirectory, note that in this case we have to move all of the app's dependencies
-            // along with it - in this case Newtonsoft.Json.dll
-            // For framework dependent apps (dotnet build produces those) the probing directories are:
-            // - The directory where the .deps.json is
-            // - Any framework directory
-            var depsJson = MoveDepsJsonToSubdirectory(fixture);
-            File.Move(
-                Path.Combine(Path.GetDirectoryName(fixture.TestProject.AppDll), "Newtonsoft.Json.dll"),
-                Path.Combine(Path.GetDirectoryName(depsJson), "Newtonsoft.Json.dll"));
-
-            var dotnet = fixture.BuiltDotnet;
-            var appDll = fixture.TestProject.AppDll;
-
-            dotnet.Exec("exec", "--depsfile", depsJson, appDll)
-                .CaptureStdErr()
-                .CaptureStdOut()
-                .Execute()
-                .Should().Pass()
-                .And.HaveStdOutContaining("Hello World");
-
-        }
-
-        [Fact]
         public void Muxer_activation_of_Publish_Output_Portable_DLL_with_DepsJson_and_RuntimeConfig_Local_Succeeds()
         {
             var fixture = sharedTestState.PortableAppFixture_Published
@@ -229,24 +182,6 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation
         }
 
         [Fact]
-        public void Muxer_Exec_activation_of_Publish_Output_Portable_DLL_with_DepsJson_Remote_and_RuntimeConfig_Local_Fails()
-        {
-            var fixture = sharedTestState.PortableAppFixture_Published
-                .Copy();
-
-            var depsJson = MoveDepsJsonToSubdirectory(fixture);
-
-            var dotnet = fixture.BuiltDotnet;
-            var appDll = fixture.TestProject.AppDll;
-
-            dotnet.Exec("exec", "--depsfile", depsJson, appDll)
-                .CaptureStdErr()
-                .CaptureStdOut()
-                .Execute(expectedToFail: true)
-                .Should().Fail();
-        }
-
-        [Fact]
         public void AppHost_FrameworkDependent_Succeeds()
         {
             var fixture = sharedTestState.PortableAppFixture_Published
@@ -639,25 +574,6 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation
             }
         }
 
-        private string MoveDepsJsonToSubdirectory(TestProjectFixture testProjectFixture)
-        {
-            var subdirectory = Path.Combine(testProjectFixture.TestProject.ProjectDirectory, "d");
-            if (!Directory.Exists(subdirectory))
-            {
-                Directory.CreateDirectory(subdirectory);
-            }
-
-            var destDepsJson = Path.Combine(subdirectory, Path.GetFileName(testProjectFixture.TestProject.DepsJson));
-
-            if (File.Exists(destDepsJson))
-            {
-                File.Delete(destDepsJson);
-            }
-            File.Move(testProjectFixture.TestProject.DepsJson, destDepsJson);
-
-            return destDepsJson;
-        }
-
         private string MoveRuntimeConfigToSubdirectory(TestProjectFixture testProjectFixture)
         {
             var subdirectory = Path.Combine(testProjectFixture.TestProject.ProjectDirectory, "r");
index 90fddc1..df9cb53 100644 (file)
@@ -43,6 +43,11 @@ namespace Microsoft.DotNet.CoreSetup.Test
             public const string CommandLineArgument = "--additional-deps";
         }
 
+        public static class DepsFile
+        {
+            public const string CommandLineArgument = "--depsfile";
+        }
+
         public static class RollForwardToPreRelease
         {
             public const string EnvironmentVariable = "DOTNET_ROLL_FORWARD_TO_PRERELEASE";