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

[xharness] Look at the exact NUnit version an NUnit project is referencing to figure out how to run it in a makefile. #9322

Merged
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
85 changes: 53 additions & 32 deletions tests/xharness/Jenkins/TestTasks/NUnitExecuteTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,25 @@ public NUnitExecuteTask (Jenkins jenkins, BuildToolTask build_task, IProcessMana
{
}

public void FindNUnitConsoleExecutable (ILog log)
public static bool TryGetNUnitVersion (ILog log, string csproj, out string version, out bool isPackageRef)
{
if (!string.IsNullOrEmpty (TestExecutable)) {
log.WriteLine ("Using existing executable: {0}", TestExecutable);
return;
}

var packages_conf = Path.Combine (Path.GetDirectoryName (TestProject.Path), "packages.config");
var nunit_version = string.Empty;
var is_packageref = false;
const string default_nunit_version = "3.9.0";

isPackageRef = false;
version = string.Empty;

var packages_conf = Path.Combine (Path.GetDirectoryName (csproj), "packages.config");
if (!File.Exists (packages_conf)) {
var xml = new XmlDocument ();
xml.LoadWithoutNetworkAccess (TestProject.Path);
xml.LoadWithoutNetworkAccess (csproj);
var packageref = xml.SelectSingleNode ("//*[local-name()='PackageReference' and @Include = 'NUnit.ConsoleRunner']");
if (packageref != null) {
is_packageref = true;
nunit_version = packageref.Attributes ["Version"].InnerText;
log.WriteLine ("Found PackageReference in {0} for NUnit.ConsoleRunner {1}", TestProject, nunit_version);
isPackageRef = true;
version = packageref.Attributes ["Version"].InnerText;
log.WriteLine ("Found PackageReference in {0} for NUnit.ConsoleRunner {1}", csproj, version);
} else {
nunit_version = default_nunit_version;
log.WriteLine ("No packages.config found for {0}: assuming nunit version is {1}", TestProject, nunit_version);
version = default_nunit_version;
log.WriteLine ("No packages.config found for {0}: assuming nunit version is {1}", csproj, version);
}
} else {
using (var str = new StreamReader (packages_conf)) {
Expand All @@ -57,35 +53,60 @@ public void FindNUnitConsoleExecutable (ILog log)
var id = reader.GetAttribute ("id");
if (id != "NUnit.ConsoleRunner" && id != "NUnit.Runners")
continue;
nunit_version = reader.GetAttribute ("version");
version = reader.GetAttribute ("version");
break;
}
}
}
if (nunit_version == string.Empty) {
nunit_version = default_nunit_version;
log.WriteLine ("Could not find the NUnit.ConsoleRunner element in {0}, using the default version ({1})", packages_conf, nunit_version);
if (version == string.Empty) {
version = default_nunit_version;
log.WriteLine ("Could not find the NUnit.ConsoleRunner element in {0}, using the default version ({1})", packages_conf, version);
} else {
log.WriteLine ("Found the NUnit.ConsoleRunner/NUnit.Runners element in {0} for {2}, version is: {1}", packages_conf, nunit_version, TestProject.Path);
log.WriteLine ("Found the NUnit.ConsoleRunner/NUnit.Runners element in {0} for {2}, version is: {1}", packages_conf, version, csproj);
}
}

return true;
}

public static bool TryGetNUnitExecutionSettings (ILog log, string csproj, string testLibrary, out string testExecutable, out string workingDirectory)
{
if (!TryGetNUnitVersion (log, csproj, out var nunit_version, out var is_packageref)) {
log.WriteLine ($"Failed to find NUnit version for {csproj}");
throw new Exception ($"Failed to find NUnit version for {csproj}");
}

if (is_packageref) {
TestExecutable = Path.Combine (RootDirectory, "..", "tools", $"nunit3-console-{nunit_version}");
if (!File.Exists (TestExecutable))
throw new FileNotFoundException ($"The helper script to execute the unit tests does not exist: {TestExecutable}");
WorkingDirectory = Path.GetDirectoryName (TestProject.Path);
testExecutable = Path.Combine (HarnessConfiguration.RootDirectory, "..", "tools", $"nunit3-console-{nunit_version}");
if (!File.Exists (testExecutable))
throw new FileNotFoundException ($"The helper script to execute the unit tests does not exist: {testExecutable}");
workingDirectory = Path.GetDirectoryName (csproj);
} else if (nunit_version [0] == '2') {
TestExecutable = Path.Combine (RootDirectory, "..", "packages", "NUnit.Runners." + nunit_version, "tools", "nunit-console.exe");
WorkingDirectory = Path.Combine (Path.GetDirectoryName (TestExecutable), "lib");
testExecutable = Path.Combine (HarnessConfiguration.RootDirectory, "..", "packages", "NUnit.Runners." + nunit_version, "tools", "nunit-console.exe");
workingDirectory = Path.Combine (Path.GetDirectoryName (testExecutable), "lib");
} else {
TestExecutable = Path.Combine (RootDirectory, "..", "packages", "NUnit.ConsoleRunner." + nunit_version, "tools", "nunit3-console.exe");
WorkingDirectory = Path.GetDirectoryName (TestLibrary);
testExecutable = Path.Combine (HarnessConfiguration.RootDirectory, "..", "packages", "NUnit.ConsoleRunner." + nunit_version, "tools", "nunit3-console.exe");
workingDirectory = Path.GetDirectoryName (testLibrary);
}
TestExecutable = Path.GetFullPath (TestExecutable);
WorkingDirectory = Path.GetFullPath (WorkingDirectory);
if (!File.Exists (TestExecutable))
throw new FileNotFoundException ($"The nunit executable '{TestExecutable}' doesn't exist.");
testExecutable = Path.GetFullPath (testExecutable);
workingDirectory = Path.GetFullPath (workingDirectory);
if (!File.Exists (testExecutable))
throw new FileNotFoundException ($"The nunit executable '{testExecutable}' doesn't exist.");

return true;
}

