Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map incoming and outgoing requests #3314

Merged
merged 15 commits into from
Feb 22, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;

using System.Collections.Generic;
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
using ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
Expand All @@ -10,25 +11,25 @@ namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;

internal interface IPathConverter
{
internal string UpdatePath(string path, PathConversionDirection updateDirection);
string? UpdatePath(string? path, PathConversionDirection updateDirection);

internal IEnumerable<string> UpdatePaths(IEnumerable<string> enumerable, PathConversionDirection updateDirection);
IEnumerable<string?> UpdatePaths(IEnumerable<string?> paths, PathConversionDirection updateDirection);

internal TestCase UpdateTestCase(TestCase testCase, PathConversionDirection updateDirection);
TestCase UpdateTestCase(TestCase testCase, PathConversionDirection updateDirection);

internal IEnumerable<TestCase> UpdateTestCases(IEnumerable<TestCase> testCases, PathConversionDirection updateDirection);
IEnumerable<TestCase> UpdateTestCases(IEnumerable<TestCase> testCases, PathConversionDirection updateDirection);

internal TestRunCompleteEventArgs UpdateTestRunCompleteEventArgs(TestRunCompleteEventArgs testRunCompleteEventArgs, PathConversionDirection updateDirection);
TestRunCompleteEventArgs UpdateTestRunCompleteEventArgs(TestRunCompleteEventArgs testRunCompleteEventArgs, PathConversionDirection updateDirection);

internal TestRunChangedEventArgs UpdateTestRunChangedEventArgs(TestRunChangedEventArgs testRunChangedArgs, PathConversionDirection updateDirection);
TestRunChangedEventArgs UpdateTestRunChangedEventArgs(TestRunChangedEventArgs testRunChangedArgs, PathConversionDirection updateDirection);

internal Collection<AttachmentSet> UpdateAttachmentSets(Collection<AttachmentSet> attachmentSets, PathConversionDirection updateDirection);
Collection<AttachmentSet> UpdateAttachmentSets(Collection<AttachmentSet> attachmentSets, PathConversionDirection updateDirection);

internal ICollection<AttachmentSet> UpdateAttachmentSets(ICollection<AttachmentSet> attachmentSets, PathConversionDirection updateDirection);
ICollection<AttachmentSet> UpdateAttachmentSets(ICollection<AttachmentSet> attachmentSets, PathConversionDirection updateDirection);

internal DiscoveryCriteria UpdateDiscoveryCriteria(DiscoveryCriteria discoveryCriteria, PathConversionDirection updateDirection);
DiscoveryCriteria UpdateDiscoveryCriteria(DiscoveryCriteria discoveryCriteria, PathConversionDirection updateDirection);

internal TestRunCriteriaWithSources UpdateTestRunCriteriaWithSources(TestRunCriteriaWithSources testRunCriteriaWithSources, PathConversionDirection updateDirection);
TestRunCriteriaWithSources UpdateTestRunCriteriaWithSources(TestRunCriteriaWithSources testRunCriteriaWithSources, PathConversionDirection updateDirection);

internal TestRunCriteriaWithTests UpdateTestRunCriteriaWithTests(TestRunCriteriaWithTests testRunCriteriaWithTests, PathConversionDirection updateDirection);
TestRunCriteriaWithTests UpdateTestRunCriteriaWithTests(TestRunCriteriaWithTests testRunCriteriaWithTests, PathConversionDirection updateDirection);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,41 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;

using System.Collections.Generic;
using ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using System.Collections.ObjectModel;
using System;

internal class NullPathConverter : IPathConverter
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
{
Collection<AttachmentSet> IPathConverter.UpdateAttachmentSets(Collection<AttachmentSet> attachmentSets, PathConversionDirection updateDirection)
{
return attachmentSets;
}

ICollection<AttachmentSet> IPathConverter.UpdateAttachmentSets(ICollection<AttachmentSet> attachmentSets, PathConversionDirection updateDirection)
{
return attachmentSets;
}

DiscoveryCriteria IPathConverter.UpdateDiscoveryCriteria(DiscoveryCriteria discoveryCriteria, PathConversionDirection updateDirection)
{
return discoveryCriteria;
}

string IPathConverter.UpdatePath(string path, PathConversionDirection updateDirection)
{
return path;
}

IEnumerable<string> IPathConverter.UpdatePaths(IEnumerable<string> enumerable, PathConversionDirection updateDirection)
{
return enumerable;
}

TestCase IPathConverter.UpdateTestCase(TestCase testCase, PathConversionDirection updateDirection)
{
return testCase;
}

IEnumerable<TestCase> IPathConverter.UpdateTestCases(IEnumerable<TestCase> testCases, PathConversionDirection updateDirection)
{
return testCases;
}

TestRunChangedEventArgs IPathConverter.UpdateTestRunChangedEventArgs(TestRunChangedEventArgs testRunChangedArgs, PathConversionDirection updateDirection)
{
return testRunChangedArgs;
}

TestRunCompleteEventArgs IPathConverter.UpdateTestRunCompleteEventArgs(TestRunCompleteEventArgs testRunCompleteEventArgs, PathConversionDirection updateDirection)
{
return testRunCompleteEventArgs;
}

TestRunCriteriaWithSources IPathConverter.UpdateTestRunCriteriaWithSources(TestRunCriteriaWithSources testRunCriteriaWithSources, PathConversionDirection updateDirection)
{
return testRunCriteriaWithSources;
}

TestRunCriteriaWithTests IPathConverter.UpdateTestRunCriteriaWithTests(TestRunCriteriaWithTests testRunCriteriaWithTests, PathConversionDirection updateDirection)
{
return testRunCriteriaWithTests;
}
private static readonly Lazy<NullPathConverter> LazyInstance= new(() => new NullPathConverter());
nohwnd marked this conversation as resolved.
Show resolved Hide resolved

private NullPathConverter() { }

public static NullPathConverter Instance => LazyInstance.Value;

Collection<AttachmentSet> IPathConverter.UpdateAttachmentSets(Collection<AttachmentSet> attachmentSets, PathConversionDirection _) => attachmentSets;

ICollection<AttachmentSet> IPathConverter.UpdateAttachmentSets(ICollection<AttachmentSet> attachmentSets, PathConversionDirection _) => attachmentSets;

DiscoveryCriteria IPathConverter.UpdateDiscoveryCriteria(DiscoveryCriteria discoveryCriteria, PathConversionDirection _) => discoveryCriteria;

string? IPathConverter.UpdatePath(string? path, PathConversionDirection _) => path;

IEnumerable<string?> IPathConverter.UpdatePaths(IEnumerable<string?> paths, PathConversionDirection _) => paths;

TestCase IPathConverter.UpdateTestCase(TestCase testCase, PathConversionDirection _) => testCase;

IEnumerable<TestCase> IPathConverter.UpdateTestCases(IEnumerable<TestCase> testCases, PathConversionDirection _) => testCases;

TestRunChangedEventArgs IPathConverter.UpdateTestRunChangedEventArgs(TestRunChangedEventArgs testRunChangedArgs, PathConversionDirection _) => testRunChangedArgs;

TestRunCompleteEventArgs IPathConverter.UpdateTestRunCompleteEventArgs(TestRunCompleteEventArgs testRunCompleteEventArgs, PathConversionDirection _) => testRunCompleteEventArgs;

TestRunCriteriaWithSources IPathConverter.UpdateTestRunCriteriaWithSources(TestRunCriteriaWithSources testRunCriteriaWithSources, PathConversionDirection _) => testRunCriteriaWithSources;

TestRunCriteriaWithTests IPathConverter.UpdateTestRunCriteriaWithTests(TestRunCriteriaWithTests testRunCriteriaWithTests, PathConversionDirection _) => testRunCriteriaWithTests;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;

using System.Collections.Generic;
using System.Linq;
using ObjectModel;
Expand All @@ -11,6 +12,13 @@ namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using System.IO;

/// <summary>
/// Converts paths in received and sent objects, to make testhost seem like it run a local test,
/// while it was in fact running a test on a remote system, in a totally different path. This is for UWP which
/// does testhost deployment.
/// The modifications here rely on combination of side-effects, and actually replacing the values, because
/// we cannot modify the properties on our public objects, and add setters.
/// </summary>
internal class PathConverter : IPathConverter
{
// The path on this computer to which we deployed the test dll and test runner
Expand All @@ -20,14 +28,17 @@ internal class PathConverter : IPathConverter
// are inverted, it sends us their local path, and thinks about our local path as remote.
private readonly string _originalPath = "";

public PathConverter(string originalPath, string deploymentPath, IFileHelper fileHelper)
public PathConverter(string originalPath!!, string deploymentPath!!, IFileHelper fileHelper!!)
{
_originalPath = fileHelper.GetFullPath(originalPath).TrimEnd('\\').TrimEnd('/') + Path.DirectorySeparatorChar;
_deploymentPath = fileHelper.GetFullPath(deploymentPath).TrimEnd('\\').TrimEnd('/') + Path.DirectorySeparatorChar;
}

public string UpdatePath(string path, PathConversionDirection updateDirection)
public string? UpdatePath(string? path, PathConversionDirection updateDirection)
{
if (path == null)
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
return path;

string find;
string replaceWith;
if (updateDirection == PathConversionDirection.Receive)
Expand All @@ -47,78 +58,72 @@ public string UpdatePath(string path, PathConversionDirection updateDirection)
return result;
}

public IEnumerable<string> UpdatePaths(IEnumerable<string> enumerable, PathConversionDirection updateDirection)
public IEnumerable<string?> UpdatePaths(IEnumerable<string?> paths!!, PathConversionDirection updateDirection)
{
var updatedPaths = enumerable.Select(i => UpdatePath(i, updateDirection)).ToList();
return updatedPaths;
return paths.Select(i => UpdatePath(i, updateDirection)).ToList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to match same style as other implementations:

Suggested change
return paths.Select(i => UpdatePath(i, updateDirection)).ToList();
paths.ToList().ForEach(i => UpdatePath(i, updateDirection));
return paths;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not work, strings are immutable, so you would create a new string and output it to null, so the original string would not be updated.

}

public TestCase UpdateTestCase(TestCase testCase, PathConversionDirection updateDirection)
public TestCase UpdateTestCase(TestCase testCase!!, PathConversionDirection updateDirection)
{
testCase.CodeFilePath = UpdatePath(testCase.CodeFilePath, updateDirection);
testCase.Source = UpdatePath(testCase.Source, updateDirection);
return testCase;
}

public IEnumerable<TestCase> UpdateTestCases(IEnumerable<TestCase> testCases, PathConversionDirection updateDirection)
public IEnumerable<TestCase> UpdateTestCases(IEnumerable<TestCase> testCases!!, PathConversionDirection updateDirection)
{
var updatedTestCases = testCases.Select(tc => UpdateTestCase(tc, updateDirection)).ToList();

return updatedTestCases;
testCases.ToList().ForEach(tc => UpdateTestCase(tc, updateDirection));
return testCases;
}

public TestRunCompleteEventArgs UpdateTestRunCompleteEventArgs(TestRunCompleteEventArgs testRunCompleteEventArgs, PathConversionDirection updateDirection)
public TestRunCompleteEventArgs UpdateTestRunCompleteEventArgs(TestRunCompleteEventArgs testRunCompleteEventArgs!!, PathConversionDirection updateDirection)
{
UpdateAttachmentSets(testRunCompleteEventArgs.AttachmentSets, updateDirection);
return testRunCompleteEventArgs;
}

public TestRunChangedEventArgs UpdateTestRunChangedEventArgs(TestRunChangedEventArgs testRunChangedArgs, PathConversionDirection updateDirection)
public TestRunChangedEventArgs UpdateTestRunChangedEventArgs(TestRunChangedEventArgs testRunChangedArgs!!, PathConversionDirection updateDirection)
{
UpdateTestResults(testRunChangedArgs.NewTestResults, updateDirection);
UpdateTestCases(testRunChangedArgs.ActiveTests, updateDirection);
return testRunChangedArgs;
}

public Collection<AttachmentSet> UpdateAttachmentSets(Collection<AttachmentSet> attachmentSets, PathConversionDirection updateDirection)
public Collection<AttachmentSet> UpdateAttachmentSets(Collection<AttachmentSet> attachmentSets!!, PathConversionDirection updateDirection)
{
attachmentSets.Select(i => UpdateAttachmentSet(i, updateDirection)).ToList();
attachmentSets.ToList().ForEach(i => UpdateAttachmentSet(i, updateDirection));
return attachmentSets;
}

public ICollection<AttachmentSet> UpdateAttachmentSets(ICollection<AttachmentSet> attachmentSets, PathConversionDirection updateDirection)
public ICollection<AttachmentSet> UpdateAttachmentSets(ICollection<AttachmentSet> attachmentSets!!, PathConversionDirection updateDirection)
{
attachmentSets.Select(i => UpdateAttachmentSet(i, updateDirection)).ToList();
attachmentSets.ToList().ForEach(i => UpdateAttachmentSet(i, updateDirection));
return attachmentSets;
}

private AttachmentSet UpdateAttachmentSet(AttachmentSet attachmentSet, PathConversionDirection updateDirection)
private AttachmentSet UpdateAttachmentSet(AttachmentSet attachmentSet!!, PathConversionDirection updateDirection)
{
attachmentSet.Attachments.Select(a => UpdateAttachment(a, updateDirection)).ToList();
attachmentSet.Attachments.ToList().ForEach(a => UpdateAttachment(a, updateDirection));
return attachmentSet;
}

private UriDataAttachment UpdateAttachment(UriDataAttachment attachment, PathConversionDirection updateDirection)
private UriDataAttachment UpdateAttachment(UriDataAttachment attachment!!, PathConversionDirection _)
{
// todo: convert uri?
// todo: convert uri? https://github.com/microsoft/vstest/issues/3367
return attachment;
}

private IEnumerable<TestResult> UpdateTestResults(IEnumerable<TestResult> testResults, PathConversionDirection updateDirection)
private IEnumerable<TestResult> UpdateTestResults(IEnumerable<TestResult> testResults!!, PathConversionDirection updateDirection)
{
// The incoming collection is IEnumerable, use foreach to make sure we always do the changes,
// as opposed to using .Select which will never run unless you ask for results (which totally
// did not happen to me, of course).
foreach (var tr in testResults)
{
tr.Attachments.Select(a => UpdateAttachmentSet(a, updateDirection));
UpdateAttachmentSets(tr.Attachments, updateDirection);
UpdateTestCase(tr.TestCase, updateDirection);
}

return testResults;
}

public DiscoveryCriteria UpdateDiscoveryCriteria(DiscoveryCriteria discoveryCriteria, PathConversionDirection updateDirection)
public DiscoveryCriteria UpdateDiscoveryCriteria(DiscoveryCriteria discoveryCriteria!!, PathConversionDirection updateDirection)
{
discoveryCriteria.Package = UpdatePath(discoveryCriteria.Package, updateDirection);
foreach (var adapter in discoveryCriteria.AdapterSourceMap.ToList())
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -129,14 +134,14 @@ public DiscoveryCriteria UpdateDiscoveryCriteria(DiscoveryCriteria discoveryCrit
return discoveryCriteria;
}

public TestRunCriteriaWithSources UpdateTestRunCriteriaWithSources(TestRunCriteriaWithSources testRunCriteriaWithSources, PathConversionDirection updateDirection)
public TestRunCriteriaWithSources UpdateTestRunCriteriaWithSources(TestRunCriteriaWithSources testRunCriteriaWithSources!!, PathConversionDirection updateDirection)
{
testRunCriteriaWithSources.AdapterSourceMap.Select(adapter => testRunCriteriaWithSources.AdapterSourceMap[adapter.Key] = UpdatePaths(adapter.Value, updateDirection));
testRunCriteriaWithSources.AdapterSourceMap.ToList().ForEach(adapter => testRunCriteriaWithSources.AdapterSourceMap[adapter.Key] = UpdatePaths(adapter.Value, updateDirection));
var package = UpdatePath(testRunCriteriaWithSources.Package, updateDirection);
return new TestRunCriteriaWithSources(testRunCriteriaWithSources.AdapterSourceMap, package, testRunCriteriaWithSources.RunSettings, testRunCriteriaWithSources.TestExecutionContext);
}

public TestRunCriteriaWithTests UpdateTestRunCriteriaWithTests(TestRunCriteriaWithTests testRunCriteriaWithTests, PathConversionDirection updateDirection)
public TestRunCriteriaWithTests UpdateTestRunCriteriaWithTests(TestRunCriteriaWithTests testRunCriteriaWithTests!!, PathConversionDirection updateDirection)
{
var tests = UpdateTestCases(testRunCriteriaWithTests.Tests, updateDirection);
var package = UpdatePath(testRunCriteriaWithTests.Package, updateDirection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected TestRequestHandler(IDataSerializer dataSerializer, ICommunicationEndpo
_onLaunchAdapterProcessWithDebuggerAttachedAckReceived = (message) => throw new NotImplementedException();
_onAttachDebuggerAckRecieved = (message) => throw new NotImplementedException();

_pathConverter = new NullPathConverter();
_pathConverter = NullPathConverter.Instance;
_jobQueue = new JobQueue<Action>(
(action) => action(),
"TestHostOperationQueue",
Expand All @@ -107,14 +107,10 @@ public virtual void InitializeCommunication()
{
if (this is IDeploymentAwareTestRequestHandler self
&& !string.IsNullOrWhiteSpace(self.LocalPath)
&& !string.IsNullOrEmpty(self.RemotePath))
&& !string.IsNullOrWhiteSpace(self.RemotePath))
{
_pathConverter = new PathConverter(self.LocalPath, self.RemotePath, _fileHelper);
}
else
{
_pathConverter = new NullPathConverter();
}

_communicationEndPoint = _communicationEndpointFactory.Create(ConnectionInfo.Role);
_communicationEndPoint.Connected += (sender, connectedArgs) =>
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.TestPlatform.CrossPlatEngine/Friends.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@
[assembly: InternalsVisibleTo("vstest.console, PublicKey = 002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
[assembly: InternalsVisibleTo("ConsoleApp1, PublicKey = 002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]