Even more tests
This commit is contained in:
parent
dcaea8c7ca
commit
0a0c4a830e
8 changed files with 537 additions and 84 deletions
|
@ -13,25 +13,44 @@ using NuGet.Frameworks;
|
||||||
|
|
||||||
namespace Microsoft.Extensions.DependencyModel
|
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)
|
private readonly string _referenceAssembliesPath;
|
||||||
{
|
|
||||||
var dependencies = libraryExporter.GetAllExports();
|
|
||||||
|
|
||||||
// Sometimes we have package and reference assembly with the same name (System.Runtime for example) thats why we
|
public DependencyContextBuilder() : this(FrameworkReferenceResolver.Default.ReferenceAssembliesPath)
|
||||||
// deduplicating them prefering reference assembly
|
{
|
||||||
var dependencyLookup = dependencies
|
}
|
||||||
.OrderBy(export => export.Library.Identity.Type == LibraryType.ReferenceAssembly)
|
|
||||||
.GroupBy(export => export.Library.Identity.Name)
|
public DependencyContextBuilder(string referenceAssembliesPath)
|
||||||
.Select(exports => exports.First())
|
{
|
||||||
.Select(export => new Dependency(export.Library.Identity.Name, export.Library.Identity.Version.ToString()))
|
_referenceAssembliesPath = referenceAssembliesPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DependencyContext Build(CommonCompilerOptions compilerOptions,
|
||||||
|
IEnumerable<LibraryExport> compilationExports,
|
||||||
|
IEnumerable<LibraryExport> runtimeExports,
|
||||||
|
NuGetFramework target,
|
||||||
|
string runtime)
|
||||||
|
{
|
||||||
|
if (compilationExports == null)
|
||||||
|
{
|
||||||
|
compilationExports = Enumerable.Empty<LibraryExport>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var dependencyLookup = compilationExports
|
||||||
|
.Concat(runtimeExports)
|
||||||
|
.Select(export => export.Library.Identity)
|
||||||
|
.Distinct()
|
||||||
|
.Select(identity => new Dependency(identity.Name, identity.Version.ToString()))
|
||||||
.ToDictionary(dependency => dependency.Name);
|
.ToDictionary(dependency => dependency.Name);
|
||||||
|
|
||||||
return new DependencyContext(target.DotNetFrameworkName, runtime, false,
|
return new DependencyContext(
|
||||||
|
target.DotNetFrameworkName,
|
||||||
|
runtime,
|
||||||
|
false,
|
||||||
GetCompilationOptions(compilerOptions),
|
GetCompilationOptions(compilerOptions),
|
||||||
GetLibraries(dependencies, dependencyLookup, target, configuration, runtime: false).Cast<CompilationLibrary>().ToArray(),
|
GetLibraries(compilationExports, dependencyLookup, runtime: false).Cast<CompilationLibrary>().ToArray(),
|
||||||
GetLibraries(dependencies, dependencyLookup, target, configuration, runtime: true).Cast<RuntimeLibrary>().ToArray(),
|
GetLibraries(runtimeExports, dependencyLookup, runtime: true).Cast<RuntimeLibrary>().ToArray(),
|
||||||
new KeyValuePair<string, string[]>[0]);
|
new KeyValuePair<string, string[]>[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,18 +70,14 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
compilerOptions.GenerateXmlDocumentation);
|
compilerOptions.GenerateXmlDocumentation);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<Library> GetLibraries(IEnumerable<LibraryExport> dependencies,
|
private IEnumerable<Library> GetLibraries(IEnumerable<LibraryExport> exports,
|
||||||
IDictionary<string, Dependency> dependencyLookup,
|
IDictionary<string, Dependency> dependencyLookup,
|
||||||
NuGetFramework target,
|
|
||||||
string configuration,
|
|
||||||
bool runtime)
|
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,
|
private Library GetLibrary(LibraryExport export,
|
||||||
NuGetFramework target,
|
|
||||||
string configuration,
|
|
||||||
bool runtime,
|
bool runtime,
|
||||||
IDictionary<string, Dependency> dependencyLookup)
|
IDictionary<string, Dependency> dependencyLookup)
|
||||||
{
|
{
|
||||||
|
@ -73,11 +88,9 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
|
|
||||||
var libraryAssets = runtime ? export.RuntimeAssemblies : export.CompilationAssemblies;
|
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
|
LibraryRange libraryDependency = libraryDependenciesGroup;
|
||||||
.OrderByDescending(d => d.Target == LibraryType.ReferenceAssembly)
|
|
||||||
.First();
|
|
||||||
|
|
||||||
Dependency dependency;
|
Dependency dependency;
|
||||||
if (dependencyLookup.TryGetValue(libraryDependency.Name, out dependency))
|
if (dependencyLookup.TryGetValue(libraryDependency.Name, out dependency))
|
||||||
|
@ -87,19 +100,7 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
}
|
}
|
||||||
|
|
||||||
string[] assemblies;
|
string[] assemblies;
|
||||||
if (type == LibraryType.Project)
|
if (type == LibraryType.ReferenceAssembly)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
assemblies = ResolveReferenceAssembliesPath(libraryAssets);
|
assemblies = ResolveReferenceAssembliesPath(libraryAssets);
|
||||||
}
|
}
|
||||||
|
@ -135,11 +136,11 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string[] ResolveReferenceAssembliesPath(IEnumerable<LibraryAsset> libraryAssets)
|
private string[] ResolveReferenceAssembliesPath(IEnumerable<LibraryAsset> libraryAssets)
|
||||||
{
|
{
|
||||||
var resolvedPaths = new List<string>();
|
var resolvedPaths = new List<string>();
|
||||||
var referenceAssembliesPath =
|
var referenceAssembliesPath =
|
||||||
PathUtility.EnsureTrailingSlash(FrameworkReferenceResolver.Default.ReferenceAssembliesPath);
|
PathUtility.EnsureTrailingSlash(_referenceAssembliesPath);
|
||||||
foreach (var libraryAsset in libraryAssets)
|
foreach (var libraryAsset in libraryAssets)
|
||||||
{
|
{
|
||||||
// If resolved path is under ReferenceAssembliesPath store it as a relative to it
|
// If resolved path is under ReferenceAssembliesPath store it as a relative to it
|
||||||
|
|
|
@ -23,6 +23,31 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
RuntimeLibrary[] runtimeLibraries,
|
RuntimeLibrary[] runtimeLibraries,
|
||||||
IReadOnlyList<KeyValuePair<string, string[]>> runtimeGraph)
|
IReadOnlyList<KeyValuePair<string, string[]>> 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;
|
Target = target;
|
||||||
Runtime = runtime;
|
Runtime = runtime;
|
||||||
IsPortable = isPortable;
|
IsPortable = isPortable;
|
||||||
|
|
|
@ -28,8 +28,8 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
|
|
||||||
private DependencyContext Read(JObject root)
|
private DependencyContext Read(JObject root)
|
||||||
{
|
{
|
||||||
string runtime = null;
|
string runtime = string.Empty;
|
||||||
string target = null;
|
string target = string.Empty;
|
||||||
|
|
||||||
var runtimeTargetInfo = ReadRuntimeTargetInfo(root);
|
var runtimeTargetInfo = ReadRuntimeTargetInfo(root);
|
||||||
var libraryStubs = ReadLibraryStubs((JObject) root[DependencyContextStrings.LibrariesPropertyName]);
|
var libraryStubs = ReadLibraryStubs((JObject) root[DependencyContextStrings.LibrariesPropertyName]);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.Extensions.PlatformAbstractions;
|
using Microsoft.Extensions.PlatformAbstractions;
|
||||||
|
@ -105,6 +106,10 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
{
|
{
|
||||||
o[DependencyContextStrings.EmitEntryPointPropertyName] = compilationOptions.EmitEntryPoint;
|
o[DependencyContextStrings.EmitEntryPointPropertyName] = compilationOptions.EmitEntryPoint;
|
||||||
}
|
}
|
||||||
|
if (compilationOptions.GenerateXmlDocumentation != null)
|
||||||
|
{
|
||||||
|
o[DependencyContextStrings.GenerateXmlDocumentationPropertyName] = compilationOptions.GenerateXmlDocumentation;
|
||||||
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,19 +118,15 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
if (context.IsPortable)
|
if (context.IsPortable)
|
||||||
{
|
{
|
||||||
return new JObject(
|
return new JObject(
|
||||||
new JProperty(context.Target, WriteTarget(context.CompileLibraries)),
|
new JProperty(context.Target, WritePortableTarget(context.RuntimeLibraries, 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))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<Library> libraries)
|
private JObject WriteTarget(IReadOnlyList<Library> libraries)
|
||||||
|
@ -135,6 +136,40 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
new JProperty(library.PackageName + DependencyContextStrings.VersionSeperator + library.Version, WriteTargetLibrary(library))));
|
new JProperty(library.PackageName + DependencyContextStrings.VersionSeperator + library.Version, WriteTargetLibrary(library))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private JObject WritePortableTarget(IReadOnlyList<RuntimeLibrary> runtimeLibraries, IReadOnlyList<CompilationLibrary> 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)
|
private JObject WriteTargetLibrary(Library library)
|
||||||
{
|
{
|
||||||
string propertyName;
|
string propertyName;
|
||||||
|
@ -167,45 +202,76 @@ namespace Microsoft.Extensions.DependencyModel
|
||||||
WriteAssemblies(assemblies))
|
WriteAssemblies(assemblies))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
private JObject WritePortableTargetLibrary(RuntimeLibrary compilationLibrary, CompilationLibrary runtimeLibrary)
|
|
||||||
|
private JObject WritePortableTargetLibrary(RuntimeLibrary runtimeLibrary, CompilationLibrary compilationLibrary)
|
||||||
{
|
{
|
||||||
var libraryObject = new JObject();
|
var libraryObject = new JObject();
|
||||||
|
var dependencies = new HashSet<Dependency>();
|
||||||
string propertyName;
|
|
||||||
string[] assemblies;
|
|
||||||
|
|
||||||
if (runtimeLibrary != null)
|
if (runtimeLibrary != null)
|
||||||
{
|
{
|
||||||
propertyName = DependencyContextStrings.RuntimeAssembliesKey;
|
libraryObject.Add(new JProperty(DependencyContextStrings.RuntimeAssembliesKey,
|
||||||
assemblies = runtimeLibrary.Assemblies.Select(assembly => assembly.Path).ToArray();
|
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)
|
if (compilationLibrary != null)
|
||||||
{
|
{
|
||||||
propertyName = DependencyContextStrings.CompileTimeAssembliesKey;
|
libraryObject.Add(new JProperty(DependencyContextStrings.CompileTimeAssembliesKey,
|
||||||
compilationAssemblies = compilationLibrary.Assemblies.ToArray();
|
WriteAssemblies(compilationLibrary.Assemblies))
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return new JObject(
|
|
||||||
new JProperty(DependencyContextStrings.DependenciesPropertyName, WriteDependencies(library.Dependencies)),
|
|
||||||
new JProperty(propertyName,
|
|
||||||
WriteAssemblies(assemblies))
|
|
||||||
);
|
);
|
||||||
|
dependencies.UnionWith(compilationLibrary.Dependencies);
|
||||||
|
}
|
||||||
|
|
||||||
|
libraryObject.Add(
|
||||||
|
new JProperty(DependencyContextStrings.DependenciesPropertyName, WriteDependencies(dependencies)));
|
||||||
|
|
||||||
|
return libraryObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JObject WriteAssemblies(IReadOnlyList<string> assemblies)
|
private IEnumerable<JProperty> 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<JProperty> WriteRuntimeTargetAssemblies(IEnumerable<string> 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<string> assemblies)
|
||||||
{
|
{
|
||||||
return new JObject(assemblies.Select(assembly => new JProperty(assembly, new JObject())));
|
return new JObject(assemblies.Select(assembly => new JProperty(assembly, new JObject())));
|
||||||
}
|
}
|
||||||
|
|
||||||
private JObject WriteDependencies(IReadOnlyList<Dependency> dependencies)
|
private JObject WriteDependencies(IEnumerable<Dependency> dependencies)
|
||||||
{
|
{
|
||||||
return new JObject(
|
return new JObject(
|
||||||
dependencies.Select(dependency => new JProperty(dependency.Name, dependency.Version))
|
dependencies.Select(dependency => new JProperty(dependency.Name, dependency.Version))
|
||||||
|
|
|
@ -111,11 +111,12 @@ namespace Microsoft.DotNet.Tools.Compiler
|
||||||
|
|
||||||
if (compilationOptions.PreserveCompilationContext == true)
|
if (compilationOptions.PreserveCompilationContext == true)
|
||||||
{
|
{
|
||||||
var dependencyContext = DependencyContextBuilder.Build(compilationOptions,
|
var allExports = exporter.GetAllExports().ToList();
|
||||||
exporter,
|
var dependencyContext = new DependencyContextBuilder().Build(compilationOptions,
|
||||||
args.ConfigValue,
|
allExports,
|
||||||
|
allExports,
|
||||||
context.TargetFramework,
|
context.TargetFramework,
|
||||||
context.RuntimeIdentifier);
|
context.RuntimeIdentifier ?? string.Empty);
|
||||||
|
|
||||||
var writer = new DependencyContextWriter();
|
var writer = new DependencyContextWriter();
|
||||||
var depsJsonFile = Path.Combine(intermediateOutputPath, context.ProjectFile.Name + "dotnet-compile.deps.json");
|
var depsJsonFile = Path.Combine(intermediateOutputPath, context.ProjectFile.Name + "dotnet-compile.deps.json");
|
||||||
|
|
|
@ -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<LibraryExport> compilationExports = null,
|
||||||
|
IEnumerable<LibraryExport> 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<LibraryAsset> compilationAssemblies = null,
|
||||||
|
IEnumerable<LibraryAsset> runtimeAssemblies = null)
|
||||||
|
{
|
||||||
|
return new LibraryExport(
|
||||||
|
description,
|
||||||
|
compilationAssemblies ?? Enumerable.Empty<LibraryAsset>(),
|
||||||
|
Enumerable.Empty<LibraryAsset>(),
|
||||||
|
runtimeAssemblies ?? Enumerable.Empty<LibraryAsset>(),
|
||||||
|
Enumerable.Empty<LibraryAsset>(),
|
||||||
|
Enumerable.Empty<LibraryAsset>(),
|
||||||
|
Enumerable.Empty<LibraryAsset>(),
|
||||||
|
Enumerable.Empty<AnalyzerReference>()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private PackageDescription PackageDescription(
|
||||||
|
string name = null,
|
||||||
|
NuGetVersion version = null,
|
||||||
|
string hash = null,
|
||||||
|
IEnumerable<LibraryRange> 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<LibraryRange>(),
|
||||||
|
true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ProjectDescription ProjectDescription(
|
||||||
|
string name = null,
|
||||||
|
NuGetVersion version = null,
|
||||||
|
IEnumerable<LibraryRange> dependencies = null)
|
||||||
|
{
|
||||||
|
return new ProjectDescription(
|
||||||
|
new LibraryRange(
|
||||||
|
name ?? _defaultName,
|
||||||
|
new VersionRange(version ?? _defaultVersion),
|
||||||
|
LibraryType.Project,
|
||||||
|
LibraryDependencyType.Default
|
||||||
|
),
|
||||||
|
new Project(),
|
||||||
|
dependencies ?? Enumerable.Empty<LibraryRange>(),
|
||||||
|
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<LibraryRange>(),
|
||||||
|
_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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -196,6 +196,36 @@ namespace Microsoft.Extensions.DependencyModel.Tests
|
||||||
target.NativeLibraries.Should().Contain("lib/win7/Banana.dll");
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,8 +40,8 @@ namespace Microsoft.Extensions.DependencyModel.Tests
|
||||||
IReadOnlyList<KeyValuePair<string, string[]>> runtimeGraph = null)
|
IReadOnlyList<KeyValuePair<string, string[]>> runtimeGraph = null)
|
||||||
{
|
{
|
||||||
return new DependencyContext(
|
return new DependencyContext(
|
||||||
target,
|
target ?? string.Empty,
|
||||||
runtime,
|
runtime ?? string.Empty,
|
||||||
isPortable ?? false,
|
isPortable ?? false,
|
||||||
compilationOptions ?? CompilationOptions.Default,
|
compilationOptions ?? CompilationOptions.Default,
|
||||||
compileLibraries ?? new CompilationLibrary[0],
|
compileLibraries ?? new CompilationLibrary[0],
|
||||||
|
@ -191,7 +191,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests
|
||||||
library.Should().HavePropertyAsObject("runtime")
|
library.Should().HavePropertyAsObject("runtime")
|
||||||
.Subject.Should().HaveProperty("Banana.dll");
|
.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;
|
var runtimeAssembly = runtimeTargets.Should().HavePropertyAsObject("Banana.Win7-x64.dll").Subject;
|
||||||
runtimeAssembly.Should().HavePropertyValue("rid", "win7-x64");
|
runtimeAssembly.Should().HavePropertyValue("rid", "win7-x64");
|
||||||
|
@ -208,5 +208,149 @@ namespace Microsoft.Extensions.DependencyModel.Tests
|
||||||
library.Should().HavePropertyValue("type", "package");
|
library.Should().HavePropertyValue("type", "package");
|
||||||
library.Should().HavePropertyValue("serviceable", true);
|
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<string>().Should().BeEquivalentTo(new [] {"MY", "DEFINES" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue