Skip to content

Commit 4c99537

Browse files
committed
Add color to tests
1 parent 40fa9b4 commit 4c99537

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

libs/test/Test/Golden.idr

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464

6565
module Test.Golden
6666

67+
import Control.ANSI
68+
6769
import Data.Either
6870
import Data.Maybe
6971
import Data.List
@@ -92,20 +94,23 @@ record Options where
9294
onlyNames : List String
9395
||| Should we run the test suite interactively?
9496
interactive : Bool
97+
||| Should we use colors?
98+
color : Bool
9599
||| Should we time and display the tests
96100
timing : Bool
97101
||| How many threads should we use?
98102
threads : Nat
99103
||| Should we write the list of failing cases from a file?
100-
failureFile : Maybe String
104+
failureFile : Maybe String
101105

102106
export
103-
initOptions : String -> Options
104-
initOptions exe
107+
initOptions : String -> Bool -> Options
108+
initOptions exe color
105109
= MkOptions exe
106110
Nothing
107111
[]
108112
False
113+
color
109114
False
110115
1
111116
Nothing
@@ -117,6 +122,7 @@ usage exe = unwords
117122
, "runtests <path>"
118123
, "[--timing]"
119124
, "[--interactive]"
125+
, "[--[no-]color, --[no-]colour]"
120126
, "[--cg CODEGEN]"
121127
, "[--threads N]"
122128
, "[--failure-file PATH]"
@@ -144,6 +150,10 @@ options args = case args of
144150
[] => pure (only, opts)
145151
("--timing" :: xs) => go xs only (record { timing = True} opts)
146152
("--interactive" :: xs) => go xs only (record { interactive = True } opts)
153+
("--color" :: xs) => go xs only (record { color = True } opts)
154+
("--colour" :: xs) => go xs only (record { color = True } opts)
155+
("--no-color" :: xs) => go xs only (record { color = False } opts)
156+
("--no-colour" :: xs) => go xs only (record { color = False } opts)
147157
("--cg" :: cg :: xs) => go xs only (record { codegen = Just cg } opts)
148158
("--threads" :: n :: xs) => do let pos : Nat = !(parsePositive n)
149159
go xs only (record { threads = pos } opts)
@@ -154,7 +164,8 @@ options args = case args of
154164

155165
mkOptions : String -> List String -> IO (Maybe Options)
156166
mkOptions exe rest
157-
= do let Just (mfp, opts) = go rest Nothing (initOptions exe)
167+
= do color <- (Just "DUMB" /=) <$> getEnv "TERM"
168+
let Just (mfp, opts) = go rest Nothing (initOptions exe color)
158169
| Nothing => pure Nothing
159170
let Just fp = mfp
160171
| Nothing => pure (Just opts)
@@ -211,9 +222,11 @@ runTest opts testPath = forkIO $ do
211222
let time = timeDifference end start
212223

213224
if result
214-
then printTiming (timing opts) time $ testPath ++ ": success"
225+
then printTiming (timing opts) time $ testPath ++ ": " ++
226+
(if opts.color then show . colored BrightGreen else id) "success"
215227
else do
216-
printTiming (timing opts) time $ testPath ++ ": FAILURE"
228+
printTiming (timing opts) time $ testPath ++ ": " ++
229+
(if opts.color then show . colored BrightRed else id) "FAILURE"
217230
if interactive opts
218231
then mayOverwrite (Just exp) out
219232
else putStrLn . unlines $ expVsOut exp out
@@ -242,7 +255,8 @@ runTest opts testPath = forkIO $ do
242255
, "Accept new golden value? [yn]"
243256
]
244257
Just exp => do
245-
code <- system $ "git diff --no-index --exit-code --word-diff=color " ++
258+
code <- system $ "git diff --no-index --exit-code " ++
259+
(if opts.color then "--word-diff=color " else "") ++
246260
testPath ++ "/expected " ++ testPath ++ "/output"
247261
putStrLn . unlines $
248262
["Golden value differs from actual value."] ++

libs/test/test.ipkg

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package test
22
version = 0.3.0
33

4-
opts = "--ignore-missing-ipkg -p contrib"
4+
depends = contrib
5+
6+
opts = "--ignore-missing-ipkg"
57

68
modules = Test.Golden
79

0 commit comments

Comments
 (0)