Permalink
Browse files

Add a test that ensures that ghc --info contains "Have interpreter"

  • Loading branch information...
1 parent ac2d60d commit 334e2c9ab135eafc51682b7439942bfaa4dc43df @sol committed Apr 1, 2013
Showing with 21 additions and 5 deletions.
  1. +13 −4 src/Interpreter.hs
  2. +8 −1 test/InterpreterSpec.hs
View
@@ -5,13 +5,18 @@ module Interpreter (
, withInterpreter
, ghc
, interpreterSupported
+
+-- exported for testing
+, ghcInfo
+, haveInterpreterKey
) where
import System.IO
import System.Process
import System.Exit
import System.Directory (getPermissions, executable)
import Control.Monad (when, unless)
+import Control.Applicative
import Control.Exception hiding (handle)
import Data.Char
import Data.List
@@ -32,17 +37,21 @@ data Interpreter = Interpreter {
, process :: ProcessHandle
}
+haveInterpreterKey :: String
+haveInterpreterKey = "Have interpreter"
+
+ghcInfo :: IO [(String, String)]
+ghcInfo = read <$> readProcess ghc ["--info"] []
+
interpreterSupported :: IO Bool
interpreterSupported = do
-- in a perfect world this permission check should never fail, but I know of
-- at least one case where it did..
x <- getPermissions ghc
unless (executable x) $ do
fail $ ghc ++ " is not executable!"
-
- info <- readProcess ghc ["--info"] []
- return $ maybe False (=="YES") $ lookup "Have interpreter" (read info)
+ maybe False (== "YES") . lookup haveInterpreterKey <$> ghcInfo
newInterpreter :: [String] -> IO Interpreter
newInterpreter flags = do
@@ -94,7 +103,7 @@ closeInterpreter repl = do
e <- waitForProcess $ process repl
hClose $ hOut repl
- when (e /= ExitSuccess) $ error $ "Interpreter exited with an error: " ++ show e
+ when (e /= ExitSuccess) $ error $ "Interpreter exited with an error: " ++ show e
return ()
putExpression :: Interpreter -> String -> IO ()
View
@@ -4,13 +4,14 @@ module InterpreterSpec (main, spec) where
import Test.Hspec
import Data.List (isSuffixOf)
+import Control.Applicative
#ifndef mingw32_HOST_OS
import System.Process (readProcess)
#endif
import qualified Interpreter
-import Interpreter (safeEval)
+import Interpreter (safeEval, haveInterpreterKey, ghcInfo)
main :: IO ()
main = hspec spec
@@ -35,6 +36,12 @@ spec = do
s `shouldBe` "success\n"
#endif
+ describe "ghcInfo" $ do
+ it ("includes " ++ show haveInterpreterKey) $ do
+ info <- ghcInfo
+ lookup haveInterpreterKey info `shouldSatisfy`
+ (||) <$> (== Just "YES") <*> (== Just "NO")
+
describe "eval" $ do
it "shows literals" $ withInterpreter $ \ghci -> do
ghci "23" `shouldEvaluateTo` "23\n"

0 comments on commit 334e2c9

Please sign in to comment.