Skip to content

Commit

Permalink
Rework test execution to run with .NET Core projects
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeuchler committed Sep 8, 2020
1 parent 449f539 commit b0cb3dd
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 98 deletions.
137 changes: 60 additions & 77 deletions .build/BuildToolkit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ $OpenCoverVersion = "4.7.922";
$DocFxVersion = "2.52.0";
$CodecovVersion = "1.10.0";
$ReportGeneratorVersion = "4.5.6";
$GitLinkVersion = "3.1.0";

# Folder Pathes
$RootPath = $MyInvocation.PSScriptRoot;
Expand All @@ -31,9 +30,9 @@ $NugetPackageTarget = "https://www.myget.org/F/moryx/api/v2/package";

# Define Tools
$global:MSBuildCli = "msbuild.exe";
$global:DotNetCli = "dotnet.exe";
$global:NugetCli = "nuget.exe";
$global:GitCli = "";
$global:GitLink = "$BuildTools\GitLink.$GitLinkVersion\build\GitLink.exe";
$global:OpenCoverCli = "$BuildTools\OpenCover.$OpenCoverVersion\tools\OpenCover.Console.exe";
$global:NunitCli = "$BuildTools\NUnit.ConsoleRunner.$NunitVersion\tools\nunit3-console.exe";
$global:CodecovCli = "$BuildTools\Codecov.$CodecovVersion\tools\codecov.exe";
Expand Down Expand Up @@ -83,6 +82,10 @@ function Invoke-Initialize([string]$Version = "1.0.0", [bool]$Cleanup = $False)
$env:MORYX_BUILD_VERBOSITY = "minimal"
}

if (-not $env:MORYX_TEST_VERBOSITY) {
$env:MORYX_TEST_VERBOSITY = "normal"
}

if (-not $env:MORYX_NUGET_VERBOSITY) {
$env:MORYX_NUGET_VERBOSITY = "normal"
}
Expand Down Expand Up @@ -116,15 +119,15 @@ function Invoke-Initialize([string]$Version = "1.0.0", [bool]$Cleanup = $False)
Write-Variable "ReportGeneratorCli" $global:ReportGeneratorCli;
Write-Variable "DocFxCli" $global:DocFxCli;
Write-Variable "GitCli" $global:GitCli;
Write-Variable "GitLink" $global:GitLink;
Write-Variable "GitCommitHash" $global:GitCommitHash;
Write-Variable "MORYX_BRANCH" $env:MORYX_BRANCH;
Write-Variable "MORYX_VERSION" $env:MORYX_VERSION;
Write-Variable "MORYX_ASSEMBLY_VERSION" $env:MORYX_ASSEMBLY_VERSION;
Write-Variable "MORYX_OPTIMIZE_CODE" $env:MORYX_OPTIMIZE_CODE;
Write-Variable "MORYX_BUILDNUMBER" $env:MORYX_BUILDNUMBER;
Write-Variable "MORYX_BUILD_CONFIG" $env:MORYX_BUILD_CONFIG;
Write-Variable "MORYX_BUILD_VERBOSITY" $env:MORYX_BUILD_VERBOSITY;
Write-Variable "MORYX_OPTIMIZE_CODE" $env:MORYX_OPTIMIZE_CODE;
Write-Variable "MORYX_TEST_VERBOSITY" $env:MORYX_TEST_VERBOSITY;
Write-Variable "MORYX_NUGET_VERBOSITY" $env:MORYX_NUGET_VERBOSITY;

