Skip to content

Commit

Permalink
Added more integration
Browse files Browse the repository at this point in the history
* Added integration test helpers
* refactored integration test runner code
* Added opencover samples
* Added more integration tests
* fixed jobid bug
  • Loading branch information
csMACnz committed Feb 5, 2015
1 parent 3244a41 commit 91f7953
Show file tree
Hide file tree
Showing 11 changed files with 625 additions and 81 deletions.
301 changes: 301 additions & 0 deletions CoverageSamples/opencover/Sample1/EmptyReport.xml

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions CoverageSamples/opencover/Sample2/SingleFileReport.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<CoverageSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Summary numSequencePoints="1" visitedSequencePoints="1" numBranchPoints="1" visitedBranchPoints="1" sequenceCoverage="100" branchCoverage="100" maxCyclomaticComplexity="1" minCyclomaticComplexity="1" />
<Modules>
<Module skippedDueTo="Filter" hash="2C-B8-8B-0E-27-14-38-8E-71-A5-1D-A0-EE-33-D9-22-5F-A4-AF-79">
<FullName>C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll</FullName>
<ModuleName>System.Xml</ModuleName>
<Classes />
</Module>
<Module hash="00-E1-88-FB-D8-24-FF-C7-7D-2E-F0-DB-AB-41-5B-73-BF-5A-A5-18">
<Summary numSequencePoints="1" visitedSequencePoints="1" numBranchPoints="1" visitedBranchPoints="1" sequenceCoverage="100" branchCoverage="100" maxCyclomaticComplexity="1" minCyclomaticComplexity="1" />
<FullName>C:\Users\Mark\Documents\Visual Studio 2013\Projects\OpenCoverTesting\TestProject\bin\debug\OpenCoverTesting.dll</FullName>
<ModuleName>OpenCoverTesting</ModuleName>
<Files>
<File uid="1" fullPath="c:\Users\Mark\Documents\Visual Studio 2013\Projects\OpenCoverTesting\OpenCoverTesting\Class1.cs" />
</Files>
<Classes>
<Class>
<Summary numSequencePoints="0" visitedSequencePoints="0" numBranchPoints="0" visitedBranchPoints="0" sequenceCoverage="0" branchCoverage="0" maxCyclomaticComplexity="0" minCyclomaticComplexity="0" />
<FullName>&lt;Module&gt;</FullName>
<Methods />
</Class>
<Class>
<Summary numSequencePoints="1" visitedSequencePoints="1" numBranchPoints="1" visitedBranchPoints="1" sequenceCoverage="100" branchCoverage="100" maxCyclomaticComplexity="1" minCyclomaticComplexity="1" />
<FullName>OpenCoverTesting.Class1</FullName>
<Methods>
<Method visited="false" cyclomaticComplexity="1" sequenceCoverage="0" branchCoverage="0" isConstructor="true" isStatic="false" isGetter="false" isSetter="false">
<Summary numSequencePoints="0" visitedSequencePoints="0" numBranchPoints="0" visitedBranchPoints="0" sequenceCoverage="0" branchCoverage="0" maxCyclomaticComplexity="1" minCyclomaticComplexity="1" />
<MetadataToken>100663297</MetadataToken>
<Name>System.Void OpenCoverTesting.Class1::.ctor()</Name>
<SequencePoints />
<BranchPoints />
<MethodPoint vc="0" uspid="1" ordinal="0" offset="0" />
</Method>
<Method visited="true" cyclomaticComplexity="1" sequenceCoverage="100" branchCoverage="100" isConstructor="true" isStatic="true" isGetter="false" isSetter="false">
<Summary numSequencePoints="1" visitedSequencePoints="1" numBranchPoints="1" visitedBranchPoints="1" sequenceCoverage="100" branchCoverage="100" maxCyclomaticComplexity="1" minCyclomaticComplexity="1" />
<MetadataToken>100663298</MetadataToken>
<Name>System.Void OpenCoverTesting.Class1::.cctor()</Name>
<FileRef uid="1" />
<SequencePoints>
<SequencePoint vc="1" uspid="2" ordinal="0" offset="0" sl="5" sc="9" el="5" ec="51" bec="0" bev="0" fileid="1" />
</SequencePoints>
<BranchPoints />
<MethodPoint xsi:type="SequencePoint" vc="1" uspid="2" ordinal="0" offset="0" sl="5" sc="9" el="5" ec="51" bec="0" bev="0" fileid="1" />
</Method>
</Methods>
</Class>
</Classes>
</Module>
<Module skippedDueTo="Filter" hash="7E-D6-0C-5F-08-95-B3-B7-AE-9C-0F-24-97-E9-46-63-43-CF-F3-C3">
<FullName>C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\Extensions\CppUnitFramework\Microsoft.VisualStudio.TestTools.CppUnitTestFramework.TestEngine.dll</FullName>
<ModuleName>Microsoft.VisualStudio.TestTools.CppUnitTestFramework.TestEngine</ModuleName>
<Classes />
</Module>
<Module skippedDueTo="Filter" hash="49-FB-E6-5F-63-17-3F-47-76-D5-73-EE-00-1C-ED-B8-E9-ED-07-82">
<FullName>C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\Extensions\CppUnitFramework\Microsoft.VisualStudio.TestTools.CppUnitTestFramework.Discoverer.dll</FullName>
<ModuleName>Microsoft.VisualStudio.TestTools.CppUnitTestFramework.Discoverer</ModuleName>
<Classes />
</Module>
</Modules>
</CoverageSession>
12 changes: 12 additions & 0 deletions CoverageSamples/opencover/Sample2/SingleFileReportSourceFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace OpenCoverTesting
{
public static class Class1
{
public static void CallThis()
{
Console.WriteLine("called");
}
}
}
9 changes: 9 additions & 0 deletions src/csmacnz.Coveralls.Tests.Integration/CoverageRunResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace csmacnz.Coveralls.Tests.Integration
{
public class CoverageRunResults
{
public string StandardOutput { get; set; }
public string StandardError { get; set; }
public int ExitCode { get; set; }
}
}
87 changes: 87 additions & 0 deletions src/csmacnz.Coveralls.Tests.Integration/CoverallsTestRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using TimeoutException = Xunit.Sdk.TimeoutException;

