diff --git a/src/Cli/dotnet/commands/dotnet-test/Logger.cs b/src/Cli/dotnet/commands/dotnet-test/Logger.cs index b3beb944033e..e26bcce5af73 100644 --- a/src/Cli/dotnet/commands/dotnet-test/Logger.cs +++ b/src/Cli/dotnet/commands/dotnet-test/Logger.cs @@ -15,6 +15,12 @@ static Logger() { _traceFilePath = Environment.GetEnvironmentVariable(CliConstants.TestTraceLoggingEnvVar); TraceEnabled = !string.IsNullOrEmpty(_traceFilePath); + + string directoryPath = Path.GetDirectoryName(_traceFilePath); + if (!string.IsNullOrEmpty(directoryPath)) + { + Directory.CreateDirectory(directoryPath); + } } public static void LogTrace(Func messageLog) diff --git a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs index daddd60cb705..bc1728942dd7 100644 --- a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs +++ b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs @@ -519,5 +519,23 @@ public void RunWithTraceFileLogging_ShouldReturnExitCodeGenericFailure(string co result.ExitCode.Should().Be(ExitCode.GenericFailure); } + [InlineData(TestingConstants.Debug)] + [InlineData(TestingConstants.Release)] + [Theory] + public void RunWithTraceFileLoggingAndNonExistingDirectory_ShouldReturnExitCodeGenericFailure(string configuration) + { + TestAsset testInstance = _testAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithTests", Guid.NewGuid().ToString()).WithSource(); + + string traceFile = $"directory_{configuration}{Path.DirectorySeparatorChar}logs.txt"; + CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false) + .WithWorkingDirectory(testInstance.Path) + .WithEnvironmentVariable(CliConstants.TestTraceLoggingEnvVar, traceFile) + .WithEnableTestingPlatform() + .Execute(TestingPlatformOptions.ConfigurationOption.Name, configuration); + + Assert.True(File.Exists(Path.Combine(testInstance.Path, traceFile)), "Trace file should exist after test execution."); + + result.ExitCode.Should().Be(ExitCode.GenericFailure); + } } }