|
| 1 | +{-# LANGUAGE LambdaCase #-} |
| 2 | + |
| 3 | +module Main (main) where |
| 4 | + |
| 5 | +import Control.Exception (bracket) |
| 6 | +import Foreign.C.String (peekCString) |
| 7 | +import System.Exit |
| 8 | +import System.Posix.Directory |
| 9 | +import System.Posix.Directory.Internals |
| 10 | +import System.Process (system) |
| 11 | + |
| 12 | +system_x :: String -> IO ExitCode |
| 13 | +system_x = system . ("set -x; " ++) |
| 14 | + |
| 15 | +done :: IO a |
| 16 | +done = system_x "rm -f DirEnt-test" >> exitSuccess |
| 17 | + |
| 18 | +peekDirEnt :: DirEnt -> IO (String, DirType) |
| 19 | +peekDirEnt dirEnt = do |
| 20 | + dName <- dirEntName dirEnt >>= peekCString |
| 21 | + dType <- dirEntType dirEnt |
| 22 | + return (dName, dType) |
| 23 | + |
| 24 | +testDirTypeOfDot :: DirStream -> IO () |
| 25 | +testDirTypeOfDot dirStream = go where |
| 26 | + go = do |
| 27 | + (dName, dType) <- readDirStreamWith peekDirEnt dirStream >>= maybe |
| 28 | + (die "Read cwd in Haskell and didn't find . dir!") |
| 29 | + return |
| 30 | + |
| 31 | + case dName of |
| 32 | + "." -> case dType of |
| 33 | + DirectoryType -> putStrLn "Got DirectoryType for . dir" >> done |
| 34 | + _ -> die $ "Got " ++ show dType ++ " for . dir!" |
| 35 | + _ -> go |
| 36 | + |
| 37 | +main :: IO () |
| 38 | +main = do |
| 39 | + putStrLn "Preparing Haskell test of dirEntType" |
| 40 | + |
| 41 | + system_x "cc --version" >>= \case |
| 42 | + ExitSuccess -> return () |
| 43 | + ec -> exitWith ec |
| 44 | + |
| 45 | + system_x "[ -f tests/DirEnt.c ]" >>= \case |
| 46 | + ExitSuccess -> return () |
| 47 | + ec -> do |
| 48 | + putStrLn "Not running tests from root of repo?" |
| 49 | + exitWith ec |
| 50 | + |
| 51 | + system_x "cc tests/DirEnt.c -o DirEnt-test" >>= \case |
| 52 | + ExitSuccess -> return () |
| 53 | + ExitFailure _ -> do |
| 54 | + putStrLn "d_type not available? Skipping Haskell test" |
| 55 | + done |
| 56 | + |
| 57 | + -- As written, this C code exits with 2 if it determines the Haskell test |
| 58 | + -- for broken dirEntType will be a false positive |
| 59 | + system_x "./DirEnt-test" >>= \case |
| 60 | + ExitSuccess -> return () |
| 61 | + ExitFailure 2 -> putStrLn "Skipping Haskell test" >> done |
| 62 | + ec -> exitWith ec |
| 63 | + |
| 64 | + putStrLn "Running Haskell test of dirEntType" |
| 65 | + |
| 66 | + bracket (openDirStream "/home/steve/foo") closeDirStream testDirTypeOfDot |
0 commit comments