Skip to content

Commit bf42a4c

Browse files
Create logger directory if it doesn't exist in dotnet test (#47411)
Co-authored-by: Youssef Victor <[email protected]>
1 parent 0bb5378 commit bf42a4c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Cli/dotnet/commands/dotnet-test/Logger.cs

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ static Logger()
1515
{
1616
_traceFilePath = Environment.GetEnvironmentVariable(CliConstants.TestTraceLoggingEnvVar);
1717
TraceEnabled = !string.IsNullOrEmpty(_traceFilePath);
18+
19+
string directoryPath = Path.GetDirectoryName(_traceFilePath);
20+
if (!string.IsNullOrEmpty(directoryPath))
21+
{
22+
Directory.CreateDirectory(directoryPath);
23+
}
1824
}
1925

2026
public static void LogTrace(Func<string> messageLog)

test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs

+18
Original file line numberDiff line numberDiff line change
@@ -519,5 +519,23 @@ public void RunWithTraceFileLogging_ShouldReturnExitCodeGenericFailure(string co
519519
result.ExitCode.Should().Be(ExitCode.GenericFailure);
520520
}
521521

522+
[InlineData(TestingConstants.Debug)]
523+
[InlineData(TestingConstants.Release)]
524+
[Theory]
525+
public void RunWithTraceFileLoggingAndNonExistingDirectory_ShouldReturnExitCodeGenericFailure(string configuration)
526+
{
527+
TestAsset testInstance = _testAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithTests", Guid.NewGuid().ToString()).WithSource();
528+
529+
string traceFile = $"directory_{configuration}{Path.DirectorySeparatorChar}logs.txt";
530+
CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false)
531+
.WithWorkingDirectory(testInstance.Path)
532+
.WithEnvironmentVariable(CliConstants.TestTraceLoggingEnvVar, traceFile)
533+
.WithEnableTestingPlatform()
534+
.Execute(TestingPlatformOptions.ConfigurationOption.Name, configuration);
535+
536+
Assert.True(File.Exists(Path.Combine(testInstance.Path, traceFile)), "Trace file should exist after test execution.");
537+
538+
result.ExitCode.Should().Be(ExitCode.GenericFailure);
539+
}
522540
}
523541
}

0 commit comments

Comments
 (0)