Skip to content

Commit

Permalink
Use -fshow-loaded-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Oct 15, 2024
1 parent 3d49898 commit 91280f4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 65 deletions.
7 changes: 6 additions & 1 deletion src/Language/Haskell/GhciWrapper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ new :: FilePath -> Config -> [String] -> IO Interpreter
new startupFile Config{..} args_ = do
checkDotGhci
env <- sanitizeEnv <$> getEnvironment

let
mandatoryArgs :: [String]
mandatoryArgs = ["-fshow-loaded-modules"]

args :: [String]
args = "-ghci-script" : startupFile : args_ ++ catMaybes [
if configIgnoreDotGhci then Just "-ignore-dot-ghci" else Nothing
]
] ++ mandatoryArgs

(stdoutReadEnd, stdoutWriteEnd) <- createPipe

Expand Down
7 changes: 1 addition & 6 deletions src/Trigger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Trigger (
, trigger
, triggerAll
#ifdef TEST
, reloadedSuccessfully
, removeProgress
#endif
) where
Expand Down Expand Up @@ -52,11 +51,7 @@ reloadedSuccessfully :: String -> Bool
reloadedSuccessfully = any success . lines
where
success :: String -> Bool
success x = case stripPrefix "Ok, " x of
Just "no modules to be reloaded." -> True
Just xs | [_number, modules, loaded] <- words xs, modules `elem` ["module", "modules"], loaded `elem` ["loaded.", "reloaded."] -> True
Just xs -> "modules loaded: " `isPrefixOf` xs
Nothing -> False
success = isPrefixOf "Ok, modules loaded: "

removeProgress :: String -> String
removeProgress xs = case break (== '\r') xs of
Expand Down
27 changes: 8 additions & 19 deletions test/Helper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,12 @@ data Status = Ok | Failed
deriving (Eq, Show)

modulesLoaded :: Status -> [String] -> String
modulesLoaded status xs = show status ++ ", " ++ mods ++ " " ++ loaded ++ "."
modulesLoaded status xs = show status ++ ", modules loaded: " <> mods <> "."
where
n = length xs
mods
| n == 0 = "no modules"
| n == 1 = "one module"
| n == 2 = "two modules"
| n == 3 = "three modules"
| n == 4 = "four modules"
| n == 5 = "five modules"
| n == 6 = "six modules"
| otherwise = show n ++ " modules"

#if __GLASGOW_HASKELL__ < 910
loaded = "loaded"
#else
loaded
| n == 0 = "to be reloaded"
| otherwise = "reloaded"
#endif
mods = case xs of
[] -> "none"
[name] -> formatModule name
_ -> undefined

formatModule :: String -> String
formatModule name = name <> " (" <> name <> ".o)"
47 changes: 8 additions & 39 deletions test/TriggerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module TriggerSpec (spec) where

import Helper

import qualified Data.Text as Text

import qualified Session
import Session (Session)
import Language.Haskell.GhciWrapper (Config(..))
Expand All @@ -11,7 +13,7 @@ import Trigger hiding (trigger, triggerAll)
import qualified Trigger

normalize :: String -> [String]
normalize = normalizeTiming . lines
normalize = normalizeTiming . lines . forGhc9dot4
where
normalizeTiming :: [String] -> [String]
normalizeTiming = normalizeLine "Finished in "
Expand All @@ -23,6 +25,9 @@ normalize = normalizeTiming . lines
| message `isPrefixOf` line = message ++ "..."
| otherwise = line

forGhc9dot4 :: String -> String
forGhc9dot4 = Text.unpack . Text.replace "Ok, modules loaded: Spec." "Ok, modules loaded: Spec (Spec.o)." . Text.pack

withSession :: FilePath -> [String] -> (Session -> IO a) -> IO a
withSession specPath args = do
Session.withSession ghciConfig {configWorkingDirectory = Just dir} $
Expand Down Expand Up @@ -70,42 +75,6 @@ failingHook = return $ HookFailure "hook failed"

spec :: Spec
spec = do
describe "reloadedSuccessfully" $ do
context "with GHC < 8.2.1" $ do
it "detects success" $ do
reloadedSuccessfully "Ok, modules loaded: Spec." `shouldBe` True

context "with GHC >= 8.2.1" $ do
context "with a single module" $ do
it "detects success" $ do
reloadedSuccessfully "Ok, 1 module loaded." `shouldBe` True

context "with multiple modules" $ do
it "detects success" $ do
reloadedSuccessfully "Ok, 5 modules loaded." `shouldBe` True

context "with GHC >= 8.2.2" $ do
context "with a single module" $ do
it "detects success" $ do
reloadedSuccessfully "Ok, one module loaded." `shouldBe` True

context "with multiple modules" $ do
it "detects success" $ do
reloadedSuccessfully "Ok, four modules loaded." `shouldBe` True

context "with GHC >= 9.10.1 (reload)" $ do
context "without any modules" $ do
it "detects success" $ do
reloadedSuccessfully "Ok, no modules to be reloaded." `shouldBe` True

context "with a single module" $ do
it "detects success" $ do
reloadedSuccessfully "Ok, one module reloaded." `shouldBe` True

context "with multiple modules" $ do
it "detects success" $ do
reloadedSuccessfully "Ok, four modules reloaded." `shouldBe` True

describe "removeProgress" $ do
it "removes transient output" $ do
(removeProgress . unlines) [
Expand Down Expand Up @@ -284,9 +253,9 @@ spec = do
context "with a module that does not expose a spec" $ do
it "only reloads" $ \ name -> do
withSession name [] $ \ session -> do
writeFile name "module Foo where"
writeFile name "module Spec where"
(trigger session >> trigger session) `shouldReturn` (Success, [
modulesLoaded Ok ["Foo"]
modulesLoaded Ok ["Spec"]
, withColor Green "RELOADING SUCCEEDED"
])

Expand Down

0 comments on commit 91280f4

Please sign in to comment.