Skip to content

Commit

Permalink
Merge pull request #85 from TheAngryByrd/test-coverage
Browse files Browse the repository at this point in the history
Test coverage
  • Loading branch information
TheAngryByrd authored May 14, 2018
2 parents 8870d9a + c1ff228 commit db22e5e
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 51 deletions.
1 change: 1 addition & 0 deletions Content/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,4 @@ paket-files/
TestResults.xml

dist/
lcov.info
19 changes: 11 additions & 8 deletions Content/.paket/Paket.Restore.targets
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@
<!-- Step 1 Check if lockfile is properly restored -->
<PropertyGroup>
<PaketRestoreRequired>true</PaketRestoreRequired>
<NoWarn>$(NoWarn);NU1603</NoWarn>
<NoWarn>$(NoWarn);NU1603;NU1604;NU1605;NU1608</NoWarn>
</PropertyGroup>

<!-- Because ReadAllText is slow on osx/linux, try to find shasum and awk -->
<PropertyGroup>
<PaketRestoreCachedHasher Condition="'$(OS)' != 'Windows_NT' And '$(PaketRestoreCachedHasher)' == '' And Exists('/usr/bin/shasum') And Exists('/usr/bin/awk')">/usr/bin/shasum $(PaketRestoreCacheFile) | /usr/bin/awk '{ print $1 }'</PaketRestoreCachedHasher>
<PaketRestoreLockFileHasher Condition="'$(OS)' != 'Windows_NT' And '$(PaketRestoreLockFileHash)' == '' And Exists('/usr/bin/shasum') And Exists('/usr/bin/awk')">/usr/bin/shasum $(PaketLockFilePath) | /usr/bin/awk '{ print $1 }'</PaketRestoreLockFileHasher>
<PaketRestoreCachedHasher Condition="'$(OS)' != 'Windows_NT' And '$(PaketRestoreCachedHasher)' == '' And Exists('/usr/bin/shasum') And Exists('/usr/bin/awk')">/usr/bin/shasum "$(PaketRestoreCacheFile)" | /usr/bin/awk '{ print $1 }'</PaketRestoreCachedHasher>
<PaketRestoreLockFileHasher Condition="'$(OS)' != 'Windows_NT' And '$(PaketRestoreLockFileHash)' == '' And Exists('/usr/bin/shasum') And Exists('/usr/bin/awk')">/usr/bin/shasum "$(PaketLockFilePath)" | /usr/bin/awk '{ print $1 }'</PaketRestoreLockFileHasher>
</PropertyGroup>

<!-- If shasum and awk exist get the hashes -->
<Exec StandardOutputImportance="Low" Condition=" '$(PaketRestoreCachedHasher)' != '' " Command="$(PaketRestoreCachedHasher)" ConsoleToMSBuild='true'>
<Output TaskParameter="ConsoleOutput" PropertyName="PaketRestoreCachedHash" />
<Output TaskParameter="ConsoleOutput" PropertyName="PaketRestoreCachedHash" />
</Exec>
<Exec StandardOutputImportance="Low" Condition=" '$(PaketRestoreLockFileHasher)' != '' " Command="$(PaketRestoreLockFileHasher)" ConsoleToMSBuild='true'>
<Output TaskParameter="ConsoleOutput" PropertyName="PaketRestoreLockFileHash" />
<Output TaskParameter="ConsoleOutput" PropertyName="PaketRestoreLockFileHash" />
</Exec>

<PropertyGroup Condition="Exists('$(PaketRestoreCacheFile)') ">
Expand All @@ -69,9 +69,11 @@
<PaketRestoreRequired Condition=" '$(PaketRestoreLockFileHash)' == '' ">true</PaketRestoreRequired>
</PropertyGroup>


<!-- Do a global restore if required -->
<Exec Command='$(PaketBootStrapperCommand)' Condition="Exists('$(PaketBootStrapperExePath)') AND !(Exists('$(PaketExePath)'))" ContinueOnError="false" />
<Exec Command='$(PaketCommand) restore' Condition=" '$(PaketRestoreRequired)' == 'true' " ContinueOnError="false" />
<Exec Command='$(PaketCommand) restore --target-framework "$(TargetFrameworks)"' Condition=" '$(PaketRestoreRequired)' == 'true' AND '$(TargetFramework)' == '' " ContinueOnError="false" />
<Exec Command='$(PaketCommand) restore --target-framework "$(TargetFramework)"' Condition=" '$(PaketRestoreRequired)' == 'true' AND '$(TargetFramework)' != '' " ContinueOnError="false" />

<!-- Step 2 Detect project specific changes -->
<PropertyGroup>
Expand Down Expand Up @@ -127,6 +129,7 @@
<PackageReference Include="%(PaketReferencesFileLinesInfo.PackageName)">
<Version>%(PaketReferencesFileLinesInfo.PackageVersion)</Version>
<PrivateAssets Condition="%(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'true'">All</PrivateAssets>
<ExcludeAssets Condition="%(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'exclude'">runtime</ExcludeAssets>
</PackageReference>
</ItemGroup>