namespace csmacnz.Coveralls.Tests.Integration
{
public static class CoverallsTestRunner
{
private const string CoverallsExe = "csmacnz.Coveralls.exe";

public static CoverageRunResults RunCoveralls(string arguments)
{
var exePath = Path.Combine(GetCoverallsExePath(), CoverallsExe);
var argumentsToUse = arguments;
var fileNameToUse = exePath;
if (Environment.GetEnvironmentVariable("MONO_INTEGRATION_MODE") == "True")
{
fileNameToUse = GetMonoPath();

argumentsToUse = exePath + " " + arguments;
}

var process = new Process();
var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = fileNameToUse,
Arguments = argumentsToUse,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8
};
process.StartInfo = startInfo;

string results;
string errorsResults;
int exitCode;
using (process)
{
process.Start();

results = process.StandardOutput.ReadToEnd();
errorsResults = process.StandardError.ReadToEnd();
Console.WriteLine(results);

const int timeoutInMilliseconds = 10000;
if (!process.WaitForExit(timeoutInMilliseconds))
{
throw new TimeoutException(timeoutInMilliseconds);
}
exitCode = process.ExitCode;
}

return new CoverageRunResults
{
StandardOutput=results,
StandardError = errorsResults,
ExitCode=exitCode
};
}

private static string GetMonoPath()
{
var monoApp = "mono";
var monoPath = Environment.GetEnvironmentVariable("MONO_INTEGRATION_MONOPATH");
if (!string.IsNullOrWhiteSpace(monoPath))
{
monoApp = monoPath + Path.DirectorySeparatorChar + "mono";
}
return monoApp;
}

private static string GetCoverallsExePath()
{
#if DEBUG
var configuration = "Debug";
#else
var configuration = "Release";
#endif
return Path.Combine("..", "..", "..", "csmacnz.Coveralls", "bin", configuration);
}
}
}
46 changes: 46 additions & 0 deletions src/csmacnz.Coveralls.Tests.Integration/OpenCoverTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using Xunit;
using Xunit.Should;

