Skip to content

Commit 28c4fa8

Browse files
committed
cabal-testsuite: Add assert{Any,No}FileContains helpers
1 parent 6c955ed commit 28c4fa8

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

cabal-testsuite/src/Test/Cabal/Prelude.hs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import Text.Regex.TDFA ((=~))
5353
import Control.Concurrent.Async (withAsync)
5454
import qualified Data.Aeson as JSON
5555
import qualified Data.ByteString.Lazy as BSL
56-
import Control.Monad (unless, when, void, forM_, liftM2, liftM4)
56+
import Control.Monad (unless, when, void, forM_, foldM, liftM2, liftM4)
5757
import Control.Monad.Catch ( bracket_ )
5858
import Control.Monad.Trans.Reader (asks, withReaderT, runReaderT)
5959
import Control.Monad.IO.Class (MonadIO (..))
@@ -836,6 +836,31 @@ assertFileDoesNotContain path needle =
836836
(assertFailure ("expected: " ++ needle ++ "\n" ++
837837
" in file: " ++ path)))
838838

839+
-- | Assert that at least one of the given paths contains the given search string.
840+
assertAnyFileContains :: MonadIO m => WithCallStack ([FilePath] -> String -> m ())
841+
assertAnyFileContains paths needle = do
842+
let findOne found path =
843+
if found
844+
then pure found
845+
else withFileContents path $ \contents ->
846+
pure $! needle `isInfixOf` contents
847+
foundNeedle <- liftIO $ foldM findOne False paths
848+
withFrozenCallStack $
849+
unless foundNeedle $
850+
assertFailure $
851+
"expected: " <>
852+
needle <>
853+
"\nin one of:\n" <>
854+
unlines (map ("* " <>) paths)
855+
856+
-- | Assert that none of the given paths contains the given search string.
857+
assertNoFileContains :: MonadIO m => WithCallStack ([FilePath] -> String -> m ())
858+
assertNoFileContains paths needle =
859+
liftIO $
860+
forM_ paths $
861+
\path ->
862+
assertFileDoesNotContain path needle
863+
839864
-- | Replace line breaks with spaces, correctly handling "\r\n".
840865
concatOutput :: String -> String
841866
concatOutput = unwords . lines . filter ((/=) '\r')
@@ -857,13 +882,16 @@ matchGlob root glob = do
857882
liftIO $ Glob.globDir1 (Glob.compile glob) root
858883

859884
-- | Assert that a glob matches at least one path in the given root directory.
860-
assertGlobMatches :: MonadIO m => WithCallStack (FilePath -> String -> m ())
885+
--
886+
-- The matched paths are returned for further validation.
887+
assertGlobMatches :: MonadIO m => WithCallStack (FilePath -> String -> m [FilePath])
861888
assertGlobMatches root glob = do
862889
results <- matchGlob root glob
863890
withFrozenCallStack $
864891
when (null results) $
865892
assertFailure $
866893
"Expected glob " <> show glob <> " to match in " <> show root
894+
pure results
867895

868896
-- | Assert that a glob matches no paths in the given root directory.
869897
assertGlobDoesNotMatch :: MonadIO m => WithCallStack (FilePath -> String -> m ())
@@ -882,7 +910,9 @@ assertGlobDoesNotMatch root glob = do
882910
-- | Assert that a glob matches a path in the given root directory.
883911
--
884912
-- The root directory is determined from the `TestEnv` with a function like `testDistDir`.
885-
assertGlobMatchesTestDir :: WithCallStack ((TestEnv -> FilePath) -> String -> TestM ())
913+
--
914+
-- The matched paths are returned for further validation.
915+
assertGlobMatchesTestDir :: WithCallStack ((TestEnv -> FilePath) -> String -> TestM [FilePath])
886916
assertGlobMatchesTestDir rootSelector glob = do
887917
root <- asks rootSelector
888918
assertGlobMatches root glob

0 commit comments

Comments
 (0)