From f61a88bb18ac314037e750d885bad59fcf6def70 Mon Sep 17 00:00:00 2001 From: Morten Trydal Date: Tue, 15 Apr 2014 14:20:24 +0200 Subject: [PATCH 1/6] Support for OpenCover --- src/app/FakeLib/FakeLib.fsproj | 1 + src/app/FakeLib/OpenCoverHelper.fs | 68 ++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/app/FakeLib/OpenCoverHelper.fs diff --git a/src/app/FakeLib/FakeLib.fsproj b/src/app/FakeLib/FakeLib.fsproj index e7e4a057adc..27b54da8f09 100644 --- a/src/app/FakeLib/FakeLib.fsproj +++ b/src/app/FakeLib/FakeLib.fsproj @@ -130,6 +130,7 @@ + diff --git a/src/app/FakeLib/OpenCoverHelper.fs b/src/app/FakeLib/OpenCoverHelper.fs new file mode 100644 index 00000000000..4321086cae2 --- /dev/null +++ b/src/app/FakeLib/OpenCoverHelper.fs @@ -0,0 +1,68 @@ +[] +/// Contains a task which can be used to run [OpenCover](https://github.com/sawilde/opencover) on .NET assemblies. +module Fake.OpenCoverHelper + +open System +open System.Text + +type RegisterType = + | None + | Register + | RegisterUser + +// Open Cover parameter type +type OpenCoverParams = + { ExePath : string + TestRunnerExePath : string + Output : string + Register : RegisterType + Filter : string + TimeOut : TimeSpan + WorkingDir : string } + +// Open Cover default parameters +let OpenCoverDefaults = + { ExePath = environVar "LOCALAPPDATA" @@ "Apps" @@ "OpenCover" @@ "OpenCover.Console.exe" + TestRunnerExePath = ProgramFiles @@ "NUnit" @@ "bin" @@ "nunit-console.exe" + Output = String.Empty + Register = None + Filter = String.Empty + TimeOut = TimeSpan.FromMinutes 5. + WorkingDir = currentDirectory } + +/// Runs OpenCover on a group of assemblies. +/// ## Parameters +/// +/// - `setParams` - Function used to overwrite the default OpenCover parameters. +/// - `targetArgs` - Test runner arguments. +let OpenCover setParams targetArgs = + let param = setParams OpenCoverDefaults + + let processArgs = + let args = ref (new StringBuilder()) + let append (s : string) = args := (!args).Append(s) + let appendQuoted (s : string) = args := (!args).Append("\"").Append(s).Append("\" ") + append "-target:" + param.TestRunnerExePath + |> FullName + |> appendQuoted + append "-targetargs:" + appendQuoted targetArgs + if param.Output <> String.Empty then + append "-output:" + appendQuoted param.Output + append (match param.Register with + | None -> String.Empty + | Register -> "-register " + | RegisterUser -> "-register:user ") + if param.Filter <> String.Empty then + append "-filter:" + appendQuoted param.Filter + (!args).ToString() + tracefn "OpenCover command\n%s %s" param.ExePath processArgs + let ok = + execProcess (fun info -> + info.FileName <- param.ExePath + if param.WorkingDir <> String.Empty then info.WorkingDirectory <- param.WorkingDir + info.Arguments <- processArgs) param.TimeOut + if not ok then failwithf "OpenCover reported errors." \ No newline at end of file From 0e653a0b0fc93517b33e70a72d9a978273591095 Mon Sep 17 00:00:00 2001 From: Morten Trydal Date: Tue, 15 Apr 2014 14:50:17 +0200 Subject: [PATCH 2/6] Fixed a rookie mistake --- src/app/FakeLib/OpenCoverHelper.fs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/FakeLib/OpenCoverHelper.fs b/src/app/FakeLib/OpenCoverHelper.fs index 4321086cae2..d2922c81a01 100644 --- a/src/app/FakeLib/OpenCoverHelper.fs +++ b/src/app/FakeLib/OpenCoverHelper.fs @@ -6,7 +6,7 @@ open System open System.Text type RegisterType = - | None + | Manual | Register | RegisterUser @@ -25,7 +25,7 @@ let OpenCoverDefaults = { ExePath = environVar "LOCALAPPDATA" @@ "Apps" @@ "OpenCover" @@ "OpenCover.Console.exe" TestRunnerExePath = ProgramFiles @@ "NUnit" @@ "bin" @@ "nunit-console.exe" Output = String.Empty - Register = None + Register = Manual Filter = String.Empty TimeOut = TimeSpan.FromMinutes 5. WorkingDir = currentDirectory } @@ -52,7 +52,7 @@ let OpenCover setParams targetArgs = append "-output:" appendQuoted param.Output append (match param.Register with - | None -> String.Empty + | Manual -> String.Empty | Register -> "-register " | RegisterUser -> "-register:user ") if param.Filter <> String.Empty then From 4255ee5b2e59f594dd5d83a700afb17af79601a2 Mon Sep 17 00:00:00 2001 From: Morten Trydal Date: Tue, 15 Apr 2014 17:31:58 +0200 Subject: [PATCH 3/6] Removed the AutoOpen attribute --- src/app/FakeLib/OpenCoverHelper.fs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app/FakeLib/OpenCoverHelper.fs b/src/app/FakeLib/OpenCoverHelper.fs index d2922c81a01..f7c485db522 100644 --- a/src/app/FakeLib/OpenCoverHelper.fs +++ b/src/app/FakeLib/OpenCoverHelper.fs @@ -1,5 +1,4 @@ -[] -/// Contains a task which can be used to run [OpenCover](https://github.com/sawilde/opencover) on .NET assemblies. +/// Contains a task which can be used to run [OpenCover](https://github.com/sawilde/opencover) on .NET assemblies. module Fake.OpenCoverHelper open System From ce8ad483a7e107f75d35b51554d59821b3eaa3ee Mon Sep 17 00:00:00 2001 From: Morten Trydal Date: Tue, 15 Apr 2014 18:00:03 +0200 Subject: [PATCH 4/6] Documented the properties of the OpenCoverParams type --- src/app/FakeLib/OpenCoverHelper.fs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/app/FakeLib/OpenCoverHelper.fs b/src/app/FakeLib/OpenCoverHelper.fs index f7c485db522..f266a1fd63c 100644 --- a/src/app/FakeLib/OpenCoverHelper.fs +++ b/src/app/FakeLib/OpenCoverHelper.fs @@ -9,17 +9,26 @@ type RegisterType = | Register | RegisterUser -// Open Cover parameter type +/// OpenCover parameters, for more details see: https://github.com/OpenCover/opencover/wiki/Usage#console-application-usage. type OpenCoverParams = - { ExePath : string + { /// (Required) Path to the OpenCover console application + ExePath : string + /// (Required) Path to the NUnit/XUnit console runner TestRunnerExePath : string + /// The location and name of the output xml file. + /// If no value is supplied then the current directory + /// will be used and the output filename will be results.xml. Output : string + /// Use this to register and de-register the code coverage profiler. Register : RegisterType + /// A list of filters to apply to selectively include or exclude assemblies and classes from coverage results. Filter : string + /// The timeout for the OpenCover process. TimeOut : TimeSpan + /// The directory where the OpenCover process will be started. WorkingDir : string } -// Open Cover default parameters +/// OpenCover default parameters let OpenCoverDefaults = { ExePath = environVar "LOCALAPPDATA" @@ "Apps" @@ "OpenCover" @@ "OpenCover.Console.exe" TestRunnerExePath = ProgramFiles @@ "NUnit" @@ "bin" @@ "nunit-console.exe" From 411cf6955d8b6892de7d78cfc5e026541d336d95 Mon Sep 17 00:00:00 2001 From: Morten Trydal Date: Tue, 15 Apr 2014 18:12:04 +0200 Subject: [PATCH 5/6] Added traceStartTask and traceEndTask --- src/app/FakeLib/OpenCoverHelper.fs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/app/FakeLib/OpenCoverHelper.fs b/src/app/FakeLib/OpenCoverHelper.fs index f266a1fd63c..b930e01e8f4 100644 --- a/src/app/FakeLib/OpenCoverHelper.fs +++ b/src/app/FakeLib/OpenCoverHelper.fs @@ -44,6 +44,9 @@ let OpenCoverDefaults = /// - `setParams` - Function used to overwrite the default OpenCover parameters. /// - `targetArgs` - Test runner arguments. let OpenCover setParams targetArgs = + let taskName = "OpenCover" + let description = "Gathering coverage statistics" + traceStartTask taskName description let param = setParams OpenCoverDefaults let processArgs = @@ -73,4 +76,5 @@ let OpenCover setParams targetArgs = info.FileName <- param.ExePath if param.WorkingDir <> String.Empty then info.WorkingDirectory <- param.WorkingDir info.Arguments <- processArgs) param.TimeOut - if not ok then failwithf "OpenCover reported errors." \ No newline at end of file + if not ok then failwithf "OpenCover reported errors." + traceEndTask taskName description \ No newline at end of file From 098359b3edaf9ae5c10774ecb7664376b09a54ee Mon Sep 17 00:00:00 2001 From: Morten Trydal Date: Tue, 15 Apr 2014 19:17:54 +0200 Subject: [PATCH 6/6] Added sample to the docs --- src/app/FakeLib/OpenCoverHelper.fs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/app/FakeLib/OpenCoverHelper.fs b/src/app/FakeLib/OpenCoverHelper.fs index b930e01e8f4..1c4108b2084 100644 --- a/src/app/FakeLib/OpenCoverHelper.fs +++ b/src/app/FakeLib/OpenCoverHelper.fs @@ -43,6 +43,11 @@ let OpenCoverDefaults = /// /// - `setParams` - Function used to overwrite the default OpenCover parameters. /// - `targetArgs` - Test runner arguments. +/// +/// ## Sample +/// +/// OpenCover (fun p -> { p with TestRunnerExePath = "./Tools/NUnit/nunit-console.exe" }) +/// "project-file.nunit /config:Release /noshadow /xml:artifacts/nunit.xml /framework:net-4.0" let OpenCover setParams targetArgs = let taskName = "OpenCover" let description = "Gathering coverage statistics"