From 36744a9a520570310a3e8e037ab4b916c3c9583d Mon Sep 17 00:00:00 2001 From: mariam-abdulla Date: Mon, 10 Mar 2025 16:22:25 +0100 Subject: [PATCH 1/5] Create logger directory if it doesn't exist in dotnet test --- src/Cli/dotnet/commands/dotnet-test/Logger.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Cli/dotnet/commands/dotnet-test/Logger.cs b/src/Cli/dotnet/commands/dotnet-test/Logger.cs index b3beb944033e..0f2b2c214fff 100644 --- a/src/Cli/dotnet/commands/dotnet-test/Logger.cs +++ b/src/Cli/dotnet/commands/dotnet-test/Logger.cs @@ -30,6 +30,12 @@ public static void LogTrace(Func messageLog) lock (_lock) { + string directoryPath = Path.GetDirectoryName(_traceFilePath); + if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath)) + { + Directory.CreateDirectory(directoryPath); + } + using StreamWriter logFile = File.AppendText(_traceFilePath); logFile.WriteLine(message); } From a4823d4e2ebe99b74b714516107c68fd8d0182fc Mon Sep 17 00:00:00 2001 From: Mariam Abdullah <122357303+mariam-abdulla@users.noreply.github.com> Date: Tue, 11 Mar 2025 09:36:23 +0100 Subject: [PATCH 2/5] Update src/Cli/dotnet/commands/dotnet-test/Logger.cs Co-authored-by: Youssef Victor --- src/Cli/dotnet/commands/dotnet-test/Logger.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cli/dotnet/commands/dotnet-test/Logger.cs b/src/Cli/dotnet/commands/dotnet-test/Logger.cs index 0f2b2c214fff..8b1e223c9574 100644 --- a/src/Cli/dotnet/commands/dotnet-test/Logger.cs +++ b/src/Cli/dotnet/commands/dotnet-test/Logger.cs @@ -31,7 +31,7 @@ public static void LogTrace(Func messageLog) lock (_lock) { string directoryPath = Path.GetDirectoryName(_traceFilePath); - if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath)) + if (!string.IsNullOrEmpty(directoryPath)) { Directory.CreateDirectory(directoryPath); } From 395fc387c26e3efa5e523ec497417b70d00815ee Mon Sep 17 00:00:00 2001 From: mariam-abdulla Date: Tue, 11 Mar 2025 10:11:07 +0100 Subject: [PATCH 3/5] Move logic to consutrctor --- src/Cli/dotnet/commands/dotnet-test/Logger.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Cli/dotnet/commands/dotnet-test/Logger.cs b/src/Cli/dotnet/commands/dotnet-test/Logger.cs index 8b1e223c9574..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) @@ -30,12 +36,6 @@ public static void LogTrace(Func messageLog) lock (_lock) { - string directoryPath = Path.GetDirectoryName(_traceFilePath); - if (!string.IsNullOrEmpty(directoryPath)) - { - Directory.CreateDirectory(directoryPath); - } - using StreamWriter logFile = File.AppendText(_traceFilePath); logFile.WriteLine(message); } From 4ccefc14b5151b7a7fec213271586d5417dd5c41 Mon Sep 17 00:00:00 2001 From: mariam-abdulla Date: Tue, 11 Mar 2025 10:25:21 +0100 Subject: [PATCH 4/5] Add test --- ...stBuildsAndRunsTestsWithDifferentOptions.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs index daddd60cb705..f82a56d22263 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{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); + } } } From 65f661de151a005ba7051efc8c1e59489e5ea798 Mon Sep 17 00:00:00 2001 From: Mariam Abdullah <122357303+mariam-abdulla@users.noreply.github.com> Date: Tue, 11 Mar 2025 11:14:58 +0100 Subject: [PATCH 5/5] Update test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs Co-authored-by: Youssef Victor --- .../GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs index f82a56d22263..bc1728942dd7 100644 --- a/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs +++ b/test/dotnet-test.Tests/GivenDotnetTestBuildsAndRunsTestsWithDifferentOptions.cs @@ -526,7 +526,7 @@ public void RunWithTraceFileLoggingAndNonExistingDirectory_ShouldReturnExitCodeG { TestAsset testInstance = _testAssetsManager.CopyTestAsset("MultiTestProjectSolutionWithTests", Guid.NewGuid().ToString()).WithSource(); - string traceFile = $"directory{Path.DirectorySeparatorChar}logs.txt"; + string traceFile = $"directory_{configuration}{Path.DirectorySeparatorChar}logs.txt"; CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false) .WithWorkingDirectory(testInstance.Path) .WithEnvironmentVariable(CliConstants.TestTraceLoggingEnvVar, traceFile)