Example #1
0
        public void MissingFrameworkInRuntimeConfig_Fails(bool useAppHost)
        {
            TestApp app = sharedTestState.MockApp.Copy();

            RuntimeConfig.FromFile(app.RuntimeConfigJson).Save();

            Command command;

            if (useAppHost)
            {
                command = Command.Create(app.AppExe)
                          .DotNetRoot(sharedTestState.BuiltDotNet.BinPath);
            }
            else
            {
                command = sharedTestState.BuiltDotNet.Exec(app.AppDll);
            }

            string hostPolicyName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy");

            command.EnableTracingAndCaptureOutputs()
            .MultilevelLookup(false)
            .Execute()
            .Should().Fail()
            .And.HaveStdErrContaining($"The library '{hostPolicyName}' required to execute the application was not found")
            .And.HaveStdErrContaining("Failed to run as a self-contained app")
            .And.HaveStdErrContaining($"'{app.RuntimeConfigJson}' did not specify a framework");
        }
Example #2
0
        /// <summary>
        /// Add a mock of the Microsoft.NETCore.App framework with the specified version
        /// </summary>
        /// <param name="version">Version to add</param>
        /// <param name="customizer">Customizer to customize the framework before it is built</param>
        /// <remarks>
        /// Product runtime binaries are not added. All the added mock framework will contain is hostpolicy,
        /// a mock version of coreclr, and a minimal Microsoft.NETCore.App.deps.json.
        /// </remarks>
        public DotNetBuilder AddMicrosoftNETCoreAppFrameworkMockCoreClr(string version, Action <NetCoreAppBuilder> customizer = null)
        {
            // ./shared/Microsoft.NETCore.App/<version> - create a mock of the root framework
            string netCoreAppPath = Path.Combine(_path, "shared", "Microsoft.NETCore.App", version);

            Directory.CreateDirectory(netCoreAppPath);

            string hostPolicyFileName  = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy");
            string coreclrFileName     = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("coreclr");
            string mockCoreclrFileName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("mockcoreclr");

            string currentRid = _repoDirectories.TargetRID;

            NetCoreAppBuilder.ForNETCoreApp("Microsoft.NETCore.App", currentRid)
            .WithStandardRuntimeFallbacks()
            .WithProject("Microsoft.NETCore.App", version, p => p
                         .WithNativeLibraryGroup(null, g => g
                                                 // ./shared/Microsoft.NETCore.App/<version>/coreclr.dll - this is a mock, will not actually run CoreClr
                                                 .WithAsset((new NetCoreAppBuilder.RuntimeFileBuilder($"runtimes/{currentRid}/native/{coreclrFileName}"))
                                                            .CopyFromFile(Path.Combine(_repoDirectories.Artifacts, "corehost_test", mockCoreclrFileName))
                                                            .WithFileOnDiskPath(coreclrFileName))))
            .WithPackage($"runtime.{currentRid}.Microsoft.NETCore.DotNetHostPolicy", version, p => p
                         .WithNativeLibraryGroup(null, g => g
                                                 // ./shared/Microsoft.NETCore.App/<version>/hostpolicy.dll - this is the real component and will load CoreClr library
                                                 .WithAsset((new NetCoreAppBuilder.RuntimeFileBuilder($"runtimes/{currentRid}/native/{hostPolicyFileName}"))
                                                            .CopyFromFile(Path.Combine(_repoDirectories.Artifacts, "corehost", hostPolicyFileName))
                                                            .WithFileOnDiskPath(hostPolicyFileName))))
            .WithCustomizer(customizer)
            .Build(new TestApp(netCoreAppPath, "Microsoft.NETCore.App"));

            return(this);
        }
