Skip to content

Commit

Permalink
Make tests into cabal tests.
Browse files Browse the repository at this point in the history
Test with 'cabal test'.
  • Loading branch information
kolmodin committed May 11, 2014
1 parent 3f9e9b1 commit b17f5fa
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 54 deletions.
37 changes: 36 additions & 1 deletion hinotify.cabal
Expand Up @@ -15,7 +15,7 @@ license-file: LICENSE
author: Lennart Kolmodin
maintainer: Lennart Kolmodin <kolmodin@gmail.com>
extra-source-files: README.md
cabal-version: >= 1.6
cabal-version: >= 1.8

source-repository head
type: git
Expand All @@ -40,3 +40,38 @@ library
ghc-options: -Wall

hs-source-dirs: src

test-suite test001
type: exitcode-stdio-1.0
build-depends: base, directory, hinotify
hs-source-dirs: src tests
main-is: test001-list-dir-contents.hs
ghc-options: -Wall

test-suite test002
type: exitcode-stdio-1.0
build-depends: base, directory, hinotify
hs-source-dirs: src tests
main-is: test002-writefile.hs
ghc-options: -Wall

test-suite test003
type: exitcode-stdio-1.0
build-depends: base, directory, hinotify
hs-source-dirs: src tests
main-is: test003-removefile.hs
ghc-options: -Wall

test-suite test004
type: exitcode-stdio-1.0
build-depends: base, directory, hinotify
hs-source-dirs: src tests
main-is: test004-modify-file.hs
ghc-options: -Wall

test-suite test005
type: exitcode-stdio-1.0
build-depends: base, directory, hinotify
hs-source-dirs: src tests
main-is: test005-move-file.hs
ghc-options: -Wall
25 changes: 15 additions & 10 deletions tests/Utils.hs
Expand Up @@ -9,31 +9,35 @@ import System.Exit

import System.INotify

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

withTempDir :: (String -> IO a) -> IO a
withTempDir f = do
path <- testName
bracket
( createDirectory path >> return path )
( removeDirectoryRecursive )
( f )

withWatch :: INotify -> [EventVariety] -> FilePath -> (Event -> IO ()) -> IO a -> IO a
withWatch inot events path action f =
bracket
( addWatch inot events path action )
removeWatch
( const f )

inTestEnviron :: [EventVariety] -> (String -> IO a) -> ([Event] -> IO b) -> IO b
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
_ <- action testPath
events' <- getChanContents chan
f events'

(~=) :: Eq a => [a] -> [a] -> Bool
[] ~= _ = True
Expand All @@ -43,13 +47,14 @@ _ ~= _ = 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
explainFailure :: Show a => [a] -> [a] -> String
explainFailure expected reality = unlines $
[ "Expected:" ] ++
[ "> " ++ show x | x <- expected ] ++
[ "But got:" ] ++
[ "< " ++ show x | x <- asMany expected reality ]

testFailure, testSuccess :: IO a
testFailure = exitFailure

testSuccess = exitWith ExitSuccess

31 changes: 0 additions & 31 deletions tests/test-all

This file was deleted.

5 changes: 4 additions & 1 deletion tests/test001-list-dir-contents.hs
Expand Up @@ -8,12 +8,15 @@ import System.INotify as INotify

import Utils

main :: IO ()
main =
inTestEnviron [Open, Close] getDirectoryContents $ \ events -> do
when (expected ~= events)
testSuccess
explainFailure expected events
putStrLn $ explainFailure expected events
testFailure

expected :: [Event]
expected =
[ Opened True Nothing
, Closed True Nothing False
Expand Down
6 changes: 5 additions & 1 deletion tests/test002-writefile.hs
Expand Up @@ -6,16 +6,20 @@ import System.INotify as INotify

import Utils

write :: String -> IO ()
write path = do
writeFile (path ++ "/hello") ""
-- actually writing any contents gives me two Modified

main :: IO ()
main =
inTestEnviron [AllEvents] write $ \ events -> do
when (expected ~= events)
testSuccess
explainFailure expected events
putStrLn $ explainFailure expected events
testFailure

expected :: [Event]
expected =
[ Created False "hello"
, Opened False (Just "hello")
Expand Down
11 changes: 9 additions & 2 deletions tests/test003-removefile.hs
Expand Up @@ -8,24 +8,31 @@ import System.INotify as INotify

import Utils

file :: String
file = "hello"

write :: String -> IO ()
write path = do
writeFile (path ++ '/':file) ""

remove :: String -> IO ()
remove path = do
removeFile (path ++ '/':file)

action :: String -> IO ()
action path = do
write path
remove path


main :: IO ()
main =
inTestEnviron [AllEvents] action $ \ events -> do
when (expected ~= events)
testSuccess
explainFailure expected events
putStrLn $ explainFailure expected events
testFailure

expected :: [Event]
expected =
[ Created False file
, Opened False (Just file)
Expand Down
12 changes: 10 additions & 2 deletions tests/test004-modify-file.hs
Expand Up @@ -10,31 +10,39 @@ import System.INotify as INotify

import Utils

file :: String
file = "hello"

write :: String -> IO ()
write path = do
writeFile (path ++ '/':file) ""

modify :: String -> IO ()
modify path = do
bracket
(openFile (path ++ '/':file) AppendMode)
(hClose)
(\h -> hPutStr h "yarr!")

remove :: String -> IO ()
remove path = do
removeFile (path ++ '/':file)

action :: String -> IO ()
action path = do
write path
modify path
remove path


main :: IO ()
main =
inTestEnviron [AllEvents] action $ \ events -> do
when (expected ~= events)
testSuccess
explainFailure expected events
putStrLn $ explainFailure expected events
testFailure

expected :: [Event]
expected =
[ Created False file
, Opened False (Just file)
Expand Down
16 changes: 10 additions & 6 deletions tests/test005-move-file.hs
@@ -1,40 +1,44 @@
module Main where

import Data.Maybe

import Control.Monad

import System.Directory
import System.IO

import System.INotify as INotify

import Utils

file, file2 :: String
file = "hello"
file2 = file ++ "2"

write :: String -> IO ()
write path = do
writeFile (path ++ '/':file) ""

move :: String -> IO ()
move path = do
renameFile (path ++ '/':file) (path ++ '/':file2)

remove :: String -> IO ()
remove path = do
removeFile (path ++ '/':file2)

action :: String -> IO ()
action path = do
write path
move path
remove path


main :: IO ()
main =
inTestEnviron [AllEvents] action $ \ events -> do
let cookie = head [ c | MovedOut _ _ c <- events ]
when (expected cookie ~= events)
testSuccess
explainFailure (expected cookie) events
putStrLn $ explainFailure (expected cookie) events
testFailure

expected :: Cookie -> [Event]
expected cookie =
[ Created False file
, Opened False (Just file)
Expand Down

0 comments on commit b17f5fa

Please sign in to comment.