projectmodelserver

This commit is contained in:
piotrp 2016-01-27 22:38:20 -08:00 committed by PiotrP
parent 9a7cab4cda
commit 63d79b06b4
45 changed files with 38 additions and 39 deletions

View file

@ -0,0 +1,24 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
namespace Microsoft.DotNet.ProjectModel.Server.Tests
{
public class DthMessage
{
public string HostId { get; set; }
public string MessageType { get; set; }
public int ContextId { get; set; }
public int Version { get; set; }
public JToken Payload { get; set; }
// for ProjectContexts message only
public Dictionary<string, int> Projects { get; set; }
}
}

View file

@ -0,0 +1,81 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Versioning;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Microsoft.DotNet.ProjectModel.Server.Tests
{
public static class DthMessageCollectionExtension
{
public static IList<DthMessage> GetMessagesByFramework(this IEnumerable<DthMessage> messages, FrameworkName targetFramework)
{
return messages.Where(msg => MatchesFramework(targetFramework, msg)).ToList();
}
public static IList<DthMessage> GetMessagesByType(this IEnumerable<DthMessage> messages, string typename)
{
return messages.Where(msg => string.Equals(msg.MessageType, typename)).ToList();
}
public static DthMessage RetrieveSingleMessage(this IEnumerable<DthMessage> messages,
string typename)
{
var result = messages.SingleOrDefault(msg => string.Equals(msg.MessageType, typename, StringComparison.Ordinal));
if (result == null)
{
if (messages.FirstOrDefault(msg => string.Equals(msg.MessageType, typename, StringComparison.Ordinal)) != null)
{
Assert.False(true, $"More than one {typename} messages exist.");
}
else
{
Assert.False(true, $"{typename} message doesn't exists.");
}
}
return result;
}
public static IEnumerable<DthMessage> ContainsMessage(this IEnumerable<DthMessage> messages,
string typename)
{
var contain = messages.FirstOrDefault(msg => string.Equals(msg.MessageType, typename, StringComparison.Ordinal)) != null;
Assert.True(contain, $"Messages collection doesn't contain message of type {typename}.");
return messages;
}
public static IEnumerable<DthMessage> AssertDoesNotContain(this IEnumerable<DthMessage> messages, string typename)
{
var notContain = messages.FirstOrDefault(msg => string.Equals(msg.MessageType, typename, StringComparison.Ordinal)) == null;
Assert.True(notContain, $"Message collection contains message of type {typename}.");
return messages;
}
private static bool MatchesFramework(FrameworkName targetFramework, DthMessage msg)
{
if (msg.Payload.Type != JTokenType.Object)
{
return false;
}
var frameworkObj = msg.Payload["Framework"];
if (frameworkObj == null || !frameworkObj.HasValues)
{
return false;
}
return string.Equals(frameworkObj.Value<string>("FrameworkName"), targetFramework.FullName, StringComparison.OrdinalIgnoreCase);
}
}
}

View file

@ -0,0 +1,78 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Microsoft.DotNet.ProjectModel.Server.Tests
{
public static class DthMessageExtension
{
public static JObject RetrieveDependency(this DthMessage message, string dependencyName)
{
Assert.NotNull(message);
Assert.Equal(MessageTypes.Dependencies, message.MessageType);
var payload = message.Payload as JObject;
Assert.NotNull(payload);
var dependency = payload[MessageTypes.Dependencies][dependencyName] as JObject;
Assert.NotNull(dependency);
Assert.Equal(dependencyName, dependency["Name"].Value<string>());
return dependency;
}
public static DthMessage EnsureNotContainDependency(this DthMessage message, string dependencyName)
{
Assert.NotNull(message);
Assert.Equal(MessageTypes.Dependencies, message.MessageType);
var payload = message.Payload as JObject;
Assert.NotNull(payload);
Assert.True(payload[MessageTypes.Dependencies][dependencyName] == null, $"Unexpected dependency {dependencyName} exists.");
return message;
}
public static JObject RetrieveDependencyDiagnosticsCollection(this DthMessage message)
{
Assert.NotNull(message);
Assert.Equal(MessageTypes.DependencyDiagnostics, message.MessageType);
var payload = message.Payload as JObject;
Assert.NotNull(payload);
return payload;
}
public static T RetrievePayloadAs<T>(this DthMessage message)
where T : JToken
{
Assert.NotNull(message);
AssertType<T>(message.Payload, "Payload");
return (T)message.Payload;
}
/// <summary>
/// Throws if the message is not generated in communication between given server and client
/// </summary>
public static DthMessage EnsureSource(this DthMessage message, DthTestServer server, DthTestClient client)
{
if (message.HostId != server.HostId)
{
throw new Exception($"{nameof(message.HostId)} doesn't match the one of server. Expected {server.HostId} but actually {message.HostId}.");
}
return message;
}
public static void AssertType<T>(object obj, string name)
{
Assert.True(obj is T, $"{name} is not of type {typeof(T).Name}.");
}
}
}

