Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ private static ApplicationLoggingState CreateFileLoggerIfDiagnosticIsEnabled(
}

// Set the directory to the default test result directory
string directory = Path.Combine(testApplicationModuleInfo.GetCurrentTestApplicationDirectory(), AggregatedConfiguration.DefaultTestResultFolderName);
string? effectiveWorkingDirectory = environment.GetEnvironmentVariable(EnvironmentVariableConstants.DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY);
effectiveWorkingDirectory ??= testApplicationModuleInfo.GetCurrentTestApplicationDirectory();
string directory = Path.Combine(effectiveWorkingDirectory, AggregatedConfiguration.DefaultTestResultFolderName);
bool customDirectory = false;

if (result.TryGetOptionArgumentList(PlatformCommandLineProvider.ResultDirectoryOptionKey, out string[]? resultDirectoryArg))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ internal sealed class AggregatedConfiguration(
IConfigurationProvider[] configurationProviders,
ITestApplicationModuleInfo testApplicationModuleInfo,
IFileSystem fileSystem,
IEnvironment environment,
CommandLineParseResult commandLineParseResult) : IConfiguration
{
public const string DefaultTestResultFolderName = "TestResults";
private readonly IConfigurationProvider[] _configurationProviders = configurationProviders;
private readonly ITestApplicationModuleInfo _testApplicationModuleInfo = testApplicationModuleInfo;
private readonly IFileSystem _fileSystem = fileSystem;
private readonly IEnvironment _environment = environment;
private readonly CommandLineParseResult _commandLineParseResult = commandLineParseResult;
private string? _resultsDirectory;
private string? _currentWorkingDirectory;
Expand Down Expand Up @@ -94,7 +96,7 @@ private string GetResultsDirectoryCore(CommandLineParseResult commandLineParseRe
// If not specified by command line, then use the configuration providers.
// And finally fallback to DefaultTestResultFolderName relative to the current working directory.
return CalculateFromConfigurationProviders(PlatformConfigurationConstants.PlatformResultDirectory)
?? Path.Combine(this[PlatformConfigurationConstants.PlatformCurrentWorkingDirectory]!, DefaultTestResultFolderName);
?? Path.Combine(_environment.GetEnvironmentVariable(EnvironmentVariableConstants.DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY) ?? this[PlatformConfigurationConstants.PlatformCurrentWorkingDirectory]!, DefaultTestResultFolderName);
}

private string GetCurrentWorkingDirectoryCore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

namespace Microsoft.Testing.Platform.Configurations;

internal sealed class ConfigurationManager(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo) : IConfigurationManager
internal sealed class ConfigurationManager(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment) : IConfigurationManager
{
private readonly List<Func<IConfigurationSource>> _configurationSources = [];
private readonly IFileSystem _fileSystem = fileSystem;
private readonly ITestApplicationModuleInfo _testApplicationModuleInfo = testApplicationModuleInfo;
private readonly IEnvironment _environment = environment;

public void AddConfigurationSource(Func<IConfigurationSource> source) => _configurationSources.Add(source);

Expand Down Expand Up @@ -58,6 +59,6 @@ internal async Task<IConfiguration> BuildAsync(IFileLoggerProvider? syncFileLogg

return defaultJsonConfiguration is null
? throw new InvalidOperationException(PlatformResources.ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage)
: new AggregatedConfiguration([.. configurationProviders.OrderBy(x => x.Order).Select(x => x.ConfigurationProvider)], _testApplicationModuleInfo, _fileSystem, commandLineParseResult);
: new AggregatedConfiguration([.. configurationProviders.OrderBy(x => x.Order).Select(x => x.ConfigurationProvider)], _testApplicationModuleInfo, _fileSystem, _environment, commandLineParseResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal static class EnvironmentVariableConstants

// dotnet test
public const string TESTINGPLATFORM_DOTNETTEST_EXECUTIONID = nameof(TESTINGPLATFORM_DOTNETTEST_EXECUTIONID);
public const string DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY = nameof(DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY);

// Unhandled Exception
public const string TESTINGPLATFORM_EXIT_PROCESS_ON_UNHANDLED_EXCEPTION = nameof(TESTINGPLATFORM_EXIT_PROCESS_ON_UNHANDLED_EXCEPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal sealed class TestHostBuilder(IFileSystem fileSystem, IRuntimeFeature ru

public ITestHostManager TestHost { get; } = new TestHostManager();

public IConfigurationManager Configuration { get; } = new ConfigurationManager(fileSystem, testApplicationModuleInfo);
public IConfigurationManager Configuration { get; } = new ConfigurationManager(fileSystem, testApplicationModuleInfo, environment);

public ILoggingManager Logging { get; } = new LoggingManager();

Expand Down Expand Up @@ -481,11 +481,6 @@ await LogTestHostCreatedAsync(
policiesService.ProcessRole = TestProcessRole.TestHost;
await proxyOutputDevice.HandleProcessRoleAsync(TestProcessRole.TestHost, testApplicationCancellationTokenSource.CancellationToken).ConfigureAwait(false);

// Setup the test host working folder.
// Out of the test host controller extension the current working directory is the test host working directory.
string? currentWorkingDirectory = configuration[PlatformConfigurationConstants.PlatformCurrentWorkingDirectory];
ApplicationStateGuard.Ensure(currentWorkingDirectory is not null);

testHostControllerInfo.IsCurrentProcessTestHostController = false;

// If we're under test controllers and currently we're inside the started test host we connect to the out of process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,6 @@ public ExecutableInfo GetCurrentExecutableInfo()
_ => commandLineArguments,
};

return new(GetProcessPath(), arguments, GetCurrentTestApplicationDirectory());
return new ExecutableInfo(GetProcessPath(), arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@

namespace Microsoft.Testing.Platform.Services;

internal sealed class ExecutableInfo(string filePath, IEnumerable<string> arguments, string workspace)
internal sealed class ExecutableInfo(string filePath, IEnumerable<string> arguments)
{
public string FilePath { get; } = filePath;

public IEnumerable<string> Arguments { get; } = arguments;

public string Workspace { get; } = workspace;

public override string ToString()
=> $"Process: {FilePath}, Arguments: {string.Join(' ', Arguments)}, Workspace: {Workspace}";
=> $"Process: {FilePath}, Arguments: {string.Join(' ', Arguments)}";
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Testing.Platform.Configurations;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.TestHostControllers;
using Microsoft.Testing.Platform.Helpers;
Expand Down Expand Up @@ -108,13 +107,6 @@ public void AddDataConsumer<T>(CompositeExtensionFactory<T> compositeServiceFact

internal async Task<TestHostControllerConfiguration> BuildAsync(ServiceProvider serviceProvider)
{
// For now the test host working directory and the current working directory are the same.
// In future we could move the test host in a different directory for instance in case of
// the need to rewrite binary files. If we don't move files are locked by ourself.
var aggregatedConfiguration = (AggregatedConfiguration)serviceProvider.GetConfiguration();
string? currentWorkingDirectory = aggregatedConfiguration[PlatformConfigurationConstants.PlatformCurrentWorkingDirectory];
ApplicationStateGuard.Ensure(currentWorkingDirectory is not null);

List<(ITestHostEnvironmentVariableProvider TestHostEnvironmentVariableProvider, int RegistrationOrder)> environmentVariableProviders = [];
foreach (Func<IServiceProvider, ITestHostEnvironmentVariableProvider> environmentVariableProviderFactory in _environmentVariableProviderFactories)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Services()
ServiceProvider.AddService(new LoggerFactory());
ServiceProvider.AddService(new FakeClock());
ServiceProvider.AddService(new SystemTask());
ServiceProvider.AddService(new AggregatedConfiguration([], new CurrentTestApplicationModuleInfo(new SystemEnvironment(), new SystemProcessHandler()), new SystemFileSystem(), new(null, [], [])));
ServiceProvider.AddService(new AggregatedConfiguration([], new CurrentTestApplicationModuleInfo(new SystemEnvironment(), new SystemProcessHandler()), new SystemFileSystem(), new SystemEnvironment(), new(null, [], [])));
}

public MessageBus MessageBus { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void IndexerTest_DirectoryNotSetAndNoConfigurationProviders(string key)
_ => throw ApplicationStateGuard.Unreachable(),
};

Assert.AreEqual(expected, new AggregatedConfiguration([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new(null, [], []))[key]);
Assert.AreEqual(expected, new AggregatedConfiguration([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new SystemEnvironment(), new(null, [], []))[key]);
}

[TestMethod]
Expand All @@ -43,7 +43,7 @@ public void IndexerTest_DirectoryNotSetButConfigurationProvidersPresent_Director
{
Mock<IConfigurationProvider> mockProvider = new();

AggregatedConfiguration aggregatedConfiguration = new([mockProvider.Object], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new(null, [], []));
AggregatedConfiguration aggregatedConfiguration = new([mockProvider.Object], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new SystemEnvironment(), new(null, [], []));
Assert.IsNull(aggregatedConfiguration[key]);
}

Expand All @@ -53,21 +53,21 @@ public void IndexerTest_DirectoryNotSetButConfigurationProvidersPresent_Director
[DataRow(PlatformConfigurationConstants.PlatformTestHostWorkingDirectory)]
public void IndexerTest_DirectoryNotSetButConfigurationProvidersPresent_DirectoryIsNotNull(string key)
{
AggregatedConfiguration aggregatedConfiguration = new([new FakeConfigurationProvider(ExpectedPath)], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new(null, [], []));
AggregatedConfiguration aggregatedConfiguration = new([new FakeConfigurationProvider(ExpectedPath)], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new SystemEnvironment(), new(null, [], []));
Assert.AreEqual(ExpectedPath, aggregatedConfiguration[key]);
}

[TestMethod]
public void IndexerTest_ResultDirectorySet_DirectoryIsNotNull()
{
AggregatedConfiguration aggregatedConfiguration = new([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new(null, [new CommandLineParseOption("results-directory", [ExpectedPath])], []));
AggregatedConfiguration aggregatedConfiguration = new([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new SystemEnvironment(), new(null, [new CommandLineParseOption("results-directory", [ExpectedPath])], []));
Assert.AreEqual(ExpectedPath, aggregatedConfiguration[PlatformConfigurationConstants.PlatformResultDirectory]);
}

[TestMethod]
public void IndexerTest_CurrentWorkingDirectorySet_DirectoryIsNotNull()
{
AggregatedConfiguration aggregatedConfiguration = new([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new(null, [], []));
AggregatedConfiguration aggregatedConfiguration = new([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new SystemEnvironment(), new(null, [], []));

aggregatedConfiguration.SetCurrentWorkingDirectory(ExpectedPath);
Assert.AreEqual(ExpectedPath, aggregatedConfiguration[PlatformConfigurationConstants.PlatformCurrentWorkingDirectory]);
Expand All @@ -86,7 +86,7 @@ public async ValueTask CheckTestResultsDirectoryOverrideAndCreateItAsync_Results
Mock<IFileLoggerProvider> mockFileLogger = new();
mockFileLogger.Setup(x => x.CheckLogFolderAndMoveToTheNewIfNeededAsync(It.IsAny<string>())).Callback(() => { });

AggregatedConfiguration aggregatedConfiguration = new([], mockTestApplicationModuleInfo.Object, mockFileSystem.Object, new(null, [new CommandLineParseOption("results-directory", [ExpectedPath])], []));
AggregatedConfiguration aggregatedConfiguration = new([], mockTestApplicationModuleInfo.Object, mockFileSystem.Object, new SystemEnvironment(), new(null, [new CommandLineParseOption("results-directory", [ExpectedPath])], []));
await aggregatedConfiguration.CheckTestResultsDirectoryOverrideAndCreateItAsync(mockFileLogger.Object);

mockFileSystem.Verify(x => x.CreateDirectory(ExpectedPath), Times.Once);
Expand All @@ -108,7 +108,7 @@ public async ValueTask CheckTestResultsDirectoryOverrideAndCreateItAsync_Results
Mock<IFileLoggerProvider> mockFileLogger = new();
mockFileLogger.Setup(x => x.CheckLogFolderAndMoveToTheNewIfNeededAsync(It.IsAny<string>())).Callback(() => { });

AggregatedConfiguration aggregatedConfiguration = new([], mockTestApplicationModuleInfo.Object, mockFileSystem.Object, new(null, [new CommandLineParseOption("results-directory", [ExpectedPath])], []));
AggregatedConfiguration aggregatedConfiguration = new([], mockTestApplicationModuleInfo.Object, mockFileSystem.Object, new SystemEnvironment(), new(null, [new CommandLineParseOption("results-directory", [ExpectedPath])], []));
await aggregatedConfiguration.CheckTestResultsDirectoryOverrideAndCreateItAsync(mockFileLogger.Object);

mockFileSystem.Verify(x => x.CreateDirectory(ExpectedPath), Times.Once);
Expand All @@ -130,7 +130,7 @@ public async ValueTask CheckTestResultsDirectoryOverrideAndCreateItAsync_Results
Mock<IFileLoggerProvider> mockFileLogger = new();
mockFileLogger.Setup(x => x.CheckLogFolderAndMoveToTheNewIfNeededAsync(It.IsAny<string>())).Callback(() => { });

AggregatedConfiguration aggregatedConfiguration = new([], mockTestApplicationModuleInfo.Object, mockFileSystem.Object, new(null, [], []));
AggregatedConfiguration aggregatedConfiguration = new([], mockTestApplicationModuleInfo.Object, mockFileSystem.Object, new SystemEnvironment(), new(null, [], []));
await aggregatedConfiguration.CheckTestResultsDirectoryOverrideAndCreateItAsync(mockFileLogger.Object);

string expectedPath = "a" + Path.DirectorySeparatorChar + "b" + Path.DirectorySeparatorChar + "TestResults";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async ValueTask GetConfigurationValueFromJson(string jsonFileConfig, stri
fileSystem.Setup(x => x.NewFileStream(It.IsAny<string>(), FileMode.Open, FileAccess.Read))
.Returns(new MemoryFileStream(Encoding.UTF8.GetBytes(jsonFileConfig)));
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(fileSystem.Object, testApplicationModuleInfo);
ConfigurationManager configurationManager = new(fileSystem.Object, testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() => new JsonConfigurationSource(testApplicationModuleInfo, fileSystem.Object, null));
IConfiguration configuration = await configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), []));
Assert.AreEqual(result, configuration[key], $"Expected '{result}' found '{configuration[key]}'");
Expand Down Expand Up @@ -57,7 +57,7 @@ public async ValueTask InvalidJson_Fail()
fileSystem.Setup(x => x.ExistFile(It.IsAny<string>())).Returns(true);
fileSystem.Setup(x => x.NewFileStream(It.IsAny<string>(), FileMode.Open, FileAccess.Read)).Returns(() => new MemoryFileStream(Encoding.UTF8.GetBytes(string.Empty)));
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(fileSystem.Object, testApplicationModuleInfo);
ConfigurationManager configurationManager = new(fileSystem.Object, testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() =>
new JsonConfigurationSource(testApplicationModuleInfo, fileSystem.Object, null));

Expand Down Expand Up @@ -87,7 +87,7 @@ public async ValueTask GetConfigurationValueFromJsonWithFileLoggerProvider(strin
loggerProviderMock.Setup(x => x.CreateLogger(It.IsAny<string>())).Returns(loggerMock.Object);

CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(fileSystem.Object, testApplicationModuleInfo);
ConfigurationManager configurationManager = new(fileSystem.Object, testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() =>
new JsonConfigurationSource(testApplicationModuleInfo, fileSystem.Object, null));

Expand All @@ -101,7 +101,7 @@ public async ValueTask GetConfigurationValueFromJsonWithFileLoggerProvider(strin
public async ValueTask BuildAsync_EmptyConfigurationSources_ThrowsException()
{
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(new SystemFileSystem(), testApplicationModuleInfo);
ConfigurationManager configurationManager = new(new SystemFileSystem(), testApplicationModuleInfo, new SystemEnvironment());
await Assert.ThrowsAsync<InvalidOperationException>(() => configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), [])));
}

Expand All @@ -112,7 +112,7 @@ public async ValueTask BuildAsync_ConfigurationSourcesNotEnabledAsync_ThrowsExce
mockConfigurationSource.Setup(x => x.IsEnabledAsync()).ReturnsAsync(false);

CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(new SystemFileSystem(), testApplicationModuleInfo);
ConfigurationManager configurationManager = new(new SystemFileSystem(), testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() => mockConfigurationSource.Object);

await Assert.ThrowsAsync<InvalidOperationException>(() => configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), [])));
Expand All @@ -132,7 +132,7 @@ public async ValueTask BuildAsync_ConfigurationSourceIsAsyncInitializableExtensi
};

CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(new SystemFileSystem(), testApplicationModuleInfo);
ConfigurationManager configurationManager = new(new SystemFileSystem(), testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() => fakeConfigurationSource);

await Assert.ThrowsAsync<InvalidOperationException>(() => configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), [])));
Expand Down
Loading