public void FindNUnitConsoleExecutable (ILog log)
{
if (!string.IsNullOrEmpty (TestExecutable)) {
log.WriteLine ("Using existing executable: {0}", TestExecutable);
return;
}

if (!TryGetNUnitExecutionSettings (log, TestProject.Path, TestLibrary, out var testExecutable, out var workingDirectory))
throw new Exception ($"Unable to get NUnit execution settings for {TestProject.Path}");
TestExecutable = testExecutable;
WorkingDirectory = workingDirectory;
}

public bool IsNUnit3 {
Expand Down
16 changes: 13 additions & 3 deletions tests/xharness/MakefileGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Linq;
using System.Text;
using Microsoft.DotNet.XHarness.iOS.Shared;
using Microsoft.DotNet.XHarness.iOS.Shared.Logging;
using Xharness.Jenkins.TestTasks;
using Xharness.Targets;

namespace Xharness
Expand Down Expand Up @@ -109,9 +111,17 @@ public static void CreateMacMakefile (IHarness harness, IEnumerable<MacTarget> t
writer.WriteTarget (MakeMacUnifiedTargetName (target, MacTargetNameType.Exec), "");
if (target.IsNUnitProject) {
writer.WriteLine ("\t$(Q)rm -f $(CURDIR)/.{0}-failed.stamp", make_escaped_name);
writer.WriteLine ("\t$(SYSTEM_MONO) --debug $(XIBUILD_EXE_PATH) -t -- $(TOP)/packages/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe \"{1}/bin/$(CONFIG)/{0}.dll\" \"--result=$(abspath $(CURDIR)/{0}-TestResult.xml);format=nunit2\" $(TEST_FIXTURE) --labels=All || touch $(CURDIR)/.{0}-failed.stamp", make_escaped_name, Path.GetDirectoryName (target.ProjectPath));
writer.WriteLine ("\t$(Q)[[ -z \"$$BUILD_REPOSITORY\" ]] || ( xsltproc $(TOP)/tests/HtmlTransform.xslt {0}-TestResult.xml > {0}-index.html && echo \"@MonkeyWrench: AddFile: $$PWD/{0}-index.html\")", make_escaped_name);
writer.WriteLine ("\t$(Q)[[ ! -e .{0}-failed.stamp ]]", make_escaped_name);
var testLibrary = $"{Path.GetDirectoryName (target.ProjectPath)}/bin/$(CONFIG)/{make_escaped_name}.dll";
var log = new MemoryLog ();
if (NUnitExecuteTask.TryGetNUnitExecutionSettings (log, target.ProjectPath, testLibrary, out var testExecutable, out var workingDirectory)) {
if (testExecutable.EndsWith (".exe", StringComparison.Ordinal))
testExecutable = "$(SYSTEM_MONO) --debug $(XIBUILD_EXE_PATH) -t -- " + testExecutable;
writer.WriteLine ($"\tcd \"{workingDirectory}\" && {testExecutable} \"{testLibrary}\" \"--result=$(abspath $(CURDIR)/{make_escaped_name}-TestResult.xml);format=nunit2\" $(TEST_FIXTURE) --labels=All || touch $(CURDIR)/.{make_escaped_name}-failed.stamp", make_escaped_name, Path.GetDirectoryName (target.ProjectPath));
writer.WriteLine ("\t$(Q)[[ -z \"$$BUILD_REPOSITORY\" ]] || ( xsltproc $(TOP)/tests/HtmlTransform.xslt {0}-TestResult.xml > {0}-index.html && echo \"@MonkeyWrench: AddFile: $$PWD/{0}-index.html\")", make_escaped_name);
writer.WriteLine ("\t$(Q)[[ ! -e .{0}-failed.stamp ]]", make_escaped_name);
} else {
throw new Exception ($"Failed to compute NUNit execution settings:\n" + log.ToString ());
}
} else
writer.WriteLine ("\t$(Q) {2}/bin/x86/$(CONFIG){1}/{0}.app/Contents/MacOS/{0}", make_escaped_name, target.Suffix, CreateRelativePath (Path.GetDirectoryName (target.ProjectPath).Replace (" ", "\\ "), Path.GetDirectoryName (makefile)));
writer.WriteLine ();
Expand Down
3 changes: 3 additions & 0 deletions tools/nunit3-console-3.9.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash -eu

exec mono ~/.nuget/packages/nunit.consolerunner/3.9.0/tools/nunit3-console.exe "$@"