Skip to content

Commit c418ba0

Browse files
committed
Merge branch 'dev'
2 parents 01f9e73 + 154250c commit c418ba0

24 files changed

+702
-369
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ _UpgradeReport*
1313
# NuGet Packages Directory
1414
packages
1515
.nuget
16+
.paket
1617

1718
# Ignore Visual Studio files
1819
*.pdb

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
"editor.wrappingColumn": 120
4+
}

XakeLibTests/ActionTests.fs

Lines changed: 118 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module ``action block allows``
1+
module ``action block is capable of``
22

33
open NUnit.Framework
44
open Xake
@@ -7,7 +7,7 @@ let makeStringList() = new System.Collections.Generic.List<string>()
77
let DebugOptions = {ExecOptions.Default with FailOnError = true; FileLog = ""}
88

99
[<Test>]
10-
let ``executes the body``() =
10+
let ``execute the body``() =
1111

1212
let wasExecuted = ref false
1313

@@ -20,7 +20,7 @@ let ``executes the body``() =
2020
Assert.IsTrue(!wasExecuted)
2121

2222
[<Test>]
23-
let ``execution is ordered``() =
23+
let ``ordered execution``() =
2424

2525
let wasExecuted = ref false
2626

@@ -41,7 +41,7 @@ let ``execution is ordered``() =
4141
Assert.That(errorlist, Is.EquivalentTo(["1"; "2"; "3"]))
4242

4343
[<Test>]
44-
let ``allows async operations``() =
44+
let ``starting async operations``() =
4545

4646
let wasExecuted = ref false
4747

@@ -63,7 +63,7 @@ let ``allows async operations``() =
6363
Assert.That(errorlist, Is.EqualTo(["1"; "2"; "3"; "4"]))
6464

6565
[<Test>]
66-
let ``do! action``() =
66+
let ``invoke other actions within action (do!)``() =
6767

6868
let wasExecuted = ref false
6969

@@ -88,7 +88,7 @@ let ``do! action``() =
8888

8989

9090
[<Test>]
91-
let ``do! action with result ignored``() =
91+
let ``ignoring result of do! action``() =
9292

9393
let wasExecuted = ref false
9494

@@ -117,7 +117,7 @@ let ``do! action with result ignored``() =
117117

118118

119119
[<Test>]
120-
let ``let! returning value``() =
120+
let ``obtaining action result``() =
121121

122122
let errorlist = makeStringList()
123123
let note = errorlist.Add
@@ -140,7 +140,7 @@ let ``let! returning value``() =
140140
Assert.That(errorlist, Is.EqualTo(["1"; "2+1"; "3"] |> List.toArray))
141141

142142
[<Test>]
143-
let ``if of various kinds``() =
143+
let ``ifs within actions``() =
144144

145145
let errorlist = makeStringList()
146146
let note = errorlist.Add
@@ -172,7 +172,7 @@ let ``if of various kinds``() =
172172
Assert.That(errorlist, Is.EqualTo(["i1-t"; "i2-f"; "2"; "3"] |> List.toArray))
173173

174174
[<Test>]
175-
let ``if without else``() =
175+
let ``branching using if without else``() =
176176

177177
let errorlist = makeStringList()
178178
let note = errorlist.Add
@@ -202,7 +202,7 @@ let ``if without else``() =
202202
Assert.That(errorlist, Is.EqualTo(["i1-t"; "3"; "4"] |> List.toArray))
203203

204204
[<Test>]
205-
let ``for and while``() =
205+
let ``for and while loops``() =
206206

207207
let errorlist = makeStringList()
208208
let note = errorlist.Add
@@ -230,40 +230,128 @@ let ``for and while``() =
230230

231231
Assert.That(errorlist, Is.EqualTo(["1"; "i=1"; "i=2"; "i=3"; "j=3"; "j=4"; "4"] |> List.toArray))
232232

233-
[<Test; Explicit>]
234-
let ``try catch finally``() =
233+
[<Test>]
234+
let ``exception handling with 'try finally'``() =
235235

236236
let errorlist = makeStringList()
237237
let note = errorlist.Add
238+
let anote txt = action {
239+
do note txt
240+
}
238241

239242
do xake DebugOptions {
240243

241244
phony "main" (action {
245+
note "before try"
246+
try
247+
printfn "Body executed"
248+
do! anote "try"
249+
finally
250+
printfn "Finally executed"
251+
do note "finally"
252+
do note "4"
253+
})
254+
}
242255

243-
let! s1 = action {return "122"}
244-
do note s1
256+
// printfn "%A" errorlist
257+
Assert.That(errorlist, Is.EqualTo(["before try"; "try"; "finally"; "4"] |> List.toArray))
245258

246-
note "before try"
259+
[<Test>]
260+
let ``try finally fail``() =
247261

