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