Add more tests and some reading logic
This commit is contained in:
parent
3f4c263670
commit
9bd9ca1512
10 changed files with 433 additions and 120 deletions
|
@ -0,0 +1,141 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyModel.Tests
|
||||
{
|
||||
public class DependencyContextJsonReaderTest
|
||||
{
|
||||
private DependencyContext Read(string text)
|
||||
{
|
||||
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
|
||||
{
|
||||
return new DependencyContextJsonReader().Read(stream);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadsRuntimeTargetInfo()
|
||||
{
|
||||
var context = Read(
|
||||
@"{
|
||||
""runtimeTarget"": {
|
||||
""portable"": false,
|
||||
""name"": "".NETStandardApp,Version=v1.5/osx.10.10-x64"",
|
||||
},
|
||||
""targets"": {
|
||||
"".NETStandardApp,Version=v1.5"": {},
|
||||
"".NETStandardApp,Version=v1.5/osx.10.10-x64"": {},
|
||||
}
|
||||
}");
|
||||
context.IsPortable.Should().BeFalse();
|
||||
context.Target.Should().Be(".NETStandardApp,Version=v1.5");
|
||||
context.Runtime.Should().Be("osx.10.10-x64");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultsToPortable()
|
||||
{
|
||||
var context = Read(
|
||||
@"{
|
||||
}");
|
||||
context.IsPortable.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadsMainTarget()
|
||||
{
|
||||
var context = Read(
|
||||
@"{
|
||||
""targets"": {
|
||||
"".NETStandardApp,Version=v1.5"": {}
|
||||
}
|
||||
}");
|
||||
context.Target.Should().Be(".NETStandardApp,Version=v1.5");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadsRuntimeGraph()
|
||||
{
|
||||
var context = Read(
|
||||
@"{
|
||||
""runtimes"": {
|
||||
"".NETStandardApp,Version=v1.5"": {
|
||||
""osx.10.10-x64"": [ ],
|
||||
""osx.10.11-x64"": [ ""osx"" ],
|
||||
""rhel.7-x64"": [ ""linux-x64"", ""unix"" ]
|
||||
}
|
||||
}
|
||||
}");
|
||||
context.RuntimeGraph.Should().Contain(p => p.Key == "osx.10.10-x64").Which
|
||||
.Value.Should().BeEquivalentTo();
|
||||
|
||||
context.RuntimeGraph.Should().Contain(p => p.Key == "osx.10.11-x64").Which
|
||||
.Value.Should().BeEquivalentTo("osx");
|
||||
|
||||
context.RuntimeGraph.Should().Contain(p => p.Key == "rhel.7-x64").Which
|
||||
.Value.Should().BeEquivalentTo("linux-x64", "unix");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadsCompilationTarget()
|
||||
{
|
||||
var context = Read(
|
||||
@"{
|
||||
""targets"": {
|
||||
"".NETStandardApp,Version=v1.5"": {
|
||||
""MyApp/1.0.1"": {
|
||||
""dependencies"": {
|
||||
""AspNet.Mvc"": ""1.0.0""
|
||||
},
|
||||
""compile"": {
|
||||
""MyApp.dll"": { }
|
||||
}
|
||||
},
|
||||
""System.Banana/1.0.0"": {
|
||||
""dependencies"": {
|
||||
""System.Foo"": ""1.0.0""
|
||||
},
|
||||
""compile"": {
|
||||
""ref/dotnet5.4/System.Banana.dll"": { }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
""libraries"":{
|
||||
""MyApp/1.0.1"": {
|
||||
""type"": ""project"",
|
||||
""serviceable"": true,
|
||||
""sha512"": ""HASH-MyApp""
|
||||
},
|
||||
""System.Banana/1.0.0"": {
|
||||
""type"": ""package"",
|
||||
""serviceable"": false,
|
||||
""sha512"": ""HASH-System.Banana""
|
||||
},
|
||||
}
|
||||
}");
|
||||
context.CompileLibraries.Should().HaveCount(2);
|
||||
var project = context.CompileLibraries.Should().Contain(l => l.PackageName == "MyApp").Subject;
|
||||
project.Version.Should().Be("1.0.1");
|
||||
project.Assemblies.Should().BeEquivalentTo("MyApp.dll");
|
||||
project.Hash.Should().Be("HASH-MyApp");
|
||||
project.LibraryType.Should().Be("project");
|
||||
project.Serviceable.Should().Be(true);
|
||||
project.Hash.Should().BeEquivalentTo("HASH-MyApp");
|
||||
|
||||
|
||||
var package = context.CompileLibraries.Should().Contain(l => l.PackageName == "System.Banana").Subject;
|
||||
package.Version.Should().Be("1.0.0");
|
||||
package.Assemblies.Should().BeEquivalentTo("ref/dotnet5.4/System.Banana.dll");
|
||||
package.Hash.Should().Be("HASH-System.Banana");
|
||||
package.LibraryType.Should().Be("package");
|
||||
package.Serviceable.Should().Be(false);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Xunit;
|
||||
using FluentAssertions;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyModel.Tests
|
||||
{
|
||||
public class DependencyContextJsonWriterTests
|
||||
{
|
||||
public JObject Save(DependencyContext dependencyContext)
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
new DependencyContextWriter().Write(dependencyContext, memoryStream);
|
||||
using (var readStream = new MemoryStream(memoryStream.ToArray()))
|
||||
{
|
||||
using (var textReader = new StreamReader(readStream))
|
||||
{
|
||||
using (var reader = new JsonTextReader(textReader))
|
||||
{
|
||||
return JObject.Load(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SavesRuntimeGraph()
|
||||
{
|
||||
var result = Save(new DependencyContext(
|
||||
"Target",
|
||||
"Target/runtime",
|
||||
false,
|
||||
CompilationOptions.Default,
|
||||
new CompilationLibrary[0],
|
||||
new RuntimeLibrary[0],
|
||||
new[]
|
||||
{
|
||||
new KeyValuePair<string, string[]>("win7-x64", new [] { "win6", "win5"}),
|
||||
new KeyValuePair<string, string[]>("win8-x64", new [] { "win7-x64"}),
|
||||
}));
|
||||
|
||||
var runtimes = result.Should().HaveProperty("runtimes")
|
||||
.Subject.Should().BeOfType<JObject>().Subject;
|
||||
|
||||
var rids = runtimes.Should().HaveProperty("Target")
|
||||
.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()
|
||||
{
|
||||
var result = Save(new DependencyContext(
|
||||
"Target",
|
||||
"runtime",
|
||||
false,
|
||||
CompilationOptions.Default,
|
||||
new CompilationLibrary[0],
|
||||
new RuntimeLibrary[0],
|
||||
new KeyValuePair<string, string[]>[0])
|
||||
);
|
||||
|
||||
var runtimeTarget = result.Should().HaveProperty("runtimeTarget")
|
||||
.Subject.Should().BeOfType<JObject>().Subject;
|
||||
|
||||
runtimeTarget.Should().HaveProperty("name")
|
||||
.Subject.Value<string>().Should().Be("Target/runtime");
|
||||
|
||||
runtimeTarget.Should().HaveProperty("portable")
|
||||
.Subject.Value<bool>().Should().Be(false);
|
||||
}
|
||||
[Fact]
|
||||
public void DoesNotWritesRuntimeTargetPropertyIfPortable()
|
||||
{
|
||||
var result = Save(new DependencyContext(
|
||||
"Target",
|
||||
"runtime",
|
||||
false,
|
||||
CompilationOptions.Default,
|
||||
new CompilationLibrary[0],
|
||||
new RuntimeLibrary[0],
|
||||
new KeyValuePair<string, string[]>[0])
|
||||
);
|
||||
|
||||
result.Should().NotHaveProperty("runtimeTarget");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,94 +0,0 @@
|
|||
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.Extensions.EnvironmentAbstractions;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyModel.Tests
|
||||
{
|
||||
class FileSystemMockBuilder
|
||||
{
|
||||
private Dictionary<string, string> _files = new Dictionary<string, string>();
|
||||
|
||||
public static IFileSystem Empty { get; } = Create().Build();
|
||||
|
||||
public static FileSystemMockBuilder Create()
|
||||
{
|
||||
return new FileSystemMockBuilder();
|
||||
}
|
||||
|
||||
public FileSystemMockBuilder AddFile(string name, string content = "")
|
||||
{
|
||||
_files.Add(name, content);
|
||||
return this;
|
||||
}
|
||||
|
||||
public FileSystemMockBuilder AddFiles(string basePath, params string[] files)
|
||||
{
|
||||
foreach (var file in files)
|
||||
{
|
||||
AddFile(Path.Combine(basePath, file));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public IFileSystem Build()
|
||||
{
|
||||
return new FileSystemMock(_files);
|
||||
}
|
||||
|
||||
private class FileSystemMock : IFileSystem
|
||||
{
|
||||
public FileSystemMock(Dictionary<string, string> files)
|
||||
{
|
||||
File = new FileMock(files);
|
||||
Directory = new DirectoryMock(files);
|
||||
}
|
||||
|
||||
public IFile File { get; }
|
||||
|
||||
public IDirectory Directory { get; }
|
||||
}
|
||||
|
||||
private class FileMock : IFile
|
||||
{
|
||||
private Dictionary<string, string> _files;
|
||||
public FileMock(Dictionary<string, string> files)
|
||||
{
|
||||
_files = files;
|
||||
}
|
||||
|
||||
public bool Exists(string path)
|
||||
{
|
||||
return _files.ContainsKey(path);
|
||||
}
|
||||
|
||||
public string ReadAllText(string path)
|
||||
{
|
||||
string text;
|
||||
if (!_files.TryGetValue(path, out text))
|
||||
{
|
||||
throw new FileNotFoundException(path);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
private class DirectoryMock : IDirectory
|
||||
{
|
||||
private Dictionary<string, string> _files;
|
||||
public DirectoryMock(Dictionary<string, string> files)
|
||||
{
|
||||
_files = files;
|
||||
}
|
||||
|
||||
public bool Exists(string path)
|
||||
{
|
||||
return _files.Keys.Any(k => k.StartsWith(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using FluentAssertions.Execution;
|
||||
using FluentAssertions.Primitives;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyModel.Tests
|
||||
{
|
||||
public static class JsonAssertionExtensions
|
||||
{
|
||||
public static JsonAssetions Should(this JToken jToken)
|
||||
{
|
||||
return new JsonAssetions(jToken);
|
||||
}
|
||||
}
|
||||
|
||||
public class JsonAssetions: ReferenceTypeAssertions<JToken, JsonAssetions>
|
||||
{
|
||||
public JsonAssetions(JToken token)
|
||||
{
|
||||
Subject = token;
|
||||
}
|
||||
|
||||
protected override string Context => nameof(JToken);
|
||||
|
||||
public AndWhichConstraint<JsonAssetions, JToken> HaveProperty(string expected)
|
||||
{
|
||||
var token = Subject[expected];
|
||||
Execute.Assertion
|
||||
.ForCondition(token != null)
|
||||
.FailWith($"Expected {Subject} to have property '" + expected + "'");
|
||||
|
||||
return new AndWhichConstraint<JsonAssetions, JToken>(this, token);
|
||||
}
|
||||
|
||||
public AndConstraint<JsonAssetions> NotHaveProperty(string expected)
|
||||
{
|
||||
var token = Subject[expected];
|
||||
Execute.Assertion
|
||||
.ForCondition(token == null)
|
||||
.FailWith($"Expected {Subject} not to have property '" + expected + "'");
|
||||
|
||||
return new AndConstraint<JsonAssetions>(this);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue