Skip to content

Commit 004679f

Browse files
committed
Respect working directory of 'dotnet test' when determining results directory
1 parent 24c072c commit 004679f

7 files changed

Lines changed: 20 additions & 14 deletions

File tree

src/Platform/Microsoft.Testing.Platform/Builder/TestApplication.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@ private static ApplicationLoggingState CreateFileLoggerIfDiagnosticIsEnabled(
310310
}
311311

312312
// Set the directory to the default test result directory
313-
string directory = Path.Combine(testApplicationModuleInfo.GetCurrentTestApplicationDirectory(), AggregatedConfiguration.DefaultTestResultFolderName);
313+
string? effectiveWorkingDirectory = environment.GetEnvironmentVariable(EnvironmentVariableConstants.DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY);
314+
effectiveWorkingDirectory ??= testApplicationModuleInfo.GetCurrentTestApplicationDirectory();
315+
string directory = Path.Combine(effectiveWorkingDirectory, AggregatedConfiguration.DefaultTestResultFolderName);
314316
bool customDirectory = false;
315317

316318
if (result.TryGetOptionArgumentList(PlatformCommandLineProvider.ResultDirectoryOptionKey, out string[]? resultDirectoryArg))

src/Platform/Microsoft.Testing.Platform/Configurations/AggregatedConfiguration.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ internal sealed class AggregatedConfiguration(
1212
IConfigurationProvider[] configurationProviders,
1313
ITestApplicationModuleInfo testApplicationModuleInfo,
1414
IFileSystem fileSystem,
15+
IEnvironment environment,
1516
CommandLineParseResult commandLineParseResult) : IConfiguration
1617
{
1718
public const string DefaultTestResultFolderName = "TestResults";
1819
private readonly IConfigurationProvider[] _configurationProviders = configurationProviders;
1920
private readonly ITestApplicationModuleInfo _testApplicationModuleInfo = testApplicationModuleInfo;
2021
private readonly IFileSystem _fileSystem = fileSystem;
22+
private readonly IEnvironment _environment = environment;
2123
private readonly CommandLineParseResult _commandLineParseResult = commandLineParseResult;
2224
private string? _resultsDirectory;
2325
private string? _currentWorkingDirectory;
@@ -94,7 +96,7 @@ private string GetResultsDirectoryCore(CommandLineParseResult commandLineParseRe
9496
// If not specified by command line, then use the configuration providers.
9597
// And finally fallback to DefaultTestResultFolderName relative to the current working directory.
9698
return CalculateFromConfigurationProviders(PlatformConfigurationConstants.PlatformResultDirectory)
97-
?? Path.Combine(this[PlatformConfigurationConstants.PlatformCurrentWorkingDirectory]!, DefaultTestResultFolderName);
99+
?? Path.Combine(_environment.GetEnvironmentVariable(EnvironmentVariableConstants.DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY) ?? this[PlatformConfigurationConstants.PlatformCurrentWorkingDirectory]!, DefaultTestResultFolderName);
98100
}
99101

100102
private string GetCurrentWorkingDirectoryCore()

src/Platform/Microsoft.Testing.Platform/Configurations/ConfigurationManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
namespace Microsoft.Testing.Platform.Configurations;
1414

15-
internal sealed class ConfigurationManager(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo) : IConfigurationManager
15+
internal sealed class ConfigurationManager(IFileSystem fileSystem, ITestApplicationModuleInfo testApplicationModuleInfo, IEnvironment environment) : IConfigurationManager
1616
{
1717
private readonly List<Func<IConfigurationSource>> _configurationSources = [];
1818
private readonly IFileSystem _fileSystem = fileSystem;
1919
private readonly ITestApplicationModuleInfo _testApplicationModuleInfo = testApplicationModuleInfo;
20+
private readonly IEnvironment _environment = environment;
2021

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

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

5960
return defaultJsonConfiguration is null
6061
? throw new InvalidOperationException(PlatformResources.ConfigurationManagerCannotFindDefaultJsonConfigurationErrorMessage)
61-
: new AggregatedConfiguration([.. configurationProviders.OrderBy(x => x.Order).Select(x => x.ConfigurationProvider)], _testApplicationModuleInfo, _fileSystem, commandLineParseResult);
62+
: new AggregatedConfiguration([.. configurationProviders.OrderBy(x => x.Order).Select(x => x.ConfigurationProvider)], _testApplicationModuleInfo, _fileSystem, _environment, commandLineParseResult);
6263
}
6364
}