View file

@ -0,0 +1,85 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Microsoft.DotNet.ProjectModel.Server.Tests
{
public static class JArrayExtensions
{
public static JArray AssertJArrayEmpty(this JArray array)
{
Assert.NotNull(array);
Assert.Empty(array);
return array;
}
public static JArray AssertJArrayNotEmpty(this JArray array)
{
Assert.NotNull(array);
Assert.NotEmpty(array);
return array;
}
public static JArray AssertJArrayCount(this JArray array, int expectedCount)
{
Assert.NotNull(array);
Assert.Equal(expectedCount, array.Count);
return array;
}
public static JArray AssertJArrayElement<T>(this JArray array, int index, T expectedElementValue)
{
Assert.NotNull(array);
var element = array[index];
Assert.NotNull(element);
Assert.Equal(expectedElementValue, element.Value<T>());
return array;
}
public static JArray AssertJArrayContains<T>(this JArray array, T value)
{
AssertJArrayContains<T>(array, element => object.Equals(element, value));
return array;
}
public static JArray AssertJArrayContains<T>(this JArray array, Func<T, bool> critiera)
{
bool contains = false;
foreach (var element in array)
{
var value = element.Value<T>();
contains = critiera(value);
if (contains)
{
break;
}
}
Assert.True(contains, "JArray doesn't contains the specified element.");
return array;
}
public static T RetrieveArraryElementAs<T>(this JArray json, int index)
where T : JToken
{
Assert.NotNull(json);
Assert.True(index >= 0 && index < json.Count, "Index out of range");
var element = json[index];
DthMessageExtension.AssertType<T>(element, $"Element at {index}");
return (T)element;
}
}
}

View file

@ -0,0 +1,83 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Newtonsoft.Json.Linq;
using Xunit;
namespace Microsoft.DotNet.ProjectModel.Server.Tests
{
public static class JObjectExtensions
{
public static JObject AsJObject(this JToken token)
{
DthMessageExtension.AssertType<JObject>(token, nameof(JToken));
return (JObject)token;
}
public static JObject RetrieveDependencyDiagnosticsErrorAt(this JObject payload, int index)
{
Assert.NotNull(payload);
return payload.RetrievePropertyAs<JArray>("Errors")
.RetrieveArraryElementAs<JObject>(index);
}
public static T RetrieveDependencyDiagnosticsErrorAt<T>(this JObject payload, int index)
where T : JToken
{
Assert.NotNull(payload);
return payload.RetrievePropertyAs<JArray>("Errors")
.RetrieveArraryElementAs<T>(index);
}
public static T RetrievePropertyAs<T>(this JObject json, string propertyName)
where T : JToken
{
Assert.NotNull(json);
var property = json[propertyName];
Assert.NotNull(property);
DthMessageExtension.AssertType<T>(property, $"Property {propertyName}");
return (T)property;
}
public static JObject AssertProperty<T>(this JObject json, string propertyName, T expectedPropertyValue)
{
Assert.NotNull(json);
var property = json[propertyName];
Assert.NotNull(property);
Assert.Equal(expectedPropertyValue, property.Value<T>());
return json;
}
public static JObject AssertProperty<T>(this JObject json, string propertyName, Func<T, bool> assertion)
{
return AssertProperty<T>(json,
propertyName,
assertion,
value => $"Assert failed on {propertyName}.");
}
public static JObject AssertProperty<T>(this JObject json, string propertyName, Func<T, bool> assertion, Func<T, string> errorMessage)
{
Assert.NotNull(json);
var property = json[propertyName];
Assert.False(property == null, $"Property {propertyName} doesn't exist.");
var propertyValue = property.Value<T>();
Assert.False(propertyValue == null, $"Property {propertyName} of type {typeof(T).Name} doesn't exist.");
Assert.True(assertion(propertyValue),
errorMessage(propertyValue));
return json;
}
}
}

