module Language.Haskell.LSP.Test.Server (withServer) where
import Control.Concurrent
+import Control.Exception
import Control.Monad
-import Data.Maybe
+import Language.Haskell.LSP.Test.Compat
import System.IO
import System.Process
-withServer :: String -> (Handle -> Handle -> Int -> IO a) -> IO a
-withServer serverExe f = do
+withServer :: String -> Bool -> (Handle -> Handle -> Int -> IO a) -> IO a
+withServer serverExe logStdErr f = do
-- TODO Probably should just change runServer to accept
-- separate command and arguments
let cmd:args = words serverExe
-- Need to continuously consume to stderr else it gets blocked
-- Can't pass NoStream either to std_err
hSetBuffering serverErr NoBuffering
- errSinkThread <- forkIO $ forever $ hGetLine serverErr
+ errSinkThread <- forkIO $ forever $ hGetLine serverErr >>= when logStdErr . putStrLn
- pid <- fromIntegral . fromJust <$> getPid serverProc
-
- result <- f serverIn serverOut pid
+ pid <- getProcessID serverProc
+ finally (f serverIn serverOut pid) $ do
killThread errSinkThread
terminateProcess serverProc
- return result