| 
 | 1 | +// Licensed to the .NET Foundation under one or more agreements.  | 
 | 2 | +// The .NET Foundation licenses this file to you under the MIT license.  | 
 | 3 | + | 
 | 4 | +using System;  | 
 | 5 | +using System.IO;  | 
 | 6 | +using Microsoft.DotNet.Cli.Build;  | 
 | 7 | +using Xunit;  | 
 | 8 | +using static Microsoft.DotNet.CoreSetup.Test.NetCoreAppBuilder;  | 
 | 9 | + | 
 | 10 | +namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.DependencyResolution  | 
 | 11 | +{  | 
 | 12 | +    public class LocalPath : IClassFixture<LocalPath.SharedTestState>  | 
 | 13 | +    {  | 
 | 14 | +        private readonly SharedTestState sharedState;  | 
 | 15 | + | 
 | 16 | +        public LocalPath(SharedTestState sharedState)  | 
 | 17 | +        {  | 
 | 18 | +            this.sharedState = sharedState;  | 
 | 19 | +        }  | 
 | 20 | + | 
 | 21 | +        [Theory]  | 
 | 22 | +        [InlineData(true)]  | 
 | 23 | +        [InlineData(false)]  | 
 | 24 | +        public void RuntimeAssemblies_FrameworkDependent(bool useLocalPath) => RuntimeAssemblies(isSelfContained: false, useLocalPath);  | 
 | 25 | + | 
 | 26 | +        [Theory]  | 
 | 27 | +        [InlineData(true)]  | 
 | 28 | +        [InlineData(false)]  | 
 | 29 | +        public void RuntimeAssemblies_SelfContained(bool useLocalPath) => RuntimeAssemblies(isSelfContained: true, useLocalPath);  | 
 | 30 | + | 
 | 31 | +        [Theory]  | 
 | 32 | +        [InlineData(true)]  | 
 | 33 | +        [InlineData(false)]  | 
 | 34 | +        public void NativeLibraries_FrameworkDependent(bool useLocalPath) => NativeLibraries(isSelfContained: false, useLocalPath);  | 
 | 35 | + | 
 | 36 | +        [Theory]  | 
 | 37 | +        [InlineData(true)]  | 
 | 38 | +        [InlineData(false)]  | 
 | 39 | +        public void NativeLibraries_SelfContained(bool useLocalPath) => NativeLibraries(isSelfContained: true, useLocalPath);  | 
 | 40 | + | 
 | 41 | +        [Theory]  | 
 | 42 | +        [InlineData(true)]  | 
 | 43 | +        [InlineData(false)]  | 
 | 44 | +        public void ResourceAssemblies_FrameworkDependent(bool useLocalPath) => ResourceAssemblies(isSelfContained: false, useLocalPath);  | 
 | 45 | + | 
 | 46 | +        [Theory]  | 
 | 47 | +        [InlineData(true)]  | 
 | 48 | +        [InlineData(false)]  | 
 | 49 | +        public void ResourceAssemblies_SelfContained(bool useLocalPath) => ResourceAssemblies(isSelfContained: true, useLocalPath);  | 
 | 50 | + | 
 | 51 | +        private void RuntimeAssemblies(bool isSelfContained, bool useLocalPath)  | 
 | 52 | +        {  | 
 | 53 | +            RuntimeLibraryType[] libraryTypes = [ RuntimeLibraryType.project, RuntimeLibraryType.package, RuntimeLibraryType.runtimepack ];  | 
 | 54 | + | 
 | 55 | +            Action<NetCoreAppBuilder> customizer = b =>  | 
 | 56 | +            {  | 
 | 57 | +                foreach (var libraryType in libraryTypes)  | 
 | 58 | +                {  | 
 | 59 | +                    string library = $"Test{libraryType}";  | 
 | 60 | +                    (string path, string localPath) = GetPaths(libraryType, false);  | 
 | 61 | +                    b.WithRuntimeLibrary(libraryType, library, "1.0.0", p => p  | 
 | 62 | +                        .WithAssemblyGroup(null, g => g  | 
 | 63 | +                            .WithAsset(path, useLocalPath ? f => f.WithLocalPath(localPath) : null)));  | 
 | 64 | + | 
 | 65 | +                    if (!isSelfContained)  | 
 | 66 | +                    {  | 
 | 67 | +                        // Add RID-specific assembly  | 
 | 68 | +                        (string ridPath, string localRidPath) = GetPaths(libraryType, true);  | 
 | 69 | +                        b.WithRuntimeLibrary(libraryType, $"{library}-{TestContext.BuildRID}", "1.0.0", p => p  | 
 | 70 | +                            .WithAssemblyGroup(TestContext.BuildRID, g => g  | 
 | 71 | +                                .WithAsset(ridPath, useLocalPath ? f => f.WithLocalPath(localRidPath) : null)));  | 
 | 72 | +                    }  | 
 | 73 | +                }  | 
 | 74 | + | 
 | 75 | +                b.WithLocalPathsInDepsJson(useLocalPath);  | 
 | 76 | +            };  | 
 | 77 | + | 
 | 78 | +            using TestApp app = CreateApp(isSelfContained, customizer);  | 
 | 79 | +            var result = sharedState.DotNetWithNetCoreApp.Exec(app.AppDll)  | 
 | 80 | +                .EnableTracingAndCaptureOutputs()  | 
 | 81 | +                .Execute();  | 
 | 82 | +            result.Should().Pass();  | 
 | 83 | + | 
 | 84 | +            // Check all library types  | 
 | 85 | +            foreach (var libraryType in libraryTypes)  | 
 | 86 | +            {  | 
 | 87 | +                // Check RID-agnostic assembly  | 
 | 88 | +                (string path, string localPath) = GetPaths(libraryType, false);  | 
 | 89 | + | 
 | 90 | +                // Without localPath, RID-agnostic non-runtimepack runtime assemblies are assumed to be in <app_directory>  | 
 | 91 | +                string relativePath = useLocalPath  | 
 | 92 | +                    ? localPath  | 
 | 93 | +                    : libraryType == RuntimeLibraryType.runtimepack ? path : Path.GetFileName(path);  | 
 | 94 | +                string expectedPath = Path.Join(app.Location, relativePath);  | 
 | 95 | +                result.Should().HaveResolvedAssembly(expectedPath);  | 
 | 96 | +                if (useLocalPath)  | 
 | 97 | +                {  | 
 | 98 | +                    result.Should().NotHaveResolvedAssembly(Path.Join(app.Location, path));  | 
 | 99 | +                }  | 
 | 100 | + | 
 | 101 | +                // Check RID-specific assembly  | 
 | 102 | +                if (!isSelfContained)  | 
 | 103 | +                {  | 
 | 104 | +                    (string ridPath, string localRidPath) = GetPaths(libraryType, true);  | 
 | 105 | +                    string expectedRidPath = Path.Join(app.Location, useLocalPath ? localRidPath : ridPath);  | 
 | 106 | +                    result.Should().HaveResolvedAssembly(expectedRidPath);  | 
 | 107 | +                    if (useLocalPath)  | 
 | 108 | +                    {  | 
 | 109 | +                        result.Should().NotHaveResolvedAssembly(Path.Join(app.Location, ridPath));  | 
 | 110 | +                    }  | 
 | 111 | +                }  | 
 | 112 | +            }  | 
 | 113 | + | 
 | 114 | +            static (string Path, string LocalPath) GetPaths(RuntimeLibraryType libraryType, bool useRid)  | 
 | 115 | +            {  | 
 | 116 | +                string library = $"Test{libraryType}";  | 
 | 117 | +                string path = useRid ? $"lib/{TestContext.BuildRID}/{library}-{TestContext.BuildRID}.dll" : $"lib/{library}.dll";  | 
 | 118 | +                return (path, $"{libraryType}/{path}");  | 
 | 119 | +            }  | 
 | 120 | +        }  | 
 | 121 | + | 
 | 122 | +        private void NativeLibraries(bool isSelfContained, bool useLocalPath)  | 
 | 123 | +        {  | 
 | 124 | +            NetCoreAppBuilder.RuntimeLibraryType[] libraryTypes = [NetCoreAppBuilder.RuntimeLibraryType.project, NetCoreAppBuilder.RuntimeLibraryType.package, NetCoreAppBuilder.RuntimeLibraryType.runtimepack];  | 
 | 125 | + | 
 | 126 | +            Action<NetCoreAppBuilder> customizer = b =>  | 
 | 127 | +            {  | 
 | 128 | +                foreach (var libraryType in libraryTypes)  | 
 | 129 | +                {  | 
 | 130 | +                    string library = $"Test{libraryType}";  | 
 | 131 | +                    (string path, string localPath) = GetPaths(libraryType, false);  | 
 | 132 | +                    b.WithRuntimeLibrary(libraryType, library, "1.0.0", p => p  | 
 | 133 | +                        .WithNativeLibraryGroup(null, g => g  | 
 | 134 | +                            .WithAsset($"{path}/{library}.native", useLocalPath ? f => f.WithLocalPath($"{localPath}/{library}.native") : null)));  | 
 | 135 | + | 
 | 136 | +                    if (!isSelfContained)  | 
 | 137 | +                    {  | 
 | 138 | +                        // Add RID-specific native library  | 
 | 139 | +                        (string ridPath, string localRidPath) = GetPaths(libraryType, true);  | 
 | 140 | +                        b.WithRuntimeLibrary(libraryType, $"{library}-{TestContext.BuildRID}", "1.0.0", p => p  | 
 | 141 | +                            .WithNativeLibraryGroup(TestContext.BuildRID, g => g  | 
 | 142 | +                                .WithAsset($"{ridPath}/{library}-{TestContext.BuildRID}.native", useLocalPath ? f => f.WithLocalPath($"{localRidPath}/{library}-{TestContext.BuildRID}.native") : null)));  | 
 | 143 | +                    }  | 
 | 144 | +                }  | 
 | 145 | + | 
 | 146 | +                b.WithLocalPathsInDepsJson(useLocalPath);  | 
 | 147 | +            };  | 
 | 148 | + | 
 | 149 | +            using TestApp app = CreateApp(isSelfContained, customizer);  | 
 | 150 | +            var result = sharedState.DotNetWithNetCoreApp.Exec(app.AppDll)  | 
 | 151 | +                .EnableTracingAndCaptureOutputs()  | 
 | 152 | +                .Execute();  | 
 | 153 | +            result.Should().Pass();  | 
 | 154 | + | 
 | 155 | +            // Check all library types  | 
 | 156 | +            foreach (NetCoreAppBuilder.RuntimeLibraryType libraryType in libraryTypes)  | 
 | 157 | +            {  | 
 | 158 | +                // Check RID-agnostic native library path  | 
 | 159 | +                (string path, string localPath) = GetPaths(libraryType, false);  | 
 | 160 | + | 
 | 161 | +                // Without localPath, RID-agnostic non-runtimepack native libraries are assumed to be in <app_directory>  | 
 | 162 | +                string relativePath = useLocalPath  | 
 | 163 | +                    ? localPath  | 
 | 164 | +                    : libraryType == RuntimeLibraryType.runtimepack ? path : string.Empty;  | 
 | 165 | +                string expectedPath = Path.Join(app.Location, relativePath);  | 
 | 166 | +                result.Should().HaveResolvedNativeLibraryPath(expectedPath);  | 
 | 167 | +                if (useLocalPath)  | 
 | 168 | +                {  | 
 | 169 | +                    result.Should().NotHaveResolvedNativeLibraryPath(Path.Join(app.Location, path));  | 
 | 170 | +                }  | 
 | 171 | + | 
 | 172 | +                // Check RID-specific native library path  | 
 | 173 | +                if (!isSelfContained)  | 
 | 174 | +                {  | 
 | 175 | +                    (string ridPath, string localRidPath) = GetPaths(libraryType, true);  | 
 | 176 | +                    string expectedRidPath = Path.Join(app.Location, useLocalPath ? localRidPath : ridPath);  | 
 | 177 | +                    result.Should().HaveResolvedNativeLibraryPath(expectedRidPath);  | 
 | 178 | +                    if (useLocalPath)  | 
 | 179 | +                    {  | 
 | 180 | +                        result.Should().NotHaveResolvedNativeLibraryPath(Path.Join(app.Location, ridPath));  | 
 | 181 | +                    }  | 
 | 182 | +                }  | 
 | 183 | +            }  | 
 | 184 | + | 
 | 185 | +            static (string Path, string LocalPath) GetPaths(NetCoreAppBuilder.RuntimeLibraryType libraryType, bool useRid)  | 
 | 186 | +            {  | 
 | 187 | +                string path = useRid ? $"native/{TestContext.BuildRID}" : "native";  | 
 | 188 | +                return (path, $"{libraryType}/{path}");  | 
 | 189 | +            }  | 
 | 190 | +        }  | 
 | 191 | + | 
 | 192 | +        private void ResourceAssemblies(bool isSelfContained, bool useLocalPath)  | 
 | 193 | +        {  | 
 | 194 | +            NetCoreAppBuilder.RuntimeLibraryType[] libraryTypes = [NetCoreAppBuilder.RuntimeLibraryType.project, NetCoreAppBuilder.RuntimeLibraryType.package, NetCoreAppBuilder.RuntimeLibraryType.runtimepack];  | 
 | 195 | + | 
 | 196 | +            Action<NetCoreAppBuilder> customizer = b =>  | 
 | 197 | +            {  | 
 | 198 | +                foreach (var libraryType in libraryTypes)  | 
 | 199 | +                {  | 
 | 200 | +                    string library = $"Test{libraryType}";  | 
 | 201 | +                    (string path, string localPath) = GetPaths(libraryType);  | 
 | 202 | +                    b.WithRuntimeLibrary(libraryType, library, "1.0.0", p => p  | 
 | 203 | +                        .WithResourceAssembly($"{path}/fr/{library}.resources.dll", useLocalPath ? f => f.WithLocalPath($"{localPath}/fr/{library}.resources.dll") : null));  | 
 | 204 | +                }  | 
 | 205 | + | 
 | 206 | +                b.WithLocalPathsInDepsJson(useLocalPath);  | 
 | 207 | +            };  | 
 | 208 | + | 
 | 209 | +            using TestApp app = CreateApp(isSelfContained, customizer);  | 
 | 210 | +            var result = sharedState.DotNetWithNetCoreApp.Exec(app.AppDll)  | 
 | 211 | +                .EnableTracingAndCaptureOutputs()  | 
 | 212 | +                .Execute();  | 
 | 213 | +            result.Should().Pass();  | 
 | 214 | + | 
 | 215 | +            // Check all library types  | 
 | 216 | +            foreach (var libraryType in libraryTypes)  | 
 | 217 | +            {  | 
 | 218 | +                (string path, string localPath) = GetPaths(libraryType);  | 
 | 219 | + | 
 | 220 | +                // Without localPath, non-runtimepack resource assemblies are assumed to be in <app_directory>/<locale>/  | 
 | 221 | +                string relativePath = useLocalPath  | 
 | 222 | +                    ? localPath  | 
 | 223 | +                    : libraryType == RuntimeLibraryType.runtimepack ? path : string.Empty;  | 
 | 224 | +                string expectedPath = Path.Join(app.Location, relativePath);  | 
 | 225 | +                result.Should().HaveResolvedResourceRootPath(expectedPath);  | 
 | 226 | +                if (useLocalPath)  | 
 | 227 | +                {  | 
 | 228 | +                    result.Should().NotHaveResolvedResourceRootPath(Path.Join(app.Location, path));  | 
 | 229 | +                }  | 
 | 230 | +            }  | 
 | 231 | + | 
 | 232 | +            static (string Path, string LocalPath) GetPaths(NetCoreAppBuilder.RuntimeLibraryType libraryType)  | 
 | 233 | +            {  | 
 | 234 | +                string path = $"resources";  | 
 | 235 | +                return (path, $"{libraryType}/{path}");  | 
 | 236 | +            }  | 
 | 237 | +        }  | 
 | 238 | + | 
 | 239 | +        private static TestApp CreateApp(bool isSelfContained, Action<NetCoreAppBuilder> customizer)  | 
 | 240 | +        {  | 
 | 241 | +            TestApp app = TestApp.CreateEmpty("App");  | 
 | 242 | +            if (isSelfContained)  | 
 | 243 | +            {  | 
 | 244 | +                app.PopulateSelfContained(TestApp.MockedComponent.CoreClr, customizer);  | 
 | 245 | +            }  | 
 | 246 | +            else  | 
 | 247 | +            {  | 
 | 248 | +                app.PopulateFrameworkDependent(Constants.MicrosoftNETCoreApp, TestContext.MicrosoftNETCoreAppVersion, customizer);  | 
 | 249 | +            }  | 
 | 250 | +            return app;  | 
 | 251 | +        }  | 
 | 252 | + | 
 | 253 | +        public class SharedTestState : SharedTestStateBase  | 
 | 254 | +        {  | 
 | 255 | +            public DotNetCli DotNetWithNetCoreApp { get; }  | 
 | 256 | + | 
 | 257 | +            public SharedTestState()  | 
 | 258 | +            {  | 
 | 259 | +                DotNetWithNetCoreApp = DotNet("WithNetCoreApp")  | 
 | 260 | +                    .AddMicrosoftNETCoreAppFrameworkMockCoreClr(TestContext.MicrosoftNETCoreAppVersion)  | 
 | 261 | +                    .Build();  | 
 | 262 | +            }  | 
 | 263 | +        }  | 
 | 264 | +    }  | 
 | 265 | +}  | 
0 commit comments