Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3462e43
feat(controller): add example discovery for external runners
GiuseppePiscopo Feb 8, 2017
451a6cf
chore(deps): upgrade dotnet-test-nunit to 3.4.0-beta-3
GiuseppePiscopo Feb 8, 2017
0046781
fix(tools): remove nunit --labels option
GiuseppePiscopo Feb 8, 2017
da28946
fix(samples): avoid Microsoft.Extensions.Testing.Abstractions not fou…
GiuseppePiscopo Feb 9, 2017
16eca1f
feat(controller): add example execution for external runners
GiuseppePiscopo Feb 13, 2017
db87e3d
fix(controller): avoid clashes in Run method names
GiuseppePiscopo Feb 13, 2017
30038bd
test(api): unit test Api.Discovery.ExampleSelector
GiuseppePiscopo Feb 16, 2017
b8abeca
test(api): test NSpec.Api.Execution.ExampleRunner
GiuseppePiscopo Feb 16, 2017
065989d
chore(test): delete obsolete code in ExampleRunner tests
GiuseppePiscopo Feb 16, 2017
9fe08a1
fix(tools): CI build shoud include new SampleSpecsApi too
GiuseppePiscopo Feb 16, 2017
c863544
test(api): execution test fails in Release build due to ...
GiuseppePiscopo Feb 16, 2017
f937406
chore(samples): upgrade to dev packages with working VS execution
GiuseppePiscopo Feb 16, 2017
5eb40ba
feat(api): discovered examples have source information too
GiuseppePiscopo Feb 17, 2017
d3bacc8
fix(test): test project directory path is wrong in CI build
GiuseppePiscopo Feb 17, 2017
dffab1e
fix(test): test project directory path is wrong in .NET 4.5.1
GiuseppePiscopo Feb 17, 2017
8207b4d
fix(samples): avoid DllNotFoundException on Microsoft.DiaSymReader.Na…
GiuseppePiscopo Feb 17, 2017
f09265b
refactor(api): move console running logic to dedicated class
GiuseppePiscopo Feb 17, 2017
debc190
docs(samples): mention new dependencies in DotNetTestSample
GiuseppePiscopo Feb 17, 2017
51aeba0
feat(api): executed examples also have duration
GiuseppePiscopo Feb 19, 2017
94a25a4
chore(samples): upgrade to dev packages showing duration
GiuseppePiscopo Feb 19, 2017
fb43031
refactor(sourceInfo): get body MethodInfo directly from Example
GiuseppePiscopo Feb 19, 2017
b77a5dc
fix(execution): examples in sub-context were run multiple times
GiuseppePiscopo Feb 20, 2017
4695084
fix(test): source code line numbers differ in Release build
GiuseppePiscopo Feb 20, 2017
20cb480
chore(deps): upgrade to dev packages after Disposable refactoring
GiuseppePiscopo Feb 20, 2017
61f7e42
test(api): discovery/execute against async code spec sample
GiuseppePiscopo Feb 21, 2017
23cb97a
chore(api): rename execution-related entry points
GiuseppePiscopo Feb 27, 2017
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
2 changes: 2 additions & 0 deletions sln/Build-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function BuildVersioningOptions() {
"sln\src\NSpecRunner", `
"sln\test\NSpec.Tests", `
"sln\test\Samples\SampleSpecs", `
"sln\test\Samples\SampleSpecsApi", `
"sln\test\Samples\SampleSpecsFocus"

) | ForEach-Object { CleanProject $_ }
Expand All @@ -79,6 +80,7 @@ function BuildVersioningOptions() {
"sln\src\NSpecRunner", `
"sln\test\NSpec.Tests", `
"sln\test\Samples\SampleSpecs", `
"sln\test\Samples\SampleSpecsApi", `
"sln\test\Samples\SampleSpecsFocus"

) | ForEach-Object { Exec { & dotnet restore $_ } }
Expand Down
7 changes: 7 additions & 0 deletions sln/NSpec.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "content", "content", "{6D53
content\DebuggerShim.cs = content\DebuggerShim.cs
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SampleSpecsApi", "test\Samples\SampleSpecsApi\SampleSpecsApi.xproj", "{70692496-9329-4145-899F-5B141C590326}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -73,6 +75,10 @@ Global
{11728D62-6D9B-4D49-9B82-536969751801}.Release|Any CPU.Build.0 = Release|Any CPU
{FBCF1115-4F77-4706-AE32-B4251561C492}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FBCF1115-4F77-4706-AE32-B4251561C492}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70692496-9329-4145-899F-5B141C590326}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70692496-9329-4145-899F-5B141C590326}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70692496-9329-4145-899F-5B141C590326}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70692496-9329-4145-899F-5B141C590326}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -86,5 +92,6 @@ Global
{11728D62-6D9B-4D49-9B82-536969751801} = {1F898A44-0A09-40F7-9335-15054FB25DE3}
{FBCF1115-4F77-4706-AE32-B4251561C492} = {08CCB349-B6B8-4F86-A1B2-FF56A8C2CFEC}
{6D535A1F-A13A-4337-8337-08690D9109BD} = {BC384EC3-E8E5-4997-B0F5-888287842481}
{70692496-9329-4145-899F-5B141C590326} = {1F898A44-0A09-40F7-9335-15054FB25DE3}
EndGlobalSection
EndGlobal
70 changes: 34 additions & 36 deletions sln/src/NSpec/Api/Controller.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,62 @@
using NSpec.Domain;
using NSpec.Domain.Formatters;
using Newtonsoft.Json;
using NSpec.Api.Discovery;
using NSpec.Api.Execution;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace NSpec.Api
{
public class Controller
{
public int Run(
public int RunBatch(
string testAssemblyPath,
string tags,
string formatterClassName,
IDictionary<string, string> formatterOptions,
bool failFast)
{
var formatter = FindFormatter(formatterClassName, formatterOptions);
var batchExampleRunner = new BatchExampleRunner(testAssemblyPath,
tags, formatterClassName, formatterOptions, failFast);

var invocation = new RunnerInvocation(testAssemblyPath, tags, formatter, failFast);

int nrOfFailures = invocation.Run().Failures().Count();
int nrOfFailures = batchExampleRunner.Start();

return nrOfFailures;
}

/// <summary>
/// Find an implementation of IFormatter with the given class name
/// </summary>
/// <param name="formatterClassName"></param>
/// <param name="formatterOptions"></param>
/// <returns></returns>
private static IFormatter FindFormatter(string formatterClassName, IDictionary<string, string> formatterOptions)
public string List(string testAssemblyPath)
{
// Default formatter is the standard console formatter
if (string.IsNullOrEmpty(formatterClassName))
{
var consoleFormatter = new ConsoleFormatter();
consoleFormatter.Options = formatterOptions;
return consoleFormatter;
}
var exampleSelector = new ExampleSelector(testAssemblyPath);

Assembly nspecAssembly = typeof(IFormatter).GetTypeInfo().Assembly;
var discoveredExamples = exampleSelector.Select();

// Look for a class that implements IFormatter with the provided name
var formatterType = nspecAssembly.GetTypes().FirstOrDefault(type =>
(type.Name.ToLowerInvariant() == formatterClassName)
&& typeof(IFormatter).IsAssignableFrom(type));
string serialized = JsonConvert.SerializeObject(discoveredExamples);

if (formatterType != null)
return serialized;
}

public void RunInteractive(
string testAssemblyPath,
IEnumerable<string> exampleFullNames,
Action<string> onExampleStarted,
Action<string> onExampleCompleted)
{
Action<DiscoveredExample> onDiscovered = example =>
{
var formatter = (IFormatter)Activator.CreateInstance(formatterType);
formatter.Options = formatterOptions;
return formatter;
}
else
string serialized = JsonConvert.SerializeObject(example);

onExampleStarted(serialized);
};

Action<ExecutedExample> onExecuted = example =>
{
throw new TypeLoadException("Could not find formatter type " + formatterClassName);
string serialized = JsonConvert.SerializeObject(example);

onExampleCompleted(serialized);
};

var exampleRunner = new ExampleRunner(testAssemblyPath, onDiscovered, onExecuted);

}
exampleRunner.Start(exampleFullNames);
}
}
}
31 changes: 31 additions & 0 deletions sln/src/NSpec/Api/Discovery/DiscoveredExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace NSpec.Api.Discovery
{
public class DiscoveredExample
{
public string FullName { get; set; }

public string SourceFilePath { get; set; }

public int SourceLineNumber { get; set; }

public string SourceAssembly { get; set; }

public string[] Tags { get; set; }

public override string ToString()
{
string tagsText = String.Join(", ", Tags);

return String.Join(", ", new[]
{
$"{nameof(FullName)}: {FullName}",
$"{nameof(SourceFilePath)}: {SourceFilePath}",
$"{nameof(SourceLineNumber)}: {SourceLineNumber}",
$"{nameof(SourceAssembly)}: {SourceAssembly}",
$"{nameof(Tags)}: [{tagsText}]",
});
}
}
}
25 changes: 25 additions & 0 deletions sln/src/NSpec/Api/Discovery/DiscoveryUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Testing.Abstractions;
using NSpec.Domain;
using System.Linq;

