From 665dc9bcce360fa8e51c0b20e59f5aca3473bb49 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Wed, 16 Mar 2016 16:31:38 -0700 Subject: [PATCH 1/2] Add null checks everywhere in dependency model --- .../CompilationLibrary.cs | 4 ++++ .../CompilationOptions.cs | 11 +++++++--- .../Dependency.cs | 9 ++++++++ .../DependencyContext.cs | 8 ++----- .../DependencyContextCsvReader.cs | 4 ++++ .../DependencyContextJsonReader.cs | 4 ++++ .../DependencyContextLoader.cs | 4 ++-- .../DependencyContextWriter.cs | 8 +++++++ .../Library.cs | 22 +++++++++++++++++++ .../ResourceAssembly.cs | 10 +++++++++ .../RuntimeAssembly.cs | 8 +++++++ .../RuntimeFallbacks.cs | 4 ++-- .../RuntimeLibrary.cs | 21 ++++++++++++++++-- .../RuntimeTarget.cs | 13 +++++++++++ 14 files changed, 115 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.Extensions.DependencyModel/CompilationLibrary.cs b/src/Microsoft.Extensions.DependencyModel/CompilationLibrary.cs index 2a85c2f8e..435068ceb 100644 --- a/src/Microsoft.Extensions.DependencyModel/CompilationLibrary.cs +++ b/src/Microsoft.Extensions.DependencyModel/CompilationLibrary.cs @@ -19,6 +19,10 @@ namespace Microsoft.Extensions.DependencyModel bool serviceable) : base(type, name, version, hash, dependencies, serviceable) { + if (assemblies == null) + { + throw new ArgumentNullException(nameof(assemblies)); + } Assemblies = assemblies.ToArray(); } diff --git a/src/Microsoft.Extensions.DependencyModel/CompilationOptions.cs b/src/Microsoft.Extensions.DependencyModel/CompilationOptions.cs index d887671cc..f37e553cb 100644 --- a/src/Microsoft.Extensions.DependencyModel/CompilationOptions.cs +++ b/src/Microsoft.Extensions.DependencyModel/CompilationOptions.cs @@ -1,6 +1,7 @@ // 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; using System.Collections.Generic; using System.Linq; @@ -8,7 +9,7 @@ namespace Microsoft.Extensions.DependencyModel { public class CompilationOptions { - public IEnumerable Defines { get; } + public IReadOnlyList Defines { get; } public string LanguageVersion { get; } @@ -26,7 +27,7 @@ namespace Microsoft.Extensions.DependencyModel public bool? PublicSign { get; } - public string DebugType { get; } + public string DebugType { get; } public bool? EmitEntryPoint { get; } @@ -59,7 +60,11 @@ namespace Microsoft.Extensions.DependencyModel bool? emitEntryPoint, bool? generateXmlDocumentation) { - Defines = defines; + if (defines == null) + { + throw new ArgumentNullException(nameof(defines)); + } + Defines = defines.ToArray(); LanguageVersion = languageVersion; Platform = platform; AllowUnsafe = allowUnsafe; diff --git a/src/Microsoft.Extensions.DependencyModel/Dependency.cs b/src/Microsoft.Extensions.DependencyModel/Dependency.cs index 8060cb4b1..76a75fa74 100644 --- a/src/Microsoft.Extensions.DependencyModel/Dependency.cs +++ b/src/Microsoft.Extensions.DependencyModel/Dependency.cs @@ -1,6 +1,7 @@ // 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; using Microsoft.Extensions.Internal; namespace Microsoft.Extensions.DependencyModel @@ -9,6 +10,14 @@ namespace Microsoft.Extensions.DependencyModel { public Dependency(string name, string version) { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentException(nameof(name)); + } + if (string.IsNullOrEmpty(version)) + { + throw new ArgumentException(nameof(version)); + } Name = name; Version = version; } diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs index f4f3e34ba..d955b02e1 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs @@ -21,13 +21,9 @@ namespace Microsoft.Extensions.DependencyModel IEnumerable runtimeLibraries, IEnumerable runtimeGraph) { - if (targetFramework == null) + if (string.IsNullOrEmpty(targetFramework)) { - throw new ArgumentNullException(nameof(targetFramework)); - } - if (runtime == null) - { - throw new ArgumentNullException(nameof(runtime)); + throw new ArgumentException(nameof(targetFramework)); } if (compilationOptions == null) { diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs index 0e1e5e54a..f79199b99 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs @@ -13,6 +13,10 @@ namespace Microsoft.Extensions.DependencyModel { public DependencyContext Read(Stream stream) { + if (stream == null) + { + throw new ArgumentNullException(nameof(stream)); + } var lines = new List(); using (var reader = new StreamReader(stream)) { diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs index 80ee0d8bd..ed8fd17da 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs @@ -14,6 +14,10 @@ namespace Microsoft.Extensions.DependencyModel { public DependencyContext Read(Stream stream) { + if (stream == null) + { + throw new ArgumentNullException(nameof(stream)); + } using (var streamReader = new StreamReader(stream)) { using (var reader = new JsonTextReader(streamReader)) diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextLoader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextLoader.cs index 4a4256bb1..39e647d70 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextLoader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextLoader.cs @@ -49,7 +49,7 @@ namespace Microsoft.Extensions.DependencyModel internal virtual bool IsEntryAssembly(Assembly assembly) { - return assembly.GetName() == Assembly.GetEntryAssembly().GetName(); + return assembly.GetName() == Assembly.GetEntryAssembly()?.GetName(); } internal virtual Stream GetResourceStream(Assembly assembly, string name) @@ -166,7 +166,7 @@ namespace Microsoft.Extensions.DependencyModel private static string[] GetHostDepsList() { - // TODO: Were going to replace this with AppContext.GetData + // TODO: We're going to replace this with AppContext.GetData var appDomainType = typeof(object).GetTypeInfo().Assembly?.GetType("System.AppDomain"); var currentDomain = appDomainType?.GetProperty("CurrentDomain")?.GetValue(null); var deps = appDomainType?.GetMethod("GetData")?.Invoke(currentDomain, new[] { "APP_CONTEXT_DEPS_FILES" }); diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs index ecc06a77b..1a6015359 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextWriter.cs @@ -16,6 +16,14 @@ namespace Microsoft.Extensions.DependencyModel { public void Write(DependencyContext context, Stream stream) { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + if (stream == null) + { + throw new ArgumentNullException(nameof(stream)); + } using (var writer = new StreamWriter(stream)) { using (var jsonWriter = new JsonTextWriter(writer) { Formatting = Formatting.Indented }) diff --git a/src/Microsoft.Extensions.DependencyModel/Library.cs b/src/Microsoft.Extensions.DependencyModel/Library.cs index a3fefef42..a522a68e2 100644 --- a/src/Microsoft.Extensions.DependencyModel/Library.cs +++ b/src/Microsoft.Extensions.DependencyModel/Library.cs @@ -1,6 +1,7 @@ // 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; using System.Linq; using System.Collections.Generic; @@ -10,6 +11,27 @@ namespace Microsoft.Extensions.DependencyModel { public Library(string type, string name, string version, string hash, IEnumerable dependencies, bool serviceable) { + if (string.IsNullOrEmpty(type)) + { + throw new ArgumentException(nameof(type)); + } + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentException(nameof(name)); + } + if (string.IsNullOrEmpty(version)) + { + throw new ArgumentException(nameof(version)); + } + // Hash could be empty for projects + if (hash == null) + { + throw new ArgumentException(nameof(hash)); + } + if (dependencies == null) + { + throw new ArgumentNullException(nameof(dependencies)); + } Type = type; Name = name; Version = version; diff --git a/src/Microsoft.Extensions.DependencyModel/ResourceAssembly.cs b/src/Microsoft.Extensions.DependencyModel/ResourceAssembly.cs index 416ed20b1..4a59c054a 100644 --- a/src/Microsoft.Extensions.DependencyModel/ResourceAssembly.cs +++ b/src/Microsoft.Extensions.DependencyModel/ResourceAssembly.cs @@ -1,12 +1,22 @@ // 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; + namespace Microsoft.Extensions.DependencyModel { public class ResourceAssembly { public ResourceAssembly(string path, string locale) { + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentException(nameof(path)); + } + if (string.IsNullOrEmpty(locale)) + { + throw new ArgumentException(nameof(locale)); + } Locale = locale; Path = path; } diff --git a/src/Microsoft.Extensions.DependencyModel/RuntimeAssembly.cs b/src/Microsoft.Extensions.DependencyModel/RuntimeAssembly.cs index 4cfc002eb..196a36c17 100644 --- a/src/Microsoft.Extensions.DependencyModel/RuntimeAssembly.cs +++ b/src/Microsoft.Extensions.DependencyModel/RuntimeAssembly.cs @@ -15,6 +15,14 @@ namespace Microsoft.Extensions.DependencyModel public RuntimeAssembly(string assemblyName, string path) { + if (string.IsNullOrEmpty(assemblyName)) + { + throw new ArgumentException(nameof(assemblyName)); + } + if (string.IsNullOrEmpty(path)) + { + throw new ArgumentException(nameof(path)); + } _assemblyName = assemblyName; Path = path; } diff --git a/src/Microsoft.Extensions.DependencyModel/RuntimeFallbacks.cs b/src/Microsoft.Extensions.DependencyModel/RuntimeFallbacks.cs index 62f716ba2..d5ff52403 100644 --- a/src/Microsoft.Extensions.DependencyModel/RuntimeFallbacks.cs +++ b/src/Microsoft.Extensions.DependencyModel/RuntimeFallbacks.cs @@ -14,9 +14,9 @@ namespace Microsoft.Extensions.DependencyModel public RuntimeFallbacks(string runtime, IEnumerable fallbacks) { - if (runtime == null) + if (string.IsNullOrEmpty(runtime)) { - throw new ArgumentNullException(nameof(runtime)); + throw new ArgumentException(nameof(runtime)); } if (fallbacks == null) { diff --git a/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs b/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs index c7858de26..cc7accd3e 100644 --- a/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs +++ b/src/Microsoft.Extensions.DependencyModel/RuntimeLibrary.cs @@ -1,6 +1,7 @@ // 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; using System.Collections.Generic; using System.Linq; @@ -21,6 +22,22 @@ namespace Microsoft.Extensions.DependencyModel bool serviceable) : base(type, name, version, hash, dependencies, serviceable) { + if (assemblies == null) + { + throw new ArgumentNullException(nameof(assemblies)); + } + if (nativeLibraries == null) + { + throw new ArgumentNullException(nameof(nativeLibraries)); + } + if (resourceAssemblies == null) + { + throw new ArgumentNullException(nameof(resourceAssemblies)); + } + if (subTargets == null) + { + throw new ArgumentNullException(nameof(subTargets)); + } Assemblies = assemblies.ToArray(); ResourceAssemblies = resourceAssemblies.ToArray(); RuntimeTargets = subTargets.ToArray(); @@ -28,8 +45,8 @@ namespace Microsoft.Extensions.DependencyModel } public IReadOnlyList Assemblies { get; } - - public IReadOnlyList NativeLibraries { get; } + + public IReadOnlyList NativeLibraries { get; } public IReadOnlyList ResourceAssemblies { get; } diff --git a/src/Microsoft.Extensions.DependencyModel/RuntimeTarget.cs b/src/Microsoft.Extensions.DependencyModel/RuntimeTarget.cs index 81771662b..709e1073d 100644 --- a/src/Microsoft.Extensions.DependencyModel/RuntimeTarget.cs +++ b/src/Microsoft.Extensions.DependencyModel/RuntimeTarget.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; @@ -7,6 +8,18 @@ namespace Microsoft.Extensions.DependencyModel { public RuntimeTarget(string runtime, IEnumerable assemblies, IEnumerable nativeLibraries) { + if (string.IsNullOrEmpty(runtime)) + { + throw new ArgumentException(nameof(runtime)); + } + if (assemblies == null) + { + throw new ArgumentNullException(nameof(assemblies)); + } + if (nativeLibraries == null) + { + throw new ArgumentNullException(nameof(nativeLibraries)); + } Runtime = runtime; Assemblies = assemblies.ToArray(); NativeLibraries = nativeLibraries.ToArray(); From 6970a757468ffec08f9a946ad4bf683bccb5d3fb Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 17 Mar 2016 08:36:37 -0700 Subject: [PATCH 2/2] Remove csv reader --- .../DependencyContextCsvReader.cs | 139 ------------------ .../DependencyContextJsonReader.cs | 65 +++++--- .../DependencyContextLoader.cs | 18 +-- .../Library.cs | 5 - .../CompositeResolverTests.cs | 20 +-- .../DependencyContextCsvReaderTests.cs | 94 ------------ .../DependencyContextJsonReaderTest.cs | 18 +-- .../DependencyContextJsonWriterTests.cs | 2 +- 8 files changed, 57 insertions(+), 304 deletions(-) delete mode 100644 src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs delete mode 100644 test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextCsvReaderTests.cs diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs deleted file mode 100644 index f79199b99..000000000 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextCsvReader.cs +++ /dev/null @@ -1,139 +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; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; - -namespace Microsoft.Extensions.DependencyModel -{ - public class DependencyContextCsvReader: IDependencyContextReader - { - public DependencyContext Read(Stream stream) - { - if (stream == null) - { - throw new ArgumentNullException(nameof(stream)); - } - var lines = new List(); - using (var reader = new StreamReader(stream)) - { - while (!reader.EndOfStream) - { - var line = new DepsFileLine(); - line.LibraryType = ReadValue(reader); - line.PackageName = ReadValue(reader); - line.PackageVersion = ReadValue(reader); - line.PackageHash = ReadValue(reader); - line.AssetType = ReadValue(reader); - line.AssetName = ReadValue(reader); - line.AssetPath = ReadValue(reader); - - if (line.AssetType == "runtime" && - !line.AssetPath.EndsWith(".ni.dll")) - { - lines.Add(line); - } - SkipWhitespace(reader); - } - } - - var runtimeLibraries = new List(); - var packageGroups = lines.GroupBy(PackageIdentity); - foreach (var packageGroup in packageGroups) - { - var identity = packageGroup.Key; - runtimeLibraries.Add(new RuntimeLibrary( - type: identity.Item1, - name: identity.Item2, - version: identity.Item3, - hash: identity.Item4, - assemblies: packageGroup.Select(l => RuntimeAssembly.Create(l.AssetPath)), - nativeLibraries: Enumerable.Empty(), - resourceAssemblies: Enumerable.Empty(), - subTargets: Enumerable.Empty(), - dependencies: Enumerable.Empty(), - serviceable: false - )); - } - - return new DependencyContext( - targetFramework: string.Empty, - runtime: string.Empty, - isPortable: false, - compilationOptions: CompilationOptions.Default, - compileLibraries: Enumerable.Empty(), - runtimeLibraries: runtimeLibraries.ToArray(), - runtimeGraph: Enumerable.Empty()); - } - - private Tuple PackageIdentity(DepsFileLine line) - { - return Tuple.Create(line.LibraryType, line.PackageName, line.PackageVersion, line.PackageHash); - } - - private void SkipWhitespace(StreamReader reader) - { - // skip all whitespace - while (!reader.EndOfStream && char.IsWhiteSpace((char)reader.Peek())) - { - reader.Read(); - } - } - - private string ReadValue(StreamReader reader) - { - SkipWhitespace(reader); - - var c = ReadSucceed(reader.Read()); - if (c != '"') - { - throw new FormatException("Deps file value should start with '\"'"); - } - - var value = new StringBuilder(); - while (ReadSucceed(reader.Peek()) != '"') - { - c = ReadSucceed(reader.Read()); - if (c == '\\') - { - value.Append(ReadSucceed(reader.Read())); - } - else - { - value.Append(c); - } - } - // Read last " - ReadSucceed(reader.Read()); - // Read comment - if (reader.Peek() == ',') - { - reader.Read(); - } - return value.ToString(); - } - - private char ReadSucceed(int c) - { - if (c == -1) - { - throw new FormatException("Unexpected end of file"); - } - return (char) c; - } - - private struct DepsFileLine - { - public string LibraryType; - public string PackageName; - public string PackageVersion; - public string PackageHash; - public string AssetType; - public string AssetName; - public string AssetPath; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs index ed8fd17da..ceb2f5c2d 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextJsonReader.cs @@ -43,35 +43,60 @@ namespace Microsoft.Extensions.DependencyModel JObject runtimeTarget = null; JObject compileTarget = null; - if (targetsObject != null) + + if (targetsObject == null) { - var compileTargetProperty = targetsObject.Properties() - .FirstOrDefault(p => !IsRuntimeTarget(p.Name)); + throw new FormatException("Dependency file does not have 'targets' section"); + } - compileTarget = (JObject)compileTargetProperty.Value; - target = compileTargetProperty.Name; - - if (!string.IsNullOrEmpty(runtimeTargetName)) + if (!string.IsNullOrEmpty(runtimeTargetName)) + { + runtimeTarget = (JObject) targetsObject[runtimeTargetName]; + if (runtimeTarget == null) { - runtimeTarget = (JObject) targetsObject[runtimeTargetName]; - if (runtimeTarget == null) - { - throw new FormatException($"Target with name {runtimeTargetName} not found"); - } + throw new FormatException($"Target with name {runtimeTargetName} not found"); + } + } + else + { + var runtimeTargetProperty = targetsObject.Properties() + .FirstOrDefault(p => IsRuntimeTarget(p.Name)); - var seperatorIndex = runtimeTargetName.IndexOf(DependencyContextStrings.VersionSeperator); - if (seperatorIndex > -1 && seperatorIndex < runtimeTargetName.Length) - { - runtime = runtimeTargetName.Substring(seperatorIndex + 1); - isPortable = false; - } + runtimeTarget = (JObject)runtimeTargetProperty?.Value; + runtimeTargetName = runtimeTargetProperty?.Name; + } + + if (runtimeTargetName != null) + { + var seperatorIndex = runtimeTargetName.IndexOf(DependencyContextStrings.VersionSeperator); + if (seperatorIndex > -1 && seperatorIndex < runtimeTargetName.Length) + { + runtime = runtimeTargetName.Substring(seperatorIndex + 1); + target = runtimeTargetName.Substring(0, seperatorIndex); + isPortable = false; } else { - runtimeTarget = compileTarget; + target = runtimeTargetName; } } + var ridlessTargetProperty = targetsObject.Properties().FirstOrDefault(p => !IsRuntimeTarget(p.Name)); + if (ridlessTargetProperty != null) + { + compileTarget = (JObject)ridlessTargetProperty.Value; + if (runtimeTarget == null) + { + runtimeTarget = compileTarget; + target = ridlessTargetProperty.Name; + } + } + + if (runtimeTarget == null) + { + throw new FormatException("No runtime target found"); + } + return new DependencyContext( target, runtime, @@ -104,7 +129,7 @@ namespace Microsoft.Extensions.DependencyModel } return new CompilationOptions( - compilationOptionsObject[DependencyContextStrings.DefinesPropertyName]?.Values(), + compilationOptionsObject[DependencyContextStrings.DefinesPropertyName]?.Values() ?? Enumerable.Empty(), compilationOptionsObject[DependencyContextStrings.LanguageVersionPropertyName]?.Value(), compilationOptionsObject[DependencyContextStrings.PlatformPropertyName]?.Value(), compilationOptionsObject[DependencyContextStrings.AllowUnsafePropertyName]?.Value(), diff --git a/src/Microsoft.Extensions.DependencyModel/DependencyContextLoader.cs b/src/Microsoft.Extensions.DependencyModel/DependencyContextLoader.cs index 39e647d70..9cc4d5f6c 100644 --- a/src/Microsoft.Extensions.DependencyModel/DependencyContextLoader.cs +++ b/src/Microsoft.Extensions.DependencyModel/DependencyContextLoader.cs @@ -14,20 +14,17 @@ namespace Microsoft.Extensions.DependencyModel private static Lazy _depsFiles = new Lazy(GetHostDepsList); private const string DepsJsonExtension = ".deps.json"; - private const string DepsExtension = ".deps"; private readonly string _entryPointDepsLocation; private readonly string _runtimeDepsLocation; private readonly IFileSystem _fileSystem; private readonly IDependencyContextReader _jsonReader; - private readonly IDependencyContextReader _csvReader; public DependencyContextLoader() : this( GetDefaultEntrypointDepsLocation(), GetDefaultRuntimeDepsLocation(), FileSystemWrapper.Default, - new DependencyContextJsonReader(), - new DependencyContextCsvReader()) + new DependencyContextJsonReader()) { } @@ -35,14 +32,12 @@ namespace Microsoft.Extensions.DependencyModel string entryPointDepsLocation, string runtimeDepsLocation, IFileSystem fileSystem, - IDependencyContextReader jsonReader, - IDependencyContextReader csvReader) + IDependencyContextReader jsonReader) { _entryPointDepsLocation = entryPointDepsLocation; _runtimeDepsLocation = runtimeDepsLocation; _fileSystem = fileSystem; _jsonReader = jsonReader; - _csvReader = csvReader; } public static DependencyContextLoader Default { get; } = new DependencyContextLoader(); @@ -132,15 +127,6 @@ namespace Microsoft.Extensions.DependencyModel } } - var depsFile = Path.ChangeExtension(assembly.Location, DepsExtension); - if (_fileSystem.File.Exists(depsFile)) - { - using (var stream = _fileSystem.File.OpenRead(depsFile)) - { - return _csvReader.Read(stream); - } - } - return null; } diff --git a/src/Microsoft.Extensions.DependencyModel/Library.cs b/src/Microsoft.Extensions.DependencyModel/Library.cs index a522a68e2..5e9b074e5 100644 --- a/src/Microsoft.Extensions.DependencyModel/Library.cs +++ b/src/Microsoft.Extensions.DependencyModel/Library.cs @@ -23,11 +23,6 @@ namespace Microsoft.Extensions.DependencyModel { throw new ArgumentException(nameof(version)); } - // Hash could be empty for projects - if (hash == null) - { - throw new ArgumentException(nameof(hash)); - } if (dependencies == null) { throw new ArgumentNullException(nameof(dependencies)); diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/CompositeResolverTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/CompositeResolverTests.cs index c78bf7d4a..eb407f548 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/CompositeResolverTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/CompositeResolverTests.cs @@ -10,7 +10,7 @@ using Moq; using Xunit; using FluentAssertions; -namespace StreamForwarderTests +namespace Microsoft.Extensions.DependencyModel.Tests { public class CompositeResolverTests { @@ -55,14 +55,7 @@ namespace StreamForwarderTests failTwo.Object }; - var library = new CompilationLibrary( - string.Empty, - string.Empty, - string.Empty, - string.Empty, - Enumerable.Empty(), - Enumerable.Empty(), - false); + var library = TestLibraryFactory.Create(); var resolver = new CompositeCompilationAssemblyResolver(resolvers); var result = resolver.TryResolveAssemblyPaths(library, null); @@ -90,14 +83,7 @@ namespace StreamForwarderTests }; var assemblies = new List(); - var library = new CompilationLibrary( - string.Empty, - string.Empty, - string.Empty, - string.Empty, - Enumerable.Empty(), - Enumerable.Empty(), - false); + var library = TestLibraryFactory.Create(); var resolver = new CompositeCompilationAssemblyResolver(resolvers); var result = resolver.TryResolveAssemblyPaths(library, assemblies); diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextCsvReaderTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextCsvReaderTests.cs deleted file mode 100644 index 7c19c1c69..000000000 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextCsvReaderTests.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; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using System.Text; -using Microsoft.Extensions.DependencyModel; -using FluentAssertions; -using Xunit; - -namespace Microsoft.Extensions.DependencyModel.Tests -{ - public class DependencyContextCsvReaderTests - { - private DependencyContext Read(string text) - { - using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text))) - { - return new DependencyContextCsvReader().Read(stream); - } - } - - [Fact] - public void GroupsAssetsCorrectlyIntoLibraries() - { - var context = Read(@" -""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll"" -""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.Runtime.dll"" -"); - context.RuntimeLibraries.Should().HaveCount(1); - var library = context.RuntimeLibraries.Single(); - 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 - .Contain(a => a.Path == "lib\\dnxcore50\\System.AppContext.dll").And - .Contain(a => a.Path == "lib\\dnxcore50\\System.Runtime.dll"); - } - - [Fact] - public void IgnoresAllButRuntimeAssets() - { - var context = Read(@" -""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll"" -""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""native"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext2.so"" -"); - context.RuntimeLibraries.Should().HaveCount(1); - var library = context.RuntimeLibraries.Single(); - library.Assemblies.Should().HaveCount(1).And - .Contain(a => a.Path == "lib\\dnxcore50\\System.AppContext.dll"); - } - - [Fact] - public void IgnoresNiDllAssemblies() - { - var context = Read(@" -""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll"" -""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.ni.dll"" -"); - context.RuntimeLibraries.Should().HaveCount(1); - var library = context.RuntimeLibraries.Single(); - library.Assemblies.Should().HaveCount(1).And - .Contain(a => a.Path == "lib\\dnxcore50\\System.AppContext.dll"); - } - - [Fact] - public void UsesTypeNameVersionAndHashToGroup() - { - var context = Read(@" -""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll"" -""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23812"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll"" -""Package"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-2"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll"" -""Package"",""runtime.any.System.AppContext2"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll"" -""Project"",""runtime.any.System.AppContext"",""4.1.0-rc2-23811"",""sha512-1"",""runtime"",""System.AppContext"",""lib\\dnxcore50\\System.AppContext.dll"" -"); - context.RuntimeLibraries.Should().HaveCount(5); - } - - [Theory] - [InlineData("text")] - [InlineData(" ")] - [InlineData("\"")] - [InlineData(@""",""")] - [InlineData(@"\\")] - public void ThrowsFormatException(string intput) - { - Assert.Throws(() => Read(intput)); - } - } -} diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs index e3cae30e5..b611ebd69 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonReaderTest.cs @@ -26,7 +26,6 @@ namespace Microsoft.Extensions.DependencyModel.Tests @"{ ""runtimeTarget"": "".NETStandardApp,Version=v1.5/osx.10.10-x64"", ""targets"": { - "".NETStandardApp,Version=v1.5"": {}, "".NETStandardApp,Version=v1.5/osx.10.10-x64"": {}, } }"); @@ -35,21 +34,11 @@ namespace Microsoft.Extensions.DependencyModel.Tests context.Runtime.Should().Be("osx.10.10-x64"); } - [Fact] - public void DefaultsToPortable() - { - var context = Read( -@"{ -}"); - context.IsPortable.Should().BeTrue(); - } - [Fact] public void SetsPortableIfRuntimeTargetHasNoRid() { var context = Read( @"{ - ""runtimeTarget"": "".NETStandardApp,Version=v1.5"", ""targets"": { "".NETStandardApp,Version=v1.5"": {} } @@ -64,7 +53,6 @@ namespace Microsoft.Extensions.DependencyModel.Tests @"{ ""runtimeTarget"": "".NETStandardApp,Version=v1.5/osx.10.10-x64"", ""targets"": { - "".NETStandardApp,Version=v1.5"": {}, "".NETStandardApp,Version=v1.5/osx.10.10-x64"": {} } }"); @@ -88,6 +76,9 @@ namespace Microsoft.Extensions.DependencyModel.Tests { var context = Read( @"{ + ""targets"": { + "".NETStandardApp,Version=v1.5/osx.10.10-x64"": {}, + }, ""runtimes"": { ""osx.10.10-x64"": [ ], ""osx.10.11-x64"": [ ""osx"" ], @@ -237,6 +228,9 @@ namespace Microsoft.Extensions.DependencyModel.Tests ""publicSign"": true, ""warningsAsErrors"": true, ""optimize"": true + }, + ""targets"": { + "".NETStandardApp,Version=v1.5/osx.10.10-x64"": {}, } }"); context.CompilationOptions.AllowUnsafe.Should().Be(true); diff --git a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs index f4d4ad721..ec7ca1a8c 100644 --- a/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs +++ b/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs @@ -40,7 +40,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests IReadOnlyList runtimeGraph = null) { return new DependencyContext( - target ?? string.Empty, + target ?? "DefaultTarget", runtime ?? string.Empty, isPortable ?? false, compilationOptions ?? CompilationOptions.Default,