Make Markdown example a code block
[cabal.git] / Cabal-tests / tests / UnitTests / Distribution / Compat / Time.hs
blobdb656db0be08a2f7d37d433c7fe9816cd3318fd2
1 module UnitTests.Distribution.Compat.Time (tests) where
3 import Control.Concurrent (threadDelay)
4 import System.FilePath
6 import Distribution.Simple.Utils (withTempDirectory)
7 import Distribution.Verbosity
9 import Distribution.Compat.Time
11 import Test.Tasty
12 import Test.Tasty.HUnit
14 tests :: Int -> [TestTree]
15 tests mtimeChange =
16 [ testCase "getModTime has sub-second resolution" $ getModTimeTest mtimeChange
17 , testCase "getCurTime works as expected" $ getCurTimeTest mtimeChange
20 getModTimeTest :: Int -> Assertion
21 getModTimeTest mtimeChange =
22 withTempDirectory silent "." "getmodtime-" $ \dir -> do
23 let fileName = dir </> "foo"
24 writeFile fileName "bar"
25 t0 <- getModTime fileName
26 threadDelay mtimeChange
27 writeFile fileName "baz"
28 t1 <- getModTime fileName
29 assertBool "expected different file mtimes" (t1 > t0)
32 getCurTimeTest :: Int -> Assertion
33 getCurTimeTest mtimeChange =
34 withTempDirectory silent "." "getmodtime-" $ \dir -> do
35 let fileName = dir </> "foo"
36 writeFile fileName "bar"
37 t0 <- getModTime fileName
38 threadDelay mtimeChange
39 t1 <- getCurTime
40 assertBool("expected file mtime (" ++ show t0
41 ++ ") to be earlier than current time (" ++ show t1 ++ ")")
42 (t0 < t1)
44 threadDelay mtimeChange
45 writeFile fileName "baz"
46 t2 <- getModTime fileName
47 assertBool ("expected current time (" ++ show t1
48 ++ ") to be earlier than file mtime (" ++ show t2 ++ ")")
49 (t1 < t2)