aboutsummaryrefslogtreecommitdiff
path: root/haddock-test/src/Test/Haddock/Process.hs
blob: a6cab9ac4529606afe33b572eb08beb7747acf7d (plain) (blame)
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
{-# LANGUAGE RecordWildCards #-}


module Test.Haddock.Process where


import Control.Monad

import System.Exit
import System.IO
import System.Process


type Environment = [(String, String)]


data ProcessConfig = ProcessConfig
    { pcArgs :: [String]
    , pcWorkDir :: Maybe FilePath
    , pcEnv :: Maybe Environment
    , pcStdIn :: Maybe Handle
    , pcStdOut :: Maybe Handle
    , pcStdErr :: Maybe Handle
    }
    deriving (Show)


processConfig :: ProcessConfig
processConfig = ProcessConfig
    { pcArgs = []
    , pcWorkDir = Nothing
    , pcEnv = Nothing
    , pcStdIn = Nothing
    , pcStdOut = Nothing
    , pcStdErr = Nothing
    }


runProcess' :: FilePath -> ProcessConfig -> IO ProcessHandle
runProcess' path (ProcessConfig { .. }) = runProcess
    path pcArgs pcWorkDir pcEnv pcStdIn pcStdOut pcStdErr

-- | Wait for a process to finish running. If it ends up failing, print out the
-- error message.
waitForSuccess :: String -> Handle -> ProcessHandle -> IO Bool
waitForSuccess msg out handle = do
    succeeded <- fmap (== ExitSuccess) $ waitForProcess handle
    unless succeeded $ hPutStrLn out msg
    pure succeeded