@@ -53,7 +53,7 @@ import Text.Regex.TDFA ((=~))
53
53
import Control.Concurrent.Async (withAsync )
54
54
import qualified Data.Aeson as JSON
55
55
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 )
57
57
import Control.Monad.Catch ( bracket_ )
58
58
import Control.Monad.Trans.Reader (asks , withReaderT , runReaderT )
59
59
import Control.Monad.IO.Class (MonadIO (.. ))
@@ -836,6 +836,31 @@ assertFileDoesNotContain path needle =
836
836
(assertFailure (" expected: " ++ needle ++ " \n " ++
837
837
" in file: " ++ path)))
838
838
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
+ " \n in 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
+
839
864
-- | Replace line breaks with spaces, correctly handling "\r\n".
840
865
concatOutput :: String -> String
841
866
concatOutput = unwords . lines . filter ((/=) ' \r ' )
@@ -857,13 +882,16 @@ matchGlob root glob = do
857
882
liftIO $ Glob. globDir1 (Glob. compile glob) root
858
883
859
884
-- | 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 ])
861
888
assertGlobMatches root glob = do
862
889
results <- matchGlob root glob
863
890
withFrozenCallStack $
864
891
when (null results) $
865
892
assertFailure $
866
893
" Expected glob " <> show glob <> " to match in " <> show root
894
+ pure results
867
895
868
896
-- | Assert that a glob matches no paths in the given root directory.
869
897
assertGlobDoesNotMatch :: MonadIO m => WithCallStack (FilePath -> String -> m () )
@@ -882,7 +910,9 @@ assertGlobDoesNotMatch root glob = do
882
910
-- | Assert that a glob matches a path in the given root directory.
883
911
--
884
912
-- 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 ])
886
916
assertGlobMatchesTestDir rootSelector glob = do
887
917
root <- asks rootSelector
888
918
assertGlobMatches root glob
0 commit comments