From 525565dba3d2cefed3522afbf478514055fbe2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Fri, 12 Jul 2024 13:55:15 +0200 Subject: [PATCH] Add option to overwrite trx without warning (#5141) --- playground/TestPlatform.Playground/Program.cs | 2 +- .../TestPlatform.Playground.csproj | 2 ++ .../TrxLogger.cs | 17 ++++++++++++++--- .../Utility/Constants.cs | 5 +++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/playground/TestPlatform.Playground/Program.cs b/playground/TestPlatform.Playground/Program.cs index 142b324cb3..0cebf6d996 100644 --- a/playground/TestPlatform.Playground/Program.cs +++ b/playground/TestPlatform.Playground/Program.cs @@ -105,7 +105,7 @@ static void Main() // var processStartInfo = new ProcessStartInfo // { // FileName = console, - // Arguments = $"{string.Join(" ", sources)} --settings:{settingsFile} --listtests", + // Arguments = $"{string.Join(" ", sources)} --settings:{settingsFile} --logger:trx;LogFileName=my.trx;WarnOnFileOverwrite=false", // UseShellExecute = false, // }; // EnvironmentVariables.Variables.ToList().ForEach(processStartInfo.Environment.Add); diff --git a/playground/TestPlatform.Playground/TestPlatform.Playground.csproj b/playground/TestPlatform.Playground/TestPlatform.Playground.csproj index f20a1bd4d8..9a5dcbffbe 100644 --- a/playground/TestPlatform.Playground/TestPlatform.Playground.csproj +++ b/playground/TestPlatform.Playground/TestPlatform.Playground.csproj @@ -32,6 +32,7 @@ + @@ -74,6 +75,7 @@ + diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs index 7809c0acd1..ea3720b118 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/TrxLogger.cs @@ -87,6 +87,7 @@ internal TrxLogger(IFileHelper fileHelper, TrxFileHelper trxFileHelper) /// Gets the directory under which default trx file and test results attachments should be saved. /// private string? _testResultsDirPath; + private bool _warnOnFileOverwrite; #region ITestLogger @@ -135,6 +136,13 @@ public void Initialize(TestLoggerEvents events, Dictionary para var isLogFilePrefixParameterExists = parameters.TryGetValue(TrxLoggerConstants.LogFilePrefixKey, out _); var isLogFileNameParameterExists = parameters.TryGetValue(TrxLoggerConstants.LogFileNameKey, out _); + _warnOnFileOverwrite = parameters.TryGetValue(TrxLoggerConstants.WarnOnFileOverwrite, out string? warnOnOverwriteString) + ? bool.TryParse(warnOnOverwriteString, out bool providedValue) + ? providedValue + // We found the option but could not parse the value. + : true + // We did not find the option and want to fallback to warning on write, because that was the default before. + : true; if (isLogFilePrefixParameterExists && isLogFileNameParameterExists) { @@ -450,9 +458,12 @@ private void ReserveTrxFilePath() if (shouldOverwrite && File.Exists(filePath)) { - var overwriteWarningMsg = string.Format(CultureInfo.CurrentCulture, TrxLoggerResources.TrxLoggerResultsFileOverwriteWarning, filePath); - ConsoleOutput.Instance.Warning(false, overwriteWarningMsg); - EqtTrace.Warning(overwriteWarningMsg); + if (_warnOnFileOverwrite) + { + var overwriteWarningMsg = string.Format(CultureInfo.CurrentCulture, TrxLoggerResources.TrxLoggerResultsFileOverwriteWarning, filePath); + ConsoleOutput.Instance.Warning(false, overwriteWarningMsg); + EqtTrace.Warning(overwriteWarningMsg); + } } else { diff --git a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs index 2987b6e448..c8cee63592 100644 --- a/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs +++ b/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Constants.cs @@ -75,6 +75,11 @@ internal static class Constants /// public const string TmiTestIdPropertyIdentifier = "MSTestDiscoverer.TmiTestId"; + /// + /// Warn when overwriting the trx file. + /// + public static string WarnOnFileOverwrite = "WarnOnFileOverwrite"; + /// /// Mstest adapter string ///