From ea43ef8ab4d63204d836769e94e4fecc6fc122bf Mon Sep 17 00:00:00 2001 From: Terje Sandstrom Date: Fri, 21 Oct 2022 17:09:07 +0200 Subject: [PATCH] Fix for #997 (#1001) --- src/NUnitTestAdapter/AdapterSettings.cs | 48 +++++++++++++++++-- src/NUnitTestAdapter/NUnit3TestExecutor.cs | 4 +- .../AdapterSettingsTests.cs | 29 ++++++----- .../NUnitEngineAdapterTests.cs | 4 +- 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/NUnitTestAdapter/AdapterSettings.cs b/src/NUnitTestAdapter/AdapterSettings.cs index eadeed36..29e34334 100644 --- a/src/NUnitTestAdapter/AdapterSettings.cs +++ b/src/NUnitTestAdapter/AdapterSettings.cs @@ -1,5 +1,5 @@ // *********************************************************************** -// Copyright (c) 2014-2021 Charlie Poole, Terje Sandstrom +// Copyright (c) 2014-2021 Charlie Poole, 2014-2022 Terje Sandstrom // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -72,6 +72,7 @@ public interface IAdapterSettings VsTestCategoryType VsTestCategoryType { get; } string TestOutputXml { get; } bool UseTestOutputXml { get; } + OutputXmlFolderMode OutputXmlFolderMode { get; } /// /// For retry runs create a new file for each run. @@ -130,6 +131,7 @@ public interface IAdapterSettings // Filter control ExplicitModeEnum ExplicitMode { get; } bool SkipExecutionWhenNoTests { get; } + string TestOutputFolder { get; } } public enum VsTestCategoryType @@ -212,7 +214,15 @@ public AdapterSettings(ITestLogger logger) public string WorkDirectory { get; private set; } public string Where { get; private set; } public string TestOutputXml { get; private set; } - public bool UseTestOutputXml => !string.IsNullOrEmpty(TestOutputXml); + public bool UseTestOutputXml => !string.IsNullOrEmpty(TestOutputXml) || OutputXmlFolderMode == OutputXmlFolderMode.UseResultDirectory; + + public OutputXmlFolderMode OutputXmlFolderMode { get; private set; } = OutputXmlFolderMode.RelativeToWorkFolder; + + /// + /// Calculated property. + /// + public string TestOutputFolder { get; private set; } = ""; + public bool NewOutputXmlFileForEachRun { get; private set; } public int DefaultTimeout { get; private set; } @@ -329,7 +339,11 @@ public void Load(string settingsXml) BasePath = GetInnerTextWithLog(nunitNode, nameof(BasePath)); PrivateBinPath = GetInnerTextWithLog(nunitNode, nameof(PrivateBinPath)); TestOutputXml = GetInnerTextWithLog(nunitNode, nameof(TestOutputXml)); + OutputXmlFolderMode = MapEnum(GetInnerText(nunitNode, nameof(OutputXmlFolderMode), Verbosity > 0), OutputXmlFolderMode.RelativeToWorkFolder); NewOutputXmlFileForEachRun = GetInnerTextAsBool(nunitNode, nameof(NewOutputXmlFileForEachRun), false); + + SetTestOutputFolder(); + RandomSeed = GetInnerTextAsNullableInt(nunitNode, nameof(RandomSeed)); RandomSeedSpecified = RandomSeed.HasValue; if (!RandomSeedSpecified) @@ -404,6 +418,24 @@ public void Load(string settingsXml) UpdateNumberOfTestWorkers(); } + private void SetTestOutputFolder() + { + if (!UseTestOutputXml) + return; + switch (OutputXmlFolderMode) + { + case OutputXmlFolderMode.UseResultDirectory: + TestOutputFolder = ResultsDirectory; + return; + case OutputXmlFolderMode.RelativeToResultDirectory: + TestOutputFolder = Path.Combine(ResultsDirectory, TestOutputXml); + return; + case OutputXmlFolderMode.RelativeToWorkFolder: + TestOutputFolder = Path.Combine(WorkDirectory, TestOutputXml); + return; + } + } + private void ExtractNUnitDiagnosticSettings(XmlNode nunitNode) { DumpXmlTestDiscovery = GetInnerTextAsBool(nunitNode, nameof(DumpXmlTestDiscovery), false); @@ -568,8 +600,7 @@ private int GetInnerTextAsInt(XmlNode startNode, string xpath, int defaultValue) int? res = null; if (!string.IsNullOrEmpty(temp)) res = int.Parse(temp); - if (log) - Log(xpath, res); + Log(xpath, res); return res; } @@ -631,4 +662,11 @@ public enum ExplicitModeEnum Strict, Relaxed } -} + + public enum OutputXmlFolderMode + { + UseResultDirectory, + RelativeToResultDirectory, + RelativeToWorkFolder + } +} \ No newline at end of file diff --git a/src/NUnitTestAdapter/NUnit3TestExecutor.cs b/src/NUnitTestAdapter/NUnit3TestExecutor.cs index 25fc5f8c..1f455146 100644 --- a/src/NUnitTestAdapter/NUnit3TestExecutor.cs +++ b/src/NUnitTestAdapter/NUnit3TestExecutor.cs @@ -359,9 +359,7 @@ private void CreateTestOutputFolder() return; } - string path = Path.IsPathRooted(Settings.TestOutputXml) - ? Settings.TestOutputXml - : Path.Combine(WorkDir, Settings.TestOutputXml); + string path = Settings.TestOutputFolder; try { Directory.CreateDirectory(path); diff --git a/src/NUnitTestAdapterTests/AdapterSettingsTests.cs b/src/NUnitTestAdapterTests/AdapterSettingsTests.cs index b285e4d9..6687d4c8 100644 --- a/src/NUnitTestAdapterTests/AdapterSettingsTests.cs +++ b/src/NUnitTestAdapterTests/AdapterSettingsTests.cs @@ -1,5 +1,5 @@ // *********************************************************************** -// Copyright (c) 2011-2021 Charlie Poole, Terje Sandstrom +// Copyright (c) 2011-2021 Charlie Poole, 2014-2022 Terje Sandstrom // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -194,11 +194,11 @@ public void WorkDirectorySetting() [Test] public void TestOutputSetting() { - _settings.Load("/my/work/dir"); + _settings.Load(@"C:\Whatever/my/work/dir"); Assert.That(_settings.UseTestOutputXml); Assert.Multiple(() => { - Assert.That(_settings.TestOutputXml, Does.Contain(@"/my/work/dir")); + Assert.That(_settings.TestOutputFolder, Does.Contain(@"/my/work/dir")); }); } @@ -212,23 +212,30 @@ public void TestNewOutputXmlFileForEachRunSetting() Assert.That(_settings.NewOutputXmlFileForEachRun, Is.True); } - /// - /// Workdir set, and is absolute, TestOutputXml is relative. - /// - [Ignore("Is not handled in the test executor, not in the test settings")] [Test] public void TestOutputSettingWithWorkDir() { - _settings.Load(@"C:\Whatevermy/testoutput/dir"); + _settings.Load(@"C:\Whatevermy/testoutput/dirRelativeToWorkFolder"); Assert.That(_settings.UseTestOutputXml, "Settings not loaded properly"); Assert.Multiple(() => { - Assert.That(_settings.TestOutputXml, Does.Contain(@"\my/testoutput/dir"), "Content not correct"); - Assert.That(_settings.TestOutputXml, Does.StartWith(@"C:\"), "Not correct start drive"); - Assert.That(Path.IsPathRooted(_settings.TestOutputXml), Is.True, "Path not properly rooted"); + Assert.That(_settings.TestOutputFolder, Does.Contain(@"\my/testoutput/dir"), "Content not correct"); + Assert.That(_settings.TestOutputFolder, Does.StartWith(@"C:\"), "Not correct start drive"); + Assert.That(Path.IsPathRooted(_settings.TestOutputFolder), Is.True, "Path not properly rooted"); }); } + /// + /// Test should set output folder to same as resultdirectory, and ignore workdirectory and testoutputxml. + /// + [Test] + public void TestOutputSettingWithUseResultDirectory() + { + _settings.Load(@"c:\whatever\resultsC:\AnotherWhatevermy/testoutput/dirUseResultDirectory"); + Assert.That(_settings.UseTestOutputXml, "Settings not loaded properly"); + Assert.That(_settings.TestOutputFolder, Is.EqualTo(@"c:\whatever\results"), "Content not correct"); + } + [Test] public void NumberOfTestWorkersSetting() diff --git a/src/NUnitTestAdapterTests/NUnitEngineTests/NUnitEngineAdapterTests.cs b/src/NUnitTestAdapterTests/NUnitEngineTests/NUnitEngineAdapterTests.cs index 4798ca1c..ef8413a9 100644 --- a/src/NUnitTestAdapterTests/NUnitEngineTests/NUnitEngineAdapterTests.cs +++ b/src/NUnitTestAdapterTests/NUnitEngineTests/NUnitEngineAdapterTests.cs @@ -12,7 +12,7 @@ public void TestXmlFileNameGeneration() { var logger = Substitute.For(); var settings = new AdapterSettings(logger); - settings.Load("/my/work/dir"); + settings.Load(@"c:\whatever/my/work/dir"); var sut = new NUnitEngineAdapter(); sut.InitializeSettingsAndLogging(settings, logger); string path = sut.GetXmlFilePath("c:/", "assembly", "xml"); @@ -24,7 +24,7 @@ public void TestXmlFileNameGenerationNewOutputXmlFileForEachRun() { var logger = Substitute.For(); var settings = new AdapterSettings(logger); - settings.Load("/my/work/dirtrue"); + settings.Load(@"c:\whatever/my/work/dirtrue"); var sut = new NUnitEngineAdapter(); sut.InitializeSettingsAndLogging(settings, logger); string path = sut.GetXmlFilePath("c:/", "assembly", "xml");