From 3f4c263670e915742af8bbed70656571356842fd Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 25 Feb 2016 11:00:48 -0800 Subject: [PATCH 01/10] Add support for json deps file --- .../DependencyContext.cs | 14 ++++++++- .../RuntimeLibrary.cs | 29 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs index b1cb8ccf7..f1caa7346 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs @@ -15,13 +15,21 @@ namespace Microsoft.Extensions.DependencyModel private static readonly Lazy _defaultContext = new Lazy(LoadDefault); - public DependencyContext(string target, string runtime, CompilationOptions compilationOptions, CompilationLibrary[] compileLibraries, RuntimeLibrary[] runtimeLibraries) + public DependencyContext(string target, + string runtime, + bool isPortable, + CompilationOptions compilationOptions, + CompilationLibrary[] compileLibraries, + RuntimeLibrary[] runtimeLibraries, + IReadOnlyList> runtimeGraph) { Target = target; Runtime = runtime; + IsPortable = isPortable; CompilationOptions = compilationOptions; CompileLibraries = compileLibraries; RuntimeLibraries = runtimeLibraries; + RuntimeGraph = runtimeGraph; } public static DependencyContext Default => _defaultContext.Value; @@ -30,12 +38,16 @@ namespace Microsoft.Extensions.DependencyModel public string Runtime { get; } + public bool IsPortable { get; } + public CompilationOptions CompilationOptions { get; } public IReadOnlyList CompileLibraries { get; } public IReadOnlyList RuntimeLibraries { get; } + public IReadOnlyList> RuntimeGraph { get; } + private static DependencyContext LoadDefault() { var entryAssembly = Assembly.GetEntryAssembly(); diff --git a/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs b/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs index 39369c6d5..02b3df530 100644 --- a/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs +++ b/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs @@ -8,12 +8,39 @@ namespace Microsoft.Extensions.DependencyModel { public class RuntimeLibrary : Library { - public RuntimeLibrary(string libraryType, string packageName, string version, string hash, string[] assemblies, Dependency[] dependencies, bool serviceable) + public RuntimeLibrary( + string libraryType, + string packageName, + string version, + string hash, + string[] assemblies, + RuntimeTarget[] subTargets, + Dependency[] dependencies, + bool serviceable) : base(libraryType, packageName, version, hash, dependencies, serviceable) { Assemblies = assemblies.Select(path => new RuntimeAssembly(path)).ToArray(); + SubTargets = subTargets; } public IReadOnlyList Assemblies { get; } + + public IReadOnlyList SubTargets { get; } + } + + public class RuntimeTarget + { + public RuntimeTarget(string runtime, IReadOnlyList assemblies, IReadOnlyList nativeLibraries) + { + Runtime = runtime; + Assemblies = assemblies; + NativeLibraries = nativeLibraries; + } + + public string Runtime { get; } + + public IReadOnlyList Assemblies { get; } + + public IReadOnlyList NativeLibraries { get; } } } \ No newline at end of file From 9bd9ca15122d8cafe600fff4c5f9ce57b08c0a0c Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 1 Mar 2016 15:11:52 -0800 Subject: [PATCH 02/10] Add more tests and some reading logic --- .../DependencyContextBuilder.cs | 6 +- .../DependencyContext.cs | 2 +- .../DependencyContextCsvReader.cs | 5 +- .../DependencyContextJsonReader.cs | 115 +++++++++++--- .../DependencyContextStrings.cs | 12 +- .../DependencyContextWriter.cs | 27 +++- .../DependencyContextJsonReaderTest.cs | 141 ++++++++++++++++++ .../DependencyContextJsonWriterTests.cs | 102 +++++++++++++ .../FileSystemMockBuilder.cs | 94 ------------ .../JsonAssetions.cs | 49 ++++++ 10 files changed, 433 insertions(+), 120 deletions(-) create mode 100644 test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs create mode 100644 test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs delete mode 100644 test/Microsoft.Extensions.DependencyModel.Tests/FileSystemMockBuilder.cs create mode 100644 test/Microsoft.Extensions.DependencyModel.Tests/JsonAssetions.cs diff --git a/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs b/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs index 2411ce72b..286d7d492 100644 --- a/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs +++ b/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs @@ -28,10 +28,11 @@ namespace Microsoft.Extensions.DependencyModel .Select(export => new Dependency(export.Library.Identity.Name, export.Library.Identity.Version.ToString())) .ToDictionary(dependency => dependency.Name); - return new DependencyContext(target.DotNetFrameworkName, runtime, + return new DependencyContext(target.DotNetFrameworkName, runtime, false, GetCompilationOptions(compilerOptions), GetLibraries(dependencies, dependencyLookup, target, configuration, runtime: false).Cast().ToArray(), - GetLibraries(dependencies, dependencyLookup, target, configuration, runtime: true).Cast().ToArray()); + GetLibraries(dependencies, dependencyLookup, target, configuration, runtime: true).Cast().ToArray(), + new KeyValuePair[0]); } private static CompilationOptions GetCompilationOptions(CommonCompilerOptions compilerOptions) @@ -115,6 +116,7 @@ namespace Microsoft.Extensions.DependencyModel export.Library.Identity.Version.ToString(), export.Library.Hash, assemblies, + new RuntimeTarget[0], libraryDependencies.ToArray(), serviceable ); diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs index f1caa7346..b2e44d397 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs @@ -46,7 +46,7 @@ namespace Microsoft.Extensions.DependencyModel public IReadOnlyList RuntimeLibraries { get; } - public IReadOnlyList> RuntimeGraph { get; } + public IReadOnlyList> RuntimeGraph { get; } private static DependencyContext LoadDefault() { diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs index d466bd15c..74419e466 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs @@ -47,6 +47,7 @@ namespace Microsoft.Extensions.DependencyModel version: identity.Item3, hash: identity.Item4, assemblies: packageGroup.Select(l => l.AssetPath).ToArray(), + subTargets: new RuntimeTarget[0], dependencies: new Dependency[] { }, serviceable: false )); @@ -55,9 +56,11 @@ namespace Microsoft.Extensions.DependencyModel return new DependencyContext( target: string.Empty, runtime: string.Empty, + isPortable: false, compilationOptions: CompilationOptions.Default, compileLibraries: new CompilationLibrary[] {}, - runtimeLibraries: runtimeLibraries.ToArray()); + runtimeLibraries: runtimeLibraries.ToArray(), + runtimeGraph: new KeyValuePair[0]); } private Tuple PackageIdentity(DepsFileLine line) diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs index 558878b35..5c5ff4188 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs @@ -28,23 +28,88 @@ namespace Microsoft.Extensions.DependencyModel private DependencyContext Read(JObject root) { + string runtime = null; + string target = null; + + var runtimeTargetInfo = ReadRuntimeTargetInfo(root); var libraryStubs = ReadLibraryStubs((JObject) root[DependencyContextStrings.LibrariesPropertyName]); var targetsObject = (IEnumerable>) root[DependencyContextStrings.TargetsPropertyName]; - var runtimeTargetProperty = targetsObject.First(target => IsRuntimeTarget(target.Key)); - var compileTargetProperty = targetsObject.First(target => !IsRuntimeTarget(target.Key)); + JObject runtimeTarget = null; + JObject compileTarget = null; + if (targetsObject != null) + { + var compileTargetProperty = targetsObject.FirstOrDefault(t => !IsRuntimeTarget(t.Key)); + compileTarget = (JObject) compileTargetProperty.Value; + target = compileTargetProperty.Key; + if (!string.IsNullOrEmpty(runtimeTargetInfo.Name)) + { + runtimeTarget = (JObject) targetsObject.FirstOrDefault(t => t.Key == runtimeTargetInfo.Name).Value; + if (runtimeTarget == null) + { + throw new FormatException($"Target with name {runtimeTargetInfo.Name} not found"); + } + runtime = runtimeTargetInfo.Name.Substring(target.Length + 1); + } + else + { + runtimeTarget = compileTarget; + } + + } + return new DependencyContext( - compileTargetProperty.Key, - runtimeTargetProperty.Key.Substring(compileTargetProperty.Key.Length + 1), + target, + runtime, + runtimeTargetInfo.Portable, ReadCompilationOptions((JObject)root[DependencyContextStrings.CompilationOptionsPropertName]), - ReadLibraries((JObject)compileTargetProperty.Value, false, libraryStubs).Cast().ToArray(), - ReadLibraries((JObject)runtimeTargetProperty.Value, true, libraryStubs).Cast().ToArray() + ReadLibraries(compileTarget, false, libraryStubs).Cast().ToArray(), + ReadLibraries(runtimeTarget, true, libraryStubs).Cast().ToArray(), + ReadRuntimeGraph((JObject)root[DependencyContextStrings.RuntimesPropertyName]).ToArray() ); } + private IEnumerable> ReadRuntimeGraph(JObject runtimes) + { + if (runtimes == null) + { + yield break; + } + + var targets = runtimes.Children(); + var runtime = (JProperty)targets.Single(); + foreach (var pair in (JObject)runtime.Value) + { + yield return new KeyValuePair(pair.Key, pair.Value.Values().ToArray()); + } + } + + private RuntimeTargetInfo ReadRuntimeTargetInfo(JObject root) + { + + var runtimeTarget = (JObject)root[DependencyContextStrings.RuntimeTargetPropertyName]; + if (runtimeTarget != null) + { + return new RuntimeTargetInfo() + { + Name = runtimeTarget[DependencyContextStrings.RuntimeTargetNamePropertyName]?.Value(), + Portable = runtimeTarget[DependencyContextStrings.PortablePropertyName]?.Value() == true + }; + } + return new RuntimeTargetInfo() + { + Portable = true + }; + } + private CompilationOptions ReadCompilationOptions(JObject compilationOptionsObject) { + if (compilationOptionsObject == null) + { + return CompilationOptions.Default; + } + return new CompilationOptions( compilationOptionsObject[DependencyContextStrings.DefinesPropertyName]?.Values(), compilationOptionsObject[DependencyContextStrings.LanguageVersionPropertyName]?.Value(), @@ -63,6 +128,10 @@ namespace Microsoft.Extensions.DependencyModel private IEnumerable ReadLibraries(JObject librariesObject, bool runtime, Dictionary libraryStubs) { + if (librariesObject == null) + { + return Enumerable.Empty(); + } return librariesObject.Properties().Select(property => ReadLibrary(property, runtime, libraryStubs)); } @@ -88,7 +157,7 @@ namespace Microsoft.Extensions.DependencyModel if (runtime) { - return new RuntimeLibrary(stub.Type, name, version, stub.Hash, assemblies, dependencies, stub.Serviceable); + return new RuntimeLibrary(stub.Type, name, version, stub.Hash, assemblies, new RuntimeTarget[0], dependencies, stub.Serviceable); } else { @@ -98,7 +167,7 @@ namespace Microsoft.Extensions.DependencyModel private static string[] ReadAssemblies(JObject libraryObject, bool runtime) { - var assembliesObject = (JObject) libraryObject[runtime ? DependencyContextStrings.RunTimeAssembliesKey : DependencyContextStrings.CompileTimeAssembliesKey]; + var assembliesObject = (JObject) libraryObject[runtime ? DependencyContextStrings.RuntimeAssembliesKey : DependencyContextStrings.CompileTimeAssembliesKey]; if (assembliesObject == null) { @@ -110,7 +179,7 @@ namespace Microsoft.Extensions.DependencyModel private static Dependency[] ReadDependencies(JObject libraryObject) { - var dependenciesObject = ((JObject) libraryObject[DependencyContextStrings.DependenciesPropertyName]); + var dependenciesObject = (JObject) libraryObject[DependencyContextStrings.DependenciesPropertyName]; if (dependenciesObject == null) { @@ -124,21 +193,31 @@ namespace Microsoft.Extensions.DependencyModel private Dictionary ReadLibraryStubs(JObject librariesObject) { var libraries = new Dictionary(); - foreach (var libraryProperty in librariesObject) + if (librariesObject != null) { - var value = (JObject) libraryProperty.Value; - var stub = new LibraryStub + foreach (var libraryProperty in librariesObject) { - Name = libraryProperty.Key, - Hash = value[DependencyContextStrings.Sha512PropertyName]?.Value(), - Type = value[DependencyContextStrings.TypePropertyName].Value(), - Serviceable = value[DependencyContextStrings.ServiceablePropertyName]?.Value() == true - }; - libraries.Add(stub.Name, stub); + var value = (JObject) libraryProperty.Value; + var stub = new LibraryStub + { + Name = libraryProperty.Key, + Hash = value[DependencyContextStrings.Sha512PropertyName]?.Value(), + Type = value[DependencyContextStrings.TypePropertyName].Value(), + Serviceable = value[DependencyContextStrings.ServiceablePropertyName]?.Value() == true + }; + libraries.Add(stub.Name, stub); + } } return libraries; } + private struct RuntimeTargetInfo + { + public string Name; + + public bool Portable; + } + private struct LibraryStub { public string Name; diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs index a3831d0be..ef3416f94 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs @@ -9,7 +9,9 @@ namespace Microsoft.Extensions.DependencyModel internal const string CompileTimeAssembliesKey = "compile"; - internal const string RunTimeAssembliesKey = "runtime"; + internal const string RuntimeAssembliesKey = "runtime"; + + internal const string RuntimeTargetPropertyName = "runtimeTarget"; internal const string LibrariesPropertyName = "libraries"; @@ -48,5 +50,11 @@ namespace Microsoft.Extensions.DependencyModel internal const string EmitEntryPointPropertyName = "emitEntryPoint"; internal const string GenerateXmlDocumentationPropertyName = "xmlDoc"; + + internal const string PortablePropertyName = "portable"; + + internal const string RuntimeTargetNamePropertyName = "name"; + + internal const string RuntimesPropertyName = "runtimes"; } -} +} \ No newline at end of file diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs index dca0e08b0..d5b1301ce 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using Microsoft.Extensions.PlatformAbstractions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -26,9 +27,31 @@ namespace Microsoft.Extensions.DependencyModel private JObject Write(DependencyContext context) { return new JObject( + new JProperty(DependencyContextStrings.RuntimeTargetPropertyName, WriteRuntimeTargetInfo(context)), new JProperty(DependencyContextStrings.CompilationOptionsPropertName, WriteCompilationOptions(context.CompilationOptions)), new JProperty(DependencyContextStrings.TargetsPropertyName, WriteTargets(context)), - new JProperty(DependencyContextStrings.LibrariesPropertyName, WriteLibraries(context)) + new JProperty(DependencyContextStrings.LibrariesPropertyName, WriteLibraries(context)), + new JProperty(DependencyContextStrings.RuntimesPropertyName, WriteRuntimeGraph(context)) + ); + } + + private JObject WriteRuntimeTargetInfo(DependencyContext context) + { + return new JObject( + new JProperty(DependencyContextStrings.RuntimeTargetNamePropertyName, + context.Target + DependencyContextStrings.VersionSeperator + context.Runtime), + new JProperty(DependencyContextStrings.PortablePropertyName, context.IsPortable) + ); + } + + private JObject WriteRuntimeGraph(DependencyContext context) + { + return new JObject( + new JProperty(context.Target, + new JObject( + context.RuntimeGraph.Select(g => new JProperty(g.Key, new JArray(g.Value))) + ) + ) ); } @@ -106,7 +129,7 @@ namespace Microsoft.Extensions.DependencyModel var runtimeLibrary = library as RuntimeLibrary; if (runtimeLibrary != null) { - propertyName = DependencyContextStrings.RunTimeAssembliesKey; + propertyName = DependencyContextStrings.RuntimeAssembliesKey; assemblies = runtimeLibrary.Assemblies.Select(assembly => assembly.Path).ToArray(); } else diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs new file mode 100644 index 000000000..c6013379f --- /dev/null +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace Microsoft.Extensions.DependencyModel.Tests +{ + public class DependencyContextJsonReaderTest + { + private DependencyContext Read(string text) + { + using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text))) + { + return new DependencyContextJsonReader().Read(stream); + } + } + + [Fact] + public void ReadsRuntimeTargetInfo() + { + var context = Read( +@"{ + ""runtimeTarget"": { + ""portable"": false, + ""name"": "".NETStandardApp,Version=v1.5/osx.10.10-x64"", + }, + ""targets"": { + "".NETStandardApp,Version=v1.5"": {}, + "".NETStandardApp,Version=v1.5/osx.10.10-x64"": {}, + } +}"); + context.IsPortable.Should().BeFalse(); + context.Target.Should().Be(".NETStandardApp,Version=v1.5"); + context.Runtime.Should().Be("osx.10.10-x64"); + } + + [Fact] + public void DefaultsToPortable() + { + var context = Read( +@"{ +}"); + context.IsPortable.Should().BeTrue(); + } + + [Fact] + public void ReadsMainTarget() + { + var context = Read( +@"{ + ""targets"": { + "".NETStandardApp,Version=v1.5"": {} + } +}"); + context.Target.Should().Be(".NETStandardApp,Version=v1.5"); + } + + [Fact] + public void ReadsRuntimeGraph() + { + var context = Read( +@"{ + ""runtimes"": { + "".NETStandardApp,Version=v1.5"": { + ""osx.10.10-x64"": [ ], + ""osx.10.11-x64"": [ ""osx"" ], + ""rhel.7-x64"": [ ""linux-x64"", ""unix"" ] + } + } +}"); + context.RuntimeGraph.Should().Contain(p => p.Key == "osx.10.10-x64").Which + .Value.Should().BeEquivalentTo(); + + context.RuntimeGraph.Should().Contain(p => p.Key == "osx.10.11-x64").Which + .Value.Should().BeEquivalentTo("osx"); + + context.RuntimeGraph.Should().Contain(p => p.Key == "rhel.7-x64").Which + .Value.Should().BeEquivalentTo("linux-x64", "unix"); + } + + [Fact] + public void ReadsCompilationTarget() + { + var context = Read( +@"{ + ""targets"": { + "".NETStandardApp,Version=v1.5"": { + ""MyApp/1.0.1"": { + ""dependencies"": { + ""AspNet.Mvc"": ""1.0.0"" + }, + ""compile"": { + ""MyApp.dll"": { } + } + }, + ""System.Banana/1.0.0"": { + ""dependencies"": { + ""System.Foo"": ""1.0.0"" + }, + ""compile"": { + ""ref/dotnet5.4/System.Banana.dll"": { } + } + } + } + }, + ""libraries"":{ + ""MyApp/1.0.1"": { + ""type"": ""project"", + ""serviceable"": true, + ""sha512"": ""HASH-MyApp"" + }, + ""System.Banana/1.0.0"": { + ""type"": ""package"", + ""serviceable"": false, + ""sha512"": ""HASH-System.Banana"" + }, + } +}"); + context.CompileLibraries.Should().HaveCount(2); + var project = context.CompileLibraries.Should().Contain(l => l.PackageName == "MyApp").Subject; + project.Version.Should().Be("1.0.1"); + project.Assemblies.Should().BeEquivalentTo("MyApp.dll"); + project.Hash.Should().Be("HASH-MyApp"); + project.LibraryType.Should().Be("project"); + project.Serviceable.Should().Be(true); + project.Hash.Should().BeEquivalentTo("HASH-MyApp"); + + + var package = context.CompileLibraries.Should().Contain(l => l.PackageName == "System.Banana").Subject; + package.Version.Should().Be("1.0.0"); + package.Assemblies.Should().BeEquivalentTo("ref/dotnet5.4/System.Banana.dll"); + package.Hash.Should().Be("HASH-System.Banana"); + package.LibraryType.Should().Be("package"); + package.Serviceable.Should().Be(false); + } + } +} diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs new file mode 100644 index 000000000..f6e1340b3 --- /dev/null +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Xunit; +using FluentAssertions; + +namespace Microsoft.Extensions.DependencyModel.Tests +{ + public class DependencyContextJsonWriterTests + { + public JObject Save(DependencyContext dependencyContext) + { + using (var memoryStream = new MemoryStream()) + { + new DependencyContextWriter().Write(dependencyContext, memoryStream); + using (var readStream = new MemoryStream(memoryStream.ToArray())) + { + using (var textReader = new StreamReader(readStream)) + { + using (var reader = new JsonTextReader(textReader)) + { + return JObject.Load(reader); + } + } + } + } + } + + [Fact] + public void SavesRuntimeGraph() + { + var result = Save(new DependencyContext( + "Target", + "Target/runtime", + false, + CompilationOptions.Default, + new CompilationLibrary[0], + new RuntimeLibrary[0], + new[] + { + new KeyValuePair("win7-x64", new [] { "win6", "win5"}), + new KeyValuePair("win8-x64", new [] { "win7-x64"}), + })); + + var runtimes = result.Should().HaveProperty("runtimes") + .Subject.Should().BeOfType().Subject; + + var rids = runtimes.Should().HaveProperty("Target") + .Subject.Should().BeOfType().Subject; + + rids.Should().HaveProperty("win7-x64") + .Subject.Should().BeOfType() + .Which.Values().ShouldBeEquivalentTo(new[] { "win6", "win5" }); + + rids.Should().HaveProperty("win8-x64") + .Subject.Should().BeOfType() + .Which.Values().ShouldBeEquivalentTo(new[] { "win7-x64" }); + } + + [Fact] + public void WritesRuntimeTargetPropertyIfNotPortable() + { + var result = Save(new DependencyContext( + "Target", + "runtime", + false, + CompilationOptions.Default, + new CompilationLibrary[0], + new RuntimeLibrary[0], + new KeyValuePair[0]) + ); + + var runtimeTarget = result.Should().HaveProperty("runtimeTarget") + .Subject.Should().BeOfType().Subject; + + runtimeTarget.Should().HaveProperty("name") + .Subject.Value().Should().Be("Target/runtime"); + + runtimeTarget.Should().HaveProperty("portable") + .Subject.Value().Should().Be(false); + } + [Fact] + public void DoesNotWritesRuntimeTargetPropertyIfPortable() + { + var result = Save(new DependencyContext( + "Target", + "runtime", + false, + CompilationOptions.Default, + new CompilationLibrary[0], + new RuntimeLibrary[0], + new KeyValuePair[0]) + ); + + result.Should().NotHaveProperty("runtimeTarget"); + } + } +} diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/FileSystemMockBuilder.cs b/test/Microsoft.Extensions.DependencyModel.Tests/FileSystemMockBuilder.cs deleted file mode 100644 index 48886c69e..000000000 --- a/test/Microsoft.Extensions.DependencyModel.Tests/FileSystemMockBuilder.cs +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Linq; -using System.Collections.Generic; -using System.IO; -using Microsoft.Extensions.EnvironmentAbstractions; - -namespace Microsoft.Extensions.DependencyModel.Tests -{ - class FileSystemMockBuilder - { - private Dictionary _files = new Dictionary(); - - public static IFileSystem Empty { get; } = Create().Build(); - - public static FileSystemMockBuilder Create() - { - return new FileSystemMockBuilder(); - } - - public FileSystemMockBuilder AddFile(string name, string content = "") - { - _files.Add(name, content); - return this; - } - - public FileSystemMockBuilder AddFiles(string basePath, params string[] files) - { - foreach (var file in files) - { - AddFile(Path.Combine(basePath, file)); - } - return this; - } - - public IFileSystem Build() - { - return new FileSystemMock(_files); - } - - private class FileSystemMock : IFileSystem - { - public FileSystemMock(Dictionary files) - { - File = new FileMock(files); - Directory = new DirectoryMock(files); - } - - public IFile File { get; } - - public IDirectory Directory { get; } - } - - private class FileMock : IFile - { - private Dictionary _files; - public FileMock(Dictionary files) - { - _files = files; - } - - public bool Exists(string path) - { - return _files.ContainsKey(path); - } - - public string ReadAllText(string path) - { - string text; - if (!_files.TryGetValue(path, out text)) - { - throw new FileNotFoundException(path); - } - return text; - } - } - - private class DirectoryMock : IDirectory - { - private Dictionary _files; - public DirectoryMock(Dictionary files) - { - _files = files; - } - - public bool Exists(string path) - { - return _files.Keys.Any(k => k.StartsWith(path)); - } - } - } - -} diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/JsonAssetions.cs b/test/Microsoft.Extensions.DependencyModel.Tests/JsonAssetions.cs new file mode 100644 index 000000000..d455415ee --- /dev/null +++ b/test/Microsoft.Extensions.DependencyModel.Tests/JsonAssetions.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using FluentAssertions; +using FluentAssertions.Execution; +using FluentAssertions.Primitives; +using Newtonsoft.Json.Linq; + +namespace Microsoft.Extensions.DependencyModel.Tests +{ + public static class JsonAssertionExtensions + { + public static JsonAssetions Should(this JToken jToken) + { + return new JsonAssetions(jToken); + } + } + + public class JsonAssetions: ReferenceTypeAssertions + { + public JsonAssetions(JToken token) + { + Subject = token; + } + + protected override string Context => nameof(JToken); + + public AndWhichConstraint HaveProperty(string expected) + { + var token = Subject[expected]; + Execute.Assertion + .ForCondition(token != null) + .FailWith($"Expected {Subject} to have property '" + expected + "'"); + + return new AndWhichConstraint(this, token); + } + + public AndConstraint NotHaveProperty(string expected) + { + var token = Subject[expected]; + Execute.Assertion + .ForCondition(token == null) + .FailWith($"Expected {Subject} not to have property '" + expected + "'"); + + return new AndConstraint(this); + } + } +} From dcaea8c7cab7e856e2cde06b855e9fc536f04641 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Wed, 2 Mar 2016 15:31:13 -0800 Subject: [PATCH 03/10] More work and tests --- .../DependencyContextBuilder.cs | 4 +- .../Dependency.cs | 21 +++ .../DependencyContextCsvReader.cs | 4 +- .../DependencyContextJsonReader.cs | 69 +++++++- .../DependencyContextStrings.cs | 10 ++ .../DependencyContextWriter.cs | 60 ++++++- .../RuntimeAssembly.cs | 10 +- .../RuntimeLibrary.cs | 20 +-- .../RuntimeTarget.cs | 20 +++ .../project.json | 4 + .../DependencyContextJsonReaderTest.cs | 74 ++++++++- .../DependencyContextJsonWriterTests.cs | 152 +++++++++++++++--- .../JsonAssetions.cs | 10 ++ 13 files changed, 389 insertions(+), 69 deletions(-) create mode 100644 src/Microsoft.Extensions.DependencyModel/RuntimeTarget.cs diff --git a/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs b/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs index 286d7d492..3f326f096 100644 --- a/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs +++ b/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs @@ -115,8 +115,8 @@ namespace Microsoft.Extensions.DependencyModel export.Library.Identity.Name, export.Library.Identity.Version.ToString(), export.Library.Hash, - assemblies, - new RuntimeTarget[0], + assemblies.Select(RuntimeAssembly.Create).ToArray(), + new RuntimeTarget[0], libraryDependencies.ToArray(), serviceable ); diff --git a/src/Microsoft.Extensions.DependencyModel/Dependency.cs b/src/Microsoft.Extensions.DependencyModel/Dependency.cs index 7950220af..8060cb4b1 100644 --- a/src/Microsoft.Extensions.DependencyModel/Dependency.cs +++ b/src/Microsoft.Extensions.DependencyModel/Dependency.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using Microsoft.Extensions.Internal; + namespace Microsoft.Extensions.DependencyModel { public struct Dependency @@ -13,5 +15,24 @@ namespace Microsoft.Extensions.DependencyModel public string Name { get; } public string Version { get; } + + public bool Equals(Dependency other) + { + return string.Equals(Name, other.Name) && string.Equals(Version, other.Version); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is Dependency && Equals((Dependency) obj); + } + + public override int GetHashCode() + { + var combiner = HashCodeCombiner.Start(); + combiner.Add(Name); + combiner.Add(Version); + return combiner.CombinedHash; + } } } \ No newline at end of file diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs index 74419e466..db2ab933c 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs @@ -46,8 +46,8 @@ namespace Microsoft.Extensions.DependencyModel packageName: identity.Item2, version: identity.Item3, hash: identity.Item4, - assemblies: packageGroup.Select(l => l.AssetPath).ToArray(), - subTargets: new RuntimeTarget[0], + assemblies: packageGroup.Select(l => RuntimeAssembly.Create(l.AssetPath)).ToArray(), + subTargets: new RuntimeTarget[0], dependencies: new Dependency[] { }, serviceable: false )); diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs index 5c5ff4188..6853a7b38 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs @@ -50,15 +50,19 @@ namespace Microsoft.Extensions.DependencyModel { throw new FormatException($"Target with name {runtimeTargetInfo.Name} not found"); } - runtime = runtimeTargetInfo.Name.Substring(target.Length + 1); + + var seperatorIndex = runtimeTargetInfo.Name.IndexOf(DependencyContextStrings.VersionSeperator); + if (seperatorIndex > -1 && seperatorIndex < runtimeTargetInfo.Name.Length) + { + runtime = runtimeTargetInfo.Name.Substring(seperatorIndex + 1); + } } else { runtimeTarget = compileTarget; } - } - + return new DependencyContext( target, runtime, @@ -153,21 +157,65 @@ namespace Microsoft.Extensions.DependencyModel var libraryObject = (JObject) property.Value; var dependencies = ReadDependencies(libraryObject); - var assemblies = ReadAssemblies(libraryObject, runtime); if (runtime) { - return new RuntimeLibrary(stub.Type, name, version, stub.Hash, assemblies, new RuntimeTarget[0], dependencies, stub.Serviceable); + var runtimeTargets = new List(); + var runtimeTargetsObject = (JObject)libraryObject[DependencyContextStrings.RuntimeTargetsPropertyName]; + + var entries = ReadRuntimeTargetEntries(runtimeTargetsObject).ToArray(); + + foreach (var ridGroup in entries.GroupBy(e => e.Rid)) + { + var runtimeAssets = entries.Where(e => e.Type == DependencyContextStrings.RuntimeAssetType) + .Select(e => RuntimeAssembly.Create(e.Path)) + .ToArray(); + + var nativeAssets = entries.Where(e => e.Type == DependencyContextStrings.NativeAssetType) + .Select(e => e.Path) + .ToArray(); + + runtimeTargets.Add(new RuntimeTarget( + ridGroup.Key, + runtimeAssets, + nativeAssets + )); + } + + var assemblies = ReadAssemblies(libraryObject, DependencyContextStrings.RuntimeAssembliesKey) + .Select(RuntimeAssembly.Create) + .ToArray(); + + return new RuntimeLibrary(stub.Type, name, version, stub.Hash, assemblies, runtimeTargets.ToArray(), dependencies, stub.Serviceable); } else { + var assemblies = ReadAssemblies(libraryObject, DependencyContextStrings.CompileTimeAssembliesKey); return new CompilationLibrary(stub.Type, name, version, stub.Hash, assemblies, dependencies, stub.Serviceable); } } - private static string[] ReadAssemblies(JObject libraryObject, bool runtime) + private static IEnumerable ReadRuntimeTargetEntries(JObject runtimeTargetObject) { - var assembliesObject = (JObject) libraryObject[runtime ? DependencyContextStrings.RuntimeAssembliesKey : DependencyContextStrings.CompileTimeAssembliesKey]; + if (runtimeTargetObject == null) + { + yield break; + } + foreach (var libraryProperty in runtimeTargetObject) + { + var libraryObject = (JObject)libraryProperty.Value; + yield return new RuntimeTargetEntryStub() + { + Path = libraryProperty.Key, + Rid = libraryObject[DependencyContextStrings.RidPropertyName].Value(), + Type = libraryObject[DependencyContextStrings.AssetTypePropertyName].Value() + }; + } + } + + private static string[] ReadAssemblies(JObject libraryObject, string name) + { + var assembliesObject = (JObject) libraryObject[name]; if (assembliesObject == null) { @@ -211,6 +259,13 @@ namespace Microsoft.Extensions.DependencyModel return libraries; } + private struct RuntimeTargetEntryStub + { + public string Type; + public string Path; + public string Rid; + } + private struct RuntimeTargetInfo { public string Name; diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs index ef3416f94..e42a850b5 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextStrings.cs @@ -56,5 +56,15 @@ namespace Microsoft.Extensions.DependencyModel internal const string RuntimeTargetNamePropertyName = "name"; internal const string RuntimesPropertyName = "runtimes"; + + internal const string RuntimeTargetsPropertyName = "runtimeTargets"; + + internal const string RidPropertyName = "rid"; + + internal const string AssetTypePropertyName = "assetType"; + + internal const string RuntimeAssetType = "runtime"; + + internal const string NativeAssetType = "native"; } } \ No newline at end of file diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs index d5b1301ce..639f84f40 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs @@ -37,9 +37,12 @@ namespace Microsoft.Extensions.DependencyModel private JObject WriteRuntimeTargetInfo(DependencyContext context) { + var target = context.IsPortable? + context.Target : + context.Target + DependencyContextStrings.VersionSeperator + context.Runtime; + return new JObject( - new JProperty(DependencyContextStrings.RuntimeTargetNamePropertyName, - context.Target + DependencyContextStrings.VersionSeperator + context.Runtime), + new JProperty(DependencyContextStrings.RuntimeTargetNamePropertyName, target), new JProperty(DependencyContextStrings.PortablePropertyName, context.IsPortable) ); } @@ -107,11 +110,22 @@ namespace Microsoft.Extensions.DependencyModel private JObject WriteTargets(DependencyContext context) { - return new JObject( - new JProperty(context.Target, WriteTarget(context.CompileLibraries)), - new JProperty(context.Target + DependencyContextStrings.VersionSeperator + context.Runtime, - WriteTarget(context.RuntimeLibraries)) - ); + if (context.IsPortable) + { + return new JObject( + new JProperty(context.Target, WriteTarget(context.CompileLibraries)), + new JProperty(context.Target + DependencyContextStrings.VersionSeperator + context.Runtime, + WriteTarget(context.RuntimeLibraries)) + ); + } + else + { + return new JObject( + new JProperty(context.Target, WriteTarget(context.CompileLibraries)), + new JProperty(context.Target + DependencyContextStrings.VersionSeperator + context.Runtime, + WriteTarget(context.RuntimeLibraries)) + ); + } } private JObject WriteTarget(IReadOnlyList libraries) @@ -147,6 +161,38 @@ namespace Microsoft.Extensions.DependencyModel } + return new JObject( + new JProperty(DependencyContextStrings.DependenciesPropertyName, WriteDependencies(library.Dependencies)), + new JProperty(propertyName, + WriteAssemblies(assemblies)) + ); + } + private JObject WritePortableTargetLibrary(RuntimeLibrary compilationLibrary, CompilationLibrary runtimeLibrary) + { + var libraryObject = new JObject(); + + string propertyName; + string[] assemblies; + + if (runtimeLibrary != null) + { + propertyName = DependencyContextStrings.RuntimeAssembliesKey; + assemblies = runtimeLibrary.Assemblies.Select(assembly => assembly.Path).ToArray(); + } + + RuntimeAssembly[] compilationAssemblies; + if (compilationLibrary != null) + { + propertyName = DependencyContextStrings.CompileTimeAssembliesKey; + compilationAssemblies = compilationLibrary.Assemblies.ToArray(); + } + else + { + throw new NotSupportedException(); + } + } + + return new JObject( new JProperty(DependencyContextStrings.DependenciesPropertyName, WriteDependencies(library.Dependencies)), new JProperty(propertyName, diff --git a/src/Microsoft.Extensions.DependencyModel/RuntimeAssembly.cs b/src/Microsoft.Extensions.DependencyModel/RuntimeAssembly.cs index fa1bc066d..dcbee3a14 100644 --- a/src/Microsoft.Extensions.DependencyModel/RuntimeAssembly.cs +++ b/src/Microsoft.Extensions.DependencyModel/RuntimeAssembly.cs @@ -10,11 +10,6 @@ namespace Microsoft.Extensions.DependencyModel { private readonly string _assemblyName; - public RuntimeAssembly(string path) - : this(System.IO.Path.GetFileNameWithoutExtension(path), path) - { - } - public RuntimeAssembly(string assemblyName, string path) { _assemblyName = assemblyName; @@ -24,5 +19,10 @@ namespace Microsoft.Extensions.DependencyModel public AssemblyName Name => new AssemblyName(_assemblyName); public string Path { get; } + + public static RuntimeAssembly Create(string path) + { + return new RuntimeAssembly(System.IO.Path.GetFileNameWithoutExtension(path), path); + } } } \ No newline at end of file diff --git a/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs b/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs index 02b3df530..ad5aaae85 100644 --- a/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs +++ b/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs @@ -13,13 +13,13 @@ namespace Microsoft.Extensions.DependencyModel string packageName, string version, string hash, - string[] assemblies, + RuntimeAssembly[] assemblies, RuntimeTarget[] subTargets, Dependency[] dependencies, bool serviceable) : base(libraryType, packageName, version, hash, dependencies, serviceable) { - Assemblies = assemblies.Select(path => new RuntimeAssembly(path)).ToArray(); + Assemblies = assemblies; SubTargets = subTargets; } @@ -27,20 +27,4 @@ namespace Microsoft.Extensions.DependencyModel public IReadOnlyList SubTargets { get; } } - - public class RuntimeTarget - { - public RuntimeTarget(string runtime, IReadOnlyList assemblies, IReadOnlyList nativeLibraries) - { - Runtime = runtime; - Assemblies = assemblies; - NativeLibraries = nativeLibraries; - } - - public string Runtime { get; } - - public IReadOnlyList Assemblies { get; } - - public IReadOnlyList NativeLibraries { get; } - } } \ No newline at end of file diff --git a/src/Microsoft.Extensions.DependencyModel/RuntimeTarget.cs b/src/Microsoft.Extensions.DependencyModel/RuntimeTarget.cs new file mode 100644 index 000000000..15b5d9d7f --- /dev/null +++ b/src/Microsoft.Extensions.DependencyModel/RuntimeTarget.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; + +namespace Microsoft.Extensions.DependencyModel +{ + public class RuntimeTarget + { + public RuntimeTarget(string runtime, IReadOnlyList assemblies, IReadOnlyList nativeLibraries) + { + Runtime = runtime; + Assemblies = assemblies; + NativeLibraries = nativeLibraries; + } + + public string Runtime { get; } + + public IReadOnlyList Assemblies { get; } + + public IReadOnlyList NativeLibraries { get; } + } +} \ No newline at end of file diff --git a/src/Microsoft.Extensions.DependencyModel/project.json b/src/Microsoft.Extensions.DependencyModel/project.json index c439b0a1b..1ad395b56 100644 --- a/src/Microsoft.Extensions.DependencyModel/project.json +++ b/src/Microsoft.Extensions.DependencyModel/project.json @@ -10,6 +10,10 @@ "keyFile": "../../tools/Key.snk" }, "dependencies": { + "Microsoft.Extensions.HashCodeCombiner.Sources": { + "type": "build", + "version": "1.0.0-rc2-16054" + }, "Microsoft.DotNet.InternalAbstractions": { "target": "project", "version": "1.0.0-*" diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs index c6013379f..6e586ea98 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs @@ -109,9 +109,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests }, ""libraries"":{ ""MyApp/1.0.1"": { - ""type"": ""project"", - ""serviceable"": true, - ""sha512"": ""HASH-MyApp"" + ""type"": ""project"" }, ""System.Banana/1.0.0"": { ""type"": ""package"", @@ -124,11 +122,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests var project = context.CompileLibraries.Should().Contain(l => l.PackageName == "MyApp").Subject; project.Version.Should().Be("1.0.1"); project.Assemblies.Should().BeEquivalentTo("MyApp.dll"); - project.Hash.Should().Be("HASH-MyApp"); project.LibraryType.Should().Be("project"); - project.Serviceable.Should().Be(true); - project.Hash.Should().BeEquivalentTo("HASH-MyApp"); - var package = context.CompileLibraries.Should().Contain(l => l.PackageName == "System.Banana").Subject; package.Version.Should().Be("1.0.0"); @@ -137,5 +131,71 @@ namespace Microsoft.Extensions.DependencyModel.Tests package.LibraryType.Should().Be("package"); package.Serviceable.Should().Be(false); } + + + [Fact] + public void ReadsRuntimeLibrariesWithSubtargetsFromMainTargetForPortable() + { + var context = Read( +@"{ + ""runtimeTarget"": { + ""portable"": true, + ""name"": "".NETStandardApp,Version=v1.5"", + }, + ""targets"": { + "".NETStandardApp,Version=v1.5"": { + ""MyApp/1.0.1"": { + ""dependencies"": { + ""AspNet.Mvc"": ""1.0.0"" + }, + ""runtime"": { + ""MyApp.dll"": { } + } + }, + ""System.Banana/1.0.0"": { + ""dependencies"": { + ""System.Foo"": ""1.0.0"" + }, + ""runtime"": { + ""lib/dotnet5.4/System.Banana.dll"": { } + }, + ""runtimeTargets"": { + ""lib/win7/System.Banana.dll"": { ""assetType"": ""runtime"", ""rid"": ""win7-x64""}, + ""lib/win7/Banana.dll"": { ""assetType"": ""native"", ""rid"": ""win7-x64""} + } + } + } + }, + ""libraries"":{ + ""MyApp/1.0.1"": { + ""type"": ""project"", + }, + ""System.Banana/1.0.0"": { + ""type"": ""package"", + ""serviceable"": false, + ""sha512"": ""HASH-System.Banana"" + }, + } +}"); + context.CompileLibraries.Should().HaveCount(2); + var project = context.RuntimeLibraries.Should().Contain(l => l.PackageName == "MyApp").Subject; + project.Version.Should().Be("1.0.1"); + project.Assemblies.Should().Contain(a => a.Path == "MyApp.dll"); + project.LibraryType.Should().Be("project"); + + + var package = context.RuntimeLibraries.Should().Contain(l => l.PackageName == "System.Banana").Subject; + package.Version.Should().Be("1.0.0"); + package.Hash.Should().Be("HASH-System.Banana"); + package.LibraryType.Should().Be("package"); + package.Serviceable.Should().Be(false); + package.Assemblies.Should().Contain(a => a.Path == "lib/dotnet5.4/System.Banana.dll"); + + var target = package.SubTargets.Should().Contain(t => t.Runtime == "win7-x64").Subject; + target.Assemblies.Should().Contain(a => a.Path == "lib/win7/System.Banana.dll"); + target.NativeLibraries.Should().Contain("lib/win7/Banana.dll"); + } + + } } diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs index f6e1340b3..2a01fe9b6 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs @@ -30,17 +30,33 @@ namespace Microsoft.Extensions.DependencyModel.Tests } } + public DependencyContext Create( + string target = null, + string runtime = null, + bool? isPortable = null, + CompilationOptions compilationOptions = null, + CompilationLibrary[] compileLibraries = null, + RuntimeLibrary[] runtimeLibraries = null, + IReadOnlyList> runtimeGraph = null) + { + return new DependencyContext( + target, + runtime, + isPortable ?? false, + compilationOptions ?? CompilationOptions.Default, + compileLibraries ?? new CompilationLibrary[0], + runtimeLibraries ?? new RuntimeLibrary[0], + runtimeGraph ?? new KeyValuePair[0] + ); + } + [Fact] public void SavesRuntimeGraph() { - var result = Save(new DependencyContext( + var result = Save(Create( "Target", "Target/runtime", - false, - CompilationOptions.Default, - new CompilationLibrary[0], - new RuntimeLibrary[0], - new[] + runtimeGraph: new[] { new KeyValuePair("win7-x64", new [] { "win6", "win5"}), new KeyValuePair("win8-x64", new [] { "win7-x64"}), @@ -64,14 +80,10 @@ namespace Microsoft.Extensions.DependencyModel.Tests [Fact] public void WritesRuntimeTargetPropertyIfNotPortable() { - var result = Save(new DependencyContext( + var result = Save(Create( "Target", "runtime", - false, - CompilationOptions.Default, - new CompilationLibrary[0], - new RuntimeLibrary[0], - new KeyValuePair[0]) + false) ); var runtimeTarget = result.Should().HaveProperty("runtimeTarget") @@ -83,20 +95,118 @@ namespace Microsoft.Extensions.DependencyModel.Tests runtimeTarget.Should().HaveProperty("portable") .Subject.Value().Should().Be(false); } + [Fact] - public void DoesNotWritesRuntimeTargetPropertyIfPortable() + public void WritesMainTargetNameToRuntimeTargetIfPortable() { - var result = Save(new DependencyContext( + var result = Save(Create( "Target", "runtime", - false, - CompilationOptions.Default, - new CompilationLibrary[0], - new RuntimeLibrary[0], - new KeyValuePair[0]) + true) ); + var runtimeTarget = result.Should().HaveProperty("runtimeTarget") + .Subject.Should().BeOfType().Subject; - result.Should().NotHaveProperty("runtimeTarget"); + runtimeTarget.Should().HaveProperty("name") + .Subject.Value().Should().Be("Target"); + + runtimeTarget.Should().HaveProperty("portable") + .Subject.Value().Should().Be(true); + } + + [Fact] + public void WritesCompilationLibraries() + { + var result = Save(Create( + "Target", + "runtime", + true, + compileLibraries: new[] + { + new CompilationLibrary( + "package", + "PackageName", + "1.2.3", + "HASH", + new [] {"Banana.dll"}, + new [] { + new Dependency("Fruits.Abstract.dll","2.0.0") + }, + true + ) + })); + + // targets + var targets = result.Should().HavePropertyAsObject("targets").Subject; + var target = targets.Should().HavePropertyAsObject("Target").Subject; + var library = target.Should().HavePropertyAsObject("PackageName/1.2.3").Subject; + var dependencies = library.Should().HavePropertyAsObject("dependencies").Subject; + dependencies.Should().HavePropertyValue("Fruits.Abstract.dll", "2.0.0"); + library.Should().HavePropertyAsObject("compile") + .Subject.Should().HaveProperty("Banana.dll"); + + //libraries + var libraries = result.Should().HavePropertyAsObject("libraries").Subject; + library = libraries.Should().HavePropertyAsObject("PackageName/1.2.3").Subject; + library.Should().HavePropertyValue("sha512", "HASH"); + library.Should().HavePropertyValue("type", "package"); + library.Should().HavePropertyValue("serviceable", true); + } + + [Fact] + public void WritesRuntimeLibrariesToRuntimeTarget() + { + var result = Save(Create( + "Target", + "runtime", + true, + runtimeLibraries: new[] + { + new RuntimeLibrary( + "package", + "PackageName", + "1.2.3", + "HASH", + new [] { RuntimeAssembly.Create("Banana.dll")}, + new [] + { + new RuntimeTarget("win7-x64", + new [] { RuntimeAssembly.Create("Banana.Win7-x64.dll") }, + new [] { "Banana.Win7-x64.so" } + ) + }, + new [] { + new Dependency("Fruits.Abstract.dll","2.0.0") + }, + true + ), + })); + + // targets + var targets = result.Should().HavePropertyAsObject("targets").Subject; + var target = targets.Should().HavePropertyAsObject("Target").Subject; + var library = target.Should().HavePropertyAsObject("PackageName/1.2.3").Subject; + var dependencies = library.Should().HavePropertyAsObject("dependencies").Subject; + dependencies.Should().HavePropertyValue("Fruits.Abstract.dll", "2.0.0"); + library.Should().HavePropertyAsObject("runtime") + .Subject.Should().HaveProperty("Banana.dll"); + + var runtimeTargets = library.Should().HavePropertyAsObject("target").Subject; + + var runtimeAssembly = runtimeTargets.Should().HavePropertyAsObject("Banana.Win7-x64.dll").Subject; + runtimeAssembly.Should().HavePropertyValue("rid", "win7-x64"); + runtimeAssembly.Should().HavePropertyValue("assetType", "runtime"); + + var nativeLibrary = runtimeTargets.Should().HavePropertyAsObject("Banana.Win7-x64.so").Subject; + nativeLibrary.Should().HavePropertyValue("rid", "win7-x64"); + nativeLibrary.Should().HavePropertyValue("assetType", "native"); + + //libraries + var libraries = result.Should().HavePropertyAsObject("libraries").Subject; + library = libraries.Should().HavePropertyAsObject("PackageName/1.2.3").Subject; + library.Should().HavePropertyValue("sha512", "HASH"); + library.Should().HavePropertyValue("type", "package"); + library.Should().HavePropertyValue("serviceable", true); } } -} +} \ No newline at end of file diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/JsonAssetions.cs b/test/Microsoft.Extensions.DependencyModel.Tests/JsonAssetions.cs index d455415ee..214d0c2dc 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/JsonAssetions.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/JsonAssetions.cs @@ -45,5 +45,15 @@ namespace Microsoft.Extensions.DependencyModel.Tests return new AndConstraint(this); } + + public AndWhichConstraint HavePropertyAsObject(string expected) + { + return HaveProperty(expected).Subject.Should().BeOfType(); + } + + public AndConstraint HavePropertyValue(string expected, T value) + { + return HaveProperty(expected).Subject.Value().Should().Be(value); + } } } From 0a0c4a830e024e7d543c9395524f8acdb502fabd Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 4 Mar 2016 09:13:04 -0800 Subject: [PATCH 04/10] Even more tests --- .../DependencyContextBuilder.cs | 81 ++++---- .../DependencyContext.cs | 25 +++ .../DependencyContextJsonReader.cs | 4 +- .../DependencyContextWriter.cs | 134 +++++++++---- .../dotnet-compile/ManagedCompiler.cs | 9 +- .../DependencyContextBuilderTests.cs | 186 ++++++++++++++++++ .../DependencyContextJsonReaderTest.cs | 32 ++- .../DependencyContextJsonWriterTests.cs | 150 +++++++++++++- 8 files changed, 537 insertions(+), 84 deletions(-) create mode 100644 test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs diff --git a/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs b/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs index 3f326f096..037a5c52e 100644 --- a/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs +++ b/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs @@ -13,25 +13,44 @@ using NuGet.Frameworks; namespace Microsoft.Extensions.DependencyModel { - public static class DependencyContextBuilder + public class DependencyContextBuilder { - public static DependencyContext Build(CommonCompilerOptions compilerOptions, LibraryExporter libraryExporter, string configuration, NuGetFramework target, string runtime) - { - var dependencies = libraryExporter.GetAllExports(); + private readonly string _referenceAssembliesPath; - // Sometimes we have package and reference assembly with the same name (System.Runtime for example) thats why we - // deduplicating them prefering reference assembly - var dependencyLookup = dependencies - .OrderBy(export => export.Library.Identity.Type == LibraryType.ReferenceAssembly) - .GroupBy(export => export.Library.Identity.Name) - .Select(exports => exports.First()) - .Select(export => new Dependency(export.Library.Identity.Name, export.Library.Identity.Version.ToString())) + public DependencyContextBuilder() : this(FrameworkReferenceResolver.Default.ReferenceAssembliesPath) + { + } + + public DependencyContextBuilder(string referenceAssembliesPath) + { + _referenceAssembliesPath = referenceAssembliesPath; + } + + public DependencyContext Build(CommonCompilerOptions compilerOptions, + IEnumerable compilationExports, + IEnumerable runtimeExports, + NuGetFramework target, + string runtime) + { + if (compilationExports == null) + { + compilationExports = Enumerable.Empty(); + } + + var dependencyLookup = compilationExports + .Concat(runtimeExports) + .Select(export => export.Library.Identity) + .Distinct() + .Select(identity => new Dependency(identity.Name, identity.Version.ToString())) .ToDictionary(dependency => dependency.Name); - return new DependencyContext(target.DotNetFrameworkName, runtime, false, + return new DependencyContext( + target.DotNetFrameworkName, + runtime, + false, GetCompilationOptions(compilerOptions), - GetLibraries(dependencies, dependencyLookup, target, configuration, runtime: false).Cast().ToArray(), - GetLibraries(dependencies, dependencyLookup, target, configuration, runtime: true).Cast().ToArray(), + GetLibraries(compilationExports, dependencyLookup, runtime: false).Cast().ToArray(), + GetLibraries(runtimeExports, dependencyLookup, runtime: true).Cast().ToArray(), new KeyValuePair[0]); } @@ -51,18 +70,14 @@ namespace Microsoft.Extensions.DependencyModel compilerOptions.GenerateXmlDocumentation); } - private static IEnumerable GetLibraries(IEnumerable dependencies, + private IEnumerable GetLibraries(IEnumerable exports, IDictionary dependencyLookup, - NuGetFramework target, - string configuration, bool runtime) { - return dependencies.Select(export => GetLibrary(export, target, configuration, runtime, dependencyLookup)); + return exports.Select(export => GetLibrary(export, runtime, dependencyLookup)); } - private static Library GetLibrary(LibraryExport export, - NuGetFramework target, - string configuration, + private Library GetLibrary(LibraryExport export, bool runtime, IDictionary dependencyLookup) { @@ -73,11 +88,9 @@ namespace Microsoft.Extensions.DependencyModel var libraryAssets = runtime ? export.RuntimeAssemblies : export.CompilationAssemblies; - foreach (var libraryDependenciesGroup in export.Library.Dependencies.GroupBy(d => d.Name)) + foreach (var libraryDependenciesGroup in export.Library.Dependencies) { - LibraryRange libraryDependency = libraryDependenciesGroup - .OrderByDescending(d => d.Target == LibraryType.ReferenceAssembly) - .First(); + LibraryRange libraryDependency = libraryDependenciesGroup; Dependency dependency; if (dependencyLookup.TryGetValue(libraryDependency.Name, out dependency)) @@ -87,19 +100,7 @@ namespace Microsoft.Extensions.DependencyModel } string[] assemblies; - if (type == LibraryType.Project) - { - var isExe = ((ProjectDescription)export.Library) - .Project - .GetCompilerOptions(target, configuration) - .EmitEntryPoint - .GetValueOrDefault(false); - - isExe &= target.IsDesktop(); - - assemblies = new[] { export.Library.Identity.Name + (isExe ? ".exe" : ".dll") }; - } - else if (type == LibraryType.ReferenceAssembly) + if (type == LibraryType.ReferenceAssembly) { assemblies = ResolveReferenceAssembliesPath(libraryAssets); } @@ -135,11 +136,11 @@ namespace Microsoft.Extensions.DependencyModel } } - private static string[] ResolveReferenceAssembliesPath(IEnumerable libraryAssets) + private string[] ResolveReferenceAssembliesPath(IEnumerable libraryAssets) { var resolvedPaths = new List(); var referenceAssembliesPath = - PathUtility.EnsureTrailingSlash(FrameworkReferenceResolver.Default.ReferenceAssembliesPath); + PathUtility.EnsureTrailingSlash(_referenceAssembliesPath); foreach (var libraryAsset in libraryAssets) { // If resolved path is under ReferenceAssembliesPath store it as a relative to it diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs index b2e44d397..2f3e4d422 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs @@ -23,6 +23,31 @@ namespace Microsoft.Extensions.DependencyModel RuntimeLibrary[] runtimeLibraries, IReadOnlyList> runtimeGraph) { + if (target == null) + { + throw new ArgumentNullException(nameof(target)); + } + if (runtime == null) + { + throw new ArgumentNullException(nameof(runtime)); + } + if (compilationOptions == null) + { + throw new ArgumentNullException(nameof(compilationOptions)); + } + if (compileLibraries == null) + { + throw new ArgumentNullException(nameof(compileLibraries)); + } + if (runtimeLibraries == null) + { + throw new ArgumentNullException(nameof(runtimeLibraries)); + } + if (runtimeGraph == null) + { + throw new ArgumentNullException(nameof(runtimeGraph)); + } + Target = target; Runtime = runtime; IsPortable = isPortable; diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs index 6853a7b38..4445575bf 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs @@ -28,8 +28,8 @@ namespace Microsoft.Extensions.DependencyModel private DependencyContext Read(JObject root) { - string runtime = null; - string target = null; + string runtime = string.Empty; + string target = string.Empty; var runtimeTargetInfo = ReadRuntimeTargetInfo(root); var libraryStubs = ReadLibraryStubs((JObject) root[DependencyContextStrings.LibrariesPropertyName]); diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs index 639f84f40..2df14313b 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using Microsoft.Extensions.PlatformAbstractions; @@ -105,6 +106,10 @@ namespace Microsoft.Extensions.DependencyModel { o[DependencyContextStrings.EmitEntryPointPropertyName] = compilationOptions.EmitEntryPoint; } + if (compilationOptions.GenerateXmlDocumentation != null) + { + o[DependencyContextStrings.GenerateXmlDocumentationPropertyName] = compilationOptions.GenerateXmlDocumentation; + } return o; } @@ -113,19 +118,15 @@ namespace Microsoft.Extensions.DependencyModel if (context.IsPortable) { return new JObject( - new JProperty(context.Target, WriteTarget(context.CompileLibraries)), - new JProperty(context.Target + DependencyContextStrings.VersionSeperator + context.Runtime, - WriteTarget(context.RuntimeLibraries)) - ); - } - else - { - return new JObject( - new JProperty(context.Target, WriteTarget(context.CompileLibraries)), - new JProperty(context.Target + DependencyContextStrings.VersionSeperator + context.Runtime, - WriteTarget(context.RuntimeLibraries)) + new JProperty(context.Target, WritePortableTarget(context.RuntimeLibraries, context.CompileLibraries)) ); } + + return new JObject( + new JProperty(context.Target, WriteTarget(context.CompileLibraries)), + new JProperty(context.Target + DependencyContextStrings.VersionSeperator + context.Runtime, + WriteTarget(context.RuntimeLibraries)) + ); } private JObject WriteTarget(IReadOnlyList libraries) @@ -135,6 +136,40 @@ namespace Microsoft.Extensions.DependencyModel new JProperty(library.PackageName + DependencyContextStrings.VersionSeperator + library.Version, WriteTargetLibrary(library)))); } + private JObject WritePortableTarget(IReadOnlyList runtimeLibraries, IReadOnlyList compilationLibraries) + { + var runtimeLookup = runtimeLibraries.ToDictionary(l => l.PackageName); + var compileLookup = compilationLibraries.ToDictionary(l => l.PackageName); + + var targetObject = new JObject(); + + foreach (var packageName in runtimeLookup.Keys.Concat(compileLookup.Keys).Distinct()) + { + RuntimeLibrary runtimeLibrary; + runtimeLookup.TryGetValue(packageName, out runtimeLibrary); + + CompilationLibrary compilationLibrary; + compileLookup.TryGetValue(packageName, out compilationLibrary); + + if (compilationLibrary != null && runtimeLibrary != null) + { + Debug.Assert(compilationLibrary.Serviceable == runtimeLibrary.Serviceable); + Debug.Assert(compilationLibrary.Version == runtimeLibrary.Version); + Debug.Assert(compilationLibrary.Hash == runtimeLibrary.Hash); + Debug.Assert(compilationLibrary.LibraryType == runtimeLibrary.LibraryType); + } + + var library = (Library)compilationLibrary ?? (Library)runtimeLibrary; + targetObject.Add( + new JProperty(library.PackageName + DependencyContextStrings.VersionSeperator + library.Version, + WritePortableTargetLibrary(runtimeLibrary, compilationLibrary) + ) + ); + + } + return targetObject; + } + private JObject WriteTargetLibrary(Library library) { string propertyName; @@ -167,45 +202,76 @@ namespace Microsoft.Extensions.DependencyModel WriteAssemblies(assemblies)) ); } - private JObject WritePortableTargetLibrary(RuntimeLibrary compilationLibrary, CompilationLibrary runtimeLibrary) + + private JObject WritePortableTargetLibrary(RuntimeLibrary runtimeLibrary, CompilationLibrary compilationLibrary) { var libraryObject = new JObject(); - - string propertyName; - string[] assemblies; + var dependencies = new HashSet(); if (runtimeLibrary != null) { - propertyName = DependencyContextStrings.RuntimeAssembliesKey; - assemblies = runtimeLibrary.Assemblies.Select(assembly => assembly.Path).ToArray(); + libraryObject.Add(new JProperty(DependencyContextStrings.RuntimeAssembliesKey, + WriteAssemblies(runtimeLibrary.Assemblies.Select(a => a.Path))) + ); + if (runtimeLibrary.SubTargets.Any()) + { + libraryObject.Add(new JProperty( + DependencyContextStrings.RuntimeTargetsPropertyName, + new JObject(runtimeLibrary.SubTargets.SelectMany(WriteRuntimeTarget))) + ); + } + + dependencies.UnionWith(runtimeLibrary.Dependencies); } - RuntimeAssembly[] compilationAssemblies; if (compilationLibrary != null) { - propertyName = DependencyContextStrings.CompileTimeAssembliesKey; - compilationAssemblies = compilationLibrary.Assemblies.ToArray(); - } - else - { - throw new NotSupportedException(); - } - } - - - return new JObject( - new JProperty(DependencyContextStrings.DependenciesPropertyName, WriteDependencies(library.Dependencies)), - new JProperty(propertyName, - WriteAssemblies(assemblies)) + libraryObject.Add(new JProperty(DependencyContextStrings.CompileTimeAssembliesKey, + WriteAssemblies(compilationLibrary.Assemblies)) ); + dependencies.UnionWith(compilationLibrary.Dependencies); + } + + libraryObject.Add( + new JProperty(DependencyContextStrings.DependenciesPropertyName, WriteDependencies(dependencies))); + + return libraryObject; } - private JObject WriteAssemblies(IReadOnlyList assemblies) + private IEnumerable WriteRuntimeTarget(RuntimeTarget target) + { + var runtime = WriteRuntimeTargetAssemblies( + target.Assemblies.Select(a => a.Path), + target.Runtime, + DependencyContextStrings.RuntimeAssetType); + + var native = WriteRuntimeTargetAssemblies( + target.NativeLibraries, + target.Runtime, + DependencyContextStrings.NativeAssetType); + + return runtime.Concat(native); + } + + private IEnumerable WriteRuntimeTargetAssemblies(IEnumerable assemblies, string runtime, string assetType) + { + foreach (var assembly in assemblies) + { + yield return new JProperty(assembly, + new JObject( + new JProperty(DependencyContextStrings.RidPropertyName, runtime), + new JProperty(DependencyContextStrings.AssetTypePropertyName, assetType) + ) + ); + } + } + + private JObject WriteAssemblies(IEnumerable assemblies) { return new JObject(assemblies.Select(assembly => new JProperty(assembly, new JObject()))); } - private JObject WriteDependencies(IReadOnlyList dependencies) + private JObject WriteDependencies(IEnumerable dependencies) { return new JObject( dependencies.Select(dependency => new JProperty(dependency.Name, dependency.Version)) diff --git a/src/dotnet/commands/dotnet-compile/ManagedCompiler.cs b/src/dotnet/commands/dotnet-compile/ManagedCompiler.cs index 674542dca..aeb692979 100644 --- a/src/dotnet/commands/dotnet-compile/ManagedCompiler.cs +++ b/src/dotnet/commands/dotnet-compile/ManagedCompiler.cs @@ -111,11 +111,12 @@ namespace Microsoft.DotNet.Tools.Compiler if (compilationOptions.PreserveCompilationContext == true) { - var dependencyContext = DependencyContextBuilder.Build(compilationOptions, - exporter, - args.ConfigValue, + var allExports = exporter.GetAllExports().ToList(); + var dependencyContext = new DependencyContextBuilder().Build(compilationOptions, + allExports, + allExports, context.TargetFramework, - context.RuntimeIdentifier); + context.RuntimeIdentifier ?? string.Empty); var writer = new DependencyContextWriter(); var depsJsonFile = Path.Combine(intermediateOutputPath, context.ProjectFile.Name + "dotnet-compile.deps.json"); diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs new file mode 100644 index 000000000..5b4706141 --- /dev/null +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs @@ -0,0 +1,186 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using FluentAssertions; +using Microsoft.DotNet.ProjectModel; +using Microsoft.DotNet.ProjectModel.Compilation; +using Microsoft.DotNet.ProjectModel.Graph; +using NuGet.Frameworks; +using NuGet.Versioning; +using Xunit; + +namespace Microsoft.Extensions.DependencyModel.Tests +{ + public class DependencyContextBuilderTests + { + private string _referenceAssembliesPath = Path.Combine("reference", "assemblies"); + private NuGetFramework _defaultFramework; + private string _defaultName = "Library.Name"; + private string _defaultHash = "Hash"; + private NuGetVersion _defaultVersion = new NuGetVersion(1, 2, 3, new []{"dev"}, string.Empty); + + public DependencyContext Build(CommonCompilerOptions compilerOptions = null, + IEnumerable compilationExports = null, + IEnumerable runtimeExports = null, + NuGetFramework target = null, + string runtime = null) + { + _defaultFramework = NuGetFramework.Parse("net451"); + return new DependencyContextBuilder(_referenceAssembliesPath).Build( + compilerOptions ?? new CommonCompilerOptions(), + compilationExports ?? new LibraryExport[] { }, + runtimeExports ?? new LibraryExport[] {}, + target ?? _defaultFramework, + runtime ?? string.Empty); + } + + [Fact] + public void PreservesCompilationOptions() + { + var context = Build(new CommonCompilerOptions() + { + AllowUnsafe = true, + Defines = new[] {"Define", "D"}, + DelaySign = true, + EmitEntryPoint = true, + GenerateXmlDocumentation = true, + KeyFile = "Key.snk", + LanguageVersion = "C#8", + Optimize = true, + Platform = "Platform", + PublicSign = true, + WarningsAsErrors = true + }); + + context.CompilationOptions.AllowUnsafe.Should().Be(true); + context.CompilationOptions.DelaySign.Should().Be(true); + context.CompilationOptions.EmitEntryPoint.Should().Be(true); + context.CompilationOptions.GenerateXmlDocumentation.Should().Be(true); + context.CompilationOptions.Optimize.Should().Be(true); + context.CompilationOptions.PublicSign.Should().Be(true); + context.CompilationOptions.WarningsAsErrors.Should().Be(true); + + context.CompilationOptions.Defines.Should().BeEquivalentTo(new[] { "Define", "D" }); + context.CompilationOptions.KeyFile.Should().Be("Key.snk"); + context.CompilationOptions.LanguageVersion.Should().Be("C#8"); + context.CompilationOptions.Platform.Should().Be("Platform"); + } + + + private LibraryExport Export( + LibraryDescription description, + IEnumerable compilationAssemblies = null, + IEnumerable runtimeAssemblies = null) + { + return new LibraryExport( + description, + compilationAssemblies ?? Enumerable.Empty(), + Enumerable.Empty(), + runtimeAssemblies ?? Enumerable.Empty(), + Enumerable.Empty(), + Enumerable.Empty(), + Enumerable.Empty(), + Enumerable.Empty() + ); + } + + private PackageDescription PackageDescription( + string name = null, + NuGetVersion version = null, + string hash = null, + IEnumerable dependencies = null, + bool? servicable = null) + { + return new PackageDescription( + "PATH", + new LockFilePackageLibrary() + { + Files = new string[] { }, + IsServiceable = servicable ?? false, + Name = name ?? _defaultName, + Version = version ?? _defaultVersion, + Sha512 = hash ?? _defaultHash + }, + new LockFileTargetLibrary(), + dependencies ?? Enumerable.Empty(), + true); + } + + private ProjectDescription ProjectDescription( + string name = null, + NuGetVersion version = null, + IEnumerable dependencies = null) + { + return new ProjectDescription( + new LibraryRange( + name ?? _defaultName, + new VersionRange(version ?? _defaultVersion), + LibraryType.Project, + LibraryDependencyType.Default + ), + new Project(), + dependencies ?? Enumerable.Empty(), + new TargetFrameworkInformation(), + true); + } + + private LibraryDescription ReferenceAssemblyDescription( + string name = null, + NuGetVersion version = null) + { + return new LibraryDescription( + new LibraryIdentity( + name ?? _defaultName, + version ?? _defaultVersion, + LibraryType.ReferenceAssembly), + string.Empty, // Framework assemblies don't have hashes + "PATH", + Enumerable.Empty(), + _defaultFramework, + true, + true); + } + + [Fact] + public void FillsRuntimeAndTarget() + { + var context = Build(target: new NuGetFramework("SomeFramework",new Version(1,2)), runtime: "win8-32"); + context.Runtime.Should().Be("win8-32"); + context.Target.Should().Be("SomeFramework,Version=v1.2"); + } + + [Fact] + public void TakesServicableFromPackageDescription() + { + var context = Build(runtimeExports: new[] + { + Export(PackageDescription("Pack.Age", servicable: true)) + }); + + var lib = context.RuntimeLibraries.Single(); + lib.Serviceable.Should().BeTrue(); + } + + [Fact] + public void FillsRuntimeLibraryProperties() + { + var context = Build(runtimeExports: new[] + { + Export(PackageDescription("Pack.Age", + servicable: true, + hash: "Hash", + version: new NuGetVersion(1,2,3), + dependencies: new [] + { + new LibraryRange() + })) + }); + + var lib = context.RuntimeLibraries.Single(); + lib.Serviceable.Should().BeTrue(); + } + + } +} diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs index 6e586ea98..375a22bda 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs @@ -196,6 +196,36 @@ namespace Microsoft.Extensions.DependencyModel.Tests target.NativeLibraries.Should().Contain("lib/win7/Banana.dll"); } - + [Fact] + public void ReadsCompilationOptions() + { + var context = Read( +@"{ + ""compilationOptions"": { + ""allowUnsafe"": true, + ""defines"": [""MY"", ""DEFINES""], + ""delaySign"": true, + ""emitEntryPoint"": true, + ""xmlDoc"": true, + ""keyFile"": ""Key.snk"", + ""languageVersion"": ""C#8"", + ""platform"": ""Platform"", + ""publicSign"": true, + ""warningsAsErrors"": true, + ""optimize"": true + } +}"); + context.CompilationOptions.AllowUnsafe.Should().Be(true); + context.CompilationOptions.Defines.Should().BeEquivalentTo(new [] {"MY", "DEFINES"}); + context.CompilationOptions.DelaySign.Should().Be(true); + context.CompilationOptions.EmitEntryPoint.Should().Be(true); + context.CompilationOptions.GenerateXmlDocumentation.Should().Be(true); + context.CompilationOptions.KeyFile.Should().Be("Key.snk"); + context.CompilationOptions.LanguageVersion.Should().Be("C#8"); + context.CompilationOptions.Optimize.Should().Be(true); + context.CompilationOptions.Platform.Should().Be("Platform"); + context.CompilationOptions.PublicSign.Should().Be(true); + context.CompilationOptions.WarningsAsErrors.Should().Be(true); + } } } diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs index 2a01fe9b6..3f69c3cc8 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs @@ -40,8 +40,8 @@ namespace Microsoft.Extensions.DependencyModel.Tests IReadOnlyList> runtimeGraph = null) { return new DependencyContext( - target, - runtime, + target ?? string.Empty, + runtime ?? string.Empty, isPortable ?? false, compilationOptions ?? CompilationOptions.Default, compileLibraries ?? new CompilationLibrary[0], @@ -191,7 +191,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests library.Should().HavePropertyAsObject("runtime") .Subject.Should().HaveProperty("Banana.dll"); - var runtimeTargets = library.Should().HavePropertyAsObject("target").Subject; + var runtimeTargets = library.Should().HavePropertyAsObject("runtimeTargets").Subject; var runtimeAssembly = runtimeTargets.Should().HavePropertyAsObject("Banana.Win7-x64.dll").Subject; runtimeAssembly.Should().HavePropertyValue("rid", "win7-x64"); @@ -208,5 +208,149 @@ namespace Microsoft.Extensions.DependencyModel.Tests library.Should().HavePropertyValue("type", "package"); library.Should().HavePropertyValue("serviceable", true); } + + [Fact] + public void MergesRuntimeAndCompileLibrariesForPortable() + { + var result = Save(Create( + "Target", + "runtime", + true, + compileLibraries: new[] + { + new CompilationLibrary( + "package", + "PackageName", + "1.2.3", + "HASH", + new [] { "ref/Banana.dll" }, + new [] { + new Dependency("Fruits.Abstract.dll","2.0.0") + }, + true + ) + }, + runtimeLibraries: new[] + { + new RuntimeLibrary( + "package", + "PackageName", + "1.2.3", + "HASH", + new [] { RuntimeAssembly.Create("Banana.dll")}, + new [] + { + new RuntimeTarget("win7-x64", + new [] { RuntimeAssembly.Create("Banana.Win7-x64.dll") }, + new [] { "Banana.Win7-x64.so" } + ) + }, + new [] { + new Dependency("Fruits.Abstract.dll","2.0.0") + }, + true + ), + })); + + // targets + var targets = result.Should().HavePropertyAsObject("targets").Subject; + var target = targets.Should().HavePropertyAsObject("Target").Subject; + var library = target.Should().HavePropertyAsObject("PackageName/1.2.3").Subject; + var dependencies = library.Should().HavePropertyAsObject("dependencies").Subject; + dependencies.Should().HavePropertyValue("Fruits.Abstract.dll", "2.0.0"); + library.Should().HavePropertyAsObject("runtime") + .Subject.Should().HaveProperty("Banana.dll"); + + library.Should().HavePropertyAsObject("compile") + .Subject.Should().HaveProperty("ref/Banana.dll"); + + var runtimeTargets = library.Should().HavePropertyAsObject("runtimeTargets").Subject; + + var runtimeAssembly = runtimeTargets.Should().HavePropertyAsObject("Banana.Win7-x64.dll").Subject; + runtimeAssembly.Should().HavePropertyValue("rid", "win7-x64"); + runtimeAssembly.Should().HavePropertyValue("assetType", "runtime"); + + var nativeLibrary = runtimeTargets.Should().HavePropertyAsObject("Banana.Win7-x64.so").Subject; + nativeLibrary.Should().HavePropertyValue("rid", "win7-x64"); + nativeLibrary.Should().HavePropertyValue("assetType", "native"); + + //libraries + var libraries = result.Should().HavePropertyAsObject("libraries").Subject; + library = libraries.Should().HavePropertyAsObject("PackageName/1.2.3").Subject; + library.Should().HavePropertyValue("sha512", "HASH"); + library.Should().HavePropertyValue("type", "package"); + library.Should().HavePropertyValue("serviceable", true); + } + + [Fact] + public void WritesRuntimeTargetForNonPortable() + { + var result = Save(Create( + "Target", + "runtime", + false, + runtimeLibraries: new[] + { + new RuntimeLibrary( + "package", + "PackageName", + "1.2.3", + "HASH", + new [] { RuntimeAssembly.Create("Banana.dll")}, + new RuntimeTarget[] {}, + new [] { + new Dependency("Fruits.Abstract.dll","2.0.0") + }, + true + ), + })); + + // targets + var targets = result.Should().HavePropertyAsObject("targets").Subject; + var target = targets.Should().HavePropertyAsObject("Target/runtime").Subject; + var library = target.Should().HavePropertyAsObject("PackageName/1.2.3").Subject; + var dependencies = library.Should().HavePropertyAsObject("dependencies").Subject; + dependencies.Should().HavePropertyValue("Fruits.Abstract.dll", "2.0.0"); + library.Should().HavePropertyAsObject("runtime") + .Subject.Should().HaveProperty("Banana.dll"); + + //libraries + var libraries = result.Should().HavePropertyAsObject("libraries").Subject; + library = libraries.Should().HavePropertyAsObject("PackageName/1.2.3").Subject; + library.Should().HavePropertyValue("sha512", "HASH"); + library.Should().HavePropertyValue("type", "package"); + library.Should().HavePropertyValue("serviceable", true); + } + + [Fact] + public void WritesCompilationOptions() + { + var result = Save(Create(compilationOptions: new CompilationOptions( + defines: new[] {"MY", "DEFINES"}, + languageVersion: "C#8", + platform: "Platform", + allowUnsafe: true, + warningsAsErrors: true, + optimize: true, + keyFile: "Key.snk", + delaySign: true, + publicSign: true, + emitEntryPoint: true, + generateXmlDocumentation: true))); + + var options = result.Should().HavePropertyAsObject("compilationOptions").Subject; + options.Should().HavePropertyValue("allowUnsafe", true); + options.Should().HavePropertyValue("delaySign", true); + options.Should().HavePropertyValue("emitEntryPoint", true); + options.Should().HavePropertyValue("xmlDoc", true); + options.Should().HavePropertyValue("publicSign", true); + options.Should().HavePropertyValue("optimize", true); + options.Should().HavePropertyValue("warningsAsErrors", true); + options.Should().HavePropertyValue("allowUnsafe", true); + options.Should().HavePropertyValue("languageVersion", "C#8"); + options.Should().HavePropertyValue("keyFile", "Key.snk"); + options.Should().HaveProperty("defines") + .Subject.Values().Should().BeEquivalentTo(new [] {"MY", "DEFINES" }); + } } } \ No newline at end of file From 7df754be36ffc43a08220d8b7f0215980d1ff044 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 4 Mar 2016 10:19:45 -0800 Subject: [PATCH 05/10] Finish builder tests --- .../DependencyContextBuilder.cs | 12 +- .../DependencyContextBuilderTests.cs | 125 +++++++++++++++++- 2 files changed, 130 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs b/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs index 037a5c52e..da9ad8444 100644 --- a/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs +++ b/src/Microsoft.DotNet.ProjectModel/DependencyContextBuilder.cs @@ -88,9 +88,17 @@ namespace Microsoft.Extensions.DependencyModel var libraryAssets = runtime ? export.RuntimeAssemblies : export.CompilationAssemblies; - foreach (var libraryDependenciesGroup in export.Library.Dependencies) + foreach (var libraryDependency in export.Library.Dependencies) { - LibraryRange libraryDependency = libraryDependenciesGroup; + // skip build time dependencies + if (!libraryDependency.Type.HasFlag( + LibraryDependencyTypeFlag.MainReference | + LibraryDependencyTypeFlag.MainExport | + LibraryDependencyTypeFlag.RuntimeComponent | + LibraryDependencyTypeFlag.BecomesNupkgDependency)) + { + continue; + } Dependency dependency; if (dependencyLookup.TryGetValue(libraryDependency.Name, out dependency)) diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs index 5b4706141..d62013fa9 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs @@ -171,15 +171,130 @@ namespace Microsoft.Extensions.DependencyModel.Tests Export(PackageDescription("Pack.Age", servicable: true, hash: "Hash", - version: new NuGetVersion(1,2,3), - dependencies: new [] + version: new NuGetVersion(1, 2, 3), + dependencies: new[] { - new LibraryRange() - })) + new LibraryRange("System.Collections", + new VersionRange(new NuGetVersion(2, 1, 2)), + LibraryType.ReferenceAssembly, + LibraryDependencyType.Default) + }), + runtimeAssemblies: new[] + { + new LibraryAsset("Dll", "lib/Pack.Age.dll", ""), + } + ), + Export(ReferenceAssemblyDescription("System.Collections", + version: new NuGetVersion(3, 3, 3)), + runtimeAssemblies: new[] + { + new LibraryAsset("Dll", "", "System.Collections.dll"), + }) }); - var lib = context.RuntimeLibraries.Single(); + context.RuntimeLibraries.Should().HaveCount(2); + + var lib = context.RuntimeLibraries.Should().Contain(l => l.PackageName == "Pack.Age").Subject; + lib.LibraryType.Should().Be("package"); lib.Serviceable.Should().BeTrue(); + lib.Hash.Should().Be("sha512-Hash"); + lib.Version.Should().Be("1.2.3"); + lib.Dependencies.Should().OnlyContain(l => l.Name == "System.Collections" && l.Version == "3.3.3"); + lib.Assemblies.Should().OnlyContain(l => l.Path == "lib/Pack.Age.dll"); + + var asm = context.RuntimeLibraries.Should().Contain(l => l.PackageName == "System.Collections").Subject; + asm.LibraryType.Should().Be("referenceassembly"); + asm.Version.Should().Be("3.3.3"); + asm.Hash.Should().BeEmpty(); + asm.Dependencies.Should().BeEmpty(); + asm.Assemblies.Should().OnlyContain(l => l.Path == "System.Collections.dll"); + } + + + [Fact] + public void FillsCompileLibraryProperties() + { + var context = Build(compilationExports: new[] + { + Export(PackageDescription("Pack.Age", + servicable: true, + hash: "Hash", + version: new NuGetVersion(1, 2, 3), + dependencies: new[] + { + new LibraryRange("System.Collections", + new VersionRange(new NuGetVersion(2, 1, 2)), + LibraryType.ReferenceAssembly, + LibraryDependencyType.Default) + }), + compilationAssemblies: new[] + { + new LibraryAsset("Dll", "lib/Pack.Age.dll", ""), + } + ), + Export(ReferenceAssemblyDescription("System.Collections", + version: new NuGetVersion(3, 3, 3)), + compilationAssemblies: new[] + { + new LibraryAsset("Dll", "", "System.Collections.dll"), + }) + }); + + context.CompileLibraries.Should().HaveCount(2); + + var lib = context.CompileLibraries.Should().Contain(l => l.PackageName == "Pack.Age").Subject; + lib.LibraryType.Should().Be("package"); + lib.Serviceable.Should().BeTrue(); + lib.Hash.Should().Be("sha512-Hash"); + lib.Version.Should().Be("1.2.3"); + lib.Dependencies.Should().OnlyContain(l => l.Name == "System.Collections" && l.Version == "3.3.3"); + lib.Assemblies.Should().OnlyContain(a => a == "lib/Pack.Age.dll"); + + var asm = context.CompileLibraries.Should().Contain(l => l.PackageName == "System.Collections").Subject; + asm.LibraryType.Should().Be("referenceassembly"); + asm.Version.Should().Be("3.3.3"); + asm.Hash.Should().BeEmpty(); + asm.Dependencies.Should().BeEmpty(); + asm.Assemblies.Should().OnlyContain(a => a == "System.Collections.dll"); + } + + [Fact] + public void ReferenceAssembliesPathRelativeToDefaultRoot() + { + var context = Build(compilationExports: new[] + { + Export(ReferenceAssemblyDescription("System.Collections", + version: new NuGetVersion(3, 3, 3)), + compilationAssemblies: new[] + { + new LibraryAsset("Dll", "", Path.Combine(_referenceAssembliesPath, "sub", "System.Collections.dll")) + }) + }); + + var asm = context.CompileLibraries.Should().Contain(l => l.PackageName == "System.Collections").Subject; + asm.Assemblies.Should().OnlyContain(a => a == Path.Combine("sub", "System.Collections.dll")); + } + + [Fact] + public void SkipsBuildDependencies() + { + var context = Build(compilationExports: new[] + { + Export(PackageDescription("Pack.Age", + dependencies: new[] + { + new LibraryRange("System.Collections", + new VersionRange(new NuGetVersion(2, 1, 2)), + LibraryType.ReferenceAssembly, + LibraryDependencyType.Build) + }) + ), + Export(ReferenceAssemblyDescription("System.Collections", + version: new NuGetVersion(3, 3, 3))) + }); + + var lib = context.CompileLibraries.Should().Contain(l => l.PackageName == "Pack.Age").Subject; + lib.Dependencies.Should().BeEmpty(); } } From 25d70c07c342e70929f78a0a629412615298794e Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 4 Mar 2016 10:57:38 -0800 Subject: [PATCH 06/10] PR --- .../DependencyContext.cs | 13 ++-- .../DependencyContextJsonReader.cs | 16 +++-- .../DependencyContextWriter.cs | 64 ++++++------------- 3 files changed, 35 insertions(+), 58 deletions(-) diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs index 2f3e4d422..1aa81cce5 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs @@ -5,6 +5,7 @@ using System; using System.IO; using System.Reflection; using System.Collections.Generic; +using System.Linq; namespace Microsoft.Extensions.DependencyModel { @@ -19,9 +20,9 @@ namespace Microsoft.Extensions.DependencyModel string runtime, bool isPortable, CompilationOptions compilationOptions, - CompilationLibrary[] compileLibraries, - RuntimeLibrary[] runtimeLibraries, - IReadOnlyList> runtimeGraph) + IEnumerable compileLibraries, + IEnumerable runtimeLibraries, + IEnumerable> runtimeGraph) { if (target == null) { @@ -52,9 +53,9 @@ namespace Microsoft.Extensions.DependencyModel Runtime = runtime; IsPortable = isPortable; CompilationOptions = compilationOptions; - CompileLibraries = compileLibraries; - RuntimeLibraries = runtimeLibraries; - RuntimeGraph = runtimeGraph; + CompileLibraries = compileLibraries.ToArray(); + RuntimeLibraries = runtimeLibraries.ToArray(); + RuntimeGraph = runtimeGraph.ToArray(); } public static DependencyContext Default => _defaultContext.Value; diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs index 4445575bf..22218d787 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs @@ -28,24 +28,26 @@ namespace Microsoft.Extensions.DependencyModel private DependencyContext Read(JObject root) { - string runtime = string.Empty; - string target = string.Empty; + var runtime = string.Empty; + var target = string.Empty; var runtimeTargetInfo = ReadRuntimeTargetInfo(root); var libraryStubs = ReadLibraryStubs((JObject) root[DependencyContextStrings.LibrariesPropertyName]); - var targetsObject = (IEnumerable>) root[DependencyContextStrings.TargetsPropertyName]; + var targetsObject = (JObject) root[DependencyContextStrings.TargetsPropertyName]; JObject runtimeTarget = null; JObject compileTarget = null; if (targetsObject != null) { - var compileTargetProperty = targetsObject.FirstOrDefault(t => !IsRuntimeTarget(t.Key)); - compileTarget = (JObject) compileTargetProperty.Value; - target = compileTargetProperty.Key; + var compileTargetProperty = targetsObject.Properties() + .FirstOrDefault(p => !IsRuntimeTarget(p.Name)); + + compileTarget = (JObject)compileTargetProperty.Value; + target = compileTargetProperty.Name; if (!string.IsNullOrEmpty(runtimeTargetInfo.Name)) { - runtimeTarget = (JObject) targetsObject.FirstOrDefault(t => t.Key == runtimeTargetInfo.Name).Value; + runtimeTarget = (JObject) targetsObject[runtimeTargetInfo.Name]; if (runtimeTarget == null) { throw new FormatException($"Target with name {runtimeTargetInfo.Name} not found"); diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs index 2df14313b..3a5b014a9 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs @@ -66,52 +66,26 @@ namespace Microsoft.Extensions.DependencyModel { o[DependencyContextStrings.DefinesPropertyName] = new JArray(compilationOptions.Defines); } - if (compilationOptions.LanguageVersion != null) - { - o[DependencyContextStrings.LanguageVersionPropertyName] = compilationOptions.LanguageVersion; - } - if (compilationOptions.Platform != null) - { - o[DependencyContextStrings.PlatformPropertyName] = compilationOptions.Platform; - } - if (compilationOptions.AllowUnsafe != null) - { - o[DependencyContextStrings.AllowUnsafePropertyName] = compilationOptions.AllowUnsafe; - } - if (compilationOptions.WarningsAsErrors != null) - { - o[DependencyContextStrings.WarningsAsErrorsPropertyName] = compilationOptions.WarningsAsErrors; - } - if (compilationOptions.Optimize != null) - { - o[DependencyContextStrings.OptimizePropertyName] = compilationOptions.Optimize; - } - if (compilationOptions.KeyFile != null) - { - o[DependencyContextStrings.KeyFilePropertyName] = compilationOptions.KeyFile; - } - if (compilationOptions.DelaySign != null) - { - o[DependencyContextStrings.DelaySignPropertyName] = compilationOptions.DelaySign; - } - if (compilationOptions.PublicSign != null) - { - o[DependencyContextStrings.PublicSignPropertyName] = compilationOptions.PublicSign; - } - if (compilationOptions.DebugType != null) - { - o[DependencyContextStrings.DebugTypePropertyName] = compilationOptions.DebugType; - } - if (compilationOptions.EmitEntryPoint != null) - { - o[DependencyContextStrings.EmitEntryPointPropertyName] = compilationOptions.EmitEntryPoint; - } - if (compilationOptions.GenerateXmlDocumentation != null) - { - o[DependencyContextStrings.GenerateXmlDocumentationPropertyName] = compilationOptions.GenerateXmlDocumentation; - } + AddPropertyIfNotNull(o, DependencyContextStrings.LanguageVersionPropertyName, compilationOptions.LanguageVersion); + AddPropertyIfNotNull(o, DependencyContextStrings.PlatformPropertyName, compilationOptions.Platform); + AddPropertyIfNotNull(o, DependencyContextStrings.AllowUnsafePropertyName, compilationOptions.AllowUnsafe); + AddPropertyIfNotNull(o, DependencyContextStrings.WarningsAsErrorsPropertyName, compilationOptions.WarningsAsErrors); + AddPropertyIfNotNull(o, DependencyContextStrings.OptimizePropertyName, compilationOptions.Optimize); + AddPropertyIfNotNull(o, DependencyContextStrings.KeyFilePropertyName, compilationOptions.KeyFile); + AddPropertyIfNotNull(o, DependencyContextStrings.DelaySignPropertyName, compilationOptions.DelaySign); + AddPropertyIfNotNull(o, DependencyContextStrings.PublicSignPropertyName, compilationOptions.PublicSign); + AddPropertyIfNotNull(o, DependencyContextStrings.EmitEntryPointPropertyName, compilationOptions.EmitEntryPoint); + AddPropertyIfNotNull(o, DependencyContextStrings.GenerateXmlDocumentationPropertyName, compilationOptions.GenerateXmlDocumentation); return o; - } + } + + private void AddPropertyIfNotNull(JObject o, string name, T value) + { + if (value != null) + { + o[name] = value.ToString(); + } + } private JObject WriteTargets(DependencyContext context) { From 304127ec0dadbe3def3aebfd68fcadc938efbd71 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 4 Mar 2016 12:16:23 -0800 Subject: [PATCH 07/10] Fix wrapped project export --- .../Compilation/LibraryExporter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExporter.cs b/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExporter.cs index 2ab423241..2679750f4 100644 --- a/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExporter.cs +++ b/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExporter.cs @@ -215,7 +215,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation var compileAsset = new LibraryAsset( project.Project.Name, - null, + Path.GetFileName(assemblyPath), assemblyPath); builder.AddCompilationAssembly(compileAsset); @@ -334,7 +334,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation var assemblyPath = Path.Combine(package.Path, analyzer); - // $/analyzers/{Framework Name}{Version}/{Supported Architecture}/{Supported Programming Language}/{Analyzer}.dll + // $/analyzers/{Framework Name}{Version}/{Supported Architecture}/{Supported Programming Language}/{Analyzer}.dll switch (specifiers.Length) { // $/analyzers/{analyzer}.dll @@ -396,7 +396,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation private static bool LibraryIsOfType(LibraryType type, LibraryDescription library) { - return type.Equals(LibraryType.Unspecified) || // No type filter was requested + return type.Equals(LibraryType.Unspecified) || // No type filter was requested library.Identity.Type.Equals(type); // OR, library type matches requested type } } From 1d7cff48d426a97f3da635e8782634df194a928e Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 4 Mar 2016 14:12:16 -0800 Subject: [PATCH 08/10] PR --- .../DependencyContextValidator/Validator.cs | 6 +- .../CompilationLibrary.cs | 6 +- .../DependencyContext.cs | 10 +- .../DependencyContextCsvReader.cs | 6 +- .../DependencyContextWriter.cs | 31 +-- .../Library.cs | 10 +- .../AppBaseCompilationAssemblyResolver.cs | 6 +- ...PackageCacheCompilationAssemblyResolver.cs | 6 +- .../PackageCompilationAssemblyResolver.cs | 2 +- .../ReferenceAssemblyPathResolver.cs | 4 +- .../Resolution/ResolverUtils.cs | 4 +- .../RuntimeLibrary.cs | 10 +- .../DependencyContextBuilderTests.cs | 177 +++++++++--------- .../DependencyContextCsvReaderTests.cs | 4 +- .../DependencyContextJsonReaderTest.cs | 22 +-- .../DependencyContextJsonWriterTests.cs | 3 +- .../PackageCacheResolverTest.cs | 4 +- .../PackageResolverTest.cs | 2 +- .../ReferenceAssemblyResolverTests.cs | 2 +- 19 files changed, 159 insertions(+), 156 deletions(-) diff --git a/TestAssets/TestProjects/DependencyContextValidator/DependencyContextValidator/Validator.cs b/TestAssets/TestProjects/DependencyContextValidator/DependencyContextValidator/Validator.cs index 81d057a29..0cfe83ce7 100644 --- a/TestAssets/TestProjects/DependencyContextValidator/DependencyContextValidator/Validator.cs +++ b/TestAssets/TestProjects/DependencyContextValidator/DependencyContextValidator/Validator.cs @@ -17,13 +17,13 @@ namespace Microsoft.Extensions.DependencyModel private static void CheckMetadata(Library library) { - if (string.Equals(library.LibraryType, "package", StringComparison.OrdinalIgnoreCase)) + if (string.Equals(library.Type, "package", StringComparison.OrdinalIgnoreCase)) { - if (string.IsNullOrWhiteSpace(library.PackageName) || + if (string.IsNullOrWhiteSpace(library.Name) || string.IsNullOrWhiteSpace(library.Hash) || string.IsNullOrWhiteSpace(library.Version)) { - Error($"Empty metadata for {library.GetType().ToString()} {library.PackageName}"); + Error($"Empty metadata for {library.GetType().ToString()} {library.Name}"); } } } diff --git a/src/Microsoft.Extensions.DependencyModel/CompilationLibrary.cs b/src/Microsoft.Extensions.DependencyModel/CompilationLibrary.cs index 73ad8187b..1d5490f0f 100644 --- a/src/Microsoft.Extensions.DependencyModel/CompilationLibrary.cs +++ b/src/Microsoft.Extensions.DependencyModel/CompilationLibrary.cs @@ -9,8 +9,8 @@ namespace Microsoft.Extensions.DependencyModel { public class CompilationLibrary : Library { - public CompilationLibrary(string libraryType, string packageName, string version, string hash, string[] assemblies, Dependency[] dependencies, bool serviceable) - : base(libraryType, packageName, version, hash, dependencies, serviceable) + public CompilationLibrary(string type, string name, string version, string hash, string[] assemblies, Dependency[] dependencies, bool serviceable) + : base(type, name, version, hash, dependencies, serviceable) { Assemblies = assemblies; } @@ -30,7 +30,7 @@ namespace Microsoft.Extensions.DependencyModel var assemblies = new List(); if (!DefaultResolver.TryResolveAssemblyPaths(this, assemblies)) { - throw new InvalidOperationException($"Can not find compilation library location for package '{PackageName}'"); + throw new InvalidOperationException($"Can not find compilation library location for package '{Name}'"); } return assemblies; } diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs index 1aa81cce5..229a090b9 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs @@ -16,7 +16,7 @@ namespace Microsoft.Extensions.DependencyModel private static readonly Lazy _defaultContext = new Lazy(LoadDefault); - public DependencyContext(string target, + public DependencyContext(string targetFramework, string runtime, bool isPortable, CompilationOptions compilationOptions, @@ -24,9 +24,9 @@ namespace Microsoft.Extensions.DependencyModel IEnumerable runtimeLibraries, IEnumerable> runtimeGraph) { - if (target == null) + if (targetFramework == null) { - throw new ArgumentNullException(nameof(target)); + throw new ArgumentNullException(nameof(targetFramework)); } if (runtime == null) { @@ -49,7 +49,7 @@ namespace Microsoft.Extensions.DependencyModel throw new ArgumentNullException(nameof(runtimeGraph)); } - Target = target; + TargetFramework = targetFramework; Runtime = runtime; IsPortable = isPortable; CompilationOptions = compilationOptions; @@ -60,7 +60,7 @@ namespace Microsoft.Extensions.DependencyModel public static DependencyContext Default => _defaultContext.Value; - public string Target { get; } + public string TargetFramework { get; } public string Runtime { get; } diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs index db2ab933c..8008fe065 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs @@ -42,8 +42,8 @@ namespace Microsoft.Extensions.DependencyModel { var identity = packageGroup.Key; runtimeLibraries.Add(new RuntimeLibrary( - libraryType: identity.Item1, - packageName: identity.Item2, + type: identity.Item1, + name: identity.Item2, version: identity.Item3, hash: identity.Item4, assemblies: packageGroup.Select(l => RuntimeAssembly.Create(l.AssetPath)).ToArray(), @@ -54,7 +54,7 @@ namespace Microsoft.Extensions.DependencyModel } return new DependencyContext( - target: string.Empty, + targetFramework: string.Empty, runtime: string.Empty, isPortable: false, compilationOptions: CompilationOptions.Default, diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs index 3a5b014a9..88be796f4 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs @@ -39,8 +39,8 @@ namespace Microsoft.Extensions.DependencyModel private JObject WriteRuntimeTargetInfo(DependencyContext context) { var target = context.IsPortable? - context.Target : - context.Target + DependencyContextStrings.VersionSeperator + context.Runtime; + context.TargetFramework : + context.TargetFramework + DependencyContextStrings.VersionSeperator + context.Runtime; return new JObject( new JProperty(DependencyContextStrings.RuntimeTargetNamePropertyName, target), @@ -51,7 +51,7 @@ namespace Microsoft.Extensions.DependencyModel private JObject WriteRuntimeGraph(DependencyContext context) { return new JObject( - new JProperty(context.Target, + new JProperty(context.TargetFramework, new JObject( context.RuntimeGraph.Select(g => new JProperty(g.Key, new JArray(g.Value))) ) @@ -76,6 +76,7 @@ namespace Microsoft.Extensions.DependencyModel AddPropertyIfNotNull(o, DependencyContextStrings.PublicSignPropertyName, compilationOptions.PublicSign); AddPropertyIfNotNull(o, DependencyContextStrings.EmitEntryPointPropertyName, compilationOptions.EmitEntryPoint); AddPropertyIfNotNull(o, DependencyContextStrings.GenerateXmlDocumentationPropertyName, compilationOptions.GenerateXmlDocumentation); + AddPropertyIfNotNull(o, DependencyContextStrings.DebugTypePropertyName, compilationOptions.DebugType); return o; } @@ -92,13 +93,13 @@ namespace Microsoft.Extensions.DependencyModel if (context.IsPortable) { return new JObject( - new JProperty(context.Target, WritePortableTarget(context.RuntimeLibraries, context.CompileLibraries)) + new JProperty(context.TargetFramework, WritePortableTarget(context.RuntimeLibraries, context.CompileLibraries)) ); } return new JObject( - new JProperty(context.Target, WriteTarget(context.CompileLibraries)), - new JProperty(context.Target + DependencyContextStrings.VersionSeperator + context.Runtime, + new JProperty(context.TargetFramework, WriteTarget(context.CompileLibraries)), + new JProperty(context.TargetFramework + DependencyContextStrings.VersionSeperator + context.Runtime, WriteTarget(context.RuntimeLibraries)) ); } @@ -107,13 +108,13 @@ namespace Microsoft.Extensions.DependencyModel { return new JObject( libraries.Select(library => - new JProperty(library.PackageName + DependencyContextStrings.VersionSeperator + library.Version, WriteTargetLibrary(library)))); + new JProperty(library.Name + DependencyContextStrings.VersionSeperator + library.Version, WriteTargetLibrary(library)))); } private JObject WritePortableTarget(IReadOnlyList runtimeLibraries, IReadOnlyList compilationLibraries) { - var runtimeLookup = runtimeLibraries.ToDictionary(l => l.PackageName); - var compileLookup = compilationLibraries.ToDictionary(l => l.PackageName); + var runtimeLookup = runtimeLibraries.ToDictionary(l => l.Name); + var compileLookup = compilationLibraries.ToDictionary(l => l.Name); var targetObject = new JObject(); @@ -130,12 +131,12 @@ namespace Microsoft.Extensions.DependencyModel Debug.Assert(compilationLibrary.Serviceable == runtimeLibrary.Serviceable); Debug.Assert(compilationLibrary.Version == runtimeLibrary.Version); Debug.Assert(compilationLibrary.Hash == runtimeLibrary.Hash); - Debug.Assert(compilationLibrary.LibraryType == runtimeLibrary.LibraryType); + Debug.Assert(compilationLibrary.Type == runtimeLibrary.Type); } var library = (Library)compilationLibrary ?? (Library)runtimeLibrary; targetObject.Add( - new JProperty(library.PackageName + DependencyContextStrings.VersionSeperator + library.Version, + new JProperty(library.Name + DependencyContextStrings.VersionSeperator + library.Version, WritePortableTargetLibrary(runtimeLibrary, compilationLibrary) ) ); @@ -187,11 +188,11 @@ namespace Microsoft.Extensions.DependencyModel libraryObject.Add(new JProperty(DependencyContextStrings.RuntimeAssembliesKey, WriteAssemblies(runtimeLibrary.Assemblies.Select(a => a.Path))) ); - if (runtimeLibrary.SubTargets.Any()) + if (runtimeLibrary.RuntimeTargets.Any()) { libraryObject.Add(new JProperty( DependencyContextStrings.RuntimeTargetsPropertyName, - new JObject(runtimeLibrary.SubTargets.SelectMany(WriteRuntimeTarget))) + new JObject(runtimeLibrary.RuntimeTargets.SelectMany(WriteRuntimeTarget))) ); } @@ -256,7 +257,7 @@ namespace Microsoft.Extensions.DependencyModel { var allLibraries = context.RuntimeLibraries.Cast().Concat(context.CompileLibraries) - .GroupBy(library => library.PackageName + DependencyContextStrings.VersionSeperator + library.Version); + .GroupBy(library => library.Name + DependencyContextStrings.VersionSeperator + library.Version); return new JObject(allLibraries.Select(libraries=> new JProperty(libraries.Key, WriteLibrary(libraries.First())))); } @@ -264,7 +265,7 @@ namespace Microsoft.Extensions.DependencyModel private JObject WriteLibrary(Library library) { return new JObject( - new JProperty(DependencyContextStrings.TypePropertyName, library.LibraryType), + new JProperty(DependencyContextStrings.TypePropertyName, library.Type), new JProperty(DependencyContextStrings.ServiceablePropertyName, library.Serviceable), new JProperty(DependencyContextStrings.Sha512PropertyName, library.Hash) ); diff --git a/src/Microsoft.Extensions.DependencyModel/Library.cs b/src/Microsoft.Extensions.DependencyModel/Library.cs index 1dcd2b169..74c5cc2ef 100644 --- a/src/Microsoft.Extensions.DependencyModel/Library.cs +++ b/src/Microsoft.Extensions.DependencyModel/Library.cs @@ -7,19 +7,19 @@ namespace Microsoft.Extensions.DependencyModel { public class Library { - public Library(string libraryType, string packageName, string version, string hash, Dependency[] dependencies, bool serviceable) + public Library(string type, string name, string version, string hash, Dependency[] dependencies, bool serviceable) { - LibraryType = libraryType; - PackageName = packageName; + Type = type; + Name = name; Version = version; Hash = hash; Dependencies = dependencies; Serviceable = serviceable; } - public string LibraryType { get; } + public string Type { get; } - public string PackageName { get; } + public string Name { get; } public string Version { get; } diff --git a/src/Microsoft.Extensions.DependencyModel/Resolution/AppBaseCompilationAssemblyResolver.cs b/src/Microsoft.Extensions.DependencyModel/Resolution/AppBaseCompilationAssemblyResolver.cs index f5f681f86..9c047365f 100644 --- a/src/Microsoft.Extensions.DependencyModel/Resolution/AppBaseCompilationAssemblyResolver.cs +++ b/src/Microsoft.Extensions.DependencyModel/Resolution/AppBaseCompilationAssemblyResolver.cs @@ -37,11 +37,11 @@ namespace Microsoft.Extensions.DependencyModel.Resolution public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) { - var isProject = string.Equals(library.LibraryType, "project", StringComparison.OrdinalIgnoreCase); + var isProject = string.Equals(library.Type, "project", StringComparison.OrdinalIgnoreCase); if (!isProject && - !string.Equals(library.LibraryType, "package", StringComparison.OrdinalIgnoreCase) && - !string.Equals(library.LibraryType, "referenceassembly", StringComparison.OrdinalIgnoreCase)) + !string.Equals(library.Type, "package", StringComparison.OrdinalIgnoreCase) && + !string.Equals(library.Type, "referenceassembly", StringComparison.OrdinalIgnoreCase)) { return false; } diff --git a/src/Microsoft.Extensions.DependencyModel/Resolution/PackageCacheCompilationAssemblyResolver.cs b/src/Microsoft.Extensions.DependencyModel/Resolution/PackageCacheCompilationAssemblyResolver.cs index 2b955b0c1..156ea0622 100644 --- a/src/Microsoft.Extensions.DependencyModel/Resolution/PackageCacheCompilationAssemblyResolver.cs +++ b/src/Microsoft.Extensions.DependencyModel/Resolution/PackageCacheCompilationAssemblyResolver.cs @@ -36,7 +36,7 @@ namespace Microsoft.Extensions.DependencyModel.Resolution public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) { - if (!string.Equals(library.LibraryType, "package", StringComparison.OrdinalIgnoreCase)) + if (!string.Equals(library.Type, "package", StringComparison.OrdinalIgnoreCase)) { return false; } @@ -46,14 +46,14 @@ namespace Microsoft.Extensions.DependencyModel.Resolution var hashSplitterPos = library.Hash.IndexOf('-'); if (hashSplitterPos <= 0 || hashSplitterPos == library.Hash.Length - 1) { - throw new InvalidOperationException($"Invalid hash entry '{library.Hash}' for package '{library.PackageName}'"); + throw new InvalidOperationException($"Invalid hash entry '{library.Hash}' for package '{library.Name}'"); } string packagePath; if (ResolverUtils.TryResolvePackagePath(_fileSystem, library, _packageCacheDirectory, out packagePath)) { var hashAlgorithm = library.Hash.Substring(0, hashSplitterPos); - var cacheHashPath = Path.Combine(packagePath, $"{library.PackageName}.{library.Version}.nupkg.{hashAlgorithm}"); + var cacheHashPath = Path.Combine(packagePath, $"{library.Name}.{library.Version}.nupkg.{hashAlgorithm}"); if (_fileSystem.File.Exists(cacheHashPath) && _fileSystem.File.ReadAllText(cacheHashPath) == library.Hash.Substring(hashSplitterPos + 1)) diff --git a/src/Microsoft.Extensions.DependencyModel/Resolution/PackageCompilationAssemblyResolver.cs b/src/Microsoft.Extensions.DependencyModel/Resolution/PackageCompilationAssemblyResolver.cs index 4f4928473..680d24e99 100644 --- a/src/Microsoft.Extensions.DependencyModel/Resolution/PackageCompilationAssemblyResolver.cs +++ b/src/Microsoft.Extensions.DependencyModel/Resolution/PackageCompilationAssemblyResolver.cs @@ -65,7 +65,7 @@ namespace Microsoft.Extensions.DependencyModel.Resolution public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) { if (string.IsNullOrEmpty(_nugetPackageDirectory) || - !string.Equals(library.LibraryType, "package", StringComparison.OrdinalIgnoreCase)) + !string.Equals(library.Type, "package", StringComparison.OrdinalIgnoreCase)) { return false; } diff --git a/src/Microsoft.Extensions.DependencyModel/Resolution/ReferenceAssemblyPathResolver.cs b/src/Microsoft.Extensions.DependencyModel/Resolution/ReferenceAssemblyPathResolver.cs index 53a36abdb..523b20eca 100644 --- a/src/Microsoft.Extensions.DependencyModel/Resolution/ReferenceAssemblyPathResolver.cs +++ b/src/Microsoft.Extensions.DependencyModel/Resolution/ReferenceAssemblyPathResolver.cs @@ -41,7 +41,7 @@ namespace Microsoft.Extensions.DependencyModel.Resolution public bool TryResolveAssemblyPaths(CompilationLibrary library, List assemblies) { - if (!string.Equals(library.LibraryType, "referenceassembly", StringComparison.OrdinalIgnoreCase)) + if (!string.Equals(library.Type, "referenceassembly", StringComparison.OrdinalIgnoreCase)) { return false; } @@ -50,7 +50,7 @@ namespace Microsoft.Extensions.DependencyModel.Resolution string fullName; if (!TryResolveReferenceAssembly(assembly, out fullName)) { - throw new InvalidOperationException($"Can not find reference assembly '{assembly}' file for package {library.PackageName}"); + throw new InvalidOperationException($"Can not find reference assembly '{assembly}' file for package {library.Name}"); } assemblies.Add(fullName); } diff --git a/src/Microsoft.Extensions.DependencyModel/Resolution/ResolverUtils.cs b/src/Microsoft.Extensions.DependencyModel/Resolution/ResolverUtils.cs index bfcf7f44f..b9b93d276 100644 --- a/src/Microsoft.Extensions.DependencyModel/Resolution/ResolverUtils.cs +++ b/src/Microsoft.Extensions.DependencyModel/Resolution/ResolverUtils.cs @@ -12,7 +12,7 @@ namespace Microsoft.Extensions.DependencyModel.Resolution { internal static bool TryResolvePackagePath(IFileSystem fileSystem, CompilationLibrary library, string basePath, out string packagePath) { - packagePath = Path.Combine(basePath, library.PackageName, library.Version); + packagePath = Path.Combine(basePath, library.Name, library.Version); if (fileSystem.Directory.Exists(packagePath)) { return true; @@ -27,7 +27,7 @@ namespace Microsoft.Extensions.DependencyModel.Resolution string fullName; if (!TryResolveAssemblyFile(fileSystem, basePath, assembly, out fullName)) { - throw new InvalidOperationException($"Can not find assembly file for package {library.PackageName} at '{fullName}'"); + throw new InvalidOperationException($"Can not find assembly file for package {library.Name} at '{fullName}'"); } yield return fullName; } diff --git a/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs b/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs index ad5aaae85..4519e8590 100644 --- a/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs +++ b/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs @@ -9,22 +9,22 @@ namespace Microsoft.Extensions.DependencyModel public class RuntimeLibrary : Library { public RuntimeLibrary( - string libraryType, - string packageName, + string type, + string name, string version, string hash, RuntimeAssembly[] assemblies, RuntimeTarget[] subTargets, Dependency[] dependencies, bool serviceable) - : base(libraryType, packageName, version, hash, dependencies, serviceable) + : base(type, name, version, hash, dependencies, serviceable) { Assemblies = assemblies; - SubTargets = subTargets; + RuntimeTargets = subTargets; } public IReadOnlyList Assemblies { get; } - public IReadOnlyList SubTargets { get; } + public IReadOnlyList RuntimeTargets { get; } } } \ No newline at end of file diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs index d62013fa9..13eaefaa0 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextBuilderTests.cs @@ -68,87 +68,12 @@ namespace Microsoft.Extensions.DependencyModel.Tests context.CompilationOptions.Platform.Should().Be("Platform"); } - - private LibraryExport Export( - LibraryDescription description, - IEnumerable compilationAssemblies = null, - IEnumerable runtimeAssemblies = null) - { - return new LibraryExport( - description, - compilationAssemblies ?? Enumerable.Empty(), - Enumerable.Empty(), - runtimeAssemblies ?? Enumerable.Empty(), - Enumerable.Empty(), - Enumerable.Empty(), - Enumerable.Empty(), - Enumerable.Empty() - ); - } - - private PackageDescription PackageDescription( - string name = null, - NuGetVersion version = null, - string hash = null, - IEnumerable dependencies = null, - bool? servicable = null) - { - return new PackageDescription( - "PATH", - new LockFilePackageLibrary() - { - Files = new string[] { }, - IsServiceable = servicable ?? false, - Name = name ?? _defaultName, - Version = version ?? _defaultVersion, - Sha512 = hash ?? _defaultHash - }, - new LockFileTargetLibrary(), - dependencies ?? Enumerable.Empty(), - true); - } - - private ProjectDescription ProjectDescription( - string name = null, - NuGetVersion version = null, - IEnumerable dependencies = null) - { - return new ProjectDescription( - new LibraryRange( - name ?? _defaultName, - new VersionRange(version ?? _defaultVersion), - LibraryType.Project, - LibraryDependencyType.Default - ), - new Project(), - dependencies ?? Enumerable.Empty(), - new TargetFrameworkInformation(), - true); - } - - private LibraryDescription ReferenceAssemblyDescription( - string name = null, - NuGetVersion version = null) - { - return new LibraryDescription( - new LibraryIdentity( - name ?? _defaultName, - version ?? _defaultVersion, - LibraryType.ReferenceAssembly), - string.Empty, // Framework assemblies don't have hashes - "PATH", - Enumerable.Empty(), - _defaultFramework, - true, - true); - } - [Fact] public void FillsRuntimeAndTarget() { - var context = Build(target: new NuGetFramework("SomeFramework",new Version(1,2)), runtime: "win8-32"); - context.Runtime.Should().Be("win8-32"); - context.Target.Should().Be("SomeFramework,Version=v1.2"); + var context = Build(target: new NuGetFramework("SomeFramework",new Version(1,2)), runtime: "win8-x86"); + context.Runtime.Should().Be("win8-x86"); + context.TargetFramework.Should().Be("SomeFramework,Version=v1.2"); } [Fact] @@ -194,16 +119,16 @@ namespace Microsoft.Extensions.DependencyModel.Tests context.RuntimeLibraries.Should().HaveCount(2); - var lib = context.RuntimeLibraries.Should().Contain(l => l.PackageName == "Pack.Age").Subject; - lib.LibraryType.Should().Be("package"); + var lib = context.RuntimeLibraries.Should().Contain(l => l.Name == "Pack.Age").Subject; + lib.Type.Should().Be("package"); lib.Serviceable.Should().BeTrue(); lib.Hash.Should().Be("sha512-Hash"); lib.Version.Should().Be("1.2.3"); lib.Dependencies.Should().OnlyContain(l => l.Name == "System.Collections" && l.Version == "3.3.3"); lib.Assemblies.Should().OnlyContain(l => l.Path == "lib/Pack.Age.dll"); - var asm = context.RuntimeLibraries.Should().Contain(l => l.PackageName == "System.Collections").Subject; - asm.LibraryType.Should().Be("referenceassembly"); + var asm = context.RuntimeLibraries.Should().Contain(l => l.Name == "System.Collections").Subject; + asm.Type.Should().Be("referenceassembly"); asm.Version.Should().Be("3.3.3"); asm.Hash.Should().BeEmpty(); asm.Dependencies.Should().BeEmpty(); @@ -242,16 +167,16 @@ namespace Microsoft.Extensions.DependencyModel.Tests context.CompileLibraries.Should().HaveCount(2); - var lib = context.CompileLibraries.Should().Contain(l => l.PackageName == "Pack.Age").Subject; - lib.LibraryType.Should().Be("package"); + var lib = context.CompileLibraries.Should().Contain(l => l.Name == "Pack.Age").Subject; + lib.Type.Should().Be("package"); lib.Serviceable.Should().BeTrue(); lib.Hash.Should().Be("sha512-Hash"); lib.Version.Should().Be("1.2.3"); lib.Dependencies.Should().OnlyContain(l => l.Name == "System.Collections" && l.Version == "3.3.3"); lib.Assemblies.Should().OnlyContain(a => a == "lib/Pack.Age.dll"); - var asm = context.CompileLibraries.Should().Contain(l => l.PackageName == "System.Collections").Subject; - asm.LibraryType.Should().Be("referenceassembly"); + var asm = context.CompileLibraries.Should().Contain(l => l.Name == "System.Collections").Subject; + asm.Type.Should().Be("referenceassembly"); asm.Version.Should().Be("3.3.3"); asm.Hash.Should().BeEmpty(); asm.Dependencies.Should().BeEmpty(); @@ -271,7 +196,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests }) }); - var asm = context.CompileLibraries.Should().Contain(l => l.PackageName == "System.Collections").Subject; + var asm = context.CompileLibraries.Should().Contain(l => l.Name == "System.Collections").Subject; asm.Assemblies.Should().OnlyContain(a => a == Path.Combine("sub", "System.Collections.dll")); } @@ -293,9 +218,85 @@ namespace Microsoft.Extensions.DependencyModel.Tests version: new NuGetVersion(3, 3, 3))) }); - var lib = context.CompileLibraries.Should().Contain(l => l.PackageName == "Pack.Age").Subject; + var lib = context.CompileLibraries.Should().Contain(l => l.Name == "Pack.Age").Subject; lib.Dependencies.Should().BeEmpty(); } + private LibraryExport Export( + LibraryDescription description, + IEnumerable compilationAssemblies = null, + IEnumerable runtimeAssemblies = null) + { + return new LibraryExport( + description, + compilationAssemblies ?? Enumerable.Empty(), + Enumerable.Empty(), + runtimeAssemblies ?? Enumerable.Empty(), + Enumerable.Empty(), + Enumerable.Empty(), + Enumerable.Empty(), + Enumerable.Empty() + ); + } + + private PackageDescription PackageDescription( + string name = null, + NuGetVersion version = null, + string hash = null, + IEnumerable dependencies = null, + bool? servicable = null) + { + return new PackageDescription( + "PATH", + new LockFilePackageLibrary() + { + Files = new string[] { }, + IsServiceable = servicable ?? false, + Name = name ?? _defaultName, + Version = version ?? _defaultVersion, + Sha512 = hash ?? _defaultHash + }, + new LockFileTargetLibrary(), + dependencies ?? Enumerable.Empty(), + true, + true); + } + + private ProjectDescription ProjectDescription( + string name = null, + NuGetVersion version = null, + IEnumerable dependencies = null) + { + return new ProjectDescription( + new LibraryRange( + name ?? _defaultName, + new VersionRange(version ?? _defaultVersion), + LibraryType.Project, + LibraryDependencyType.Default + ), + new Project(), + dependencies ?? Enumerable.Empty(), + new TargetFrameworkInformation(), + true); + } + + private LibraryDescription ReferenceAssemblyDescription( + string name = null, + NuGetVersion version = null) + { + return new LibraryDescription( + new LibraryIdentity( + name ?? _defaultName, + version ?? _defaultVersion, + LibraryType.ReferenceAssembly), + string.Empty, // Framework assemblies don't have hashes + "PATH", + Enumerable.Empty(), + _defaultFramework, + true, + true); + } + + } } diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextCsvReaderTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextCsvReaderTests.cs index e5ebe1a3c..7c19c1c69 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextCsvReaderTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextCsvReaderTests.cs @@ -32,8 +32,8 @@ namespace Microsoft.Extensions.DependencyModel.Tests "); context.RuntimeLibraries.Should().HaveCount(1); var library = context.RuntimeLibraries.Single(); - library.LibraryType.Should().Be("Package"); - library.PackageName.Should().Be("runtime.any.System.AppContext"); + library.Type.Should().Be("Package"); + library.Name.Should().Be("runtime.any.System.AppContext"); library.Version.Should().Be("4.1.0-rc2-23811"); library.Hash.Should().Be("sha512-1"); library.Assemblies.Should().HaveCount(2).And diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs index 375a22bda..4a34baf92 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs @@ -34,7 +34,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests } }"); context.IsPortable.Should().BeFalse(); - context.Target.Should().Be(".NETStandardApp,Version=v1.5"); + context.TargetFramework.Should().Be(".NETStandardApp,Version=v1.5"); context.Runtime.Should().Be("osx.10.10-x64"); } @@ -56,7 +56,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests "".NETStandardApp,Version=v1.5"": {} } }"); - context.Target.Should().Be(".NETStandardApp,Version=v1.5"); + context.TargetFramework.Should().Be(".NETStandardApp,Version=v1.5"); } [Fact] @@ -119,16 +119,16 @@ namespace Microsoft.Extensions.DependencyModel.Tests } }"); context.CompileLibraries.Should().HaveCount(2); - var project = context.CompileLibraries.Should().Contain(l => l.PackageName == "MyApp").Subject; + var project = context.CompileLibraries.Should().Contain(l => l.Name == "MyApp").Subject; project.Version.Should().Be("1.0.1"); project.Assemblies.Should().BeEquivalentTo("MyApp.dll"); - project.LibraryType.Should().Be("project"); + project.Type.Should().Be("project"); - var package = context.CompileLibraries.Should().Contain(l => l.PackageName == "System.Banana").Subject; + var package = context.CompileLibraries.Should().Contain(l => l.Name == "System.Banana").Subject; package.Version.Should().Be("1.0.0"); package.Assemblies.Should().BeEquivalentTo("ref/dotnet5.4/System.Banana.dll"); package.Hash.Should().Be("HASH-System.Banana"); - package.LibraryType.Should().Be("package"); + package.Type.Should().Be("package"); package.Serviceable.Should().Be(false); } @@ -178,20 +178,20 @@ namespace Microsoft.Extensions.DependencyModel.Tests } }"); context.CompileLibraries.Should().HaveCount(2); - var project = context.RuntimeLibraries.Should().Contain(l => l.PackageName == "MyApp").Subject; + var project = context.RuntimeLibraries.Should().Contain(l => l.Name == "MyApp").Subject; project.Version.Should().Be("1.0.1"); project.Assemblies.Should().Contain(a => a.Path == "MyApp.dll"); - project.LibraryType.Should().Be("project"); + project.Type.Should().Be("project"); - var package = context.RuntimeLibraries.Should().Contain(l => l.PackageName == "System.Banana").Subject; + var package = context.RuntimeLibraries.Should().Contain(l => l.Name == "System.Banana").Subject; package.Version.Should().Be("1.0.0"); package.Hash.Should().Be("HASH-System.Banana"); - package.LibraryType.Should().Be("package"); + package.Type.Should().Be("package"); package.Serviceable.Should().Be(false); package.Assemblies.Should().Contain(a => a.Path == "lib/dotnet5.4/System.Banana.dll"); - var target = package.SubTargets.Should().Contain(t => t.Runtime == "win7-x64").Subject; + var target = package.RuntimeTargets.Should().Contain(t => t.Runtime == "win7-x64").Subject; target.Assemblies.Should().Contain(a => a.Path == "lib/win7/System.Banana.dll"); target.NativeLibraries.Should().Contain("lib/win7/Banana.dll"); } diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs index 3f69c3cc8..fb5c79355 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs @@ -169,7 +169,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests "HASH", new [] { RuntimeAssembly.Create("Banana.dll")}, new [] - { + {Lock new RuntimeTarget("win7-x64", new [] { RuntimeAssembly.Create("Banana.Win7-x64.dll") }, new [] { "Banana.Win7-x64.so" } @@ -334,6 +334,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests optimize: true, keyFile: "Key.snk", delaySign: true, + debugType: null, publicSign: true, emitEntryPoint: true, generateXmlDocumentation: true))); diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/PackageCacheResolverTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/PackageCacheResolverTest.cs index c3879fcf3..7ea550b98 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/PackageCacheResolverTest.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/PackageCacheResolverTest.cs @@ -49,7 +49,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests var exception = Assert.Throws(() => resolver.TryResolveAssemblyPaths(library, null)); exception.Message.Should() .Contain(library.Hash) - .And.Contain(library.PackageName); + .And.Contain(library.Name); } [Fact] @@ -114,7 +114,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests var exception = Assert.Throws(() => resolver.TryResolveAssemblyPaths(library, assemblies)); exception.Message.Should() .Contain(F.SecondAssemblyPath) - .And.Contain(library.PackageName); + .And.Contain(library.Name); } private IEnvironment GetDefaultEnviroment() diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/PackageResolverTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/PackageResolverTest.cs index 35869d7fc..2eb0e095f 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/PackageResolverTest.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/PackageResolverTest.cs @@ -95,7 +95,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests var exception = Assert.Throws(() => resolver.TryResolveAssemblyPaths(library, assemblies)); exception.Message.Should() .Contain(F.SecondAssemblyPath) - .And.Contain(library.PackageName); + .And.Contain(library.Name); } } } diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/ReferenceAssemblyResolverTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/ReferenceAssemblyResolverTests.cs index 9af3e0abf..7561c77dc 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/ReferenceAssemblyResolverTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/ReferenceAssemblyResolverTests.cs @@ -153,7 +153,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests exception.Message.Should() .Contain(F.SecondAssemblyPath) - .And.Contain(library.PackageName); + .And.Contain(library.Name); } } } From 1658a4080628faf8de9b148c270db8702b5fb364 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 4 Mar 2016 15:05:29 -0800 Subject: [PATCH 09/10] Fix tests --- .../DependencyContextJsonReader.cs | 44 +++++-------------- .../DependencyContextWriter.cs | 9 +--- .../DependencyContextJsonReaderTest.cs | 31 +++++++++++-- .../DependencyContextJsonWriterTests.cs | 11 +---- 4 files changed, 42 insertions(+), 53 deletions(-) diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs index 22218d787..e16498313 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs @@ -30,8 +30,10 @@ namespace Microsoft.Extensions.DependencyModel { var runtime = string.Empty; var target = string.Empty; + var isPortable = true; + + var runtimeTargetName = root[DependencyContextStrings.RuntimeTargetPropertyName]?.Value(); - var runtimeTargetInfo = ReadRuntimeTargetInfo(root); var libraryStubs = ReadLibraryStubs((JObject) root[DependencyContextStrings.LibrariesPropertyName]); var targetsObject = (JObject) root[DependencyContextStrings.TargetsPropertyName]; @@ -45,18 +47,19 @@ namespace Microsoft.Extensions.DependencyModel compileTarget = (JObject)compileTargetProperty.Value; target = compileTargetProperty.Name; - if (!string.IsNullOrEmpty(runtimeTargetInfo.Name)) + if (!string.IsNullOrEmpty(runtimeTargetName)) { - runtimeTarget = (JObject) targetsObject[runtimeTargetInfo.Name]; + runtimeTarget = (JObject) targetsObject[runtimeTargetName]; if (runtimeTarget == null) { - throw new FormatException($"Target with name {runtimeTargetInfo.Name} not found"); + throw new FormatException($"Target with name {runtimeTargetName} not found"); } - var seperatorIndex = runtimeTargetInfo.Name.IndexOf(DependencyContextStrings.VersionSeperator); - if (seperatorIndex > -1 && seperatorIndex < runtimeTargetInfo.Name.Length) + var seperatorIndex = runtimeTargetName.IndexOf(DependencyContextStrings.VersionSeperator); + if (seperatorIndex > -1 && seperatorIndex < runtimeTargetName.Length) { - runtime = runtimeTargetInfo.Name.Substring(seperatorIndex + 1); + runtime = runtimeTargetName.Substring(seperatorIndex + 1); + isPortable = false; } } else @@ -68,7 +71,7 @@ namespace Microsoft.Extensions.DependencyModel return new DependencyContext( target, runtime, - runtimeTargetInfo.Portable, + isPortable, ReadCompilationOptions((JObject)root[DependencyContextStrings.CompilationOptionsPropertName]), ReadLibraries(compileTarget, false, libraryStubs).Cast().ToArray(), ReadLibraries(runtimeTarget, true, libraryStubs).Cast().ToArray(), @@ -91,24 +94,6 @@ namespace Microsoft.Extensions.DependencyModel } } - private RuntimeTargetInfo ReadRuntimeTargetInfo(JObject root) - { - - var runtimeTarget = (JObject)root[DependencyContextStrings.RuntimeTargetPropertyName]; - if (runtimeTarget != null) - { - return new RuntimeTargetInfo() - { - Name = runtimeTarget[DependencyContextStrings.RuntimeTargetNamePropertyName]?.Value(), - Portable = runtimeTarget[DependencyContextStrings.PortablePropertyName]?.Value() == true - }; - } - return new RuntimeTargetInfo() - { - Portable = true - }; - } - private CompilationOptions ReadCompilationOptions(JObject compilationOptionsObject) { if (compilationOptionsObject == null) @@ -268,13 +253,6 @@ namespace Microsoft.Extensions.DependencyModel public string Rid; } - private struct RuntimeTargetInfo - { - public string Name; - - public bool Portable; - } - private struct LibraryStub { public string Name; diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs index 88be796f4..a63f622d4 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs @@ -36,16 +36,11 @@ namespace Microsoft.Extensions.DependencyModel ); } - private JObject WriteRuntimeTargetInfo(DependencyContext context) + private string WriteRuntimeTargetInfo(DependencyContext context) { - var target = context.IsPortable? + return context.IsPortable? context.TargetFramework : context.TargetFramework + DependencyContextStrings.VersionSeperator + context.Runtime; - - return new JObject( - new JProperty(DependencyContextStrings.RuntimeTargetNamePropertyName, target), - new JProperty(DependencyContextStrings.PortablePropertyName, context.IsPortable) - ); } private JObject WriteRuntimeGraph(DependencyContext context) diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs index 4a34baf92..4abcbddc2 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs @@ -24,10 +24,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests { var context = Read( @"{ - ""runtimeTarget"": { - ""portable"": false, - ""name"": "".NETStandardApp,Version=v1.5/osx.10.10-x64"", - }, + ""runtimeTarget"": "".NETStandardApp,Version=v1.5/osx.10.10-x64"", ""targets"": { "".NETStandardApp,Version=v1.5"": {}, "".NETStandardApp,Version=v1.5/osx.10.10-x64"": {}, @@ -47,6 +44,32 @@ namespace Microsoft.Extensions.DependencyModel.Tests context.IsPortable.Should().BeTrue(); } + [Fact] + public void SetsPortableIfRuntimeTargetHasNoRid() + { + var context = Read( +@"{ + ""runtimeTarget"": "".NETStandardApp,Version=v1.5"", + ""targets"": { + "".NETStandardApp,Version=v1.5"": {} + } +}"); + context.IsPortable.Should().BeTrue(); + } + + [Fact] + public void SetsNotPortableIfRuntimeTargetHasRid() + { + var context = Read( +@"{ + ""runtimeTarget"": "".NETStandardApp,Version=v1.5/osx.10.10-x64"" + ""targets"": { + "".NETStandardApp,Version=v1.5/osx.10.10-x64"": {} + } +}"); + context.IsPortable.Should().BeTrue(); + } + [Fact] public void ReadsMainTarget() { diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs index fb5c79355..737ad9b4d 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs @@ -86,14 +86,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests false) ); - var runtimeTarget = result.Should().HaveProperty("runtimeTarget") - .Subject.Should().BeOfType().Subject; - - runtimeTarget.Should().HaveProperty("name") - .Subject.Value().Should().Be("Target/runtime"); - - runtimeTarget.Should().HaveProperty("portable") - .Subject.Value().Should().Be(false); + result.Should().HavePropertyValue("runtimeTarget", "Target/runtime"); } [Fact] @@ -169,7 +162,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests "HASH", new [] { RuntimeAssembly.Create("Banana.dll")}, new [] - {Lock + { new RuntimeTarget("win7-x64", new [] { RuntimeAssembly.Create("Banana.Win7-x64.dll") }, new [] { "Banana.Win7-x64.so" } From f73e447cfcd5db67616e014a4815f23f5812ea41 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 4 Mar 2016 15:09:21 -0800 Subject: [PATCH 10/10] Actually fix tests --- .../DependencyContextJsonReaderTest.cs | 10 ++++------ .../DependencyContextJsonWriterTests.cs | 9 +-------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs index 4abcbddc2..b5fb29a10 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs @@ -62,12 +62,13 @@ namespace Microsoft.Extensions.DependencyModel.Tests { var context = Read( @"{ - ""runtimeTarget"": "".NETStandardApp,Version=v1.5/osx.10.10-x64"" + ""runtimeTarget"": "".NETStandardApp,Version=v1.5/osx.10.10-x64"", ""targets"": { + "".NETStandardApp,Version=v1.5"": {}, "".NETStandardApp,Version=v1.5/osx.10.10-x64"": {} } }"); - context.IsPortable.Should().BeTrue(); + context.IsPortable.Should().BeFalse(); } [Fact] @@ -161,10 +162,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests { var context = Read( @"{ - ""runtimeTarget"": { - ""portable"": true, - ""name"": "".NETStandardApp,Version=v1.5"", - }, + ""runtimeTarget"": "".NETStandardApp,Version=v1.5"", ""targets"": { "".NETStandardApp,Version=v1.5"": { ""MyApp/1.0.1"": { diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs index 737ad9b4d..0e1639fde 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs @@ -97,14 +97,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests "runtime", true) ); - var runtimeTarget = result.Should().HaveProperty("runtimeTarget") - .Subject.Should().BeOfType().Subject; - - runtimeTarget.Should().HaveProperty("name") - .Subject.Value().Should().Be("Target"); - - runtimeTarget.Should().HaveProperty("portable") - .Subject.Value().Should().Be(true); + result.Should().HavePropertyValue("runtimeTarget", "Target"); } [Fact]