Skip to content

Commit 2d7b84d

Browse files
9999yearsMikolaj
authored andcommitted
Make cabal-testsuite filterable with --pattern
This adds the `Tasty` `-p`/`--pattern` argument to the `cabal-testsuite` tests, making it possible to filter `cabal-testsuite` tests just like the other test suites: ./validate.sh -s build -s cli-suite -p HaddockKeepTmpsCustom
1 parent 6bfdbfd commit 2d7b84d

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

cabal-testsuite/cabal-testsuite.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ executable cabal-tests
105105
-- dependencies specific to exe:cabal-tests
106106
, clock ^>= 0.7.2 || ^>=0.8
107107
, directory
108+
, tasty
109+
, containers
108110

109111
build-tool-depends: cabal-testsuite:setup
110112
default-extensions: TypeOperators

cabal-testsuite/main/cabal-tests.hs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,19 @@ import Control.Exception
2323
import Control.Monad
2424
import GHC.Conc (numCapabilities)
2525
import Data.List
26+
import Data.Proxy (Proxy(Proxy))
27+
import qualified Data.Sequence as Seq (fromList)
2628
import Text.Printf
29+
import qualified Test.Tasty.Options as Tasty
30+
( OptionSet
31+
, OptionDescription (Option)
32+
, lookupOption
33+
)
34+
import qualified Test.Tasty.Runners as Tasty
35+
( optionParser
36+
, TestPattern
37+
, testPatternMatches
38+
)
2739
import qualified System.Clock as Clock
2840
import System.IO
2941
import System.FilePath
@@ -72,7 +84,8 @@ data MainArgs = MainArgs {
7284
mainArgQuiet :: Bool,
7385
mainArgDistDir :: Maybe FilePath,
7486
mainArgCabalSpec :: Maybe CabalLibSpec,
75-
mainCommonArgs :: CommonArgs
87+
mainCommonArgs :: CommonArgs,
88+
mainTastyArgs :: Tasty.OptionSet
7689
}
7790

7891
data CabalLibSpec = BootCabalLib | InTreeCabalLib FilePath FilePath | SpecificCabalLib String FilePath
@@ -117,6 +130,17 @@ mainArgParser = MainArgs
117130
<> metavar "DIR"))
118131
<*> optional cabalLibSpecParser
119132
<*> commonArgParser
133+
<*> tastyArgParser
134+
135+
tastyArgParser :: Parser Tasty.OptionSet
136+
tastyArgParser =
137+
let (warnings, parser) =
138+
Tasty.optionParser
139+
[ Tasty.Option (Proxy @Tasty.TestPattern)
140+
]
141+
in if null warnings
142+
then parser
143+
else error $ unlines ("Failed to create parser for Tasty CLI options:" : warnings)
120144

121145
-- Unpack and build a specific released version of Cabal and Cabal-syntax libraries
122146
buildCabalLibsProject :: String -> Verbosity -> Maybe FilePath -> FilePath -> IO [FilePath]
@@ -184,6 +208,7 @@ main = do
184208
-- Parse arguments. N.B. 'helper' adds the option `--help`.
185209
args <- execParser $ info (mainArgParser <**> helper) mempty
186210
let verbosity = if mainArgVerbose args then verbose else normal
211+
testPattern = Tasty.lookupOption @Tasty.TestPattern (mainTastyArgs args)
187212

188213
pkg_dbs <-
189214
-- Not path to cabal-install so we're not going to run cabal-install tests so we
@@ -264,7 +289,7 @@ main = do
264289
-- NB: getDirectoryContentsRecursive is lazy IO, but it
265290
-- doesn't handle directories disappearing gracefully. Fix
266291
-- this!
267-
(single_tests, multi_tests) <- evaluate (partitionTests test_scripts)
292+
(single_tests, multi_tests) <- evaluate (partitionTests testPattern test_scripts)
268293
let all_tests = multi_tests ++ single_tests
269294
margin = maximum (map length all_tests) + 2
270295
hPutStrLn stderr $ "tests to run: " ++ show (length all_tests)
@@ -381,10 +406,19 @@ main = do
381406
findTests :: IO [FilePath]
382407
findTests = getDirectoryContentsRecursive "."
383408

384-
partitionTests :: [FilePath] -> ([FilePath], [FilePath])
385-
partitionTests = go [] []
409+
-- | Partition a list of paths into a tuple of test paths and multi-test paths.
410+
--
411+
-- Non-test paths and test paths that don't match the given `Tasty.TestPattern` are dropped.
412+
partitionTests :: Tasty.TestPattern -> [FilePath] -> ([FilePath], [FilePath])
413+
partitionTests testPattern paths =
414+
go [] [] paths
386415
where
387-
go ts ms [] = (ts, ms)
416+
-- Filter a list, keeping only paths that match the @pattern@.
417+
keepPatternMatches = filter (Tasty.testPatternMatches testPattern . toTastyPath)
418+
419+
toTastyPath path = Seq.fromList $ splitDirectories path
420+
421+
go ts ms [] = (keepPatternMatches ts, keepPatternMatches ms)
388422
go ts ms (f:fs) =
389423
-- NB: Keep this synchronized with isTestFile
390424
case takeExtensions f of

0 commit comments

Comments
 (0)