ghc-mod/test/TestUtils.hs

83 lines
2.1 KiB
Haskell
Raw Normal View History

2014-05-10 13:10:34 +00:00
module TestUtils (
run
, runD
2014-08-12 16:11:32 +00:00
, runD'
2014-05-10 13:10:34 +00:00
, runI
2015-02-07 15:41:15 +00:00
-- , runID
2014-05-10 13:10:34 +00:00
, runIsolatedGhcMod
, isolateCradle
2014-08-12 16:11:32 +00:00
, shouldReturnError
2015-02-07 15:41:15 +00:00
, isPkgDbAt
, isPkgConfDAt
2014-05-10 13:10:34 +00:00
, module Language.Haskell.GhcMod.Monad
, module Language.Haskell.GhcMod.Types
) where
import Language.Haskell.GhcMod.Monad
import Language.Haskell.GhcMod.Types
2015-02-07 15:41:15 +00:00
import Data.List.Split
import System.FilePath
2014-08-12 16:11:32 +00:00
import Test.Hspec
isolateCradle :: IOish m => GhcModT m a -> GhcModT m a
2014-05-10 13:10:34 +00:00
isolateCradle action =
local modifyEnv $ action
where
modifyEnv e = e { gmCradle = (gmCradle e) { cradlePkgDbStack = [GlobalDb] } }
2014-08-21 05:16:56 +00:00
extract :: Show e => IO (Either e a, w) -> IO a
2014-07-22 17:45:48 +00:00
extract action = do
2014-08-21 05:16:56 +00:00
(r,_) <- action
case r of
Right a -> return a
Left e -> error $ show e
2014-07-22 17:45:48 +00:00
runIsolatedGhcMod :: Options -> GhcModT IO a -> IO a
2014-07-22 17:45:48 +00:00
runIsolatedGhcMod opt action = do
extract $ runGhcModT opt $ isolateCradle action
2014-05-10 13:10:34 +00:00
-- | Run GhcMod in isolated cradle with default options
2015-02-07 15:41:15 +00:00
--runID :: GhcModT IO a -> IO a
--runID = runIsolatedGhcMod defaultOptions
2014-05-10 13:10:34 +00:00
-- | Run GhcMod in isolated cradle
2014-08-12 07:41:21 +00:00
runI :: Options -> GhcModT IO a -> IO a
2014-05-10 13:10:34 +00:00
runI = runIsolatedGhcMod
-- | Run GhcMod
run :: Options -> GhcModT IO a -> IO a
2014-07-22 17:45:48 +00:00
run opt a = extract $ runGhcModT opt a
2014-05-10 13:10:34 +00:00
-- | Run GhcMod with default options
runD :: GhcModT IO a -> IO a
2014-07-22 17:45:48 +00:00
runD = extract . runGhcModT defaultOptions
2014-08-12 16:11:32 +00:00
runD' :: GhcModT IO a -> IO (Either GhcModError a, GhcModLog)
runD' = runGhcModT defaultOptions
shouldReturnError :: Show a
=> IO (Either GhcModError a, GhcModLog)
-> Expectation
shouldReturnError action = do
(a,_) <- action
a `shouldSatisfy` isLeft
where
isLeft (Left _) = True
isLeft _ = False
2015-02-07 15:41:15 +00:00
isPkgConfD :: FilePath -> Bool
isPkgConfD d = let
(_dir, pkgconfd) = splitFileName d
in case splitOn "-" pkgconfd of
[_arch, _platform, _compiler, _compver, "packages.conf.d"] -> True
_ -> False
isPkgConfDAt :: FilePath -> FilePath -> Bool
isPkgConfDAt d d' | d == takeDirectory d' && isPkgConfD d' = True
isPkgConfDAt _ _ = False
isPkgDbAt :: FilePath -> GhcPkgDb -> Bool
isPkgDbAt d (PackageDb dir) = isPkgConfDAt d dir
isPkgDbAt _ _ = False