| 1 |
{-# LANGUAGE CPP #-} |
| 2 |
|
| 3 |
-- | Automatic test discovery and runner for the tasty framework. |
| 4 |
module Test.Tasty.Discover.Internal.Driver |
| 5 |
( -- * Main Test Generator |
| 6 |
generateTestDriver |
| 7 |
|
| 8 |
-- * For Testing Purposes Only |
| 9 |
, ModuleTree (..) |
| 10 |
, findTests |
| 11 |
, mkModuleTree |
| 12 |
, showTests |
| 13 |
) where |
| 14 |
|
| 15 |
import Data.List (dropWhileEnd, intercalate, isPrefixOf, nub, sort, stripPrefix) |
| 16 |
import Data.Maybe (fromMaybe) |
| 17 |
import System.FilePath (pathSeparator) |
| 18 |
import System.FilePath.Glob (compile, globDir1, match) |
| 19 |
import System.IO (IOMode (ReadMode), withFile) |
| 20 |
import Test.Tasty.Discover.Internal.Config (Config (..), GlobPattern) |
| 21 |
import Test.Tasty.Discover.Internal.Generator (Generator (..), Test (..), generators, getGenerators, mkTest, showSetup) |
| 22 |
|
| 23 |
import qualified Data.Map.Strict as M |
| 24 |
|
| 25 |
#if defined(mingw32_HOST_OS) |
| 26 |
import GHC.IO.Encoding.CodePage (mkLocaleEncoding) |
| 27 |
import GHC.IO.Encoding.Failure (CodingFailureMode (TransliterateCodingFailure)) |
| 28 |
import GHC.IO.Handle (hGetContents, hSetEncoding) |
| 29 |
#else |
| 30 |
import GHC.IO.Handle (hGetContents) |
| 31 |
#endif |
| 32 |
|
| 33 |
defaultImports :: [String] |
| 34 |
defaultImports = |
| 35 |
[ "import Prelude" |
| 36 |
, "import qualified System.Environment as E" |
| 37 |
, "import qualified Test.Tasty as T" |
| 38 |
, "import qualified Test.Tasty.Ingredients as T" |
| 39 |
] |
| 40 |
|
| 41 |
-- | Main function generator, along with all the boilerplate which |
| 42 |
-- which will run the discovered tests. |
| 43 |
generateTestDriver :: Config -> String -> [String] -> FilePath -> [Test] -> String |
| 44 |
generateTestDriver config modname is src tests = |
| 45 |
let generators' = getGenerators tests |
| 46 |
testNumVars = map (("t"++) . show) [(0 :: Int)..] |
| 47 |
testKindImports = map generatorImports generators' :: [[String]] |
| 48 |
testImports = showImports (map ingredientImport is ++ map testModule tests) :: [String] |
| 49 |
in concat |
| 50 |
[ "{-# LANGUAGE FlexibleInstances #-}\n" |
| 51 |
, "\n" |
| 52 |
, "module " ++ modname ++ " (main, ingredients, tests) where\n" |
| 53 |
, "\n" |
| 54 |
, unlines $ nub $ sort $ mconcat (defaultImports:testKindImports) ++ testImports |
| 55 |
, "\n" |
| 56 |
, "{- HLINT ignore \"Use let\" -}\n" |
| 57 |
, "\n" |
| 58 |
, unlines $ map generatorClass generators' |
| 59 |
, "tests :: IO T.TestTree\n" |
| 60 |
, "tests = do\n" |
| 61 |
, unlines $ zipWith showSetup tests testNumVars |
| 62 |
, " pure $ T.testGroup " ++ show src ++ " [" |
| 63 |
, intercalate "," $ showTests config tests testNumVars |
| 64 |
, "]\n" |
| 65 |
, "ingredients :: [T.Ingredient]\n" |
| 66 |
, "ingredients = " ++ ingredients is ++ "\n" |
| 67 |
, "main :: IO ()\n" |
| 68 |
, "main = do\n" |
| 69 |
, " args <- E.getArgs\n" |
| 70 |
, " E.withArgs (" ++ show (tastyOptions config) ++ " ++ args) $" |
| 71 |
, " tests >>= T.defaultMainWithIngredients ingredients\n" |
| 72 |
] |
| 73 |
|
| 74 |
-- | Match files by specified glob pattern. |
| 75 |
filesByModuleGlob :: FilePath -> Maybe GlobPattern -> IO [String] |
| 76 |
filesByModuleGlob directory globPattern = globDir1 pattern directory |
| 77 |
where pattern = compile ("**/" ++ fromMaybe "*.hs*" globPattern) |
| 78 |
|
| 79 |
-- | Filter and remove files by specified glob pattern. |
| 80 |
ignoreByModuleGlob :: [FilePath] -> Maybe GlobPattern -> [FilePath] |
| 81 |
ignoreByModuleGlob filePaths Nothing = filePaths |
| 82 |
ignoreByModuleGlob filePaths (Just ignoreGlob) = filter (not . match pattern) filePaths |
| 83 |
where pattern = compile ("**/" ++ ignoreGlob) |
| 84 |
|
| 85 |
-- | Discover the tests modules. |
| 86 |
findTests :: Config -> IO [Test] |
| 87 |
findTests config = do |
| 88 |
let directory = searchDir config |
| 89 |
allModules <- filesByModuleGlob directory (modules config) |
| 90 |
let filtered = ignoreByModuleGlob allModules (ignores config) |
| 91 |
-- The files to scan need to be sorted or otherwise the output of |
| 92 |
-- findTests might not be deterministic |
| 93 |
sortedFiltered = sort filtered |
| 94 |
concat <$> traverse (extract directory) sortedFiltered |
| 95 |
where extract directory filePath = |
| 96 |
withFile filePath ReadMode $ \h -> do |
| 97 |
#if defined(mingw32_HOST_OS) |
| 98 |
-- Avoid internal error: hGetContents: invalid argument (invalid byte sequence)' non UTF-8 Windows |
| 99 |
hSetEncoding h $ mkLocaleEncoding TransliterateCodingFailure |
| 100 |
#endif |
| 101 |
tests <- extractTests (dropDirectory directory filePath) <$> hGetContents h |
| 102 |
seq (length tests) (return tests) |
| 103 |
dropDirectory directory filePath = fromMaybe filePath $ |
| 104 |
stripPrefix (directory ++ [pathSeparator]) filePath |
| 105 |
|
| 106 |
-- | Extract the test names from discovered modules. |
| 107 |
extractTests :: FilePath -> String -> [Test] |
| 108 |
extractTests file = mkTestDeDuped . isKnownPrefix . parseTest |
| 109 |
where mkTestDeDuped :: [String] -> [Test] |
| 110 |
mkTestDeDuped = map (mkTest file) . nub |
| 111 |
|
| 112 |
isKnownPrefix :: [String] -> [String] |
| 113 |
isKnownPrefix = filter (\g -> any (checkPrefix g) generators) |
| 114 |
|
| 115 |
checkPrefix :: String -> Generator -> Bool |
| 116 |
checkPrefix g = (`isPrefixOf` g) . generatorPrefix |
| 117 |
|
| 118 |
parseTest :: String -> [String] |
| 119 |
parseTest = map fst . concatMap lex . lines |
| 120 |
|
| 121 |
-- | Show the imports. |
| 122 |
showImports :: [String] -> [String] |
| 123 |
showImports mods = sort $ map ("import qualified " ++) mods |
| 124 |
|
| 125 |
-- | Retrieve the ingredient name. |
| 126 |
ingredientImport :: String -> String |
| 127 |
ingredientImport = init . dropWhileEnd (/= '.') |
| 128 |
|
| 129 |
-- | Ingredients to be included. |
| 130 |
ingredients :: [String] -> String |
| 131 |
ingredients is = concat $ map (++":") is ++ ["T.defaultIngredients"] |
| 132 |
|
| 133 |
-- | Show the tests. |
| 134 |
showTests :: Config -> [Test] -> [String] -> [String] |
| 135 |
showTests config tests testNumVars = if treeDisplay config |
| 136 |
then showModuleTree $ mkModuleTree tests testNumVars |
| 137 |
else zipWith const testNumVars tests |
| 138 |
|
| 139 |
newtype ModuleTree = ModuleTree (M.Map String (ModuleTree, [String])) |
| 140 |
deriving stock (Eq, Show) |
| 141 |
|
| 142 |
showModuleTree :: ModuleTree -> [String] |
| 143 |
showModuleTree (ModuleTree mdls) = map showModule $ M.assocs mdls |
| 144 |
where -- special case, collapse to mdl.submdl |
| 145 |
showModule :: ([Char], (ModuleTree, [String])) -> [Char] |
| 146 |
showModule (mdl, (ModuleTree subMdls, [])) | M.size subMdls == 1 = |
| 147 |
case M.assocs subMdls of |
| 148 |
[(subMdl, (subSubTree, testVars))] -> showModule (mdl ++ '.' : subMdl, (subSubTree, testVars)) |
| 149 |
as -> error $ "Excepted number of submodules != 1. Found " <> show (length as) |
| 150 |
showModule (mdl, (subTree, testVars)) = concat |
| 151 |
[ "T.testGroup \"", mdl |
| 152 |
, "\" [", intercalate "," (showModuleTree subTree ++ testVars), "]" ] |
| 153 |
|
| 154 |
mkModuleTree :: [Test] -> [String] -> ModuleTree |
| 155 |
mkModuleTree tests testVars = ModuleTree $ |
| 156 |
foldr go M.empty $ zipWith (\t tVar -> (testModule t, tVar)) tests testVars |
| 157 |
where go (mdl, tVar) mdls = M.insertWith merge key val mdls |
| 158 |
where (key, val) = case break (== '.') mdl of |
| 159 |
(_, []) -> (mdl, (ModuleTree M.empty, [tVar])) |
| 160 |
(topMdl, '.':subMdl) -> (topMdl, (ModuleTree $ go (subMdl, tVar) M.empty, [])) |
| 161 |
_ -> error "impossible case in mkModuleTree.go.key" |
| 162 |
merge (ModuleTree mdls1, tVars1) (ModuleTree mdls2, tVars2) = |
| 163 |
(ModuleTree $ M.unionWith merge mdls1 mdls2, tVars1 ++ tVars2) |