Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert bmx-nunit to an Inedo.SDK extension.
- Loading branch information
0 parents
commit 5281ccc
Showing
8 changed files
with
417 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
NUnit/packages/* | ||
bin | ||
obj | ||
*.suo | ||
*.user | ||
*.vspscc | ||
NUnit/.vs | ||
*/_sgbak/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2018, Inedo, LLC. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{CACE966A-0DBD-4C66-AA0E-6D2B54F84850}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>Inedo.Extensions.NUnit</RootNamespace> | ||
<AssemblyName>NUnit</AssemblyName> | ||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<Deterministic>true</Deterministic> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="Inedo.Agents.Client, Version=1000.0.0.0, Culture=neutral, PublicKeyToken=9de986a2f8db80fc, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\Inedo.SDK.1.1.0-pre0012\lib\net452\Inedo.Agents.Client.dll</HintPath> | ||
<SpecificVersion>False</SpecificVersion> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Inedo.ExecutionEngine, Version=1000.0.0.0, Culture=neutral, PublicKeyToken=68703f0e52007e75, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\Inedo.SDK.1.1.0-pre0012\lib\net452\Inedo.ExecutionEngine.dll</HintPath> | ||
<SpecificVersion>False</SpecificVersion> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Inedo.SDK, Version=1.1.0.0, Culture=neutral, PublicKeyToken=29fae5dec3001603, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\Inedo.SDK.1.1.0-pre0012\lib\net452\Inedo.SDK.dll</HintPath> | ||
<SpecificVersion>False</SpecificVersion> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="InedoLib, Version=1000.0.0.0, Culture=neutral, PublicKeyToken=112cfb71329714a6, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\Inedo.SDK.1.1.0-pre0012\lib\net452\InedoLib.dll</HintPath> | ||
<SpecificVersion>False</SpecificVersion> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\Inedo.SDK.1.1.0-pre0012\lib\net452\Newtonsoft.Json.dll</HintPath> | ||
<SpecificVersion>False</SpecificVersion> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Operations\NUnitOperation.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
using Inedo.Agents; | ||
using Inedo.Diagnostics; | ||
using Inedo.Documentation; | ||
using Inedo.Extensibility; | ||
using Inedo.Extensibility.Operations; | ||
|
||
namespace Inedo.Extensions.NUnit.Operations | ||
{ | ||
[Tag("unit-tests")] | ||
[ScriptAlias("Execute-NUnit")] | ||
[DisplayName("Execute NUnit Tests")] | ||
[Description("Runs NUnit unit tests on a specified project, assembly, or NUnit file.")] | ||
[ScriptNamespace("NUnit", PreferUnqualified = true)] | ||
public sealed class NUnitOperation : ExecuteOperation | ||
{ | ||
[Required] | ||
[ScriptAlias("TestFile")] | ||
[DisplayName("Test file")] | ||
[Description("The file nunit will test against (could be dll, proj, or config file based on test runner).")] | ||
public string TestFile { get; set; } | ||
[Required] | ||
[ScriptAlias("NUnitExePath")] | ||
[DisplayName("nunit path")] | ||
[Description("The path to the nunit test runner executable.")] | ||
public string ExePath { get; set; } | ||
[ScriptAlias(nameof(IsNUnit3))] | ||
[DisplayName("Is NUnit v3")] | ||
[Description("When set to true, a different syntax will be used for command-line arguments.")] | ||
public bool IsNUnit3 { get; set; } | ||
[ScriptAlias("Arguments")] | ||
[DisplayName("Additional arguments")] | ||
[Description("Raw command line arguments passed to the nunit test runner.")] | ||
public string AdditionalArguments { get; set; } | ||
[ScriptAlias("OutputDirectory")] | ||
[DisplayName("Output directory")] | ||
[Description("The directory to generate the XML test results.")] | ||
public string CustomXmlOutputPath { get; set; } | ||
|
||
[Category("Advanced")] | ||
[ScriptAlias("Group")] | ||
[DisplayName("Group name")] | ||
[Description("When multiple sets of tests are performed, unique group names will categorize them in the UI.")] | ||
[PlaceholderText("NUnit")] | ||
public string GroupName { get; set; } | ||
|
||
public override async Task ExecuteAsync(IOperationExecutionContext context) | ||
{ | ||
var fileOps = await context.Agent.GetServiceAsync<IFileOperationsExecuter>(); | ||
var testFilePath = context.ResolvePath(this.TestFile); | ||
this.LogDebug("Test file: " + testFilePath); | ||
|
||
var exePath = context.ResolvePath(this.ExePath); | ||
this.LogDebug("Exe path: " + exePath); | ||
|
||
if (!fileOps.FileExists(testFilePath)) | ||
{ | ||
this.LogError($"Test file {testFilePath} does not exist."); | ||
return; | ||
} | ||
|
||
if (!fileOps.FileExists(exePath)) | ||
{ | ||
this.LogError($"NUnit runner not found at {exePath}."); | ||
return; | ||
} | ||
|
||
string outputFilePath; | ||
if (string.IsNullOrEmpty(this.CustomXmlOutputPath)) | ||
outputFilePath = fileOps.CombinePath(context.WorkingDirectory, Guid.NewGuid().ToString("N") + ".xml"); | ||
else | ||
outputFilePath = context.ResolvePath(this.CustomXmlOutputPath); | ||
|
||
this.LogDebug("Output file: " + outputFilePath); | ||
|
||
var args = this.IsNUnit3 | ||
? $"\"{testFilePath}\" --result:\"{outputFilePath}\";format=nunit2" | ||
: $"\"{testFilePath}\" /xml:\"{outputFilePath}\""; | ||
|
||
if (!string.IsNullOrEmpty(this.AdditionalArguments)) | ||
{ | ||
this.LogDebug("Additional arguments: " + this.AdditionalArguments); | ||
args += " " + this.AdditionalArguments; | ||
} | ||
|
||
try | ||
{ | ||
await this.ExecuteCommandLineAsync( | ||
context, | ||
new RemoteProcessStartInfo | ||
{ | ||
FileName = exePath, | ||
Arguments = args, | ||
WorkingDirectory = context.WorkingDirectory | ||
} | ||
); | ||
|
||
XDocument xdoc; | ||
using (var stream = await fileOps.OpenFileAsync(outputFilePath, FileMode.Open, FileAccess.Read)) | ||
{ | ||
xdoc = XDocument.Load(stream); | ||
} | ||
|
||
#if DEBUG | ||
this.LogDebug(xdoc.ToString()); | ||
#endif | ||
|
||
var testResultsElement = xdoc.Element("test-results"); | ||
|
||
var startTime = this.TryParseStartTime((string)testResultsElement.Attribute("date"), (string)testResultsElement.Attribute("time")) ?? DateTime.UtcNow; | ||
var failures = 0; | ||
|
||
var testRecorder = await context.TryGetServiceAsync<IUnitTestRecorder>(); | ||
foreach (var testCaseElement in xdoc.Descendants("test-case")) | ||
{ | ||
var testName = (string)testCaseElement.Attribute("name"); | ||
|
||
// skip tests that weren't actually run | ||
if (string.Equals((string)testCaseElement.Attribute("executed"), "False", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
this.LogInformation($"NUnit test: {testName} (skipped)"); | ||
continue; | ||
} | ||
|
||
var result = AH.Switch<string, UnitTestStatus>((string)testCaseElement.Attribute("success"), StringComparer.OrdinalIgnoreCase) | ||
.Case("True", UnitTestStatus.Passed) | ||
.Case("Inconclusive", UnitTestStatus.Inconclusive) | ||
.Default(UnitTestStatus.Failed) | ||
.End(); | ||
if (result == UnitTestStatus.Failed) | ||
failures++; | ||
|
||
var testDuration = this.TryParseTestTime((string)testCaseElement.Attribute("time")); | ||
|
||
this.LogInformation($"NUnit test: {testName}, Result: {result}, Test length: {testDuration}"); | ||
|
||
if (testRecorder != null) | ||
{ | ||
await testRecorder.RecordUnitTestAsync( | ||
groupName: AH.NullIf(this.GroupName, string.Empty) ?? "NUnit", | ||
testName: testName, | ||
testStatus: result, | ||
testResult: testCaseElement.ToString(), | ||
startTime: startTime, | ||
duration: testDuration | ||
); | ||
} | ||
|
||
startTime += testDuration; | ||
} | ||
|
||
if (failures > 0) | ||
this.LogError($"{failures} test failures were reported."); | ||
} | ||
finally | ||
{ | ||
if (string.IsNullOrEmpty(this.CustomXmlOutputPath)) | ||
{ | ||
this.LogDebug($"Deleting temp output file ({outputFilePath})..."); | ||
try | ||
{ | ||
fileOps.DeleteFile(outputFilePath); | ||
} | ||
catch | ||
{ | ||
this.LogWarning($"Could not delete {outputFilePath}."); | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected override ExtendedRichDescription GetDescription(IOperationConfiguration config) | ||
{ | ||
var longActionDescription = new RichDescription(); | ||
if (!string.IsNullOrWhiteSpace(config[nameof(this.AdditionalArguments)])) | ||
{ | ||
longActionDescription.AppendContent( | ||
"with additional arguments: ", | ||
new Hilite(config[nameof(this.AdditionalArguments)]) | ||
); | ||
} | ||
|
||
return new ExtendedRichDescription( | ||
new RichDescription( | ||
"Run NUnit on ", | ||
new DirectoryHilite(config[nameof(this.TestFile)]) | ||
), | ||
longActionDescription | ||
); | ||
} | ||
|
||
private DateTime? TryParseStartTime(string date, string time) | ||
{ | ||
try | ||
{ | ||
if (string.IsNullOrWhiteSpace(date)) | ||
{ | ||
if (DateTime.TryParse(time, out DateTime result)) | ||
return result.ToUniversalTime(); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(date) && !string.IsNullOrWhiteSpace(time)) | ||
{ | ||
var dateParts = date.Split('-'); | ||
var timeParts = time.Split(':'); | ||
|
||
return new DateTime( | ||
year: int.Parse(dateParts[0]), | ||
month: int.Parse(dateParts[1]), | ||
day: int.Parse(dateParts[2]), | ||
hour: int.Parse(timeParts[0]), | ||
minute: int.Parse(timeParts[1]), | ||
second: int.Parse(timeParts[2]) | ||
).ToUniversalTime(); | ||
} | ||
} | ||
catch | ||
{ | ||
} | ||
|
||
this.LogWarning("Unable to parse start time; using current time instead."); | ||
return null; | ||
} | ||
private TimeSpan TryParseTestTime(string time) | ||
{ | ||
if (string.IsNullOrWhiteSpace(time)) | ||
return TimeSpan.Zero; | ||
|
||
var mungedTime = time.Replace(',', '.'); | ||
bool parsed = double.TryParse( | ||
mungedTime, | ||
NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, | ||
CultureInfo.InvariantCulture, | ||
out double doubleTime | ||
); | ||
|
||
if (!parsed) | ||
this.LogWarning($"Could not parse {time} as a time in seconds."); | ||
|
||
return TimeSpan.FromSeconds(doubleTime); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
[assembly: AssemblyTitle("NUnit")] | ||
[assembly: AssemblyDescription("Contains a unit testing operation for NUnit-based tests.")] | ||
[assembly: AssemblyCompany("Inedo")] | ||
[assembly: AssemblyCopyright("Copyright © Inedo 2018")] | ||
[assembly: ComVisible(false)] | ||
[assembly: AssemblyVersion("1.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Inedo.SDK" version="1.1.0-pre0012" targetFramework="net452" /> | ||
</packages> |
Oops, something went wrong.