Skip to content

Commit 00115b5

Browse files
V-FEXrtag-eitilt
andauthored
tests: Minimal Base Refactor (#1531)
* tests: Minimal Base Refactor * Apply suggestions from code review Co-authored-by: Sam May <[email protected]> --------- Co-authored-by: Sam May <[email protected]>
1 parent 4f8c0e1 commit 00115b5

File tree

1 file changed

+67
-23
lines changed

1 file changed

+67
-23
lines changed

tests/tests.wake

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@ def wakeUnitToTest Unit =
5555

5656
Pass result
5757

58+
def showDiff (expect: String) (actual: String): Result Unit Error =
59+
require Pass expectFile = writeTempFile "diff.expect" expect
60+
else failWithError "Unable to write expect file"
61+
62+
require Pass actualFile = writeTempFile "diff.actual" actual
63+
else failWithError "Unable to write actual file"
64+
65+
def cmd =
66+
"diff",
67+
"-u",
68+
actualFile.getPathName,
69+
expectFile.getPathName,
70+
71+
# The job outputs here don't actually matter, depending on them forces the output order for
72+
# failed tests so they print out nicely.
73+
require Exited _ =
74+
makeExecPlan cmd (expectFile, actualFile, Nil)
75+
| setPlanStdout logWarning
76+
| setPlanStderr logWarning
77+
| setPlanIsAtty True
78+
| runJobWith defaultRunner
79+
| getJobStatus
80+
else failWithError "Unable to diff files"
81+
82+
Pass Unit
83+
5884
export def runTests (cmdline: List String): Result String Error =
5985
require Pass filter = match cmdline
6086
Nil -> Pass `.*`
@@ -235,42 +261,60 @@ def runTest (testScript: Path): Result Unit Error =
235261
| setPlanShare False
236262
| runJobWith localRunner # On OS/X you cannot mount fuse within fuse
237263

264+
def removeCarriageReturns = replace `\r` ""
265+
238266
require Pass jobStdout =
239267
testJob.getJobFailedStdout
240-
| rmap (replace `\r` "")
268+
| rmap removeCarriageReturns
241269

242270
require Pass jobStderr =
243271
testJob.getJobFailedStderr
244-
| rmap (replace `\r` "")
272+
| rmap removeCarriageReturns
245273

246274
require Pass _ = match shouldPass testJob.isJobOk
247275
True True -> Pass Unit
248276
False False -> Pass Unit
249277
True False ->
250-
Fail (makeError "Test failed ({format testJob.getJobStatus}) with '{replace `\n.*` "" jobStderr}'")
251-
False True ->
252-
Fail (makeError "Test should not have passed with '{replace `\n.*` "" jobStderr}'")
278+
def _ =
279+
"{testName} failed! Stdout: \n{jobStdout}"
280+
| printlnLevel logWarning
253281

254-
require Pass _ = match expectedStderr
255-
Some x if x ==* jobStderr -> Pass Unit
256-
None -> Pass Unit
257-
Some _ ->
258-
# TODO: use diff prim to compare jobStderr with expectedStderr
259282
def _ =
260-
(testName, "\n", jobStderr, Nil)
261-
| cat
262-
| println
283+
"{testName} failed! Stderr: \n{jobStderr}"
284+
| printlnLevel logWarning
263285

264-
Fail (makeError "Unexpected standard error. See above for details")
286+
Fail (makeError "Test failed. Expected: Ok, Actual: {format testJob.getJobStatus}. See above for details")
287+
False True ->
288+
def _ =
289+
"{testName} failed! Stdout: \n{jobStdout}"
290+
| printlnLevel logWarning
265291

266-
match expectedStdout
267-
Some x if x ==* jobStdout -> Pass Unit
268-
None -> Pass Unit
269-
_ ->
270-
# TODO: use diff prim to compare jobStdout with expectedStdout
271292
def _ =
272-
(testName, "\n", jobStdout, Nil)
273-
| cat
274-
| println
293+
"{testName} failed! Stderr: \n{jobStderr}"
294+
| printlnLevel logWarning
275295

276-
Fail (makeError "Unexpected standard output. See above for details")
296+
Fail (makeError "Test failed. Expected: Err, Actual: {format testJob.getJobStatus}. See above for details")
297+
298+
def expectEqualOutput (stream: String) (expected: Option String) (actual: String): Result Unit Error =
299+
# If the integration test doesn't provide an expected output then anything is allowed.
300+
require Some expect = expected
301+
else Pass Unit
302+
303+
# If they are equal, pass the expect
304+
require False = expect ==* actual
305+
else Pass Unit
306+
307+
# They are not equal, show a nice diff and fail.
308+
require Pass _ =
309+
"{testName} failed! The diff below should be applied to jobs {stream}"
310+
| printlnLevel logWarning
311+
| Pass
312+
313+
require Pass _ = showDiff expect actual
314+
315+
Fail (makeError "Unexpected {stream}. See above for details")
316+
317+
require Pass Unit = expectEqualOutput "stderr" expectedStderr jobStderr
318+
require Pass Unit = expectEqualOutput "stdout" expectedStdout jobStdout
319+
320+
Pass Unit

0 commit comments

Comments
 (0)