# Cleanp
Expand Down Expand Up @@ -244,52 +247,60 @@ function Invoke-CoverTests($SearchPath = $RootPath, $SearchFilter = "*.csproj",
CreateFolderIfNotExists $OpenCoverReportsDir;
CreateFolderIfNotExists $NunitReportsDir;

$includeFilter = "+[Moryx*]*";
$excludeFilter = "-[*nunit*]* -[*Tests]* -[*Model*]*";

if (Test-Path $FilterFile) {
$ignoreContent = Get-Content $FilterFile;

foreach ($line in $ignoreContent) {
$parts = $line.Split(":");
if ($parts.Count -lt 2) {
continue
}

$filterType = $parts[0];
$filterValue = $parts[1];

if ($filterType.StartsWith("INCLUDE")) {
$includeFilter += " $filterValue";
}

if ($filterType.StartsWith("EXCLUDE")) {
$excludeFilter += " $filterValue";
}
}

Write-Host "Active Filter: `r`n Include: $includeFilter `r`n Exclude: $excludeFilter";
}

ForEach($testProject in $testProjects ) {
$projectName = ([System.IO.Path]::GetFileNameWithoutExtension($testProject.Name));
$testAssembly = [System.IO.Path]::Combine($testProject.DirectoryName, "bin", $env:MORYX_BUILD_CONFIG, "$projectName.dll");
$isNetCore = Get-CsprojIsNetCore($testProject);

Write-Host "OpenCover Test: ${projectName}:";

$nunitXml = ($NunitReportsDir + "\$projectName.TestResult.xml");
$openCoverXml = ($OpenCoverReportsDir + "\$projectName.OpenCover.xml");

# If assembly does not exists, the project will be build
if (-not (Test-Path $testAssembly)) {
Invoke-Build $testProject
if ($isNetCore) {
$targetArgs = '"test -v ' + $env:MORYX_TEST_VERBOSITY + ' -c ' + $env:MORYX_BUILD_CONFIG + ' ' + $testProject + '"';
$openCoverAgs = "-target:$global:DotNetCli", "-targetargs:$targetArgs"
}

$includeFilter = "+[Moryx*]*";
$excludeFilter = "-[*nunit*]* -[*Tests]* -[*Model*]*";

if (Test-Path $FilterFile) {
$ignoreContent = Get-Content $FilterFile;

foreach ($line in $ignoreContent) {
$parts = $line.Split(":");
if ($parts.Count -lt 2) {
continue
}

$filterType = $parts[0];
$filterValue = $parts[1];

if ($filterType.StartsWith("INCLUDE")) {
$includeFilter += " $filterValue";
}

if ($filterType.StartsWith("EXCLUDE")) {
$excludeFilter += " $filterValue";
}
else {
# If assembly does not exists, the project will be build
if (-not (Test-Path $testAssembly)) {
Invoke-Build $testProject
}
}

Write-Host "Active Filter: `r`n Include: $includeFilter `r`n Exclude: $excludeFilter";
$openCoverAgs = "-target:$global:NunitCli", "-targetargs:/config:$env:MORYX_BUILD_CONFIG /result:$nunitXml $testAssembly"
}

$openCoverAgs = "-target:$global:NunitCli", "-targetargs:/config:$env:MORYX_BUILD_CONFIG /result:$nunitXml $testAssembly"
$openCoverAgs += "-log:Debug", "-register:administrator", "-output:$openCoverXml", "-hideskipped:all", "-skipautoprops", "-excludebyattribute:*OpenCoverIgnore*";
$openCoverAgs += "-log:Debug", "-register:user", "-output:$openCoverXml", "-hideskipped:all", "-skipautoprops";
$openCoverAgs += "-returntargetcode" # We need the nunit return code
$openCoverAgs += "-filter:$includeFilter $excludeFilter"

& $global:OpenCoverCli $openCoverAgs

$exitCode = [int]::Parse($LastExitCode);
Expand All @@ -315,6 +326,20 @@ function Invoke-CoverTests($SearchPath = $RootPath, $SearchFilter = "*.csproj",
}
}

function Get-CsprojIsNetCore($csprojFile) {
[xml]$csprojContent = Get-Content $csprojFile.FullName
$sdkProject = $csprojContent.Project.Sdk;
if ($null -ne $sdkProject) {
# Read Target Framework
$targetFramework = $csprojContent.Project.PropertyGroup.TargetFramework;
if ($targetFramework -Match "netcoreapp") {
# NETCore
return $true;
}
}
return $false;
}

function Invoke-CoverReport {
Write-Step "Creating cover report. Searching for OpenCover.xml files in $OpenCoverReportsDir."

Expand Down Expand Up @@ -375,48 +400,6 @@ function Invoke-DocFx($Metadata = [System.IO.Path]::Combine($DocumentationDir, "
CopyAndReplaceFolder $docFxDest "$DocumentationArtifcacts\DocFx";
}

function Invoke-SourceIndex([string]$RawUrl, [string]$SearchPath = [System.IO.Path]::Combine($PSScriptRoot, "..\")) {
Write-Step "Indexing SourceCode and patching PDBs to $RawUrl"

if (-not (Test-Path $global:GitLink)) {
Install-Tool "GitLink" $GitLinkVersion $global:GitLink;
}

$sourceLink = "$RawUrl/{revision}/{filename}";

Write-Host "SearchPath for Projects: $SearchPath";
$csprojs = Get-Childitem $SearchPath -recurse | Where-Object {$_.extension -eq ".csproj"}

foreach ($csporj in $csprojs) {
Write-Host;
Write-Host "Reading csproj: $($csporj.Name)";

$csprojXml = [xml](Get-Content $csporj.FullName);

$outputGroup = $csprojXml.Project.PropertyGroup | Where-Object Condition -Like "*$env:MORYX_BUILD_CONFIG|AnyCPU*";
$outputPath = $outputGroup.OutputPath;

$assemblyGroup = $csprojXml.Project.PropertyGroup | Where-Object {-not ([string]::IsNullOrEmpty($_.AssemblyName)) }
$assemblyName = $assemblyGroup.AssemblyName;

$pdbFileName = $($assemblyName + ".pdb");
$projectPdbPath = [System.IO.Path]::Combine($outputPath, $pdbFileName);
$pdbPath = [System.IO.Path]::Combine($csporj.DirectoryName, $projectPdbPath);

Write-Host "PDB path of assembly for $($csporj.Name) is: $projectPdbPath"

if (-not (Test-Path $pdbPath)) {
Write-Host "PDB was not found. Project will be ignored!"
continue;
}

$args = "-u", "$sourceLink";
$args += $pdbPath

& $global:GitLink $args
}
}

function Invoke-Pack($FilePath, [bool]$IsTool = $False, [bool]$IncludeSymbols = $False) {
CreateFolderIfNotExists $NugetPackageArtifacts;

Expand Down
16 changes: 8 additions & 8 deletions src/Moryx.TestTools.SystemTest/HeartOfGoldController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ public HeartOfGoldController(string buildDirectoryName, string runtimeDirectoryN
/// Starts the HeartOfGold executable.
/// </summary>
/// <returns><c>true</c> if start succeeds, <c>false</c> if not</returns>
public bool StartHeartOfGold()
public bool StartApplication()
{
return StartHeartOfGold(ApplicationExeName);
return StartApplication(ApplicationExeName);
}

/// <summary>
Expand All @@ -196,25 +196,25 @@ public bool StartHeartOfGold()
/// <param name="exeName">Name of the executable.</param>
/// <returns><c>true</c> if start succeeds, <c>false</c> if not</returns>
/// <exception cref="System.InvalidOperationException">
/// Can't start HeartOfGold without RuntimeDir.
/// Can't start application without RuntimeDir.
/// or
/// Can't start HeartOfGold without ConfigDir.
/// Can't start application without ConfigDir.
/// or
/// HeartOfGold is already running.
/// </exception>
private bool StartHeartOfGold(string exeName)
private bool StartApplication(string exeName)
{
// check the runtime directory
if (RuntimeDir == null)
throw new InvalidOperationException("Can't start HeartOfGold without RuntimeDir.");
throw new InvalidOperationException($"Can't start {ApplicationExeName} without RuntimeDir.");

// check the config directory
if (ConfigDir == null)
throw new InvalidOperationException("Can't start HeartOfGold without ConfigDir.");
throw new InvalidOperationException($"Can't start {ApplicationExeName} without ConfigDir.");

// check that the process is not already running
if (Process != null && !Process.HasExited)
throw new InvalidOperationException("HeartOfGold is already running.");
throw new InvalidOperationException($"{ApplicationExeName} is already running.");

var wcfConfig = Path.Combine(RuntimeDir, ConfigDir, "Moryx.Tools.Wcf.WcfConfig" + ConfigConstants.FileExtension);
if (File.Exists(wcfConfig))
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Moryx.Runtime.SystemTests/BasicConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void Setup()

Console.WriteLine("Starting HeartOfGold");

bool started = _hogController.StartHeartOfGold();
bool started = _hogController.StartApplication();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Assert.IsFalse(_hogController.Process.HasExited, "HeartOfGold has exited unexpectedly.");
Expand Down
8 changes: 4 additions & 4 deletions src/Tests/Moryx.Runtime.SystemTests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void TestStartStop()
{
PrintStartMsg("TestStartStop");

bool started = _hogController.StartHeartOfGold();
bool started = _hogController.StartApplication();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Assert.IsFalse(_hogController.Process.HasExited, "HeartOfGold has exited unexpectedly.");
Expand All @@ -90,7 +90,7 @@ public void TestStartTimeout()
PrintStartMsg("TestStartTimeout");

_hogController.ExecutionTimeout = 5;
bool started = _hogController.StartHeartOfGold();
bool started = _hogController.StartApplication();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Assert.IsFalse(_hogController.Process.HasExited, "HeartOfGold has exited unexpectedly.");
Expand All @@ -107,7 +107,7 @@ public bool TestStartWaitStop(string service, ServerModuleState state)
{
PrintStartMsg("TestStartWaitStop('{0}', '{1}')", service, state);

bool started = _hogController.StartHeartOfGold();
bool started = _hogController.StartApplication();
_hogController.CreateClients();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Expand All @@ -129,7 +129,7 @@ public void TestStartKill()
{
PrintStartMsg("TestStartKill");

bool started = _hogController.StartHeartOfGold();
bool started = _hogController.StartApplication();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Assert.IsFalse(_hogController.Process.HasExited, "HeartOfGold has exited unexpectedly.");
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Moryx.Runtime.SystemTests/DatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Setup()

Console.WriteLine("Starting HeartOfGold");

var started = _hogController.StartHeartOfGold();
var started = _hogController.StartApplication();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Assert.IsFalse(_hogController.Process.HasExited, "HeartOfGold has exited unexpectedly.");
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Moryx.Runtime.SystemTests/HogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class HogHelper
/// </summary>
public bool StartHeartOfGold(string service)
{
if (HogController.StartHeartOfGold())
if (HogController.StartApplication())
{
return HogController.WaitForService(service);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Moryx.Runtime.SystemTests/LifeCycleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void TestFixtureSetUp()

Console.WriteLine("Starting HeartOfGold");

bool started = _hogController.StartHeartOfGold();
bool started = _hogController.StartApplication();
_hogController.CreateClients();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Moryx.Runtime.SystemTests/LoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void TestFixtureSetUp()

Console.WriteLine("Starting HeartOfGold");

bool started = _hogController.StartHeartOfGold();
bool started = _hogController.StartApplication();
_hogController.CreateClients();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void ConvertFromModelAndBackToModel()

// Act
var serverEntry = EntryConvert.EncodeObject(dummyClass, new DefaultSerialization { FormatProvider = new CultureInfo("en-us") });

var clientConverter = EntryToModelConverter.Create<EntryModelDummyClient>(new CultureInfo("en-us"));
clientConverter.FromModel(serverEntry, dummyClassClient);

Expand Down
4 changes: 2 additions & 2 deletions src/Tests/Moryx.Tools.Wcf.SystemTests/ClientFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public void TestFixtureSetUp()

Console.WriteLine("Starting HeartOfGold");

bool started = _hogController.StartHeartOfGold();
bool started = _hogController.StartApplication();
_hogController.CreateClients();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Assert.IsTrue(started, "Can't start application.");
Assert.IsFalse(_hogController.Process.HasExited, "HeartOfGold has exited unexpectedly.");

bool result = _hogController.WaitForService(ModuleController.ModuleName, ServerModuleState.Running, 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TestFixtureSetUp()

Console.WriteLine("Starting HeartOfGold");

var started = _hogController.StartHeartOfGold();
var started = _hogController.StartApplication();
_hogController.CreateClients();

Assert.IsTrue(started, "Can't start HeartOfGold.");
Expand Down

0 comments on commit b0cb3dd

Please sign in to comment.