Example #3
0
            public SharedTestState()
            {
                BaseDirectory = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "nativeHosting"));
                Directory.CreateDirectory(BaseDirectory);

                string nativeHostName = RuntimeInformationExtensions.GetExeFileNameForCurrentPlatform("nativehost");

                NativeHostPath = Path.Combine(BaseDirectory, nativeHostName);

                // Copy over native host and nethost
                RepoDirectories = new RepoDirectoriesProvider();
                string nethostName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("nethost");

                File.Copy(Path.Combine(RepoDirectories.CorehostPackages, nethostName), Path.Combine(BaseDirectory, nethostName));
                File.Copy(Path.Combine(RepoDirectories.Artifacts, "corehost_test", nativeHostName), NativeHostPath);

                InvalidInstallRoot = Path.Combine(BaseDirectory, "invalid");
                Directory.CreateDirectory(InvalidInstallRoot);

                ValidInstallRoot = Path.Combine(BaseDirectory, "valid");
                HostFxrPath      = CreateHostFxr(Path.Combine(ValidInstallRoot, "dotnet"));

                string appDir = Path.Combine(BaseDirectory, "app");

                Directory.CreateDirectory(appDir);
                string assemblyPath = Path.Combine(appDir, "App.dll");

                File.WriteAllText(assemblyPath, string.Empty);
                TestAssemblyPath = assemblyPath;
            }
Example #4
0
        public SharedTestStateBase()
        {
            BaseDirectory    = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "nativeHosting"));
            _baseDirArtifact = new TestArtifact(BaseDirectory);
            Directory.CreateDirectory(BaseDirectory);

            string nativeHostName = RuntimeInformationExtensions.GetExeFileNameForCurrentPlatform("nativehost");

            NativeHostPath = Path.Combine(BaseDirectory, nativeHostName);

            // Copy over native host
            RepoDirectories = new RepoDirectoriesProvider();
            File.Copy(Path.Combine(RepoDirectories.Artifacts, "corehost_test", nativeHostName), NativeHostPath);

            // Copy nethost next to native host
            // This is done even for tests not directly using nethost because nativehost consumes nethost in the more
            // user-friendly way of linking against nethost (instead of dlopen/LoadLibrary and dlsym/GetProcAddress).
            // On Windows, we can delay load through a linker option, but on other platforms load is required on start.
            string nethostName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("nethost");

            NethostPath = Path.Combine(Path.GetDirectoryName(NativeHostPath), nethostName);
            File.Copy(
                Path.Combine(RepoDirectories.HostArtifacts, nethostName),
                NethostPath);
        }
            public SharedTestState()
            {
                RepoDirectories = new RepoDirectoriesProvider();

                HostApiInvokerAppFixture = new TestProjectFixture("HostApiInvokerApp", RepoDirectories)
                                           .EnsureRestored(RepoDirectories.CorehostPackages)
                                           .BuildProject();

                ComponentWithNoDependencies = CreateComponentWithNoDependencies(null, Location);

                ComponentWithDependencies = CreateComponentWithDependencies(null, Location);

                ComponentWithResources = CreateComponentWithResources(null, Location);

                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // On non-Windows, we can't just P/Invoke to already loaded hostpolicy, so copy it next to the app dll.
                    var fixture    = HostApiInvokerAppFixture;
                    var hostpolicy = Path.Combine(
                        fixture.BuiltDotnet.GreatestVersionSharedFxPath,
                        RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy"));

                    File.Copy(
                        hostpolicy,
                        Path.GetDirectoryName(fixture.TestProject.AppDll));
                }
            }
