dotnet-installer/test/Microsoft.Extensions.DependencyModel.Tests/DependencyContextJsonWriterTests.cs

415 lines
20 KiB
C#
Raw Normal View History

2016-03-01 23:11:52 +00:00
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);
}
}
}
}
}
2016-03-02 23:31:13 +00:00
public DependencyContext Create(
string target = null,
string runtime = null,
bool? isPortable = null,
CompilationOptions compilationOptions = null,
CompilationLibrary[] compileLibraries = null,
RuntimeLibrary[] runtimeLibraries = null,
IReadOnlyList<KeyValuePair<string, string[]>> runtimeGraph = null)
{
return new DependencyContext(
2016-03-04 17:13:04 +00:00
target ?? string.Empty,
runtime ?? string.Empty,
2016-03-02 23:31:13 +00:00
isPortable ?? false,
compilationOptions ?? CompilationOptions.Default,
compileLibraries ?? new CompilationLibrary[0],
runtimeLibraries ?? new RuntimeLibrary[0],
runtimeGraph ?? new KeyValuePair<string, string[]>[0]
);
}
2016-03-01 23:11:52 +00:00
[Fact]
public void SavesRuntimeGraph()
{
2016-03-02 23:31:13 +00:00
var result = Save(Create(
2016-03-01 23:11:52 +00:00
"Target",
"Target/runtime",
2016-03-02 23:31:13 +00:00
runtimeGraph: new[]
2016-03-01 23:11:52 +00:00
{
new KeyValuePair<string, string[]>("win7-x64", new [] { "win6", "win5"}),
new KeyValuePair<string, string[]>("win8-x64", new [] { "win7-x64"}),
}));
var rids = result.Should().HaveProperty("runtimes")
2016-03-01 23:11:52 +00:00
.Subject.Should().BeOfType<JObject>().Subject;
rids.Should().HaveProperty("win7-x64")
.Subject.Should().BeOfType<JArray>()
.Which.Values<string>().ShouldBeEquivalentTo(new[] { "win6", "win5" });
rids.Should().HaveProperty("win8-x64")
.Subject.Should().BeOfType<JArray>()
.Which.Values<string>().ShouldBeEquivalentTo(new[] { "win7-x64" });
}
[Fact]
public void WritesRuntimeTargetPropertyIfNotPortable()
{
2016-03-02 23:31:13 +00:00
var result = Save(Create(
2016-03-01 23:11:52 +00:00
"Target",
"runtime",
2016-03-02 23:31:13 +00:00
false)
2016-03-01 23:11:52 +00:00
);
2016-03-04 23:05:29 +00:00
result.Should().HavePropertyValue("runtimeTarget", "Target/runtime");
2016-03-01 23:11:52 +00:00
}
2016-03-02 23:31:13 +00:00
2016-03-01 23:11:52 +00:00
[Fact]
2016-03-02 23:31:13 +00:00
public void WritesMainTargetNameToRuntimeTargetIfPortable()
2016-03-01 23:11:52 +00:00
{
2016-03-02 23:31:13 +00:00
var result = Save(Create(
2016-03-01 23:11:52 +00:00
"Target",
"runtime",
2016-03-02 23:31:13 +00:00
true)
2016-03-01 23:11:52 +00:00
);
2016-03-04 23:09:21 +00:00
result.Should().HavePropertyValue("runtimeTarget", "Target");
2016-03-02 23:31:13 +00:00
}
[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 ResourceAssembly("en-US\\Banana.Resource.dll", "en-US")},
2016-03-02 23:31:13 +00:00
new []
2016-03-04 23:05:29 +00:00
{
2016-03-02 23:31:13 +00:00
new RuntimeTarget("win7-x64",
new [] { RuntimeAssembly.Create("Banana.Win7-x64.dll") },
new [] { "native\\Banana.Win7-x64.so" }
2016-03-02 23:31:13 +00:00
)
},
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");
2016-03-04 17:13:04 +00:00
var runtimeTargets = library.Should().HavePropertyAsObject("runtimeTargets").Subject;
2016-03-02 23:31:13 +00:00
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("native/Banana.Win7-x64.so").Subject;
2016-03-02 23:31:13 +00:00
nativeLibrary.Should().HavePropertyValue("rid", "win7-x64");
nativeLibrary.Should().HavePropertyValue("assetType", "native");
2016-03-01 23:11:52 +00:00
var resourceAssemblies = library.Should().HavePropertyAsObject("resources").Subject;
var resourceAssembly = resourceAssemblies.Should().HavePropertyAsObject("en-US/Banana.Resource.dll").Subject;
resourceAssembly.Should().HavePropertyValue("locale", "en-US");
2016-03-02 23:31:13 +00:00
//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);
2016-03-01 23:11:52 +00:00
}
2016-03-04 17:13:04 +00:00
[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 ResourceAssembly[] {},
2016-03-04 17:13:04 +00:00
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 ResourceAssembly[] {},
2016-03-04 17:13:04 +00:00
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 WritesResourceAssembliesForNonPortable()
{
var result = Save(Create(
"Target",
"runtime",
false,
runtimeLibraries: new[]
{
new RuntimeLibrary(
"package",
"PackageName",
"1.2.3",
"HASH",
new RuntimeAssembly[] { },
new []
{
new ResourceAssembly("en-US/Fruits.resources.dll", "en-US")
},
new RuntimeTarget[] { },
new Dependency[] { },
true
),
}));
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 resources = library.Should().HavePropertyAsObject("resources").Subject;
var resource = resources.Should().HavePropertyAsObject("en-US/Fruits.resources.dll").Subject;
resource.Should().HavePropertyValue("locale", "en-US");
}
[Fact]
public void WritesResourceAssembliesForPortable()
{
var result = Save(Create(
"Target",
"runtime",
true,
runtimeLibraries: new[]
{
new RuntimeLibrary(
"package",
"PackageName",
"1.2.3",
"HASH",
new RuntimeAssembly[] { },
new []
{
new ResourceAssembly("en-US/Fruits.resources.dll", "en-US")
},
new RuntimeTarget[] { },
new Dependency[] { },
true
),
}));
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 resources = library.Should().HavePropertyAsObject("resources").Subject;
var resource = resources.Should().HavePropertyAsObject("en-US/Fruits.resources.dll").Subject;
resource.Should().HavePropertyValue("locale", "en-US");
}
2016-03-04 17:13:04 +00:00
[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,
2016-03-04 22:12:16 +00:00
debugType: null,
2016-03-04 17:13:04 +00:00
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" });
}
2016-03-01 23:11:52 +00:00
}
2016-03-02 23:31:13 +00:00
}