test-lib 0.4 → 0.5
raw patch · 2 files changed
+61/−8 lines, 2 filesdep ~basedep ~containersdep ~filepathPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, containers, filepath
API changes (from Hackage documentation)
+ TestLib: [optTestFileEnvExts] :: Options -> [String]
- TestLib: Options :: String -> [String] -> Bool -> Bool -> FilePath -> [FilePath] -> Maybe String -> Bool -> [String] -> [String] -> Maybe Config -> Options
+ TestLib: Options :: String -> [String] -> Bool -> Bool -> FilePath -> [FilePath] -> Maybe String -> Bool -> [String] -> [String] -> [String] -> Maybe Config -> Options
Files
- src/TestLib.hs +57/−5
- test-lib.cabal +4/−3
src/TestLib.hs view
@@ -1,14 +1,15 @@ module TestLib (Config(..), mainWith, mainWithOpts, main, Options(..)) where +import Data.Char(isSpace) import SimpleGetOpt import Control.Monad (foldM,when) import System.Directory ( getDirectoryContents,doesDirectoryExist , doesFileExist , createDirectoryIfMissing,canonicalizePath )-import System.Environment (withArgs)+import System.Environment (withArgs, getEnvironment) import System.Info(os) import System.FilePath((</>),(<.>),splitFileName,splitDirectories,takeFileName- , isRelative, pathSeparator, takeExtension )+ , isRelative, pathSeparator, takeExtension, replaceExtension ) import System.Process ( createProcess,CreateProcess(..), StdStream(..) , proc, waitForProcess, readProcessWithExitCode )@@ -91,6 +92,7 @@ , optDiffTool :: Maybe String , optIgnoreExpected :: Bool , optTestFileExts :: [String]+ , optTestFileEnvExts :: [String] , optBinFlags :: [String] -- ^ Add this flags to the binary, followed by the test file , optCfg :: Maybe Config@@ -108,6 +110,7 @@ , optDiffTool = Nothing , optBinFlags = [] , optTestFileExts = []+ , optTestFileEnvExts = [] , optIgnoreExpected = False , optCfg = Nothing }@@ -145,6 +148,14 @@ _ -> '.' : s in Right o { optTestFileExts = e : optTestFileExts o } + , Option "" ["env-ext"]+ "files with this extension set environmental variables"+ $ ReqArg "STRING" $ \s o ->+ let e = case s of+ '.' : _ -> s+ _ -> '.' : s+ in Right o { optTestFileEnvExts = e : optTestFileEnvExts o }+ , Option "" ["version"] "display current version" $ NoArg $ \o -> Right o { optVersion = True }@@ -257,20 +268,61 @@ runBinary opts hout path file = do let bin = optBinary opts args = case optCfg opts of- Just x -> optBinFlags opts ++ cfgBinOpts x file- Nothing -> optBinFlags opts ++ [file]+ Just x -> optBinFlags opts ++ cfgBinOpts x file+ Nothing -> optBinFlags opts ++ [file]+ envVars <- getEnvVars path file (optTestFileEnvExts opts) (_, _, _, ph) <- createProcess (proc bin args) { cwd = Just path , std_out = UseHandle hout , std_in = Inherit , std_err = UseHandle hout+ , env = envVars } _ <- waitForProcess ph return () +getEnvVars :: FilePath -> String -> [String] -> IO (Maybe [(String,String)])+getEnvVars path p es =+ case es of+ [] -> pure Nothing+ e : more ->+ do mb <- getEnvVarsFrom (path </> replaceExtension p e)+ case mb of+ Just ev -> pure (Just ev)+ Nothing -> getEnvVars path p more +-- | Parse the environment variables from a file.+getEnvVarsFrom :: FilePath -> IO (Maybe [(String,String)])+getEnvVarsFrom file =+ do mbTxt <- X.try (readFile file)+ case mbTxt of+ Left X.SomeException {} -> pure Nothing+ Right txt ->+ case parse txt of+ Right as ->+ do es <- getEnvironment+ let new = map fst as+ pure (Just (as ++ [ (x,v) | (x,v) <- es, x `notElem` new ]))+ Left err -> error err + where+ parse = mapM parseLine . filter (not . isBlank) . zip [1 :: Int .. ] . lines + isBlank (_, xs) =+ case dropWhile isSpace xs of+ [] -> True+ '#' : _ -> True+ _ -> False++ trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace+ + parseLine (n,l) =+ case break (== '=') l of+ (as,_:bs) -> pure (trim as, trim bs)+ _ -> Left (unlines [ file ++ ":" ++ show n +++ " Syntax error, expected KEY=VALUE" ])++ -- Test Discovery -------------------------------------------------------------- -- | Directory structure of the discovered tests. Each entry in the map@@ -306,7 +358,7 @@ findTests :: Options -> IO TestFiles findTests opts = searchMany noTests (optTests opts) where- searchMany tests = foldM step tests+ searchMany = foldM step step tests path = do isDir <- doesDirectoryExist path
test-lib.cabal view
@@ -1,5 +1,6 @@+Cabal-version: 3.0 name: test-lib-version: 0.4+version: 0.5 synopsis: A library to make a quick test-runner script. description: This library makes it easy to define an executable, which can find and run a bunch of tests for a binary.@@ -10,13 +11,13 @@ category: Testing build-type: Simple extra-source-files: CHANGELOG.md-cabal-version: >=1.10 library hs-source-dirs: src exposed-modules: TestLib other-modules: Paths_test_lib- build-depends: base >=4.9 && <4.16,+ autogen-modules: Paths_test_lib+ build-depends: base >=4.9 && <4.20, directory >=1.3 && <1.4, filepath >=1.4 && <1.5, process >=1.4 && <1.7,