Example #6
0
            public SharedTestState()
            {
                // Copy nethost next to native host
                string nethostName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("nethost");

                File.Copy(
                    Path.Combine(RepoDirectories.CorehostPackages, nethostName),
                    Path.Combine(Path.GetDirectoryName(NativeHostPath), nethostName));

                InvalidInstallRoot = Path.Combine(BaseDirectory, "invalid");
                Directory.CreateDirectory(InvalidInstallRoot);

                ValidInstallRoot = Path.Combine(BaseDirectory, "valid");
                HostFxrPath      = CreateHostFxr(Path.Combine(ValidInstallRoot, "dotnet"));

                string appDir = Path.Combine(BaseDirectory, "app");

                Directory.CreateDirectory(appDir);
                string assemblyPath = Path.Combine(appDir, "App.dll");

                File.WriteAllText(assemblyPath, string.Empty);
                TestAssemblyPath = assemblyPath;

                string productDir = Path.Combine(BaseDirectory, "product");

                Directory.CreateDirectory(productDir);
                ProductHostFxrPath = Path.Combine(productDir, HostFxrName);
                File.Copy(Path.Combine(RepoDirectories.CorehostPackages, HostFxrName), ProductHostFxrPath);
            }
        public SingleFileSharedState()
        {
            // We include mockcoreclr in our project to test native binaries extraction.
            string mockCoreClrPath = Path.Combine(RepoDirectories.Artifacts, "corehost_test",
                                                  RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("mockcoreclr"));

            TestFixture = PreparePublishedSelfContainedTestProject("SingleFileApiTests", $"/p:AddFile={mockCoreClrPath}");
        }
            public SharedTestState()
            {
                var dotNet = new DotNetBuilder(BaseDirectory, Path.Combine(TestArtifact.TestArtifactsPath, "sharedFrameworkPublish"), "mockRuntime")
                             .AddMicrosoftNETCoreAppFrameworkMockCoreClr(NetCoreAppVersion)
                             .Build();

                DotNetRoot = dotNet.BinPath;

                HostFxrPath = Path.Combine(
                    dotNet.GreatestVersionHostFxrPath,
                    RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr"));

                string appDir = Path.Combine(BaseDirectory, "app");

                Directory.CreateDirectory(appDir);
                AppPath = Path.Combine(appDir, "App.dll");
                File.WriteAllText(AppPath, string.Empty);

                RuntimeConfig.FromFile(Path.Combine(appDir, "App.runtimeconfig.json"))
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(AppPropertyName, AppPropertyValue)
                .Save();

                AppPath_MultiProperty = Path.Combine(appDir, "App_MultiProperty.dll");
                File.WriteAllText(AppPath_MultiProperty, string.Empty);

                RuntimeConfig.FromFile(Path.Combine(appDir, "App_MultiProperty.runtimeconfig.json"))
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(AppPropertyName, AppPropertyValue)
                .WithProperty(AppMultiPropertyName, AppMultiPropertyValue)
                .Save();

                string configDir = Path.Combine(BaseDirectory, "config");

                Directory.CreateDirectory(configDir);
                RuntimeConfigPath = Path.Combine(configDir, "Component.runtimeconfig.json");
                RuntimeConfig.FromFile(RuntimeConfigPath)
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(ConfigPropertyName, ConfigPropertyValue)
                .Save();

                RuntimeConfigPath_MultiProperty = Path.Combine(configDir, "Component_MultiProperty.runtimeconfig.json");
                RuntimeConfig.FromFile(RuntimeConfigPath_MultiProperty)
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(ConfigPropertyName, ConfigPropertyValue)
                .WithProperty(ConfigMultiPropertyName, ConfigMultiPropertyValue)
                .Save();

                string secondaryDir = Path.Combine(BaseDirectory, "secondary");

                Directory.CreateDirectory(secondaryDir);
                SecondaryRuntimeConfigPath = Path.Combine(secondaryDir, "Secondary.runtimeconfig.json");
                RuntimeConfig.FromFile(SecondaryRuntimeConfigPath)
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(SecondaryConfigPropertyName, SecondaryConfigPropertyValue)
                .Save();
            }
Example #9
0
        /// <summary>
        /// Add a mock of the Microsoft.NETCore.App framework with the specified version
        /// </summary>
        /// <param name="version">Version to add</param>
        /// <remarks>
        /// Product runtime binaries are not added. All the added mock framework will contain is hostpolicy,
        /// a mock version of coreclr, and a minimal Microsoft.NETCore.App.deps.json.
        /// </remarks>
        public DotNetBuilder AddMicrosoftNETCoreAppFrameworkMockCoreClr(string version)
        {
            // ./shared/Microsoft.NETCore.App/<version> - create a mock of the root framework
            string netCoreAppPath = Path.Combine(_path, "shared", "Microsoft.NETCore.App", version);

            Directory.CreateDirectory(netCoreAppPath);

            // ./shared/Microsoft.NETCore.App/<version>/hostpolicy.dll - this is the real component and will load CoreClr library
            string hostPolicyFileName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy");

            File.Copy(
                Path.Combine(_repoDirectories.Artifacts, "corehost", hostPolicyFileName),
                Path.Combine(netCoreAppPath, hostPolicyFileName),
                true);

            // ./shared/Microsoft.NETCore.App/<version>/coreclr.dll - this is a mock, will not actually run CoreClr
            string coreclrFileName     = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("coreclr");
            string mockCoreclrFileName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("mockcoreclr");

            File.Copy(
                Path.Combine(_repoDirectories.Artifacts, "corehost_test", mockCoreclrFileName),
                Path.Combine(netCoreAppPath, coreclrFileName),
                true);

            string netCoreAppPathDepsJson = Path.Combine(netCoreAppPath, "Microsoft.NETCore.App.deps.json");

            string currentRid = _repoDirectories.TargetRID;

            string depsJsonBody = $@"{{
              ""runtimeTarget"": "".NETCoreApp"",
              ""targets"": {{
                "".NETCoreApp"": {{
                  ""Microsoft.NETCore.App/{version}"": {{
                    ""native"": {{
                      ""runtimes/{currentRid}/native/{coreclrFileName}"": {{ }}
                    }}
                  }},
                  ""runtime.{currentRid}.Microsoft.NETCore.DotNetHostPolicy/{version}"": {{
                    ""native"": {{
                      ""runtimes/{currentRid}/native/{hostPolicyFileName}"": {{}}
                    }}
                  }}
                }}
              }},
              ""libraries"": {{
                ""Microsoft.NETCore.App/{version}"": {{
                  ""type"": ""package"",
                  ""serviceable"": true,
                  ""sha512"": """"
                }}
              }}
            }}";

            File.WriteAllText(netCoreAppPathDepsJson, depsJsonBody);
            return(this);
        }
Example #10
0
        public void CoreClrLookup_WithNoDirectorySeparatorInDeps()
        {
            var fixture = SharedFxLookupPortableAppFixture
                          .Copy();

            var dotnet = fixture.BuiltDotnet;
            var appDll = fixture.TestProject.AppDll;

            string runtimeConfig = Path.Combine(fixture.TestProject.OutputDirectory, "SharedFxLookupPortableApp.runtimeconfig.json");

            SharedFramework.SetRuntimeConfigJson(runtimeConfig, "9999.0.0", null);

            // Add versions in the exe folders
            SharedFramework.AddAvailableSharedFxVersions(_builtSharedFxDir, _exeSharedFxBaseDir, "9999.0.0");
            string sharedFxPath         = Path.Combine(_exeSharedFxBaseDir, "9999.0.0");
            string sharedFxDepsJsonPath = Path.Combine(sharedFxPath, "Microsoft.NETCore.App.deps.json");

            // Modify the .deps.json for Microsoft.NETCore.App FX
            JObject root = JObject.Parse(File.ReadAllText(sharedFxDepsJsonPath));
            IEnumerable <JProperty> netCoreAppNativeAssets = root["targets"]
                                                             .Children <JProperty>().Where(p => p.Name.Contains("/"))
                                                             .Children().Children().OfType <JProperty>().Where(p => p.Name.Contains("runtime") && p.Name.Contains("Microsoft.NETCore.App"))
                                                             .Values()["native"].Children().OfType <JProperty>();

            // Change the coreclr.dll asset to specify only "coreclr.dll" as the relative path (no directories).
            string    coreClrLibraryName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("coreclr");
            JProperty coreClrProperty    = netCoreAppNativeAssets.First(p => p.Name.Contains(coreClrLibraryName));
            JProperty newCoreClrProperty = new JProperty(coreClrProperty.Name.Substring(coreClrProperty.Name.LastIndexOf('/') + 1), coreClrProperty.Value);

            coreClrProperty.Parent.Add(newCoreClrProperty);
            coreClrProperty.Remove();

            // Change the clrjit.dll asset to specify only "clrjit.dll" as the relative path (no directories).
            string    clrJitLibraryName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("clrjit");
            JProperty clrJitProperty    = netCoreAppNativeAssets.First(p => p.Name.Contains(clrJitLibraryName));
            JProperty newClrJitProperty = new JProperty(clrJitProperty.Name.Substring(clrJitProperty.Name.LastIndexOf('/') + 1), clrJitProperty.Value);

            clrJitProperty.Parent.Add(newClrJitProperty);
            clrJitProperty.Remove();

            File.WriteAllText(sharedFxDepsJsonPath, root.ToString());

            dotnet.Exec(appDll)
            .WorkingDirectory(_currentWorkingDir)
            .EnvironmentVariable("COREHOST_TRACE", "1")
            .CaptureStdOut()
            .CaptureStdErr()
            .Execute()
            .Should().Pass()
            .And.HaveStdErrContaining($"CoreCLR path = '{Path.Combine(sharedFxPath, coreClrLibraryName)}'")
            .And.HaveStdErrContaining($"The resolved JIT path is '{Path.Combine(sharedFxPath, clrJitLibraryName)}'");
        }
Example #11
0
 public SingleFileSharedState()
 {
     try
     {
         // We include mockcoreclr in our project to test native binaries extraction.
         string mockCoreClrPath = Path.Combine(RepoDirectories.Artifacts, "corehost_test",
                                               RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("mockcoreclr"));
         TestFixture = PreparePublishedSelfContainedTestProject("SingleFileApiTests", $"/p:AddFile={mockCoreClrPath}");
     }
     catch (Exception e) when(TestUtils.FailFast(e))  // Fail fast to gather a crash dump
     {
         throw;
     }
 }
Example #12
0
        /// <summary>
        /// Use a mock version of HostFxr.
        /// </summary>
        /// <param name="version">Version to add</param>
        /// <remarks>
        /// Currently, the only mock version of HostFxr that we have is mockhostfxr_2_2.
        /// </remarks>
        public DotNetBuilder AddMockHostFxr(Version version)
        {
            string hostfxrPath = Path.Combine(_path, "host", "fxr", version.ToString());

            Directory.CreateDirectory(hostfxrPath);
            bool hasCustomErrorWriter = version.Major >= 3;

            string mockHostFxrFileName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform(hasCustomErrorWriter ? "mockhostfxr" : "mockhostfxr_2_2");

            File.Copy(
                Path.Combine(_repoDirectories.Artifacts, "corehost_test", mockHostFxrFileName),
                Path.Combine(hostfxrPath, RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr")),
                true);

            return(this);
        }
Example #13
0
            public CommandResult RunComponentResolutionMultiThreadedTest(string componentOnePath, string componentTwoPath, TestApp hostApp, string hostFxrFolder)
            {
                string[] args =
                {
                    resolve_component_dependencies,
                    run_app_and_resolve_multithreaded,
                    Path.Combine(hostFxrFolder,       RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr")),
                    hostApp.AppDll,
                    componentOnePath,
                    componentTwoPath
                };

                return(Command.Create(NativeHostPath, args)
                       .EnableTracingAndCaptureOutputs()
                       .Execute());
            }
Example #14
0
        /// <summary>
        /// Add a mock of the Microsoft.NETCore.App framework with the specified version
        /// </summary>
        /// <param name="version">Version to add</param>
        /// <remarks>
        /// Product runtime binaries are not added. All the added mock framework will contain is a mock version of host policy.
        /// </remarks>
        public DotNetBuilder AddMicrosoftNETCoreAppFrameworkMockHostPolicy(string version)
        {
            // ./shared/Microsoft.NETCore.App/<version> - create a mock of the root framework
            string netCoreAppPath = Path.Combine(_path, "shared", "Microsoft.NETCore.App", version);

            Directory.CreateDirectory(netCoreAppPath);

            // ./shared/Microsoft.NETCore.App/<version>/hostpolicy.dll - this is a mock, will not actually load CoreCLR
            string mockHostPolicyFileName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("mockhostpolicy");

            File.Copy(
                Path.Combine(_repoDirectories.Artifacts, "corehost_test", mockHostPolicyFileName),
                Path.Combine(netCoreAppPath, RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy")),
                true);

            return(this);
        }
Example #15
0
        public void TestOnlyDisabledByDefault()
        {
            // Intentionally not enabling test-only behavior. This test validates that even if the test-only env. variable is set
            // it will not take effect on its own by default.
            // To make sure the test is reliable, copy the product binary again into the test folder where we run it from.
            // This is to make sure that we're using the unmodified product binary. If some previous test
            // enabled test-only product behavior on the binary and didn't correctly cleanup, this test would fail.
            File.Copy(
                Path.Combine(sharedState.RepoDirectories.CorehostPackages, RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("nethost")),
                sharedState.NethostPath,
                overwrite: true);

            Command.Create(sharedState.NativeHostPath, GetHostFxrPath)
            .EnableTracingAndCaptureOutputs()
            .EnvironmentVariable(Constants.TestOnlyEnvironmentVariables.GloballyRegisteredPath, sharedState.ValidInstallRoot)
            .Execute()
            .Should().NotHaveStdErrContaining($"Using global installation location [{sharedState.ValidInstallRoot}] as runtime location.");
        }
Example #16
0
            public CommandResult RunComponentResolutionTest(string componentPath, TestApp hostApp, string hostFxrFolder, Action <Command> commandCustomizer = null)
            {
                string[] args =
                {
                    resolve_component_dependencies,
                    run_app_and_resolve,
                    Path.Combine(hostFxrFolder,    RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr")),
                    hostApp.AppDll,
                    componentPath
                };

                Command command = Command.Create(NativeHostPath, args)
                                  .EnableTracingAndCaptureOutputs();

                commandCustomizer?.Invoke(command);

                return(command.Execute()
                       .StdErrAfter("corehost_resolve_component_dependencies = {"));
            }
            public SharedTestState()
            {
                DotNet = new DotNetBuilder(BaseDirectory, Path.Combine(TestArtifact.TestArtifactsPath, "sharedFrameworkPublish"), "mockRuntime")
                         .AddMicrosoftNETCoreAppFrameworkMockCoreClr(NetCoreAppVersion)
                         .Build();

                HostFxrPath = Path.Combine(
                    DotNet.GreatestVersionHostFxrPath,
                    RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr"));

                AppDirectory = Path.Combine(BaseDirectory, "app");
                Directory.CreateDirectory(AppDirectory);
                AppPath = Path.Combine(AppDirectory, "App.dll");
                File.WriteAllText(AppPath, string.Empty);

                RuntimeConfig.FromFile(Path.Combine(AppDirectory, "App.runtimeconfig.json"))
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .Save();
            }
        public void Running_Publish_Output_Standalone_EXE_With_DOTNET_ROOT_Fails()
        {
            var fixture = sharedTestState.StandaloneAppFixture_Published
                          .Copy();

            var appExe = fixture.TestProject.AppExe;
            var appDll = fixture.TestProject.AppDll;

            // Move whole directory to a subdirectory
            string currentOutDir   = fixture.TestProject.OutputDirectory;
            string relativeNewPath = "..";

            relativeNewPath = Path.Combine(relativeNewPath, "newDir2");
            string newOutDir = Path.Combine(currentOutDir, relativeNewPath);

            Directory.Move(currentOutDir, newOutDir);

            // Move the apphost exe and app dll back to original location
            string appExeName       = Path.GetFileName(appExe);
            string sourceAppExePath = Path.Combine(newOutDir, appExeName);

            Directory.CreateDirectory(Path.GetDirectoryName(appExe));
            File.Move(sourceAppExePath, appExe);

            string appDllName       = Path.GetFileName(appDll);
            string sourceAppDllPath = Path.Combine(newOutDir, appDllName);

            File.Move(sourceAppDllPath, appDll);

            // This verifies a self-contained apphost cannot use DOTNET_ROOT to reference a flat
            // self-contained layout since a flat layout of the shared framework is not supported.
            Command.Create(appExe)
            .EnableTracingAndCaptureOutputs()
            .DotNetRoot(newOutDir)
            .Execute(expectedToFail: true)
            .Should().Fail()
            .And.HaveUsedDotNetRootInstallLocation(Path.GetFullPath(newOutDir), fixture.CurrentRid)
            .And.HaveStdErrContaining($"The required library {RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr")} could not be found.");
        }
            public TestApp CreateSelfContainedAppWithMockHostPolicy()
            {
                string testAppDir = Path.Combine(_baseDir, "SelfContainedApp");

                Directory.CreateDirectory(testAppDir);
                TestApp testApp = new TestApp(testAppDir);

                string hostFxrFileName        = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr");
                string hostPolicyFileName     = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy");
                string mockHostPolicyFileName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("mockhostpolicy");
                string appHostFileName        = RuntimeInformationExtensions.GetExeFileNameForCurrentPlatform("apphost");

                DotNetCli builtDotNetCli = new DotNetCli(_builtDotnet);

                // ./hostfxr - the product version
                File.Copy(builtDotNetCli.GreatestVersionHostFxrFilePath, Path.Combine(testAppDir, hostFxrFileName));

                // ./hostpolicy - the mock
                File.Copy(
                    Path.Combine(_repoDirectories.Artifacts, "corehost_test", mockHostPolicyFileName),
                    Path.Combine(testAppDir, hostPolicyFileName));

                // ./SelfContainedApp.dll
                File.WriteAllText(Path.Combine(testAppDir, "SelfContainedApp.dll"), string.Empty);

                // ./SelfContainedApp.runtimeconfig.json
                File.WriteAllText(Path.Combine(testAppDir, "SelfContainedApp.runtimeconfig.json"), "{}");

                // ./SelfContainedApp.exe
                string selfContainedAppExePath = Path.Combine(testAppDir, RuntimeInformationExtensions.GetExeFileNameForCurrentPlatform("SelfContainedApp"));

                File.Copy(
                    Path.Combine(_repoDirectories.HostArtifacts, appHostFileName),
                    selfContainedAppExePath);
                AppHostExtensions.BindAppHost(selfContainedAppExePath);

                return(testApp);
            }
            public TestApp CreateSelfContainedAppWithMockCoreClr(string name, string version, Action <NetCoreAppBuilder> customizer = null, string location = null)
            {
                TestApp testApp = CreateTestApp(location, name);

                string hostFxrFileName     = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr");
                string hostPolicyFileName  = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy");
                string coreclrFileName     = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("coreclr");
                string mockCoreclrFileName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("mockcoreclr");

                string currentRid = RepoDirectories.TargetRID;

                NetCoreAppBuilder.ForNETCoreApp(name, currentRid)
                .WithProject(name, version, p => p
                             .WithAssemblyGroup(null, g => g.WithMainAssembly()))
                .WithPackage("runtimePack.Microsoft.NETCore.App", "1.0.0", p => p
                             .WithNativeLibraryGroup(null, g => g
                                                     // ./coreclr.dll - this is a mock, will not actually run CoreClr
                                                     .WithAsset((new NetCoreAppBuilder.RuntimeFileBuilder(coreclrFileName))
                                                                .CopyFromFile(Path.Combine(RepoDirectories.Artifacts, "corehost_test", mockCoreclrFileName))
                                                                .WithFileOnDiskPath(coreclrFileName))))
                .WithPackage("runtimePack.Microsoft.NETCore.DotNetHostResolver", "1.0.0", p => p
                             .WithNativeLibraryGroup(null, g => g
                                                     // ./hostfxr.dll - this is the real component and will load hostpolicy library
                                                     .WithAsset((new NetCoreAppBuilder.RuntimeFileBuilder(hostFxrFileName))
                                                                .CopyFromFile(Path.Combine(RepoDirectories.Artifacts, "corehost", hostFxrFileName))
                                                                .WithFileOnDiskPath(hostFxrFileName))))
                .WithPackage("runtimePack.Microsoft.NETCore.DotNetHostPolicy", "1.0.0", p => p
                             .WithNativeLibraryGroup(null, g => g
                                                     // ./hostpolicy.dll - this is the real component and will load CoreClr library
                                                     .WithAsset((new NetCoreAppBuilder.RuntimeFileBuilder(hostPolicyFileName))
                                                                .CopyFromFile(Path.Combine(RepoDirectories.Artifacts, "corehost", hostPolicyFileName))
                                                                .WithFileOnDiskPath(hostPolicyFileName))))
                .WithCustomizer(customizer)
                .WithRuntimeConfig(config => { })
                .Build(testApp);

                return(testApp);
            }
Example #21
0
        public void EnvironmentVariable_DotnetRootPathExistsButHasNoHost()
        {
            var fixture = sharedTestState.PortableAppFixture
                          .Copy();

            var appExe  = fixture.TestProject.AppExe;
            var projDir = fixture.TestProject.ProjectDirectory;

            using (TestOnlyProductBehavior.Enable(appExe))
            {
                Command.Create(appExe)
                .EnableTracingAndCaptureOutputs()
                .DotNetRoot(projDir)
                .MultilevelLookup(false)
                .EnvironmentVariable(
                    Constants.TestOnlyEnvironmentVariables.GloballyRegisteredPath,
                    sharedTestState.InstallLocation)
                .Execute()
                .Should().Fail()
                .And.HaveUsedDotNetRootInstallLocation(projDir, fixture.CurrentRid)
                // If DOTNET_ROOT points to a folder that exists we assume that there's a dotnet installation in it
                .And.HaveStdErrContaining($"The required library {RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr")} could not be found.");
            }
        }
            /*
             * This method is needed to workaround dotnet build not placing the host from the package
             * graph in the build output.
             * https://github.com/dotnet/cli/issues/2343
             */
            private static void ReplaceTestProjectOutputHostInTestProjectFixture(TestProjectFixture testProjectFixture)
            {
                var dotnet = testProjectFixture.BuiltDotnet;

                var testProjectHostPolicy = testProjectFixture.TestProject.HostPolicyDll;
                var testProjectHostFxr    = testProjectFixture.TestProject.HostFxrDll;

                if (!File.Exists(testProjectHostPolicy))
                {
                    throw new Exception("host or hostpolicy does not exist in test project output. Is this a standalone app?");
                }

                var dotnetHostPolicy = Path.Combine(dotnet.GreatestVersionSharedFxPath, RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy"));
                var dotnetHostFxr    = Path.Combine(dotnet.GreatestVersionHostFxrPath, RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr"));

                File.Copy(dotnetHostPolicy, testProjectHostPolicy, true);

                if (File.Exists(testProjectHostFxr))
                {
                    File.Copy(dotnetHostFxr, testProjectHostFxr, true);
                }
            }