Expand Down Expand Up @@ -183,8 +186,8 @@

<ConvertToAbsolutePath Condition="@(_NuspecFiles) != ''" Paths="@(_NuspecFiles)">
<Output TaskParameter="AbsolutePaths" PropertyName="NuspecFileAbsolutePath" />
</ConvertToAbsolutePath>
</ConvertToAbsolutePath>


<!-- Call Pack -->
<PackTask Condition="$(UseNewPack)"
Expand Down
32 changes: 31 additions & 1 deletion Content/build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ let distDir = __SOURCE_DIRECTORY__ @@ "dist"
let distGlob = distDir @@ "*.nupkg"
let toolsDir = __SOURCE_DIRECTORY__ @@ "tools"

let coverageReportDir = __SOURCE_DIRECTORY__ @@ "docs" @@ "coverage"

let gitOwner = "MyGithubUsername"
let gitRepoName = "MyLib"

Expand All @@ -36,7 +38,7 @@ let isRelease () =
|> Seq.exists ((=)"Release")

Target "Clean" (fun _ ->
["bin"; "temp" ; distDir]
["bin"; "temp" ; distDir; coverageReportDir]
|> CleanDirs

!! srcGlob
Expand Down Expand Up @@ -91,6 +93,33 @@ Target "DotnetTest" (fun _ ->
})
)

Target "GenerateCoverageReport" (fun _ ->
let reportGenerator = "packages/build/ReportGenerator/tools/ReportGenerator.exe"
let coverageReports =
!!"tests/**/_Reports/MSBuildTest.xml"
|> String.concat ";"
let sourceDirs =
!! srcGlob
|> Seq.map DirectoryName
|> String.concat ";"

let args =
String.concat " " <|
[
sprintf "-reports:%s" coverageReports
sprintf "-targetdir:%s" coverageReportDir
// Add source dir
sprintf "-sourcedirs:%s" sourceDirs
// Ignore Tests and if AltCover.Recorder.g sneaks in
sprintf "-assemblyfilters:%s" "-*.Tests;-AltCover.Recorder.g"
sprintf "-Reporttypes:%s" "Html"
]
tracefn "%s %s" reportGenerator args
let exitCode = Shell.Exec(reportGenerator, args = args)
if exitCode <> 0 then
failwithf "%s failed with exit code: %d" reportGenerator exitCode
)


