Even more tests

This commit is contained in:
Pavel Krymets 2016-03-04 09:13:04 -08:00
parent dcaea8c7ca
commit 0a0c4a830e
8 changed files with 537 additions and 84 deletions

View file

@ -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();
}
}
}

View file

@ -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);
}
}
}

View file

@ -40,8 +40,8 @@ namespace Microsoft.Extensions.DependencyModel.Tests
IReadOnlyList<KeyValuePair<string, string[]>> 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<string>().Should().BeEquivalentTo(new [] {"MY", "DEFINES" });
}
}
}