Skip to content

Commit

Permalink
Simplify the test tree
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjm committed Oct 25, 2024
1 parent 171b455 commit ec3cb7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
27 changes: 9 additions & 18 deletions test/FSNotify/Test/EventTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@ eventTests' :: (
MonadUnliftIO m, MonadThrow m
) => Int -> ThreadingMode -> Bool -> Bool -> Bool -> SpecFree context m ()
eventTests' timeInterval threadingMode poll recursive nested = do
let itWithFolder name action = introduceTestFolder timeInterval threadingMode poll recursive nested $ it name action
let withFolder = withTestFolder timeInterval threadingMode poll recursive nested

unless (nested || poll || isMac || isWin) $ itWithFolder "deletes the watched directory" $ do
TestFolderContext watchedDir _f getEvents _clearEvents <- getContext testFolderContext
unless (nested || poll || isMac || isWin) $ it "deletes the watched directory" $ withFolder $ \(TestFolderContext watchedDir _f getEvents _clearEvents) -> do
removeDirectory watchedDir

pauseAndRetryOnExpectationFailure timeInterval 3 $ liftIO getEvents >>= \case
[WatchedDirectoryRemoved {..}] | eventPath `equalFilePath` watchedDir && eventIsDirectory == IsDirectory -> return ()
events -> expectationFailure $ "Got wrong events: " <> show events

itWithFolder "works with a new file" $ do
TestFolderContext _watchedDir f getEvents _clearEvents <- getContext testFolderContext

it "works with a new file" $ withFolder $ \(TestFolderContext _watchedDir f getEvents _clearEvents) -> do
let wrapper action = if | isWin -> liftIO (writeFile f "foo") >> action
| otherwise -> withFile f AppendMode $ \_ -> action

Expand All @@ -68,8 +65,7 @@ eventTests' timeInterval threadingMode poll recursive nested = do
[Added {..}] | eventPath `equalFilePath` f && eventIsDirectory == IsFile -> return ()
_ -> expectationFailure $ "Got wrong events: " <> show events

itWithFolder "works with a new directory" $ do
TestFolderContext _watchedDir f getEvents _clearEvents <- getContext testFolderContext
it "works with a new directory" $ withFolder $ \(TestFolderContext _watchedDir f getEvents _clearEvents) -> do
createDirectory f

pauseAndRetryOnExpectationFailure timeInterval 3 $ liftIO getEvents >>= \events ->
Expand All @@ -78,8 +74,7 @@ eventTests' timeInterval threadingMode poll recursive nested = do
[Added {..}] | eventPath `equalFilePath` f && eventIsDirectory == IsDirectory -> return ()
_ -> expectationFailure $ "Got wrong events: " <> show events

itWithFolder "works with a deleted file" $ do
TestFolderContext _watchedDir f getEvents clearEvents <- getContext testFolderContext
it "works with a deleted file" $ withFolder $ \(TestFolderContext _watchedDir f getEvents clearEvents) -> do
liftIO (writeFile f "" >> clearEvents)

removeFile f
Expand All @@ -90,8 +85,7 @@ eventTests' timeInterval threadingMode poll recursive nested = do
[Removed {..}] | eventPath `equalFilePath` f && eventIsDirectory == IsFile -> return ()
_ -> expectationFailure $ "Got wrong events: " <> show events

itWithFolder "works with a deleted directory" $ do
TestFolderContext _watchedDir f getEvents clearEvents <- getContext testFolderContext
it "works with a deleted directory" $ withFolder $ \(TestFolderContext _watchedDir f getEvents clearEvents) -> do
createDirectory f >> liftIO clearEvents

removeDirectory f
Expand All @@ -102,8 +96,7 @@ eventTests' timeInterval threadingMode poll recursive nested = do
[Removed {..}] | eventPath `equalFilePath` f && eventIsDirectory == IsDirectory -> return ()
_ -> expectationFailure $ "Got wrong events: " <> show events

itWithFolder "works with modified file attributes" $ do
TestFolderContext _watchedDir f getEvents clearEvents <- getContext testFolderContext
it "works with modified file attributes" $ withFolder $ \(TestFolderContext _watchedDir f getEvents clearEvents) -> do
liftIO (writeFile f "" >> clearEvents)

liftIO $ changeFileAttributes f
Expand All @@ -120,8 +113,7 @@ eventTests' timeInterval threadingMode poll recursive nested = do
[ModifiedAttributes {..}] | eventPath `equalFilePath` f && eventIsDirectory == IsFile -> return ()
_ -> expectationFailure $ "Got wrong events: " <> show events

itWithFolder "works with a modified file" $ do
TestFolderContext _watchedDir f getEvents clearEvents <- getContext testFolderContext
it "works with a modified file" $ withFolder $ \(TestFolderContext _watchedDir f getEvents clearEvents) -> do
liftIO (writeFile f "" >> clearEvents)

(if isWin then withSingleWriteFile f "foo" else withOpenWritableAndWrite f "foo") $
Expand All @@ -136,8 +128,7 @@ eventTests' timeInterval threadingMode poll recursive nested = do
_ -> expectationFailure $ "Got wrong events: " <> show events <> " (wanted file path " <> show f <> ")"

when isLinux $ unless poll $
itWithFolder "gets a close_write" $ do
TestFolderContext _watchedDir f getEvents clearEvents <- getContext testFolderContext
it "gets a close_write" $ withFolder $ \(TestFolderContext _watchedDir f getEvents clearEvents) -> do
liftIO (writeFile f "" >> clearEvents)
liftIO $ withFile f WriteMode $ flip hPutStr "asdf"
pauseAndRetryOnExpectationFailure timeInterval 3 $ liftIO getEvents >>= \events ->
Expand Down
16 changes: 0 additions & 16 deletions test/FSNotify/Test/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,6 @@ data TestFolderContext = TestFolderContext {
, clearEvents :: IO ()
}

testFolderContext :: Label "testFolderContext" TestFolderContext
testFolderContext = Label :: Label "testFolderContext" TestFolderContext

introduceTestFolder :: (
MonadUnliftIO m
)
=> Int
-> ThreadingMode
-> Bool
-> Bool
-> Bool
-> SpecFree (LabelValue "testFolderContext" TestFolderContext :> context) m ()
-> SpecFree context m ()
introduceTestFolder timeInterval threadingMode poll recursive nested = introduceWith "Make test folder" testFolderContext $ \action ->
withTestFolder timeInterval threadingMode poll recursive nested (void . action)

withTestFolder :: (
MonadUnliftIO m, MonadLogger m
)
Expand Down

0 comments on commit ec3cb7e

Please sign in to comment.