Target "WatchTests" (fun _ ->
!! testsGlob
Expand Down Expand Up @@ -247,6 +276,7 @@ Target "Release" DoNothing
"DotnetRestore"
==> "DotnetBuild"
==> "DotnetTest"
==> "GenerateCoverageReport"
==> "DotnetPack"
==> "SourcelinkTest"
==> "Publish"
Expand Down
3 changes: 3 additions & 0 deletions Content/paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ nuget SourceLink.Create.CommandLine 2.8.0 copy_local: true
nuget YoloDev.Expecto.TestSdk
nuget Microsoft.NET.Test.Sdk


group Build
framework: >= net45
source https://www.nuget.org/api/v2
nuget FAKE
github fsharp/FAKE modules/Octokit/Octokit.fsx
nuget altcover
nuget ReportGenerator
4 changes: 3 additions & 1 deletion Content/paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1345,9 +1345,11 @@ GROUP Build
RESTRICTION: >= net45
NUGET
remote: https://www.nuget.org/api/v2
altcover (3.0.466)
FAKE (4.64.11)
Octokit (0.29)
ReportGenerator (3.1.2)
GITHUB
remote: fsharp/FAKE
modules/Octokit/Octokit.fsx (c160d4907360cfd42904008dab2bd22c0a4a9952)
modules/Octokit/Octokit.fsx (38b77b8d3ed67f0b9b0402f5c79774cb477c1c8d)
Octokit (>= 0.20)
4 changes: 4 additions & 0 deletions Content/src/MyLib/Library.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace MyLib

module Say =
let nothing name =
name |> ignore

let hello name =
sprintf "Hello %s" name

48 changes: 48 additions & 0 deletions Content/tests/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<Project>
<ItemGroup>
<_TargetFramework Include="$(TargetFrameworks)" />
</ItemGroup>
<UsingTask
TaskName="AltCover.Prepare"
AssemblyFile="../packages/build/altcover/tools/netcoreapp2.0/AltCover.dll"/>
<UsingTask
TaskName="AltCover.Collect"
AssemblyFile="../packages/build/altcover/tools/netcoreapp2.0/AltCover.dll"/>
<Target
Name="InstrumentModulesNoBuild"
BeforeTargets="VSTest"
Condition="$([MSBuild]::EnsureTrailingSlash('$([System.IO.Path]::Combine($(BaseOutputPath),$(Configuration)))')) == '$(OutputPath)'">
<AltCover.Prepare
ContinueOnError="WarnAndContinue"
Condition="'$(VSTestNoBuild)' == 'true'"
InputDirectory="$(OutputPath)/%(_TargetFramework.Identity)"
OutputDirectory="$(OutputPath)/%(_TargetFramework.Identity)/__Saved$([System.DateTime]::UtcNow.ToString().Replace(':','-').Replace('/','-').Replace(' ','+'))"
XmlReport="$(OutputPath)/%(_TargetFramework.Identity)/_Reports/MSBuildTest.xml"
OpenCover="true"
InPlace="true"
CallContext="@(CallContext)" />
</Target>
<Target
Name="InstrumentModulesAfterBuild"
AfterTargets="BuildProject"
Condition="$([MSBuild]::EnsureTrailingSlash('$([System.IO.Path]::Combine($(BaseOutputPath),$(Configuration)))')) == '$(OutputPath)'">
<AltCover.Prepare
ContinueOnError="WarnAndContinue"
Condition="'$(VSTestNoBuild)' != 'true'"
InputDirectory="$(OutputPath)/%(_TargetFramework.Identity)"
OutputDirectory="$(OutputPath)/%(_TargetFramework.Identity)/__Saved$([System.DateTime]::UtcNow.ToString().Replace(':','-').Replace('/','-').Replace(' ','+'))"
XmlReport="$(OutputPath)/%(_TargetFramework.Identity)/_Reports/MSBuildTest.xml"
OpenCover="true"
InPlace="true"
CallContext="@(CallContext)" />
</Target>
<Target
Name="GenerateCoverageResult"
AfterTargets="VSTest"
Condition="$([MSBuild]::EnsureTrailingSlash('$([System.IO.Path]::Combine($(BaseOutputPath),$(Configuration)))')) != '$(OutputPath)'">
<AltCover.Collect
ContinueOnError="WarnAndContinue"
RecorderDirectory="$(OutputPath)"
LcovReport="./lcov.info" />
</Target>
</Project>
27 changes: 0 additions & 27 deletions Content/tests/MyLib.Tests/Main.fs
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
module ExpectoTemplate
open Expecto
open System.Reflection


module AssemblyInfo =

let metaDataValue (mda : AssemblyMetadataAttribute) = mda.Value
let getMetaDataAttribute (assembly : Assembly) key =
assembly.GetCustomAttributes(typedefof<AssemblyMetadataAttribute>)
|> Seq.cast<AssemblyMetadataAttribute>
|> Seq.find(fun x -> x.Key = key)

let getReleaseDate assembly =
"ReleaseDate"
|> getMetaDataAttribute assembly
|> metaDataValue

let getGitHash assembly =
"GitHash"
|> getMetaDataAttribute assembly
|> metaDataValue

[<EntryPoint>]
let main argv =
if argv |> Seq.contains ("--version") then
let assembly = Assembly.GetEntryAssembly()
let name = assembly.GetName()
let version = assembly.GetName().Version
let releaseDate = AssemblyInfo.getReleaseDate assembly
let githash = AssemblyInfo.getGitHash assembly
printfn "%s - %A - %s - %s" name.Name version releaseDate githash
Tests.runTestsInAssembly defaultConfig argv
26 changes: 13 additions & 13 deletions Content/tests/MyLib.Tests/MyLib.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Tests.fs" />
<Compile Include="Main.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/MyLib/MyLib.fsproj" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Tests.fs" />
<Compile Include="Main.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/MyLib/MyLib.fsproj" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
3 changes: 3 additions & 0 deletions Content/tests/MyLib.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ open MyLib
[<Tests>]
let tests =
testList "samples" [
testCase "Say nothing" <| fun _ ->
let subject = Say.nothing ()
Expect.equal subject () "Not an absolute unit"
testCase "Say hello all" <| fun _ ->
let subject = Say.hello "all"
Expect.equal subject "Hello all" "You didn't say hello"
Expand Down
2 changes: 1 addition & 1 deletion Content/tests/MyLib.Tests/paket.references
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ Expecto
FSharp.Core
Microsoft.DotNet.Watcher.Tools
dotnet-mono
YoloDev.Expecto.TestSdk
Microsoft.NET.Test.Sdk
YoloDev.Expecto.TestSdk
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,4 @@ This is because dotnet template loses permissions of files. (https://github.com/
* Heavily inspired by [Project Scaffold](https://github.com/fsprojects/ProjectScaffold)
* [Buildstats.info](https://github.com/dustinmoris/CI-BuildStats)
* [SourceLink](https://github.com/ctaggart/SourceLink)
* [AltCover](https://github.com/SteveGilham/altcover)

0 comments on commit db22e5e

Please sign in to comment.