Debian Codegraph @

src/Test/Tasty/Discover/Internal/Config.hs

raw
1 -- | The test driver configuration options module.
2 --
3 -- Anything that can be passed as an argument to the test driver
4 -- definition exists as a field in the 'Config' type.
5
6 module Test.Tasty.Discover.Internal.Config
7 ( -- * Configuration Options
8 Config (..)
9 , GlobPattern
10
11 -- * Configuration Parser
12 , parseConfig
13
14 -- * Configuration Defaults
15 , defaultConfig
16 ) where
17
18 import Data.Maybe (isJust)
19 import GHC.Generics (Generic)
20 import System.Console.GetOpt (ArgDescr (NoArg, ReqArg), ArgOrder (Permute), OptDescr (Option), getOpt')
21 import System.FilePath ((</>))
22
23 -- | A tasty ingredient.
24 type Ingredient = String
25
26 -- | A glob pattern.
27 type GlobPattern = String
28
29 -- | The discovery and runner configuration.
30 data Config = Config
31 { modules :: Maybe GlobPattern -- ^ Glob pattern for matching modules during test discovery.
32 , moduleSuffix :: Maybe String -- ^ <<<DEPRECATED>>>: Module suffix.
33 , searchDir :: FilePath -- ^ Directory where to look for tests.
34 , generatedModuleName :: Maybe String -- ^ Name of the generated main module.
35 , ignores :: Maybe GlobPattern -- ^ Glob pattern for ignoring modules during test discovery.
36 , ignoredModules :: [FilePath] -- ^ <<<DEPRECATED>>>: Ignored modules by full name.
37 , tastyIngredients :: [Ingredient] -- ^ Tasty ingredients to use.
38 , tastyOptions :: [String] -- ^ Options passed to tasty
39 , inPlace :: Bool -- ^ Whether the source file should be modified in-place.
40 , noModuleSuffix :: Bool -- ^ <<<DEPRECATED>>>: suffix and look in all modules.
41 , debug :: Bool -- ^ Debug the generated module.
42 , treeDisplay :: Bool -- ^ Tree display for the test results table.
43 } deriving stock (Show, Generic)
44
45 -- | The default configuration
46 defaultConfig :: FilePath -> Config
47 defaultConfig theSearchDir = Config Nothing Nothing theSearchDir Nothing Nothing [] [] [] False False False False
48
49 -- | Deprecation message for old `--[no-]module-suffix` option.
50 moduleSuffixDeprecationMessage :: String
51 moduleSuffixDeprecationMessage = error $ concat
52 [ "\n\n"
53 , "----------------------------------------------------------\n"
54 , "DEPRECATION NOTICE: `--[no-]module-suffix` is deprecated.\n"
55 , "The default behaviour now discovers all test module suffixes.\n"
56 , "Please use the `--modules='<glob-pattern>'` option to specify.\n"
57 , "----------------------------------------------------------\n"
58 ]
59
60 -- | Deprecation message for old `--ignore-module` option.
61 ignoreModuleDeprecationMessage :: String
62 ignoreModuleDeprecationMessage = error $ concat
63 [ "\n\n"
64 , "----------------------------------------------------------\n"
65 , "DEPRECATION NOTICE: `--ignore-module` is deprecated.\n"
66 , "Please use the `--ignores='<glob-pattern>'` option instead.\n"
67 , "----------------------------------------------------------\n"
68 ]
69
70 -- | Configuration options parser.
71 parseConfig :: FilePath -> String -> [String] -> Either String Config
72 parseConfig srcDir prog args = case getOpt' Permute (options srcDir) args of
73 (opts, rest, rest', []) ->
74 let config = foldl (flip id) (defaultConfig srcDir) { tastyOptions = rest ++ rest' } opts in
75 if noModuleSuffix config || isJust (moduleSuffix config)
76 then error moduleSuffixDeprecationMessage
77 else if not $ null (ignoredModules config)
78 then error ignoreModuleDeprecationMessage
79 else Right config
80 (_, _, _, err:_) -> formatError err
81 where formatError err = Left (prog ++ ": " ++ err)
82
83 -- | All configuration options.
84 options :: FilePath -> [OptDescr (Config -> Config)]
85 options srcDir =
86 [ Option [] ["modules"]
87 (ReqArg (\s c -> c {modules = Just s}) "GLOB-PATTERN")
88 "Specify desired modules with a glob pattern (white-list)"
89 , Option [] ["module-suffix"]
90 (ReqArg (\s c -> c {moduleSuffix = Just s}) "SUFFIX")
91 "<<<DEPRECATED>>>: Specify desired test module suffix"
92 , Option [] ["search-dir"]
93 (ReqArg (\s c -> c {searchDir = srcDir </> s}) "DIR")
94 "Directory where to look for tests relative to the directory of src. By default, this is the directory of src."
95 , Option [] ["generated-module"]
96 (ReqArg (\s c -> c {generatedModuleName = Just s}) "MODULE")
97 "Qualified generated module name"
98 , Option [] ["ignores"]
99 (ReqArg (\s c -> c {ignores = Just s}) "GLOB-PATTERN")
100 "Specify desired modules to ignore with a glob pattern (black-list)"
101 , Option [] ["ignore-module"]
102 (ReqArg (\s c -> c {ignoredModules = s : ignoredModules c}) "FILE")
103 "<<<DEPRECATED>>>: Ignore a test module"
104 , Option [] ["ingredient"]
105 (ReqArg (\s c -> c {tastyIngredients = s : tastyIngredients c}) "INGREDIENT")
106 "Qualified tasty ingredient name"
107 , Option [] ["in-place"]
108 (NoArg $ \c -> c {inPlace = True})
109 "Whether the source file should be modified in-place"
110 , Option [] ["no-module-suffix"]
111 (NoArg $ \c -> c {noModuleSuffix = True})
112 "<<<DEPRECATED>>>: Ignore test module suffix and import them all"
113 , Option [] ["debug"]
114 (NoArg $ \c -> c {debug = True})
115 "Debug output of generated test module"
116 , Option [] ["tree-display"]
117 (NoArg $ \c -> c {treeDisplay = True})
118 "Display test output hierarchically"
119 ]