Commit 5a880c07 authored by Duncan Coutts's avatar Duncan Coutts
Browse files

Add a sanity unit test for dir mtime behaviour

We rely on posix & ntfs directory mtime semantics, which is that a dir
mtime changes if the set of names in the dir changes.

We rely on this both for the meaning of monitorDirectory but also we
rely on it as an optimisation within the glob checking code.

So this test should always pass on posix-compliant file systems.
Apparently it may not hold on FAT file systems.
parent 1f9c6674
......@@ -24,6 +24,7 @@ import Test.Tasty.HUnit
tests :: Int -> [TestTree]
tests mtimeChange =
[ testCase "sanity check mtimes" $ testFileMTimeSanity mtimeChange
, testCase "sanity check dirs" $ testDirChangeSanity mtimeChange
, testCase "no monitor cache" testNoMonitorCache
, testCase "corrupt monitor cache" testCorruptMonitorCache
, testCase "empty monitor" testEmptyMonitor
......@@ -70,6 +71,8 @@ tests mtimeChange =
, testCase "value updated" testValueUpdated
]
-- Check the file system behaves the way we expect it to
-- we rely on file mtimes having a reasonable resolution
testFileMTimeSanity :: Int -> Assertion
testFileMTimeSanity mtimeChange =
......@@ -82,6 +85,62 @@ testFileMTimeSanity mtimeChange =
t2 <- getModTime (dir </> "a")
assertBool "expected different file mtimes" (t2 > t1)
-- We rely on directories changing mtime when entries are added or removed
testDirChangeSanity :: Int -> Assertion
testDirChangeSanity mtimeChange =
withTempDirectory silent "." "dir-mtime-" $ \dir -> do
expectMTimeChange dir "file add" $
IO.writeFile (dir </> "file") "content"
expectMTimeSame dir "file content change" $
IO.writeFile (dir </> "file") "new content"
expectMTimeChange dir "file del" $
IO.removeFile (dir </> "file")
expectMTimeChange dir "subdir add" $
IO.createDirectory (dir </> "dir")
expectMTimeSame dir "subdir file add" $
IO.writeFile (dir </> "dir" </> "file") "content"
expectMTimeChange dir "subdir file move in" $
IO.renameFile (dir </> "dir" </> "file") (dir </> "file")
expectMTimeChange dir "subdir file move out" $
IO.renameFile (dir </> "file") (dir </> "dir" </> "file")
expectMTimeSame dir "subdir dir add" $
IO.createDirectory (dir </> "dir" </> "subdir")
expectMTimeChange dir "subdir dir move in" $
IO.renameDirectory (dir </> "dir" </> "subdir") (dir </> "subdir")
expectMTimeChange dir "subdir dir move out" $
IO.renameDirectory (dir </> "subdir") (dir </> "dir" </> "subdir")
where
expectMTimeChange, expectMTimeSame :: FilePath -> String -> IO ()
-> Assertion
expectMTimeChange dir descr action = do
t <- getModTime dir
threadDelay mtimeChange
action
t' <- getModTime dir
assertBool ("expected dir mtime change on " ++ descr) (t' > t)
expectMTimeSame dir descr action = do
t <- getModTime dir
threadDelay mtimeChange
action
t' <- getModTime dir
assertBool ("expected same dir mtime on " ++ descr) (t' == t)
-- Now for the FileMonitor tests proper...
-- first run, where we don't even call updateMonitor
testNoMonitorCache :: Assertion
testNoMonitorCache =
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment