Skip to content

Commit

Permalink
Send EnvVars to TestHost process while bootstrapping process. (#590)
Browse files Browse the repository at this point in the history
* Send EnvVars to TestHost process while bootstrapping process.

* PR feedback.
  • Loading branch information
harshjain2 committed Mar 16, 2017
1 parent 5675491 commit 4cef686
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public virtual int LaunchDataCollector(IDictionary<string, string> environmentVa

var argumentsString = string.Join(" ", commandLineArguments);

this.DataCollectorProcess = this.processHelper.LaunchProcess(dataCollectorProcessPath, argumentsString, processWorkingDirectory, null);
this.DataCollectorProcess = this.processHelper.LaunchProcess(dataCollectorProcessPath, argumentsString, processWorkingDirectory, environmentVariables, null);
return this.DataCollectorProcess.Id;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public virtual int LaunchDataCollector(IDictionary<string, string> environmentVa
var cliArgs = string.Join(" ", commandLineArguments);
var argumentsString = string.Format("{0} {1} {2} ", args, dataCollectorAssemblyPath, cliArgs);

this.DataCollectorProcess = this.processHelper.LaunchProcess(currentProcessFileName, argumentsString, currentWorkingDirectory, null);
this.DataCollectorProcess = this.processHelper.LaunchProcess(currentProcessFileName, argumentsString, currentWorkingDirectory, environmentVariables, null);
return this.DataCollectorProcess.Id;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.Interfaces
{
using System;
using System.Collections.Generic;
using System.Diagnostics;

/// <summary>
Expand All @@ -16,10 +17,11 @@ internal interface IProcessHelper
/// </summary>
/// <param name="processPath">The full file name of the process.</param>
/// <param name="arguments">The command-line arguments.</param>
/// <param name="environmentVariables">Environment variables to set while bootstrapping the process.</param>
/// <param name="workingDirectory">The working directory for this process.</param>
/// <param name="exitCallback">Call back for on process exit</param>
/// <returns>The process created.</returns>
Process LaunchProcess(string processPath, string arguments, string workingDirectory, Action<Process, string> errorCallback);
Process LaunchProcess(string processPath, string arguments, string workingDirectory, IDictionary<string, string> environmentVariables, Action<Process, string> errorCallback);

/// <summary>
/// Gets the current process file path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers
{
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

Expand All @@ -25,7 +26,7 @@ internal class ProcessHelper : IProcessHelper
/// <param name="errorCallback"></param>
/// <returns>The process spawned.</returns>
/// <exception cref="Exception">Throws any exception that could result as part of the launch.</exception>
public Process LaunchProcess(string processPath, string arguments, string workingDirectory, Action<Process, string> errorCallback)
public Process LaunchProcess(string processPath, string arguments, string workingDirectory, IDictionary<string, string> envVariables, Action<Process, string> errorCallback)
{
var process = new Process();
try
Expand All @@ -39,6 +40,14 @@ public Process LaunchProcess(string processPath, string arguments, string workin
process.StartInfo.RedirectStandardError = true;
process.EnableRaisingEvents = true;

if (envVariables != null)
{
foreach (var kvp in envVariables)
{
process.StartInfo.Environment.Add(kvp.Key, kvp.Value);
}
}

if (errorCallback != null)
{
process.ErrorDataReceived += (sender, args) => errorCallback(sender as Process, args.Data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class DefaultTestHostManager : ITestRuntimeProvider
/// <summary>
/// Callback on process exit
/// </summary>
private Action<Process, string> ErrorReceivedCallback => ((process, data) =>
private Action<Process, string> ErrorReceivedCallback => ((process, data) =>
{
if (data != null)
{
Expand Down Expand Up @@ -80,7 +80,7 @@ public class DefaultTestHostManager : ITestRuntimeProvider
this.OnHostExited(new HostProviderEventArgs(this.testHostProcessStdError.ToString(), process.ExitCode));
}
});

/// <summary>
/// Initializes a new instance of the <see cref="DefaultTestHostManager"/> class.
/// </summary>
Expand Down Expand Up @@ -150,7 +150,7 @@ private int LaunchHost(TestProcessStartInfo testHostStartInfo)

if (this.customTestHostLauncher == null)
{
this.testHostProcess = this.processHelper.LaunchProcess(testHostStartInfo.FileName, testHostStartInfo.Arguments, testHostStartInfo.WorkingDirectory, this.ErrorReceivedCallback);
this.testHostProcess = this.processHelper.LaunchProcess(testHostStartInfo.FileName, testHostStartInfo.Arguments, testHostStartInfo.WorkingDirectory, testHostStartInfo.EnvironmentVariables, this.ErrorReceivedCallback);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ public int LaunchTestHost(TestProcessStartInfo defaultTestHostStartInfo)
defaultTestHostStartInfo.FileName,
defaultTestHostStartInfo.Arguments,
defaultTestHostStartInfo.WorkingDirectory,
defaultTestHostStartInfo.EnvironmentVariables,
defaultTestHostStartInfo.ErrorReceivedCallback).Id;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public TestableProxyOperationManager(
private class TestableTestHostManager : DefaultTestHostManager
{
public TestableTestHostManager(
Architecture architecture,
Framework framework,
IProcessHelper processHelper,
Architecture architecture,
Framework framework,
IProcessHelper processHelper,
bool shared,
int errorLength) : base(architecture, framework, processHelper, shared)
{
Expand Down Expand Up @@ -150,15 +150,15 @@ public string GetTestEngineDirectory()
throw new NotImplementedException();
}

public Process LaunchProcess(string processPath, string arguments, string workingDirectory, Action<Process, string> errorCallback)
public Process LaunchProcess(string processPath, string arguments, string workingDirectory, IDictionary<string, string> envVariables, Action<Process, string> errorCallback)
{
var process = Process.GetCurrentProcess();

errorCallback(process, this.ErrorMessage);
errorCallback(process, this.ErrorMessage);
errorCallback(process, this.ErrorMessage);
errorCallback(process, this.ErrorMessage);

return process;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void GetTestHostProcessStartInfoShouldIncludeTestSourcePathInArgumentsIfN
public void LaunchTestHostShouldReturnTestHostProcessId()
{
var mockProcessHelper = new TestableProcessHelper();

var testHostManager = new DefaultTestHostManager(Architecture.X64, Framework.DefaultFramework, mockProcessHelper, true);
var startInfo = testHostManager.GetTestHostProcessStartInfo(Enumerable.Empty<string>(), null, default(TestRunnerConnectionInfo));

Expand All @@ -127,7 +127,7 @@ public void LaunchTestHostShouldReturnTestHostProcessId()
processId.Wait();
}
catch (AggregateException) { }

Assert.AreEqual(Process.GetCurrentProcess().Id, processId.Result);
}

Expand Down Expand Up @@ -157,7 +157,7 @@ public void LaunchTestHostShouldUseCustomHostIfSet()
pid.Wait(new CancellationTokenSource(3000).Token);
}
catch (Exception) { }

mockCustomLauncher.Verify(mc => mc.LaunchTestHost(It.IsAny<TestProcessStartInfo>()), Times.Once);
Assert.AreEqual(currentProcess.Id, pid.Result);
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public string GetTestEngineDirectory()
throw new NotImplementedException();
}

public Process LaunchProcess(string processPath, string arguments, string workingDirectory, Action<Process, string> errorCallback)
public Process LaunchProcess(string processPath, string arguments, string workingDirectory, IDictionary<string, string> environmentVariables, Action<Process, string> errorCallback)
{
return Process.GetCurrentProcess();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace TestPlatform.CrossPlatEngine.UnitTests.Hosting
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.Interfaces;
Expand All @@ -21,7 +22,6 @@ namespace TestPlatform.CrossPlatEngine.UnitTests.Hosting
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Moq;
using System.Threading.Tasks;

[TestClass]
public class DotnetTestHostManagerTests
Expand Down Expand Up @@ -446,14 +446,14 @@ public void DefaultTestHostLauncherShouldStartTestProcess()
var startInfo = new TestProcessStartInfo { FileName = "testhost.exe", Arguments = "a1", WorkingDirectory = "w" };
var currentProcess = Process.GetCurrentProcess();
var mockProcessHelper = new Mock<IProcessHelper>();
mockProcessHelper.Setup(ph => ph.LaunchProcess(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), null))
mockProcessHelper.Setup(ph => ph.LaunchProcess(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IDictionary<string, string>>(), null))
.Returns(currentProcess);
var hostLauncher = new DefaultTestHostLauncher(mockProcessHelper.Object);

var processId = hostLauncher.LaunchTestHost(startInfo);

Assert.AreEqual(currentProcess.Id, processId);
mockProcessHelper.Verify(ph => ph.LaunchProcess("testhost.exe", "a1", "w", null), Times.Once);
mockProcessHelper.Verify(ph => ph.LaunchProcess("testhost.exe", "a1", "w", null, null), Times.Once);
}
}
}

0 comments on commit 4cef686

Please sign in to comment.