248-
// try
249-
// printfn "Body executed"
250-
// do note "try"
251-
// finally
252-
// printfn "Finally executed"
253-
// do note "finally"
254-
255-
// try
256-
// failwith "ee"
257-
// with e ->
258-
// do note e.Message
262+
let errorlist = makeStringList()
263+
let note = errorlist.Add
264+
let anote txt = action {
265+
do errorlist.Add txt
266+
}
267+
268+
do xake DebugOptions {
269+
270+
phony "main" (action {
271+
do! anote "before try"
272+
273+
try
274+
printfn "Body executed"
275+
do! anote "try"
276+
failwith "Ouch"
277+
finally
278+
printfn "Finally executed"
279+
do note "finally"
259280

260-
do note "4"
281+
do! anote "4"
282+
} |> WhenError ignore)
283+
}
284+
285+
// printfn "%A" errorlist
286+
Assert.That(errorlist, Is.EqualTo(["before try"; "try"; "finally"] |> List.toArray))
287+
288+
[<Test>]
289+
let ``exception handling with 'try with'``() =
290+
291+
let errorlist = makeStringList()
292+
let anote txt = action {
293+
do errorlist.Add txt
294+
}
295+
296+
do xake DebugOptions {
297+
298+
phony "main" (action {
299+
do! anote "before try"
300+
301+
try
302+
printfn "Body executed"
303+
do! anote "try"
304+
failwith "Ouch"
305+
with e ->
306+
do! anote e.Message
307+
308+
do! anote "4"
261309
})
310+
}
311+
312+
// printfn "%A" errorlist
313+
Assert.That(errorlist, Is.EqualTo(["before try"; "try"; "Ouch"; "4"] |> List.toArray))
314+
315+
[<Test>]
316+
let ``WhenError function to handle exceptions within actions``() =
317+
318+
let taskReturn n = action {
319+
return n
320+
}
321+
322+
let excCount = ref 0
323+
do xake DebugOptions {
324+
rules [
325+
"main" => (
326+
WhenError (fun _ -> excCount := 1) <|
327+
action {
328+
printfn "Some useful job"
329+
do! taskReturn 3 |> FailWhen ((=) 3) "err" |> Action.Ignore
330+
printfn "This wont run"
331+
})
332+
]
333+
}
334+
335+
Assert.AreEqual(1, !excCount)
336+
337+
[<Test>]
338+
let ``try/with for the whole script body``() =
339+
let excCount = ref 0
340+
do xake DebugOptions {
341+
rules [
342+
"main" =>
343+
action {
344+
try
345+
printfn "Some useful job"
346+
do 3/0 |> ignore
347+
printfn "This wont run"
348+
with _ ->
349+
excCount := 1
350+
}
351+
]
262352
}
263353

264-
// "2222"; "ee";
265-
printfn "%A" errorlist
266-
Assert.That(errorlist, Is.EqualTo(["122"; "try"; "finally"; "4"] |> List.toArray))
354+
Assert.AreEqual(1, !excCount)
267355

268356
// TODO use!, try with exception within action
269357

XakeLibTests/MiscTests.fs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,83 @@ let ``resource set instantiation``() =
8181
printfn "%A" resset
8282
()
8383

84+
[<Test>]
85+
let ``script exits with errorlevel on script failure``() =
86+
87+
let errorCode = ref 0
88+
System.IO.Directory.CreateDirectory("1") |> ignore
89+
90+
do xake {xakeOptions with Threads = 1; FileLog="exits-with-errorlevel.log"; FileLogLevel = Verbosity.Diag; Targets = ["one"] } {
91+
rules [
92+
"one" => action {
93+
do! need ["1/script.fsx"]
94+
let! ec = system "fsi" ["1/script.fsx"]
95+
errorCode := ec
96+
}
97+
"1/script.fsx" *> fun src -> action {
98+
do File.WriteAllText (src.FullName, """
99+
#r "..\Xake.Core.dll"
100+
open Xake
101+
102+
do xake {ExecOptions.Default with DbFileName=".1err"; Threads = 4 } {
103+
104+
phony "main" (action {
105+
do! trace Message "Hello world!"
106+
failwith "error-text"
107+
})
108+
109+
}
110+
""")
111+
}
112+
]
113+
}
114+
115+
Assert.AreEqual(2, !errorCode)
116+
117+
let taskReturn n = action {
118+
return n
119+
}
120+
121+
[<Test>]
122+
let ``failif is a short circuit for task result``() =
123+
124+
let excCount = ref 0
125+
do xake {xakeOptions with Threads = 1; FileLog="failf.log"} {
126+
rules [
127+
"main" => (action {
128+
do! taskReturn 3 |> FailWhen ((=) 3) "err" |> Action.Ignore
129+
} |> WhenError (fun _ -> excCount := 1))
130+
]
131+
}
132+
133+
Assert.AreEqual(1, !excCount)
134+
135+
[<Test>]
136+
let ``WhenError handler intercepts the error``() =
137+
138+
let ex = ref 0
139+
140+
// pipe result, and provide fallback value in case of error
141+
do xake {xakeOptions with Threads = 1; FileLog="failf.log"} {
142+
rules [
143+
"main" => action {
144+
do! taskReturn 3
145+
|> FailWhen ((=) 3) "fail"
146+
|> WhenError (fun _ -> ex := 1; 0)
147+
|> Action.Ignore
148+
}
149+
]
150+
}
151+
// intercept error for resultless action
152+
do xake {xakeOptions with Threads = 1; FileLog="failf.log"} {
153+
rules [
154+
"main" => action {
155+
do! taskReturn 3
156+
|> FailWhen ((=) 3) "fail"
157+
|> Action.Ignore
158+
|> WhenError (fun _ -> ex := !ex + 1)
159+
}
160+
]
161+
}
162+
163+
Assert.AreEqual(2, !ex)

0 commit comments

Comments
 (0)