Skip to content

Commit

Permalink
Formatter redo and some PR comments changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondřej Štorc committed Sep 23, 2020
1 parent 414616b commit a3d2e74
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 727 deletions.
8 changes: 1 addition & 7 deletions src/Build/BackEnd/Node/RarNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.CodeDom;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
Expand Down Expand Up @@ -42,7 +41,7 @@ public NodeEngineShutdownReason Run(bool nodeReuse, bool lowPriority, out Except
Handshake handshake = NodeProviderOutOfProc.GetHandshake(enableNodeReuse: nodeReuse,
enableLowPriority: lowPriority, specialNode: true);

Console.WriteLine(pipeName);
CommunicationsUtilities.Trace("RAR node name: {0}", pipeName);
IRarController controller = GetController(pipeName, handshake);

Task<int> rarTask = controller.StartAsync(cts.Token);
Expand All @@ -64,11 +63,6 @@ public NodeEngineShutdownReason Run(bool nodeReuse, bool lowPriority, out Except
{
return NodeEngineShutdownReason.BuildComplete;
}
catch (Exception e)
{
shutdownException = e;
return NodeEngineShutdownReason.Error;
}

cts.Cancel();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -8,6 +12,7 @@
using Microsoft.Build.Tasks.ResolveAssemblyReferences;
using Microsoft.Build.Tasks.ResolveAssemblyReferences.Client;
using Microsoft.Build.Tasks.ResolveAssemblyReferences.Contract;
using Microsoft.Build.Tasks.ResolveAssemblyReferences.Formatters;
using Microsoft.Build.Tasks.ResolveAssemblyReferences.Server;
using Microsoft.Build.Tasks.ResolveAssemblyReferences.Services;
using Microsoft.Build.UnitTests;
Expand All @@ -27,80 +32,83 @@ public ResolveAssemblyReferenceAsAService_Tests(ITestOutputHelper output) : base
{
}

[Fact]
public void EnsureInputPropertiesMatch()
[InlineData(typeof(ResolveAssemblyReferenceRequest), false)]
[InlineData(typeof(ResolveAssemblyReferenceResponse), true)]
[Theory]
public void EnsurePropertiesMatch(Type t, bool isOutputProperty)
{
string[] rarInputProperties = typeof(ResolveAssemblyReference).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(p => !p.GetCustomAttributes(typeof(OutputAttribute), inherit: true).Any()).Select(p => p.Name).ToArray();
string[] inputProperties = typeof(ResolveAssemblyReferenceRequest).GetProperties().Select(p => p.Name).ToArray();
string[] rarProperties = typeof(ResolveAssemblyReference).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(p => isOutputProperty == p.GetCustomAttributes(typeof(OutputAttribute), inherit: true).Any()).Select(p => p.Name).ToArray();
string[] properties = t.GetProperties().Select(p => p.Name).ToArray();

foreach (var item in rarInputProperties)
foreach (var item in rarProperties)
{
inputProperties.ShouldContain(item);
properties.ShouldContain(item);
}
}

[Fact]
public void EnsureOutputPropertiesMatch()
[InlineData(typeof(ResolveAssemblyReferenceRequest), RequestFormatter.MemberCount)]
[InlineData(typeof(ResolveAssemblyReferenceResponse), ResponseFormatter.MemberCount)]
[InlineData(typeof(ResolveAssemblyReferenceResult), ResultFormatter.MemberCount)]
[Theory]
public void FormatterHeaderSizeMatchTest(Type type, int memberCount)
{
string[] rarInputProperties = typeof(ResolveAssemblyReference).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(p => p.GetCustomAttributes(typeof(OutputAttribute), true).Any()).Select(p => p.Name).ToArray();
string[] inputProperties = typeof(ResolveAssemblyReferenceResponse).GetProperties().Select(p => p.Name).ToArray();
int propertyCount = type.GetProperties().Length;

foreach (var item in rarInputProperties)
{
inputProperties.ShouldContain(item);
}
propertyCount.ShouldBe(memberCount);
}

[Fact]
public void TransferredRequestEquals()
[InlineData(typeof(ResolveAssemblyReferenceRequest))]
[InlineData(typeof(ResolveAssemblyReferenceResponse))]
[Theory]
public void TransferredObjectsEqual(Type type)
{
MessagePackSerializerOptions options = MessagePackSerializerOptions.Standard.WithResolver(ResolveAssemblyReferneceResolver.Instance);
ResolveAssemblyReferenceRequest request = GetPopulatedObject<ResolveAssemblyReferenceRequest>("test", new[] { "testArr" }, true, new[] { new ReadOnlyTaskItem("test") });
object obj = GetPopulatedObject(type, "test", new[] { "testArr" }, true, new[] { new TaskItem("test") });

byte[] data = MessagePackSerializer.Serialize(request, options);
ResolveAssemblyReferenceRequest requestDes = MessagePackSerializer.Deserialize<ResolveAssemblyReferenceRequest>(data, options);
byte[] data = MessagePackSerializer.Serialize(type, obj, options);
object objDes = MessagePackSerializer.Deserialize(type, data, options);

ResolveAssemblyReferenceComparer.CompareInput(request, requestDes).ShouldBeTrue();
}

[Fact]
public void TransferredResponseEquals()
{
MessagePackSerializerOptions options = MessagePackSerializerOptions.Standard.WithResolver(ResolveAssemblyReferneceResolver.Instance);
ResolveAssemblyReferenceResponse response = GetPopulatedObject<ResolveAssemblyReferenceResponse>("test", new[] { "testArr" }, true, new[] { new ReadOnlyTaskItem("test") });
objDes.ShouldBeOfType(type);

byte[] data = MessagePackSerializer.Serialize(response, options);
ResolveAssemblyReferenceResponse responseDes = MessagePackSerializer.Deserialize<ResolveAssemblyReferenceResponse>(data, options);

ResolveAssemblyReferenceComparer.CompareOutput(response, responseDes).ShouldBeTrue();
if (typeof(ResolveAssemblyReferenceRequest).Equals(type))
{
ResolveAssemblyReferenceComparer.CompareRequest((ResolveAssemblyReferenceRequest)obj, (ResolveAssemblyReferenceRequest)objDes).ShouldBeTrue();
}
else if (typeof(ResolveAssemblyReferenceResponse).Equals(type))
{
ResolveAssemblyReferenceComparer.CompareResponse((ResolveAssemblyReferenceResponse)obj, (ResolveAssemblyReferenceResponse)objDes).ShouldBeTrue();
}
else
{
objDes.ShouldBe(obj);
}
}

[Fact]
public void RarOutputPropertyTest()
{
ResolveAssemblyReferenceResponse expectedResponse = GetPopulatedObject<ResolveAssemblyReferenceResponse>("test", new[] { "testArr" }, true, new[] { new ReadOnlyTaskItem("test") });
ResolveAssemblyReferenceResponse expectedResponse = GetPopulatedObject<ResolveAssemblyReferenceResponse>("test", new[] { "testArr" }, true, new[] { new TaskItem("test") });

ResolveAssemblyReference rar = new ResolveAssemblyReference();
rar.ResolveAssemblyReferenceOutput = expectedResponse;
ResolveAssemblyReferenceResponse response = rar.ResolveAssemblyReferenceOutput;

ResolveAssemblyReferenceComparer.CompareOutput(expectedResponse, response).ShouldBeTrue();
ResolveAssemblyReferenceComparer.CompareResponse(expectedResponse, response).ShouldBeTrue();
}

[Fact]
public void RarIputPropertyTest()
{
ResolveAssemblyReferenceRequest expectedRequest = GetPopulatedObject<ResolveAssemblyReferenceRequest>("test", new[] { "testArr" }, true, new[] { new ReadOnlyTaskItem("test") });
ResolveAssemblyReferenceRequest expectedRequest = GetPopulatedObject<ResolveAssemblyReferenceRequest>("test", new[] { "testArr" }, true, new[] { new TaskItem("test") });
expectedRequest.CurrentPath = Directory.GetCurrentDirectory();
expectedRequest.WarnOrErrorOnTargetArchitectureMismatch = "None"; // Serialized into enum, so we have to provide correct value

ResolveAssemblyReference rar = new ResolveAssemblyReference();
rar.ResolveAssemblyReferenceInput = expectedRequest;
ResolveAssemblyReferenceRequest request = rar.ResolveAssemblyReferenceInput;

ResolveAssemblyReferenceComparer.CompareInput(expectedRequest, request).ShouldBeTrue();
ResolveAssemblyReferenceComparer.CompareRequest(expectedRequest, request).ShouldBeTrue();
}


Expand Down Expand Up @@ -134,24 +142,27 @@ public void TransmitDataTest()
ResolveAssemblyReferenceResult result = client.Execute(rar.ResolveAssemblyReferenceInput);
cts.Cancel();

ResolveAssemblyReferenceComparer.CompareOutput(expectedResult.Response, result.Response).ShouldBeTrue();
ResolveAssemblyReferenceComparer.CompareResponse(expectedResult.Response, result.Response).ShouldBeTrue();

serverStream.Dispose();
clientStream.Dispose();
}

private T GetPopulatedObject<T>(string str, string[] strArray, bool boolVal, ReadOnlyTaskItem[] taskItems) where T : new()
private T GetPopulatedObject<T>(string str, string[] strArray, bool boolVal, ITaskItem[] taskItems) where T : new()
{
return (T)GetPopulatedObject(typeof(T), str, strArray, boolVal, taskItems);
}

private object GetPopulatedObject(Type type, string str, string[] strArray, bool boolVal, ITaskItem[] taskItems)
{
int count = 0;
T request = new T();
Type t = typeof(T);
t.GetConstructor(Type.EmptyTypes).Invoke(null);
foreach (var prop in t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
object obj = Activator.CreateInstance(type);
foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
var propType = prop.PropertyType;
if (propType == typeof(string))
{
prop.SetValue(request, str + count++);
prop.SetValue(obj, str + count++);
}
else if (propType == typeof(string[]))
{
Expand All @@ -160,28 +171,28 @@ public void TransmitDataTest()
strArray[0] += count++;
}

prop.SetValue(request, strArray);
prop.SetValue(obj, strArray);
}
else if (propType == typeof(bool))
{
prop.SetValue(request, boolVal);
prop.SetValue(obj, boolVal);
}
else if (propType == typeof(ReadOnlyTaskItem[]))
else if (propType == typeof(ITaskItem[]))
{
if (taskItems?.Length > 0 && taskItems[0] != null)
{
taskItems[0].ItemSpec += count++;
}

prop.SetValue(request, taskItems);
prop.SetValue(obj, taskItems);
}
else
{
// Fix by adding new if with this type
throw new NotImplementedException($"Invalid type: {propType.FullName}");
}
}
return request;
return obj;
}
}
}
Loading

0 comments on commit a3d2e74

Please sign in to comment.