Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -305,7 +305,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();
Comment thread
Evangelink marked this conversation as resolved.
Outdated
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,14 +96,17 @@ 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);
Comment thread
Evangelink marked this conversation as resolved.
Outdated
}
Comment thread
Evangelink marked this conversation as resolved.
Comment thread
Evangelink marked this conversation as resolved.

private string GetCurrentWorkingDirectoryCore()
// If the value is already set, use that.
=> _currentWorkingDirectory
// If first time calculating it, prefer the value from configuration,
?? CalculateFromConfigurationProviders(PlatformConfigurationConstants.PlatformCurrentWorkingDirectory)
// then check if dotnet test working directory is set (to keep PlatformCurrentWorkingDirectory and
// PlatformResultDirectory consistent when running under 'dotnet test'),
?? _environment.GetEnvironmentVariable(EnvironmentVariableConstants.DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY)
// then fallback to the actual working directory.
?? _testApplicationModuleInfo.GetCurrentTestApplicationDirectory();
}
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 @@ -68,6 +69,6 @@ internal async Task<IConfiguration> BuildAsync(IFileLoggerProvider? syncFileLogg
}
}

return new AggregatedConfiguration(configurationProvidersArray, _testApplicationModuleInfo, _fileSystem, commandLineParseResult);
return new AggregatedConfiguration(configurationProvidersArray, _testApplicationModuleInfo, _fileSystem, _environment, commandLineParseResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 @@ -474,11 +474,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 @@ -113,6 +113,6 @@ public ExecutableInfo GetCurrentExecutableInfo()
_ => commandLineArguments,
};

return new(GetProcessPath(), arguments, GetCurrentTestApplicationDirectory());
return new ExecutableInfo(GetProcessPath(), arguments, GetCurrentTestApplicationDirectory());
}
}
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 @@ -107,13 +106,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
Loading
Loading