Skip to content

Commit

Permalink
Add color to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianhjr committed Jun 13, 2021
1 parent 40fa9b4 commit 4c99537
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 21 additions & 7 deletions libs/test/Test/Golden.idr
Expand Up @@ -64,6 +64,8 @@

module Test.Golden

import Control.ANSI

import Data.Either
import Data.Maybe
import Data.List
Expand Down Expand Up @@ -92,20 +94,23 @@ record Options where
onlyNames : List String
||| Should we run the test suite interactively?
interactive : Bool
||| Should we use colors?
color : Bool
||| Should we time and display the tests
timing : Bool
||| How many threads should we use?
threads : Nat
||| Should we write the list of failing cases from a file?
failureFile : Maybe String
failureFile : Maybe String

export
initOptions : String -> Options
initOptions exe
initOptions : String -> Bool -> Options
initOptions exe color
= MkOptions exe
Nothing
[]
False
color
False
1
Nothing
Expand All @@ -117,6 +122,7 @@ usage exe = unwords
, "runtests <path>"
, "[--timing]"
, "[--interactive]"
, "[--[no-]color, --[no-]colour]"
, "[--cg CODEGEN]"
, "[--threads N]"
, "[--failure-file PATH]"
Expand Down Expand Up @@ -144,6 +150,10 @@ options args = case args of
[] => pure (only, opts)
("--timing" :: xs) => go xs only (record { timing = True} opts)
("--interactive" :: xs) => go xs only (record { interactive = True } opts)
("--color" :: xs) => go xs only (record { color = True } opts)
("--colour" :: xs) => go xs only (record { color = True } opts)
("--no-color" :: xs) => go xs only (record { color = False } opts)
("--no-colour" :: xs) => go xs only (record { color = False } opts)
("--cg" :: cg :: xs) => go xs only (record { codegen = Just cg } opts)
("--threads" :: n :: xs) => do let pos : Nat = !(parsePositive n)
go xs only (record { threads = pos } opts)
Expand All @@ -154,7 +164,8 @@ options args = case args of

mkOptions : String -> List String -> IO (Maybe Options)
mkOptions exe rest
= do let Just (mfp, opts) = go rest Nothing (initOptions exe)
= do color <- (Just "DUMB" /=) <$> getEnv "TERM"
let Just (mfp, opts) = go rest Nothing (initOptions exe color)
| Nothing => pure Nothing
let Just fp = mfp
| Nothing => pure (Just opts)
Expand Down Expand Up @@ -211,9 +222,11 @@ runTest opts testPath = forkIO $ do
let time = timeDifference end start

if result
then printTiming (timing opts) time $ testPath ++ ": success"
then printTiming (timing opts) time $ testPath ++ ": " ++
(if opts.color then show . colored BrightGreen else id) "success"
else do
printTiming (timing opts) time $ testPath ++ ": FAILURE"
printTiming (timing opts) time $ testPath ++ ": " ++
(if opts.color then show . colored BrightRed else id) "FAILURE"
if interactive opts
then mayOverwrite (Just exp) out
else putStrLn . unlines $ expVsOut exp out
Expand Down Expand Up @@ -242,7 +255,8 @@ runTest opts testPath = forkIO $ do
, "Accept new golden value? [yn]"
]
Just exp => do
code <- system $ "git diff --no-index --exit-code --word-diff=color " ++
code <- system $ "git diff --no-index --exit-code " ++
(if opts.color then "--word-diff=color " else "") ++
testPath ++ "/expected " ++ testPath ++ "/output"
putStrLn . unlines $
["Golden value differs from actual value."] ++
Expand Down
4 changes: 3 additions & 1 deletion libs/test/test.ipkg
@@ -1,7 +1,9 @@
package test
version = 0.3.0

opts = "--ignore-missing-ipkg -p contrib"
depends = contrib

opts = "--ignore-missing-ipkg"

modules = Test.Golden

0 comments on commit 4c99537

Please sign in to comment.