namespace NSpec.Api.Discovery
{
public static class DiscoveryUtils
{
public static DiscoveredExample MapToDiscovered(ExampleBase example, string binaryPath, SourceInformation sourceInfo)
{
var discoveredExample = new DiscoveredExample()
{
FullName = example.FullName(),
SourceAssembly = binaryPath,
SourceFilePath = sourceInfo.Filename,
SourceLineNumber = sourceInfo.LineNumber,
Tags = example.Tags
.Select(tag => tag.Replace("_", " "))
.ToArray(),
};

return discoveredExample;
}
}
}
49 changes: 49 additions & 0 deletions sln/src/NSpec/Api/Discovery/ExampleSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NSpec.Api.Shared;
using NSpec.Domain;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NSpec.Api.Discovery
{
public class ExampleSelector
{
public ExampleSelector(string testAssemblyPath)
{
this.testAssemblyPath = testAssemblyPath;

debugInfoProvider = new DebugInfoProvider(testAssemblyPath);
}

public IEnumerable<DiscoveredExample> Select()
{
var selector = new ContextSelector();

string noTags = String.Empty;

selector.Select(testAssemblyPath, noTags);

var contexts = selector.Contexts;

var examples = contexts.Examples();

var discoveredExamples =
from exm in examples
select MapToDiscovered(exm, testAssemblyPath);

return discoveredExamples;
}

DiscoveredExample MapToDiscovered(ExampleBase example, string binaryPath)
{
var sourceInfo = debugInfoProvider.GetSourceInfo(example);

var discoveredExample = DiscoveryUtils.MapToDiscovered(example, binaryPath, sourceInfo);

return discoveredExample;
}

readonly string testAssemblyPath;
readonly DebugInfoProvider debugInfoProvider;
}
}
68 changes: 68 additions & 0 deletions sln/src/NSpec/Api/Execution/BatchExampleRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using NSpec.Domain;
using NSpec.Domain.Formatters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace NSpec.Api.Execution
{
public class BatchExampleRunner
{
public BatchExampleRunner(
string testAssemblyPath,
string tags,
string formatterClassName,
IDictionary<string, string> formatterOptions,
bool failFast)
{
var formatter = FindFormatter(formatterClassName, formatterOptions);

invocation = new RunnerInvocation(testAssemblyPath, tags, formatter, failFast);
}

public int Start()
{
int nrOfFailures = invocation.Run().Failures().Count();

return nrOfFailures;
}

readonly RunnerInvocation invocation;

/// <summary>
/// Find an implementation of IFormatter with the given class name
/// </summary>
/// <param name="formatterClassName"></param>
/// <param name="formatterOptions"></param>
/// <returns></returns>
static IFormatter FindFormatter(string formatterClassName, IDictionary<string, string> formatterOptions)
{
// Default formatter is the standard console formatter
if (string.IsNullOrEmpty(formatterClassName))
{
var consoleFormatter = new ConsoleFormatter();
consoleFormatter.Options = formatterOptions;
return consoleFormatter;
}

Assembly nspecAssembly = typeof(IFormatter).GetTypeInfo().Assembly;

// Look for a class that implements IFormatter with the provided name
var formatterType = nspecAssembly.GetTypes().FirstOrDefault(type =>
(type.Name.ToLowerInvariant() == formatterClassName)
&& typeof(IFormatter).IsAssignableFrom(type));

if (formatterType != null)
{
var formatter = (IFormatter)Activator.CreateInstance(formatterType);
formatter.Options = formatterOptions;
return formatter;
}
else
{
throw new TypeLoadException("Could not find formatter type " + formatterClassName);
}
}
}
}
Loading