namespace csmacnz.Coveralls.Tests.Integration
{
public class OpenCoverTests
{
[Fact]
public void EmptyReport_RunsSuccessfully()
{
var emptyFilePath = Path.Combine(RepositoryPaths.GetSamplesPath(), "opencover", "Sample1", "EmptyReport.xml");
var results = DryRunCoverallsWithInputFile(emptyFilePath);

results.ExitCode.ShouldBe(0);
}

[Fact]
public void ReportWithOneFile_RunsSuccessfully()
{
string sampleFolderPath = Path.Combine(RepositoryPaths.GetSamplesPath(), "opencover", "Sample2");
var sampleCoverageFile = Path.Combine(sampleFolderPath, "SingleFileReport.xml");
var sampleClassFile = Path.Combine(sampleFolderPath, "SingleFileReportSourceFile.txt");
var coverageFilePath = TestFolders.GetTempFilePath(Guid.NewGuid() + ".xml");
var classFilePath = TestFolders.GetTempFilePath(Guid.NewGuid() + ".cs");
File.Copy(sampleClassFile, classFilePath);
var doc = XDocument.Load(sampleCoverageFile);
var classFile = doc.XPathSelectElements("//CoverageSession/Modules/Module/Files/File").FirstOrDefault(e => e.Attribute("fullPath").Value.EndsWith("Class1.cs"));
classFile.Attribute("fullPath").SetValue(classFilePath);
doc.Save(coverageFilePath);

var results = DryRunCoverallsWithInputFile(coverageFilePath);

results.ExitCode.ShouldBe(0);
}

private static CoverageRunResults DryRunCoverallsWithInputFile(string inputFilePath)
{
return CoverallsTestRunner.RunCoveralls(string.Format("--opencover -i {0} --dryrun --repoToken MYTESTREPOTOKEN", inputFilePath));
}
}
}
12 changes: 12 additions & 0 deletions src/csmacnz.Coveralls.Tests.Integration/RepositoryPaths.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.IO;

namespace csmacnz.Coveralls.Tests.Integration
{
public class RepositoryPaths
{
public static string GetSamplesPath()
{
return Path.Combine("..", "..", "..", "..", "CoverageSamples");
}
}
}
55 changes: 55 additions & 0 deletions src/csmacnz.Coveralls.Tests.Integration/TestFolders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Globalization;
using System.IO;
using System.Reflection;

namespace csmacnz.Coveralls.Tests.Integration
{
/*
* http://gasparnagy.com/2014/01/integration-testing-tips-input-and-output-files/
*/
internal static class TestFolders
{
public static readonly string UniqueId = DateTime.Now.ToString("s", CultureInfo.InvariantCulture).Replace(":", "");

public static string InputFolder
{
get { return Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath); }
}

public static string OutputFolder
{
//a simple solution that puts everyting to the output folder directly would look like this:
//get { return Directory.GetCurrentDirectory(); }
get
{
var outputFolder = Path.Combine(Directory.GetCurrentDirectory(), UniqueId);
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);
return outputFolder;
}
}

public static string TempFolder
{
get { return Path.GetTempPath(); }
}

// very simple helper methods that can improve the test code readability

public static string GetInputFilePath(string fileName)
{
return Path.GetFullPath(Path.Combine(InputFolder, fileName));
}

public static string GetOutputFilePath(string fileName)
{
return Path.GetFullPath(Path.Combine(OutputFolder, fileName));
}

public static string GetTempFilePath(string fileName)
{
return Path.GetFullPath(Path.Combine(TempFolder, fileName));
}
}
}
Loading

0 comments on commit 91f7953

Please sign in to comment.