View file

@ -0,0 +1,134 @@
using System;
using System.IO;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.Logging;
namespace Microsoft.DotNet.ProjectModel.Server.Tests.Helpers
{
public class TestHelper
{
private readonly string _tempPath;
public TestHelper()
{
LoggerFactory = new LoggerFactory();
var testVerbose = Environment.GetEnvironmentVariable("DOTNET_TEST_VERBOSE");
if (testVerbose == "2")
{
LoggerFactory.AddConsole(LogLevel.Trace);
}
else if (testVerbose == "1")
{
LoggerFactory.AddConsole(LogLevel.Information);
}
else
{
LoggerFactory.AddConsole(LogLevel.Warning);
}
_tempPath = CreateTempFolder();
var dthTestProjectsFolder = Path.Combine(FindRoot(), "testapp", "DthTestProjects");
CopyFiles(dthTestProjectsFolder, _tempPath);
var logger = LoggerFactory.CreateLogger<TestHelper>();
logger.LogInformation($"Test projects are copied to {_tempPath}");
}
public ILoggerFactory LoggerFactory { get; }
public string FindSampleProject(string name)
{
var result = Path.Combine(_tempPath, "src", name);
if (Directory.Exists(result))
{
return result;
}
else
{
return null;
}
}
public string CreateSampleProject(string name)
{
var source = Path.Combine(FindRoot(), "test", name);
if (!Directory.Exists(source))
{
return null;
}
var target = Path.Combine(CreateTempFolder(), name);
CopyFiles(source, target);
return target;
}
public string BuildProjectCopy(string projectName)
{
var projectPath = FindSampleProject(projectName);
var movedProjectPath = Path.Combine(CreateTempFolder(), projectName);
CopyFiles(projectPath, movedProjectPath);
return movedProjectPath;
}
public void DeleteLockFile(string folder)
{
var lockFilePath = Path.Combine(folder, LockFile.FileName);
if (File.Exists(lockFilePath))
{
File.Delete(lockFilePath);
}
}
private static string FindRoot()
{
var solutionName = "Microsoft.DotNet.Cli.sln";
var root = new DirectoryInfo(Directory.GetCurrentDirectory());
while (root != null && root.GetFiles(solutionName).Length == 0)
{
root = Directory.GetParent(root.FullName);
}
if (root != null)
{
return root.FullName;
}
else
{
return null;
}
}
private static string CreateTempFolder()
{
var result = Path.GetTempFileName();
File.Delete(result);
Directory.CreateDirectory(result);
return result;
}
private static void CopyFiles(string sourceFolder, string targetFolder)
{
if (!Directory.Exists(targetFolder))
{
Directory.CreateDirectory(targetFolder);
}
foreach (var filePath in Directory.EnumerateFiles(sourceFolder))
{
var filename = Path.GetFileName(filePath);
File.Copy(filePath, Path.Combine(targetFolder, filename));
}
foreach (var folderPath in Directory.EnumerateDirectories(sourceFolder))
{
var folderName = new DirectoryInfo(folderPath).Name;
CopyFiles(folderPath, Path.Combine(targetFolder, folderName));
}
}
}
}