Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #997 #1001

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions src/NUnitTestAdapter/AdapterSettings.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -72,6 +72,7 @@ public interface IAdapterSettings
VsTestCategoryType VsTestCategoryType { get; }
string TestOutputXml { get; }
bool UseTestOutputXml { get; }
OutputXmlFolderMode OutputXmlFolderMode { get; }

/// <summary>
/// For retry runs create a new file for each run.
Expand Down Expand Up @@ -130,6 +131,7 @@ public interface IAdapterSettings
// Filter control
ExplicitModeEnum ExplicitMode { get; }
bool SkipExecutionWhenNoTests { get; }
string TestOutputFolder { get; }
}

public enum VsTestCategoryType
Expand Down Expand Up @@ -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;

/// <summary>
/// Calculated property.
/// </summary>
public string TestOutputFolder { get; private set; } = "";

public bool NewOutputXmlFileForEachRun { get; private set; }
public int DefaultTimeout { get; private set; }

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -631,4 +662,11 @@ public enum ExplicitModeEnum
Strict,
Relaxed
}
}

public enum OutputXmlFolderMode
{
UseResultDirectory,
RelativeToResultDirectory,
RelativeToWorkFolder
}
}
4 changes: 1 addition & 3 deletions src/NUnitTestAdapter/NUnit3TestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 18 additions & 11 deletions src/NUnitTestAdapterTests/AdapterSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -194,11 +194,11 @@ public void WorkDirectorySetting()
[Test]
public void TestOutputSetting()
{
_settings.Load("<RunSettings><NUnit><TestOutputXml>/my/work/dir</TestOutputXml></NUnit></RunSettings>");
_settings.Load(@"<RunSettings><NUnit><WorkDirectory>C:\Whatever</WorkDirectory><TestOutputXml>/my/work/dir</TestOutputXml></NUnit></RunSettings>");
Assert.That(_settings.UseTestOutputXml);
Assert.Multiple(() =>
{
Assert.That(_settings.TestOutputXml, Does.Contain(@"/my/work/dir"));
Assert.That(_settings.TestOutputFolder, Does.Contain(@"/my/work/dir"));
});
}

Expand All @@ -212,23 +212,30 @@ public void TestNewOutputXmlFileForEachRunSetting()
Assert.That(_settings.NewOutputXmlFileForEachRun, Is.True);
}

/// <summary>
/// Workdir set, and is absolute, TestOutputXml is relative.
/// </summary>
[Ignore("Is not handled in the test executor, not in the test settings")]
[Test]
public void TestOutputSettingWithWorkDir()
{
_settings.Load(@"<RunSettings><NUnit><WorkDirectory>C:\Whatever</WorkDirectory><TestOutputXml>my/testoutput/dir</TestOutputXml></NUnit></RunSettings>");
_settings.Load(@"<RunSettings><NUnit><WorkDirectory>C:\Whatever</WorkDirectory><TestOutputXml>my/testoutput/dir</TestOutputXml><OutputXmlFolderMode>RelativeToWorkFolder</OutputXmlFolderMode></NUnit></RunSettings>");
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");
});
}

/// <summary>
/// Test should set output folder to same as resultdirectory, and ignore workdirectory and testoutputxml.
/// </summary>
[Test]
public void TestOutputSettingWithUseResultDirectory()
{
_settings.Load(@"<RunSettings><RunConfiguration><ResultsDirectory>c:\whatever\results</ResultsDirectory></RunConfiguration><NUnit><WorkDirectory>C:\AnotherWhatever</WorkDirectory><TestOutputXml>my/testoutput/dir</TestOutputXml><OutputXmlFolderMode>UseResultDirectory</OutputXmlFolderMode></NUnit></RunSettings>");
Assert.That(_settings.UseTestOutputXml, "Settings not loaded properly");
Assert.That(_settings.TestOutputFolder, Is.EqualTo(@"c:\whatever\results"), "Content not correct");
}


[Test]
public void NumberOfTestWorkersSetting()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void TestXmlFileNameGeneration()
{
var logger = Substitute.For<ITestLogger>();
var settings = new AdapterSettings(logger);
settings.Load("<RunSettings><NUnit><TestOutputXml>/my/work/dir</TestOutputXml></NUnit></RunSettings>");
settings.Load(@"<RunSettings><NUnit><WorkDirectory>c:\whatever</WorkDirectory><TestOutputXml>/my/work/dir</TestOutputXml></NUnit></RunSettings>");
var sut = new NUnitEngineAdapter();
sut.InitializeSettingsAndLogging(settings, logger);
string path = sut.GetXmlFilePath("c:/", "assembly", "xml");
Expand All @@ -24,7 +24,7 @@ public void TestXmlFileNameGenerationNewOutputXmlFileForEachRun()
{
var logger = Substitute.For<ITestLogger>();
var settings = new AdapterSettings(logger);
settings.Load("<RunSettings><NUnit><TestOutputXml>/my/work/dir</TestOutputXml><NewOutputXmlFileForEachRun>true</NewOutputXmlFileForEachRun></NUnit></RunSettings>");
settings.Load(@"<RunSettings><NUnit><WorkDirectory>c:\whatever</WorkDirectory><TestOutputXml>/my/work/dir</TestOutputXml><NewOutputXmlFileForEachRun>true</NewOutputXmlFileForEachRun></NUnit></RunSettings>");
var sut = new NUnitEngineAdapter();
sut.InitializeSettingsAndLogging(settings, logger);
string path = sut.GetXmlFilePath("c:/", "assembly", "xml");
Expand Down