-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
167 lines (129 loc) · 6.07 KB
/
Program.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
namespace FSharpSpec.ConsoleRunner
open System
open System.IO
open System.Diagnostics
open System.Reflection
open FSharpSpec
open FSharpSpec.RunnerUtils
open SpecsExtractor
open SpecsRunnerUtils
module main =
let helpFlag = ("--help", "-h")
let silentFlag = ("--silent", "-s")
let debugFlag = ("--debug", "-d")
let helpText = "
Usage: FsharpSpec.ConsoleRunner [options] [Path1 Path2 ...]
Path: The full or relative path to the assembly containing the specifications to run
Options:
--help: prints this help
--silent: supresses printing out of the detailed specification tree
"
let getFlags (args : string list) =
let validFlags = [helpFlag; silentFlag; debugFlag]
let flags = args |> List.filter(fun arg -> arg.StartsWith("--"))
let shortFlags = args |> List.filter(fun arg -> arg.StartsWith("-") && arg.Length = 2)
let allFlags = List.concat [flags; shortFlags]
let invalidFlags =
allFlags
|> List.filter(fun flag -> not (validFlags |> List.exists(fun f -> fst f = flag || snd f = flag)))
match invalidFlags with
| [] -> allFlags
| x -> failwithf "\n\nFound invalid option(s), use --help for more information. \nInvalid option(s): %A\n\n" x
let getPaths (args : string list) =
let paths =
args
|> List.filter(fun arg -> not (arg.StartsWith("-")))
|> List.map (fun path ->
match path with
| p when File.Exists(p) && Path.IsPathRooted(p) -> p
| p -> Path.Combine(Environment.CurrentDirectory, p)
)
let invalidPaths = paths |> List.filter(fun path -> File.Exists(path) |> not)
match invalidPaths with
| [] -> paths
| x -> failwithf "\n\nUnable to find the following path(s) \n%A\n\n" x
let extraxtAllContexts args =
let getContexts path =
let asm = getAssembly path
hookAssemblyResolve asm
let ctx = asm |> getAllContexts
match ctx.Length with
| 0 ->
let warnings = [sprintf "No contexts/specifications found in %s" path]
(Array.empty, warnings)
| _ -> (ctx, [])
let mutable allWarnings = List.empty
let mutable allContexts = Array.empty
for path in args |> getPaths do
let result = getContexts path
allContexts <- Array.append allContexts (fst result)
allWarnings <- List.append allWarnings (snd result)
(allContexts, allWarnings)
(*
[
@"C:\dev\FSharp\FSharpSpec\src\Specs\FSharpSpec.Specs\bin\Debug\FSharpSpec.Specs.dll"
@"C:\dev\FSharp\FSharpSpec\src\Samples\FSharpSpec.FSharpSampleSpecs\bin\Debug\FSharpSpec.FSharpSampleSpecs.dll"
@"C:\dev\FSharp\FSharpSpec\src\Samples\Katas\FSharpSpec.Katas.StringCalculator\bin\Debug\FSharpSpec.Katas.dll"
@"C:\dev\FSharp\FSharpSpec\src\FSharpSpec.GuiRunner.Specs\bin\Debug\FSharpSpec.GuiRunner.Specs.dll"
@"C:\dev\FSharp\FSharpSpec\src\FSharpSpec\bin\Debug\FSharpSpec.dll"
]
*)
[<EntryPoint>]
let main(args:string[]) =
let printer = writeToConsole
let containFlag flag flags =
flags |> List.exists(fun f -> f = fst flag || f = snd flag)
try
let args = args |> Array.toList |> List.map(fun arg -> arg.Trim())
(*
Environment.CurrentDirectory <- @"C:\dev\FSharp\FSharpSpec\src\Specs\FSharpSpec.Specs\bin\Debug"
let args = ["FSharpSpec.Specs.dll"]
*)
(*
let args = [
@"C:\dev\FSharp\FSharpSpec\src\Specs\FSharpSpec.Specs\bin\Debug\FSharpSpec.Specs.dll"
@"C:\dev\FSharp\FSharpSpec\src\Samples\FSharpSpec.FSharpSampleSpecs\bin\Debug\FSharpSpec.FSharpSampleSpecs.dll"
@"C:\dev\FSharp\FSharpSpec\src\Samples\Katas\FSharpSpec.Katas.StringCalculator\bin\Debug\FSharpSpec.Katas.dll"
@"C:\dev\FSharp\FSharpSpec\src\FSharpSpec.GuiRunner.Specs\bin\Debug\FSharpSpec.GuiRunner.Specs.dll"
@"C:\dev\FSharp\FSharpSpec\src\FSharpSpec\bin\Debug\FSharpSpec.dll"
]
*)
let flags = args |> getFlags
let needHelp = flags |> containFlag helpFlag || args |> List.isEmpty
let isSilent = flags |> containFlag silentFlag
let isDebug = flags |> containFlag debugFlag
if needHelp then
helpText |> printer
0
else
let allContexts, allWarnings = args |> extraxtAllContexts
let tree = allContexts |> getContextTreeOfContexts
if (isDebug) then
printfn "Attaching debugger - Just click yes in the dialog."
Debugger.Launch() |> ignore
Debugger.Log(0, "FSharpSpec", "Successfully attached Debugger.\n")
let results = tree.RunSpecs()
if (not isSilent) then printResultTree results printer
printFailureDetails results printer
printPendingSummary results printer
printFailureSummary results printer
let logger = new ConsoleLogger() :> ISpecsResultsLogger
results
|> List.iter(fun (msg, passes, failures, pendings) ->
passes |> List.iter(fun passedSpec -> logger.SpecPassed passedSpec)
failures |> List.iter(fun failedSpec ->
logger.SpecFailed failedSpec.FullSpecName (
getFailureMessage failedSpec) (getFailureStackTrace failedSpec))
pendings |> List.iter(fun pendingSpec -> logger.SpecPending pendingSpec))
(logger :?> ConsoleLogger).Report |> printer
match allWarnings with
| [] -> // Console.ReadLine() |> ignore
0
| x -> sprintf "\nWarnings: \n%A\n\n" x |> printer
// Console.ReadLine() |> ignore
1
with
| ex -> ex.Message |> printer
"Use -help for more information\n" |> printer
// Console.ReadLine() |> ignore
1