src/Platform/Microsoft.Testing.Platform/Helpers/EnvironmentVariableConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ internal static class EnvironmentVariableConstants
3636

3737
// dotnet test
3838
public const string TESTINGPLATFORM_DOTNETTEST_EXECUTIONID = nameof(TESTINGPLATFORM_DOTNETTEST_EXECUTIONID);
39+
public const string DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY = nameof(DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY);
3940

4041
// Unhandled Exception
4142
public const string TESTINGPLATFORM_EXIT_PROCESS_ON_UNHANDLED_EXCEPTION = nameof(TESTINGPLATFORM_EXIT_PROCESS_ON_UNHANDLED_EXCEPTION);

src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal sealed class TestHostBuilder(IFileSystem fileSystem, IRuntimeFeature ru
4343

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

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

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

test/UnitTests/MSTest.Engine.UnitTests/Adapter_ExecuteRequestAsyncTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public Services()
109109
ServiceProvider.AddService(new LoggerFactory());
110110
ServiceProvider.AddService(new FakeClock());
111111
ServiceProvider.AddService(new SystemTask());
112-
ServiceProvider.AddService(new AggregatedConfiguration([], new CurrentTestApplicationModuleInfo(new SystemEnvironment(), new SystemProcessHandler()), new SystemFileSystem(), new(null, [], [])));
112+
ServiceProvider.AddService(new AggregatedConfiguration([], new CurrentTestApplicationModuleInfo(new SystemEnvironment(), new SystemProcessHandler()), new SystemFileSystem(), new SystemEnvironment(), new(null, [], [])));
113113
}
114114

115115
public MessageBus MessageBus { get; }

test/UnitTests/Microsoft.Testing.Platform.UnitTests/Configuration/AggregatedConfigurationTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void IndexerTest_DirectoryNotSetAndNoConfigurationProviders(string key)
3333
_ => throw ApplicationStateGuard.Unreachable(),
3434
};
3535

36-
Assert.AreEqual(expected, new AggregatedConfiguration([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new(null, [], []))[key]);
36+
Assert.AreEqual(expected, new AggregatedConfiguration([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new SystemEnvironment(), new(null, [], []))[key]);
3737
}
3838

3939
[TestMethod]
@@ -43,7 +43,7 @@ public void IndexerTest_DirectoryNotSetButConfigurationProvidersPresent_Director
4343
{
4444
Mock<IConfigurationProvider> mockProvider = new();
4545

46-
AggregatedConfiguration aggregatedConfiguration = new([mockProvider.Object], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new(null, [], []));
46+
AggregatedConfiguration aggregatedConfiguration = new([mockProvider.Object], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new SystemEnvironment(), new(null, [], []));
4747
Assert.IsNull(aggregatedConfiguration[key]);
4848
}
4949

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

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

6767
[TestMethod]
6868
public void IndexerTest_CurrentWorkingDirectorySet_DirectoryIsNotNull()
6969
{
70-
AggregatedConfiguration aggregatedConfiguration = new([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new(null, [], []));
70+
AggregatedConfiguration aggregatedConfiguration = new([], _testApplicationModuleInfoMock.Object, _fileSystemMock.Object, new SystemEnvironment(), new(null, [], []));
7171

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

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

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

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

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

133-
AggregatedConfiguration aggregatedConfiguration = new([], mockTestApplicationModuleInfo.Object, mockFileSystem.Object, new(null, [], []));
133+
AggregatedConfiguration aggregatedConfiguration = new([], mockTestApplicationModuleInfo.Object, mockFileSystem.Object, new SystemEnvironment(), new(null, [], []));
134134
await aggregatedConfiguration.CheckTestResultsDirectoryOverrideAndCreateItAsync(mockFileLogger.Object);
135135

136136
string expectedPath = "a" + Path.DirectorySeparatorChar + "b" + Path.DirectorySeparatorChar + "TestResults";

0 commit comments

Comments
 (0)