2020-10-19 11:26:23 +03:00
|
|
|
||| Core features required to perform Golden file testing.
|
|
|
|
|||
|
|
|
|
||| We provide the core functionality to run a *single* golden file test, or
|
|
|
|
||| a whole test tree.
|
|
|
|
||| This allows the developer freedom to use as is or design the rest of the
|
|
|
|
||| test harness to their liking.
|
|
|
|
|||
|
|
|
|
||| This was originally used as part of Idris2's own test suite and
|
|
|
|
||| the core functionality is useful for the many and not the few.
|
|
|
|
||| Please see Idris2 test harness for example usage.
|
|
|
|
|||
|
|
|
|
||| # Test Structure
|
|
|
|
|||
|
|
|
|
||| This harness works from the assumption that each individual golden test
|
|
|
|
||| comprises of a directory with the following structure:
|
|
|
|
|||
|
|
|
|
||| + `run` a *shell* script that runs the test. We expect it to:
|
|
|
|
||| * Use `$1` as the variable standing for the idris executable to be tested
|
|
|
|
||| * May use `${IDRIS2_TESTS_CG}` to pick a codegen that ought to work
|
|
|
|
||| * Clean up after itself (e.g. by running `rm -rf build/`)
|
|
|
|
|||
|
|
|
|
||| + `expected` a file containting the expected output of `run`
|
|
|
|
|||
|
2021-03-15 20:46:41 +03:00
|
|
|
||| During testing, the test harness will generate an artefact named `output`
|
|
|
|
||| and display both outputs if there is a failure.
|
|
|
|
||| During an interactive session the following command is used to compare them
|
|
|
|
||| as they are:
|
2020-10-19 11:26:23 +03:00
|
|
|
|||
|
|
|
|
||| ```sh
|
2022-02-18 14:29:52 +03:00
|
|
|
||| git diff --no-index --exit-code --word-diff-regex=. --color expected output
|
2020-10-19 11:26:23 +03:00
|
|
|
||| ```
|
|
|
|
|||
|
|
|
|
||| If `git` fails then the runner will simply present the expected and 'given'
|
|
|
|
||| files side-by-side.
|
|
|
|
|||
|
2021-03-15 20:46:41 +03:00
|
|
|
||| Of note, it is helpful to add `output` to a local `.gitignore` instance
|
2020-10-19 11:26:23 +03:00
|
|
|
||| to ensure that it is not mistakenly versioned.
|
|
|
|
|||
|
|
|
|
||| # Options
|
|
|
|
|||
|
|
|
|
||| The test harness has several options that may be set:
|
|
|
|
|||
|
|
|
|
||| + `idris2` The path of the executable we are testing.
|
2021-03-15 20:46:41 +03:00
|
|
|
||| + `codegen` The backend to use for code generation.
|
|
|
|
||| + `onlyNames` The tests to run relative to the generated executable.
|
2021-05-11 11:46:21 +03:00
|
|
|
||| + `onlyFile` The file listing the tests to run relative to the generated executable.
|
2020-10-19 11:26:23 +03:00
|
|
|
||| + `interactive` Whether to offer to update the expected file or not.
|
|
|
|
||| + `timing` Whether to display time taken for each test.
|
2021-03-15 20:46:41 +03:00
|
|
|
||| + `threads` The maximum numbers to use (default: number of cores).
|
2021-05-11 11:46:21 +03:00
|
|
|
||| + `failureFile` The file in which to write the list of failing tests.
|
2020-10-19 11:26:23 +03:00
|
|
|
|||
|
2021-03-15 20:46:41 +03:00
|
|
|
||| We provide an options parser (`options`) that takes the list of command line
|
|
|
|
||| arguments and constructs this for you.
|
2020-10-19 11:26:23 +03:00
|
|
|
|||
|
|
|
|
||| # Usage
|
|
|
|
|||
|
|
|
|
||| When compiled to an executable the expected usage is:
|
|
|
|
|||
|
|
|
|
|||```sh
|
2021-06-21 19:30:11 +03:00
|
|
|
||| runtests <path to executable under test>
|
|
|
|
||| [--timing]
|
|
|
|
||| [--interactive]
|
|
|
|
||| [--only-file PATH]
|
|
|
|
||| [--failure-file PATH]
|
|
|
|
||| [--threads N]
|
|
|
|
||| [--cg CODEGEN]
|
|
|
|
||| [--only [NAMES]]
|
2020-10-19 11:26:23 +03:00
|
|
|
|||```
|
|
|
|
|||
|
|
|
|
||| assuming that the test runner is compiled to an executable named `runtests`.
|
|
|
|
|
|
|
|
module Test.Golden
|
|
|
|
|
2021-06-09 18:38:08 +03:00
|
|
|
import Control.ANSI
|
|
|
|
|
2021-05-11 11:46:21 +03:00
|
|
|
import Data.Either
|
2020-10-19 11:26:23 +03:00
|
|
|
import Data.Maybe
|
|
|
|
import Data.List
|
|
|
|
import Data.List1
|
2021-06-21 19:30:11 +03:00
|
|
|
import Data.String
|
2020-10-19 11:26:23 +03:00
|
|
|
|
|
|
|
import System
|
|
|
|
import System.Clock
|
|
|
|
import System.Directory
|
2022-02-02 14:17:10 +03:00
|
|
|
import System.File
|
2020-10-19 11:26:23 +03:00
|
|
|
import System.Info
|
|
|
|
import System.Path
|
2022-02-02 14:17:10 +03:00
|
|
|
import System.Concurrency
|
2020-10-19 11:26:23 +03:00
|
|
|
|
|
|
|
-- [ Options ]
|
|
|
|
|
|
|
|
||| Options for the test driver.
|
|
|
|
public export
|
|
|
|
record Options where
|
|
|
|
constructor MkOptions
|
|
|
|
||| Name of the idris2 executable
|
|
|
|
exeUnderTest : String
|
|
|
|
||| Which codegen should we use?
|
|
|
|
codegen : Maybe String
|
|
|
|
||| Should we only run some specific cases?
|
|
|
|
onlyNames : List String
|
|
|
|
||| Should we run the test suite interactively?
|
|
|
|
interactive : Bool
|
2021-06-09 18:38:08 +03:00
|
|
|
||| Should we use colors?
|
|
|
|
color : Bool
|
2020-10-19 11:26:23 +03:00
|
|
|
||| Should we time and display the tests
|
|
|
|
timing : Bool
|
2021-03-15 20:46:41 +03:00
|
|
|
||| How many threads should we use?
|
|
|
|
threads : Nat
|
2021-06-21 19:30:11 +03:00
|
|
|
||| Should we write the list of failing cases to a file?
|
|
|
|
failureFile : Maybe String
|
2021-03-15 20:46:41 +03:00
|
|
|
|
|
|
|
export
|
2021-06-09 18:38:08 +03:00
|
|
|
initOptions : String -> Bool -> Options
|
|
|
|
initOptions exe color
|
2021-03-15 20:46:41 +03:00
|
|
|
= MkOptions exe
|
|
|
|
Nothing
|
|
|
|
[]
|
|
|
|
False
|
2021-06-09 18:38:08 +03:00
|
|
|
color
|
2021-03-15 20:46:41 +03:00
|
|
|
False
|
|
|
|
1
|
2021-05-11 11:46:21 +03:00
|
|
|
Nothing
|
2020-10-19 11:26:23 +03:00
|
|
|
|
|
|
|
export
|
2021-06-21 19:30:11 +03:00
|
|
|
usage : String
|
|
|
|
usage = unwords
|
|
|
|
["Usage:"
|
2021-03-15 20:46:41 +03:00
|
|
|
, "runtests <path>"
|
|
|
|
, "[--timing]"
|
|
|
|
, "[--interactive]"
|
2021-06-09 18:38:08 +03:00
|
|
|
, "[--[no-]color, --[no-]colour]"
|
2021-03-15 20:46:41 +03:00
|
|
|
, "[--cg CODEGEN]"
|
|
|
|
, "[--threads N]"
|
2021-05-11 11:46:21 +03:00
|
|
|
, "[--failure-file PATH]"
|
|
|
|
, "[--only-file PATH]"
|
2021-03-15 20:46:41 +03:00
|
|
|
, "[--only [NAMES]]"
|
|
|
|
]
|
2020-10-19 11:26:23 +03:00
|
|
|
|
2021-05-11 11:46:21 +03:00
|
|
|
export
|
|
|
|
fail : String -> IO a
|
|
|
|
fail err
|
|
|
|
= do putStrLn err
|
|
|
|
exitWith (ExitFailure 1)
|
|
|
|
|
2020-10-19 11:26:23 +03:00
|
|
|
||| Process the command line options.
|
|
|
|
export
|
2021-05-11 11:46:21 +03:00
|
|
|
options : List String -> IO (Maybe Options)
|
2020-10-19 11:26:23 +03:00
|
|
|
options args = case args of
|
2021-05-11 11:46:21 +03:00
|
|
|
(_ :: exe :: rest) => mkOptions exe rest
|
|
|
|
_ => pure Nothing
|
2020-10-19 11:26:23 +03:00
|
|
|
|
|
|
|
where
|
|
|
|
|
2021-05-11 11:46:21 +03:00
|
|
|
go : List String -> Maybe String -> Options -> Maybe (Maybe String, Options)
|
|
|
|
go rest only opts = case rest of
|
|
|
|
[] => pure (only, opts)
|
2021-12-16 21:23:18 +03:00
|
|
|
("--timing" :: xs) => go xs only ({ timing := True} opts)
|
|
|
|
("--interactive" :: xs) => go xs only ({ interactive := True } opts)
|
|
|
|
("--color" :: xs) => go xs only ({ color := True } opts)
|
|
|
|
("--colour" :: xs) => go xs only ({ color := True } opts)
|
|
|
|
("--no-color" :: xs) => go xs only ({ color := False } opts)
|
|
|
|
("--no-colour" :: xs) => go xs only ({ color := False } opts)
|
|
|
|
("--cg" :: cg :: xs) => go xs only ({ codegen := Just cg } opts)
|
2021-05-11 11:46:21 +03:00
|
|
|
("--threads" :: n :: xs) => do let pos : Nat = !(parsePositive n)
|
2021-12-16 21:23:18 +03:00
|
|
|
go xs only ({ threads := pos } opts)
|
|
|
|
("--failure-file" :: p :: xs) => go xs only ({ failureFile := Just p } opts)
|
|
|
|
("--only" :: xs) => pure (only, { onlyNames := xs } opts)
|
2021-05-11 11:46:21 +03:00
|
|
|
("--only-file" :: p :: xs) => go xs (Just p) opts
|
2020-10-19 11:26:23 +03:00
|
|
|
_ => Nothing
|
|
|
|
|
2021-05-11 11:46:21 +03:00
|
|
|
mkOptions : String -> List String -> IO (Maybe Options)
|
|
|
|
mkOptions exe rest
|
2021-06-09 18:38:08 +03:00
|
|
|
= do color <- (Just "DUMB" /=) <$> getEnv "TERM"
|
|
|
|
let Just (mfp, opts) = go rest Nothing (initOptions exe color)
|
2021-05-11 11:46:21 +03:00
|
|
|
| Nothing => pure Nothing
|
|
|
|
let Just fp = mfp
|
|
|
|
| Nothing => pure (Just opts)
|
|
|
|
Right only <- readFile fp
|
|
|
|
| Left err => fail (show err)
|
2021-12-16 21:23:18 +03:00
|
|
|
pure $ Just $ { onlyNames $= ((lines only) ++) } opts
|
2020-10-19 11:26:23 +03:00
|
|
|
|
|
|
|
||| Normalise strings between different OS.
|
|
|
|
|||
|
|
|
|
||| on Windows, we just ignore backslashes and slashes when comparing,
|
|
|
|
||| similarity up to that is good enough. Leave errors that depend
|
|
|
|
||| on the confusion of slashes and backslashes to unix machines.
|
|
|
|
normalize : String -> String
|
|
|
|
normalize str =
|
|
|
|
if isWindows
|
|
|
|
then pack $ filter (\ch => ch /= '/' && ch /= '\\') (unpack str)
|
|
|
|
else str
|
|
|
|
|
2021-05-11 11:46:21 +03:00
|
|
|
||| The result of a test run
|
|
|
|
||| `Left` corresponds to a failure, and `Right` to a success
|
|
|
|
Result : Type
|
|
|
|
Result = Either String String
|
|
|
|
|
2020-10-19 11:26:23 +03:00
|
|
|
||| Run the specified Golden test with the supplied options.
|
|
|
|
||| See the module documentation for more information.
|
2021-03-09 21:27:05 +03:00
|
|
|
||| @testPath the directory that contains the test.
|
2020-10-19 11:26:23 +03:00
|
|
|
export
|
2022-02-02 14:17:10 +03:00
|
|
|
runTest : Options -> (testPath : String) -> IO Result
|
|
|
|
runTest opts testPath = do
|
2021-06-21 19:30:11 +03:00
|
|
|
start <- clockTime UTC
|
2021-06-22 00:12:17 +03:00
|
|
|
let cg = maybe "" (" --cg " ++) (codegen opts)
|
|
|
|
let exe = "\"" ++ exeUnderTest opts ++ cg ++ "\""
|
2021-10-29 18:38:32 +03:00
|
|
|
ignore $ system $ "cd " ++ escapeArg testPath ++ " && " ++
|
2021-06-22 00:12:17 +03:00
|
|
|
"sh ./run " ++ exe ++ " | tr -d '\\r' > output"
|
2021-06-21 19:30:11 +03:00
|
|
|
end <- clockTime UTC
|
2020-12-07 23:06:19 +03:00
|
|
|
|
|
|
|
Right out <- readFile $ testPath ++ "/output"
|
2021-06-14 14:18:21 +03:00
|
|
|
| Left err => do putStrLn $ (testPath ++ "/output") ++ ": " ++ show err
|
2021-05-11 11:46:21 +03:00
|
|
|
pure (Left testPath)
|
2020-12-07 23:06:19 +03:00
|
|
|
|
|
|
|
Right exp <- readFile $ testPath ++ "/expected"
|
|
|
|
| Left FileNotFound => do
|
|
|
|
if interactive opts
|
|
|
|
then mayOverwrite Nothing out
|
2021-06-14 14:18:21 +03:00
|
|
|
else putStrLn $ (testPath ++ "/expected") ++ ": " ++ show FileNotFound
|
2021-05-11 11:46:21 +03:00
|
|
|
pure (Left testPath)
|
2021-06-14 14:18:21 +03:00
|
|
|
| Left err => do putStrLn $ (testPath ++ "/expected") ++ ": " ++ show err
|
2021-05-11 11:46:21 +03:00
|
|
|
pure (Left testPath)
|
2020-12-07 23:06:19 +03:00
|
|
|
|
|
|
|
let result = normalize out == normalize exp
|
|
|
|
let time = timeDifference end start
|
|
|
|
|
|
|
|
if result
|
2021-11-12 01:00:50 +03:00
|
|
|
then printTiming opts.timing time testPath $ maybeColored BrightGreen "success"
|
2020-12-07 23:06:19 +03:00
|
|
|
else do
|
2021-11-12 01:00:50 +03:00
|
|
|
printTiming opts.timing time testPath $ maybeColored BrightRed "FAILURE"
|
2020-12-07 23:06:19 +03:00
|
|
|
if interactive opts
|
|
|
|
then mayOverwrite (Just exp) out
|
2021-07-17 16:54:23 +03:00
|
|
|
else putStr . unlines $ expVsOut exp out
|
2020-12-07 23:06:19 +03:00
|
|
|
|
2021-05-11 11:46:21 +03:00
|
|
|
pure $ if result then Right testPath else Left testPath
|
2020-12-07 23:06:19 +03:00
|
|
|
|
|
|
|
where
|
|
|
|
getAnswer : IO Bool
|
|
|
|
getAnswer = do
|
|
|
|
str <- getLine
|
|
|
|
case str of
|
|
|
|
"y" => pure True
|
|
|
|
"n" => pure False
|
2021-07-17 10:59:52 +03:00
|
|
|
"N" => pure False
|
2021-06-21 19:30:11 +03:00
|
|
|
"" => pure False
|
|
|
|
_ => do putStrLn "Invalid answer."
|
2020-12-07 23:06:19 +03:00
|
|
|
getAnswer
|
|
|
|
|
2021-11-12 01:00:50 +03:00
|
|
|
maybeColored : Color -> String -> String
|
|
|
|
maybeColored c = if opts.color then show . colored c else id
|
|
|
|
|
2020-12-07 23:06:19 +03:00
|
|
|
expVsOut : String -> String -> List String
|
2021-11-12 01:00:50 +03:00
|
|
|
expVsOut exp out = ["Expected:", maybeColored Green exp, "Given:", maybeColored Red out]
|
2020-12-07 23:06:19 +03:00
|
|
|
|
2021-12-23 00:33:37 +03:00
|
|
|
badSystemExitCode : Int -> Bool
|
|
|
|
badSystemExitCode code = code < 0 || code == 127 {- 127 means shell couldn't start -}
|
|
|
|
|
2020-12-07 23:06:19 +03:00
|
|
|
mayOverwrite : Maybe String -> String -> IO ()
|
|
|
|
mayOverwrite mexp out = do
|
|
|
|
case mexp of
|
|
|
|
Nothing => putStr $ unlines
|
|
|
|
[ "Golden value missing. I computed the following result:"
|
2021-11-12 01:00:50 +03:00
|
|
|
, maybeColored BrightBlue out
|
2021-06-21 19:30:11 +03:00
|
|
|
, "Accept new golden value? [y/N]"
|
2020-12-07 23:06:19 +03:00
|
|
|
]
|
|
|
|
Just exp => do
|
2022-02-18 14:29:52 +03:00
|
|
|
code <- system $ "git diff --no-index --exit-code --word-diff-regex=. " ++
|
|
|
|
(if opts.color then "--color " else "") ++
|
2021-10-29 18:38:32 +03:00
|
|
|
escapeArg testPath ++ "/expected " ++ escapeArg testPath ++ "/output"
|
2021-07-17 16:54:23 +03:00
|
|
|
putStr . unlines $
|
2020-12-07 23:06:19 +03:00
|
|
|
["Golden value differs from actual value."] ++
|
2021-12-23 00:33:37 +03:00
|
|
|
(if badSystemExitCode code then expVsOut exp out else []) ++
|
2021-06-21 19:30:11 +03:00
|
|
|
["Accept actual value as new golden value? [y/N]"]
|
2021-02-22 06:14:09 +03:00
|
|
|
b <- getAnswer
|
|
|
|
when b $ do Right _ <- writeFile (testPath ++ "/expected") out
|
2021-06-14 14:18:21 +03:00
|
|
|
| Left err => putStrLn $ (testPath ++ "/expected") ++ ": " ++ show err
|
2021-02-22 06:14:09 +03:00
|
|
|
pure ()
|
2020-10-19 11:26:23 +03:00
|
|
|
|
2021-06-24 18:33:34 +03:00
|
|
|
printTiming : Bool -> Clock type -> String -> String -> IO ()
|
|
|
|
printTiming False _ path msg = putStrLn $ concat [path, ": ", msg]
|
|
|
|
printTiming True clock path msg =
|
2021-06-21 19:30:11 +03:00
|
|
|
let time = showTime 2 3 clock
|
2021-06-24 18:33:34 +03:00
|
|
|
-- We use 9 instead of `String.length msg` because:
|
|
|
|
-- 1. ": success" and ": FAILURE" have the same length
|
|
|
|
-- 2. ANSI escape codes make the msg look longer than it is
|
|
|
|
spent = String.length time + String.length path + 9
|
2021-06-21 19:30:11 +03:00
|
|
|
pad = pack $ replicate (minus 72 spent) ' '
|
2021-06-24 18:33:34 +03:00
|
|
|
in putStrLn $ concat [path, ": ", msg, pad, time]
|
2020-10-19 11:26:23 +03:00
|
|
|
|
|
|
|
||| Find the first occurrence of an executable on `PATH`.
|
|
|
|
export
|
|
|
|
pathLookup : List String -> IO (Maybe String)
|
|
|
|
pathLookup names = do
|
|
|
|
path <- getEnv "PATH"
|
2021-04-08 23:11:34 +03:00
|
|
|
let extensions = if isWindows then [".exe", ".cmd", ".bat", ""] else [""]
|
2020-10-19 11:26:23 +03:00
|
|
|
let pathList = forget $ split (== pathSeparator) $ fromMaybe "/usr/bin:/usr/local/bin" path
|
2021-04-08 23:11:34 +03:00
|
|
|
let candidates = [p ++ "/" ++ x ++ y | p <- pathList,
|
|
|
|
x <- names,
|
|
|
|
y <- extensions]
|
2020-10-19 11:26:23 +03:00
|
|
|
firstExists candidates
|
|
|
|
|
|
|
|
|
|
|
|
||| Some test may involve Idris' backends and have requirements.
|
|
|
|
||| We define here the ones supported by Idris
|
|
|
|
public export
|
2021-03-09 21:27:05 +03:00
|
|
|
data Requirement = C | Chez | Node | Racket | Gambit
|
2020-10-19 11:26:23 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
Show Requirement where
|
2021-03-09 21:27:05 +03:00
|
|
|
show C = "C"
|
2020-10-19 11:26:23 +03:00
|
|
|
show Chez = "Chez"
|
|
|
|
show Node = "node"
|
|
|
|
show Racket = "racket"
|
2021-03-09 21:27:05 +03:00
|
|
|
show Gambit = "gambit"
|
2020-10-19 11:26:23 +03:00
|
|
|
|
2021-06-22 00:12:17 +03:00
|
|
|
export
|
|
|
|
[CG] Show Requirement where
|
|
|
|
show C = "refc"
|
|
|
|
show Chez = "chez"
|
|
|
|
show Node = "node"
|
|
|
|
show Racket = "racket"
|
|
|
|
show Gambit = "gambit"
|
|
|
|
|
2020-10-19 11:26:23 +03:00
|
|
|
export
|
|
|
|
checkRequirement : Requirement -> IO (Maybe String)
|
|
|
|
checkRequirement req
|
2021-04-12 15:52:04 +03:00
|
|
|
= if platformSupport req
|
|
|
|
then do let (envvar, paths) = requirement req
|
|
|
|
Just exec <- getEnv envvar | Nothing => pathLookup paths
|
|
|
|
pure (Just exec)
|
|
|
|
else pure Nothing
|
2020-10-19 11:26:23 +03:00
|
|
|
where
|
|
|
|
requirement : Requirement -> (String, List String)
|
2021-03-09 21:27:05 +03:00
|
|
|
requirement C = ("CC", ["cc"])
|
2021-09-14 14:07:44 +03:00
|
|
|
requirement Chez = ("CHEZ", ["chez", "chezscheme9.5", "chezscheme", "chez-scheme", "scheme"])
|
2020-10-19 11:26:23 +03:00
|
|
|
requirement Node = ("NODE", ["node"])
|
|
|
|
requirement Racket = ("RACKET", ["racket"])
|
2021-03-09 21:27:05 +03:00
|
|
|
requirement Gambit = ("GAMBIT", ["gsc"])
|
2021-04-12 15:52:04 +03:00
|
|
|
platformSupport : Requirement -> Bool
|
|
|
|
platformSupport C = not isWindows
|
|
|
|
platformSupport Racket = not isWindows
|
|
|
|
platformSupport _ = True
|
2020-10-19 11:26:23 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
findCG : IO (Maybe String)
|
|
|
|
findCG
|
|
|
|
= do Nothing <- getEnv "IDRIS2_TESTS_CG" | p => pure p
|
|
|
|
Nothing <- checkRequirement Chez | p => pure (Just "chez")
|
|
|
|
Nothing <- checkRequirement Node | p => pure (Just "node")
|
|
|
|
Nothing <- checkRequirement Racket | p => pure (Just "racket")
|
2021-03-09 21:27:05 +03:00
|
|
|
Nothing <- checkRequirement Gambit | p => pure (Just "gsc")
|
2021-02-02 18:31:30 +03:00
|
|
|
Nothing <- checkRequirement C | p => pure (Just "refc")
|
2020-10-19 11:26:23 +03:00
|
|
|
pure Nothing
|
|
|
|
|
2021-06-24 18:17:17 +03:00
|
|
|
||| A choice of a codegen
|
|
|
|
public export
|
|
|
|
data Codegen
|
|
|
|
= ||| Do NOT pass a cg argument to the executable being tested
|
|
|
|
Nothing
|
|
|
|
| ||| Use whatever the test runner was passed at the toplevel,
|
|
|
|
||| and if nothing was passed guess a sensible default using findCG
|
|
|
|
Default
|
|
|
|
| ||| Use exactly the given requirement
|
|
|
|
Just Requirement
|
|
|
|
|
|
|
|
export
|
|
|
|
toList : Codegen -> List Requirement
|
|
|
|
toList (Just r) = [r]
|
|
|
|
toList _ = []
|
|
|
|
|
2020-10-19 11:26:23 +03:00
|
|
|
||| A test pool is characterised by
|
2021-06-21 19:30:11 +03:00
|
|
|
||| + a name
|
2020-10-19 11:26:23 +03:00
|
|
|
||| + a list of requirement
|
2021-06-22 00:12:17 +03:00
|
|
|
||| + a choice of codegen (overriding the default)
|
2020-10-19 11:26:23 +03:00
|
|
|
||| + and a list of directory paths
|
|
|
|
public export
|
|
|
|
record TestPool where
|
|
|
|
constructor MkTestPool
|
2021-05-11 11:46:21 +03:00
|
|
|
poolName : String
|
2020-10-19 11:26:23 +03:00
|
|
|
constraints : List Requirement
|
2021-06-24 18:17:17 +03:00
|
|
|
codegen : Codegen
|
2020-10-19 11:26:23 +03:00
|
|
|
testCases : List String
|
|
|
|
|
2021-07-16 19:06:14 +03:00
|
|
|
||| Find all the test in the given directory.
|
|
|
|
export
|
|
|
|
testsInDir : (dirName : String) -> (testNameFilter : String -> Bool) -> (poolName : String) -> List Requirement -> Codegen -> IO TestPool
|
|
|
|
testsInDir dirName testNameFilter poolName reqs cg = do
|
|
|
|
Right names <- listDir dirName
|
|
|
|
| Left e => do putStrLn ("failed to list " ++ dirName ++ ": " ++ show e)
|
|
|
|
exitFailure
|
|
|
|
let names = [n | n <- names, testNameFilter n]
|
|
|
|
let testNames = [dirName ++ "/" ++ n | n <- names]
|
|
|
|
testNames <- filter testNames
|
|
|
|
when (length testNames == 0) $ do
|
|
|
|
putStrLn ("no tests found in " ++ dirName)
|
|
|
|
exitFailure
|
|
|
|
pure $ MkTestPool poolName reqs cg testNames
|
|
|
|
where
|
|
|
|
-- Directory without `run` file is not a test
|
|
|
|
isTest : (path : String) -> IO Bool
|
|
|
|
isTest path = exists (path ++ "/run")
|
|
|
|
|
|
|
|
filter : (testPaths : List String) -> IO (List String)
|
|
|
|
filter [] = pure []
|
|
|
|
filter (p :: ps) =
|
|
|
|
do rem <- filter ps
|
|
|
|
case !(isTest p) of
|
|
|
|
True => pure $ p :: rem
|
|
|
|
False => pure rem
|
|
|
|
|
|
|
|
|
2020-10-19 11:26:23 +03:00
|
|
|
||| Only keep the tests that have been asked for
|
|
|
|
export
|
|
|
|
filterTests : Options -> List String -> List String
|
|
|
|
filterTests opts = case onlyNames opts of
|
|
|
|
[] => id
|
|
|
|
xs => filter (\ name => any (`isInfixOf` name) xs)
|
|
|
|
|
2021-05-11 11:46:21 +03:00
|
|
|
||| The summary of a test pool run
|
|
|
|
public export
|
|
|
|
record Summary where
|
|
|
|
constructor MkSummary
|
|
|
|
success : List String
|
|
|
|
failure : List String
|
|
|
|
|
2022-02-02 14:17:10 +03:00
|
|
|
||| A new, blank summary
|
2021-05-11 11:46:21 +03:00
|
|
|
export
|
|
|
|
initSummary : Summary
|
|
|
|
initSummary = MkSummary [] []
|
|
|
|
|
2022-02-02 14:17:10 +03:00
|
|
|
||| Update the summary to contain the given result
|
2021-05-11 11:46:21 +03:00
|
|
|
export
|
2022-02-02 14:17:10 +03:00
|
|
|
updateSummary : (newRes : Result) -> Summary -> Summary
|
|
|
|
updateSummary newRes =
|
|
|
|
case newRes of
|
|
|
|
Left l => { failure $= (l ::) }
|
|
|
|
Right w => { success $= (w ::) }
|
|
|
|
|
|
|
|
||| Update the summary to contain the given results
|
|
|
|
export
|
|
|
|
bulkUpdateSummary : (newRess : List Result) -> Summary -> Summary
|
|
|
|
bulkUpdateSummary newRess =
|
|
|
|
let (ls, ws) = partitionEithers newRess in
|
2021-05-11 11:46:21 +03:00
|
|
|
{ success $= (ws ++)
|
|
|
|
, failure $= (ls ++)
|
|
|
|
}
|
|
|
|
|
|
|
|
export
|
|
|
|
Semigroup Summary where
|
|
|
|
MkSummary ws1 ls1 <+> MkSummary ws2 ls2
|
|
|
|
= MkSummary (ws1 ++ ws2) (ls1 ++ ls2)
|
|
|
|
|
|
|
|
export
|
|
|
|
Monoid Summary where
|
|
|
|
neutral = initSummary
|
|
|
|
|
2022-02-02 14:17:10 +03:00
|
|
|
||| An instruction to a thread which runs tests
|
|
|
|
public export
|
|
|
|
data ThreadInstruction : Type where
|
|
|
|
||| A test to run
|
|
|
|
Run : (test : String) -> ThreadInstruction
|
|
|
|
||| An indication for the thread to stop
|
|
|
|
Stop : ThreadInstruction
|
|
|
|
|
|
|
|
||| Sends the given tests on the given @Channel@, then sends `nThreads` many
|
|
|
|
||| 'Stop' @ThreadInstruction@s to stop the threads running the tests.
|
|
|
|
|||
|
|
|
|
||| @testChan The channel to send the tests over.
|
|
|
|
||| @nThreads The number of threads being used to run the tests.
|
|
|
|
||| @tests The list of tests to send to the runners/threads.
|
|
|
|
export
|
|
|
|
testSender : (testChan : Channel ThreadInstruction) -> (nThreads : Nat)
|
|
|
|
-> (tests : List String) -> IO ()
|
|
|
|
testSender testChan 0 [] = pure ()
|
|
|
|
testSender testChan (S k) [] =
|
|
|
|
-- out of tests, so the next thing for all the threads is to stop
|
|
|
|
do channelPut testChan Stop
|
|
|
|
testSender testChan k []
|
|
|
|
testSender testChan nThreads (test :: tests) =
|
|
|
|
do channelPut testChan (Run test)
|
|
|
|
testSender testChan nThreads tests
|
|
|
|
|
|
|
|
||| A result from a test-runner/thread
|
|
|
|
public export
|
|
|
|
data ThreadResult : Type where
|
|
|
|
||| The result of running a test
|
|
|
|
Res : (res : Result) -> ThreadResult
|
|
|
|
||| An indication that the thread was told to stop
|
|
|
|
Done : ThreadResult
|
|
|
|
|
|
|
|
||| Receives results on the given @Channel@, accumulating them as a @Summary@.
|
|
|
|
||| When all results have been received (i.e. @nThreads@ many 'Done'
|
|
|
|
||| @ThreadInstruction@s have been encountered), send the resulting Summary over
|
|
|
|
||| the @accChan@ Channel (necessary to be able to @fork@ this function and
|
|
|
|
||| still obtain the Summary at the end).
|
|
|
|
|||
|
|
|
|
||| @resChan The channel to receives the results on.
|
|
|
|
||| @acc The Summary acting as an accumulator.
|
|
|
|
||| @accChan The Channel to send the final Summary over.
|
|
|
|
||| @nThreads The number of threads being used to run the tests.
|
|
|
|
export
|
|
|
|
testReceiver : (resChan : Channel ThreadResult) -> (acc : Summary)
|
|
|
|
-> (accChan : Channel Summary) -> (nThreads : Nat) -> IO ()
|
|
|
|
testReceiver resChan acc accChan 0 = channelPut accChan acc
|
|
|
|
testReceiver resChan acc accChan nThreads@(S k) =
|
|
|
|
do (Res res) <- channelGet resChan
|
|
|
|
| Done => testReceiver resChan acc accChan k
|
|
|
|
testReceiver resChan (updateSummary res acc) accChan nThreads
|
|
|
|
|
|
|
|
||| Function responsible for receiving and running tests.
|
|
|
|
|||
|
|
|
|
||| @opts The options to run the threads under.
|
|
|
|
||| @testChan The Channel to receive tests on.
|
|
|
|
||| @resChan The Channel to send results over.
|
|
|
|
testThread : (opts : Options) -> (testChan : Channel ThreadInstruction)
|
|
|
|
-> (resChan : Channel ThreadResult) -> IO ()
|
|
|
|
testThread opts testChan resChan =
|
|
|
|
do (Run test) <- channelGet testChan
|
|
|
|
| Stop => channelPut resChan Done
|
|
|
|
res <- runTest opts test
|
|
|
|
channelPut resChan (Res res)
|
|
|
|
testThread opts testChan resChan
|
|
|
|
|
|
|
|
||| A runner for a test pool. If there are tests in the @TestPool@ that we want
|
|
|
|
||| to run, spawns `opts.threads` many runners and sends them the tests,
|
|
|
|
||| collecting all the results in the @Summary@ returned at the end.
|
|
|
|
|||
|
|
|
|
||| @opts The options for the TestPool.
|
|
|
|
||| @pool The TestPool to run.
|
2020-10-19 11:26:23 +03:00
|
|
|
export
|
2021-05-11 11:46:21 +03:00
|
|
|
poolRunner : Options -> TestPool -> IO Summary
|
2020-12-07 23:06:19 +03:00
|
|
|
poolRunner opts pool
|
2020-10-19 11:26:23 +03:00
|
|
|
= do -- check that we indeed want to run some of these tests
|
|
|
|
let tests = filterTests opts (testCases pool)
|
|
|
|
let (_ :: _) = tests
|
2021-05-11 11:46:21 +03:00
|
|
|
| [] => pure initSummary
|
2020-10-19 11:26:23 +03:00
|
|
|
-- if so make sure the constraints are satisfied
|
2021-06-22 00:12:17 +03:00
|
|
|
cs <- for (toList (codegen pool) ++ constraints pool) $ \ req => do
|
2020-10-19 11:26:23 +03:00
|
|
|
mfp <- checkRequirement req
|
2021-05-26 19:38:01 +03:00
|
|
|
let msg = case mfp of
|
|
|
|
Nothing => "✗ " ++ show req ++ " not found"
|
|
|
|
Just fp => "✓ Found " ++ show req ++ " at " ++ fp
|
|
|
|
pure (mfp, msg)
|
|
|
|
let (cs, msgs) = unzip cs
|
|
|
|
|
|
|
|
putStrLn (banner msgs)
|
|
|
|
|
2020-10-19 11:26:23 +03:00
|
|
|
let Just _ = the (Maybe (List String)) (sequence cs)
|
2021-05-11 11:46:21 +03:00
|
|
|
| Nothing => pure initSummary
|
2021-06-22 00:12:17 +03:00
|
|
|
|
|
|
|
-- if the test pool requires a specific codegen then use that
|
|
|
|
let opts = case codegen pool of
|
2021-06-24 18:17:17 +03:00
|
|
|
Nothing => { codegen := Nothing } opts
|
2021-06-22 00:12:17 +03:00
|
|
|
Just cg => { codegen := Just (show @{CG} cg) } opts
|
2021-06-24 18:17:17 +03:00
|
|
|
Default => opts
|
2022-02-02 14:17:10 +03:00
|
|
|
|
|
|
|
-- set up the channels
|
|
|
|
accChan <- makeChannel
|
|
|
|
resChan <- makeChannel
|
|
|
|
testChan <- makeChannel
|
|
|
|
|
|
|
|
-- and then run all the tests
|
|
|
|
|
|
|
|
for_ (replicate opts.threads 0) $ \_ =>
|
|
|
|
fork (testThread opts testChan resChan)
|
|
|
|
-- start sending tests
|
|
|
|
senderTID <- fork $ testSender testChan opts.threads tests
|
|
|
|
-- start receiving results
|
|
|
|
receiverTID <- fork $ testReceiver resChan initSummary accChan opts.threads
|
|
|
|
-- wait until things are done, i.e. until we receive the final acc
|
|
|
|
acc <- channelGet accChan
|
|
|
|
pure acc
|
2021-03-15 20:46:41 +03:00
|
|
|
|
|
|
|
where
|
2021-05-11 11:46:21 +03:00
|
|
|
|
2021-05-26 19:38:01 +03:00
|
|
|
separator : String
|
|
|
|
separator = fastPack $ replicate 72 '-'
|
|
|
|
|
|
|
|
banner : List String -> String
|
|
|
|
banner msgs = fastUnlines
|
|
|
|
$ [ "", separator, pool.poolName ]
|
|
|
|
++ msgs
|
2021-07-17 16:54:23 +03:00
|
|
|
++ [ separator ]
|
2021-05-11 11:46:21 +03:00
|
|
|
|
2020-10-19 11:26:23 +03:00
|
|
|
||| A runner for a whole test suite
|
|
|
|
export
|
|
|
|
runner : List TestPool -> IO ()
|
|
|
|
runner tests
|
|
|
|
= do args <- getArgs
|
2021-05-11 11:46:21 +03:00
|
|
|
Just opts <- options args
|
2021-06-24 18:17:17 +03:00
|
|
|
| _ => do printLn args
|
2021-06-21 19:30:11 +03:00
|
|
|
putStrLn usage
|
2020-10-19 11:26:23 +03:00
|
|
|
-- if no CG has been set, find a sensible default based on what is available
|
|
|
|
opts <- case codegen opts of
|
2021-12-16 21:23:18 +03:00
|
|
|
Nothing => pure $ { codegen := !findCG } opts
|
2020-10-19 11:26:23 +03:00
|
|
|
Just _ => pure opts
|
|
|
|
-- run the tests
|
2020-12-07 23:06:19 +03:00
|
|
|
res <- concat <$> traverse (poolRunner opts) tests
|
2021-05-11 11:46:21 +03:00
|
|
|
|
|
|
|
-- report the result
|
|
|
|
let nsucc = length res.success
|
|
|
|
let nfail = length res.failure
|
|
|
|
let ntotal = nsucc + nfail
|
|
|
|
putStrLn (show nsucc ++ "/" ++ show ntotal ++ " tests successful")
|
|
|
|
|
|
|
|
-- deal with failures
|
|
|
|
let list = fastUnlines res.failure
|
|
|
|
when (nfail > 0) $
|
|
|
|
do putStrLn "Failing tests:"
|
2021-07-17 16:54:23 +03:00
|
|
|
putStr list
|
2021-05-11 11:46:21 +03:00
|
|
|
-- always overwrite the failure file, if it is given
|
|
|
|
whenJust opts.failureFile $ \ path =>
|
|
|
|
do Right _ <- writeFile path list
|
|
|
|
| Left err => fail (show err)
|
|
|
|
pure ()
|
|
|
|
|
|
|
|
-- exit
|
|
|
|
if nfail == 0
|
|
|
|
then exitWith ExitSuccess
|
|
|
|
else exitWith (ExitFailure 1)
|