File: Utils.hs

package info (click to toggle)
haskell-hinotify 0.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 144 kB
  • sloc: haskell: 271; sh: 25; makefile: 6
file content (55 lines) | stat: -rw-r--r-- 1,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Utils where

import Control.Concurrent.Chan
import Control.Exception

import System.Directory
import System.Environment
import System.Exit

import System.INotify

testName = do
    n <- getProgName
    return (n ++ "-playground")

withTempDir f = do
    path <- testName
    bracket
        ( createDirectory path >> return path )
        ( removeDirectoryRecursive )
        ( f )

withWatch inot events path action f =
    bracket
        ( addWatch inot events path action )
        removeWatch
        ( const f )

inTestEnviron events action f = do
    withTempDir $ \testPath -> do
        inot <- initINotify
        chan <- newChan
        withWatch inot events testPath (writeChan chan) $ do
            action testPath
            events <- getChanContents chan
            f events

(~=) :: Eq a => [a] -> [a] -> Bool
[] ~= _ = True
(x:xs) ~= (y:ys) = x == y && xs ~= ys
_ ~= _ = False

asMany :: [a] -> [a] -> [a]
asMany xs ys = take (length xs) ys

explainFailure expected reality = do
    putStrLn "Expected:"
    mapM_ (\x -> putStr "> " >> print x) expected
    putStrLn "But got:"
    mapM_ (\x -> putStr "< " >> print x) (asMany expected reality)
    testFailure

testFailure = exitFailure 

testSuccess = exitWith ExitSuccess