/
ProcessTestRunner.fs
87 lines (80 loc) · 3.69 KB
/
ProcessTestRunner.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/// Allows to execute processes as unit tests.
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
module Fake.ProcessTestRunner
open System
open System.IO
open System.Text
/// The ProcessTestRunner parameter type.
[<CLIMutable>]
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
type ProcessTestRunnerParams =
{ /// The working directory (optional).
WorkingDir : string
/// If the timeout is reached the xUnit task will be killed. Default is 5 minutes.
TimeOut : TimeSpan
/// Option which allows to specify if a test runner error should break the build.
ErrorLevel : TestRunnerErrorLevel }
/// The ProcessTestRunner defaults.
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
let ProcessTestRunnerDefaults =
{ WorkingDir = null
TimeOut = TimeSpan.FromMinutes 5.
ErrorLevel = TestRunnerErrorLevel.Error }
/// Runs the given process and returns the process result.
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
let RunConsoleTest parameters fileName args =
let taskName = sprintf "Run_%s" fileName
let result = ref None
try
let exitCode =
ExecProcess (fun info ->
info.WorkingDirectory <- parameters.WorkingDir
info.FileName <- fileName
info.Arguments <- args) parameters.TimeOut
if exitCode <> 0 then result := Some(sprintf "Exit code %d" exitCode)
with exn ->
let message = ref exn.Message
if exn.InnerException <> null then message := !message + Environment.NewLine + exn.InnerException.Message
result := Some(!message)
!result
/// Runs the given processes and returns the process result messages.
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
let runConsoleTests parameters processes =
processes
|> Seq.map (fun (fileName, args) ->
fileName, args,
match RunConsoleTest parameters fileName args with
| Some m' -> m'
| _ -> "")
|> Seq.filter (fun (_, _, m) -> m <> "")
/// Runs the given processes and returns the process results.
/// ## Parameters
///
/// - `setParams` - Function used to manipulate the default parameter value.
/// - `processes` - Sequence of one or more filenames and arguments to run.
///
/// ## Sample usage
///
/// Target "Test" (fun _ ->
/// ["process1.exe","argument1"
/// "process2.exe","argument2"]
/// |> RunConsoleTests (fun p -> {p with TimeOut = TimeSpan.FromMinutes 1. })
/// )
[<System.Obsolete("This API is obsolete. There is no alternative in FAKE 5 yet. You can help by porting this module.")>]
let RunConsoleTests setParams processes =
use __ = traceStartTaskUsing "RunConsoleTests" ""
let parameters = setParams ProcessTestRunnerDefaults
let execute() =
runConsoleTests parameters processes
|> Seq.map (fun (f, a, m) -> sprintf "Process %s %s terminated with %s" f a m)
|> toLines
match parameters.ErrorLevel with
| TestRunnerErrorLevel.DontFailBuild -> execute() |> trace
| TestRunnerErrorLevel.Error ->
let msg = execute()
if msg <> "" then failwith msg
| TestRunnerErrorLevel.FailOnFirstError ->
for fileName, args in processes do
match RunConsoleTest parameters fileName args with
| Some error -> failwithf "Process %s %s terminated with %s" fileName args error
| _ -> ()