Skip to content

Commit edeb35a

Browse files
committed
Improve error reporting when calling binaries from plan.json
1 parent d9db931 commit edeb35a

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/Hedgehog/Extras/Test/Process.hs

+13-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Hedgehog.Extras.Test.Process
2525
, defaultExecConfig
2626
) where
2727

28-
import Control.Monad (Monad (..), MonadFail (fail), void)
28+
import Control.Monad (Monad (..), MonadFail (fail), void, unless)
2929
import Control.Monad.Catch (MonadCatch)
3030
import Control.Monad.IO.Class (MonadIO, liftIO)
3131
import Control.Monad.Trans.Resource (MonadResource, ReleaseKey, register)
@@ -84,14 +84,15 @@ findDefaultPlanJsonFile :: IO FilePath
8484
findDefaultPlanJsonFile = IO.getCurrentDirectory >>= go
8585
where go :: FilePath -> IO FilePath
8686
go d = do
87-
let file = d </> "dist-newstyle/cache/plan.json"
87+
let planRelPath = "dist-newstyle/cache/plan.json"
88+
file = d </> planRelPath
8889
exists <- IO.doesFileExist file
8990
if exists
9091
then return file
9192
else do
9293
let parent = takeDirectory d
9394
if parent == d
94-
then return "dist-newstyle/cache/plan.json"
95+
then return planRelPath
9596
else go parent
9697

9798
-- | Discover the location of the plan.json file.
@@ -272,7 +273,7 @@ waitSecondsForProcess seconds hProcess = GHC.withFrozenCallStack $ do
272273

273274
-- | Compute the path to the binary given a package name or an environment variable override.
274275
binFlex
275-
:: (MonadTest m, MonadIO m)
276+
:: (HasCallStack, MonadTest m, MonadIO m)
276277
=> String
277278
-- ^ Package name
278279
-> String
@@ -288,22 +289,26 @@ binFlex pkg binaryEnv = do
288289
-- | Consult the "plan.json" generated by cabal to get the path to the executable corresponding.
289290
-- to a haskell package. It is assumed that the project has already been configured and the
290291
-- executable has been built.
292+
-- Throws an exception on failure.
291293
binDist
292-
:: (MonadTest m, MonadIO m)
294+
:: (HasCallStack, MonadTest m, MonadIO m)
293295
=> String
294296
-- ^ Package name
295297
-> m FilePath
296298
-- ^ Path to executable
297299
binDist pkg = do
300+
doesPlanExist <- liftIO $ IO.doesFileExist planJsonFile
301+
unless doesPlanExist $
302+
error $ "Could not find plan.json in the path: " <> planJsonFile
298303
contents <- H.evalIO . LBS.readFile $ planJsonFile
299304

300305
case eitherDecode contents of
301306
Right plan -> case L.filter matching (plan & installPlan) of
302307
(component:_) -> case component & binFile of
303308
Just bin -> return $ addExeSuffix (T.unpack bin)
304-
Nothing -> error $ "missing bin-file in: " <> show component
305-
[] -> error $ "Cannot find exe:" <> pkg <> " in plan"
306-
Left message -> error $ "Cannot decode plan: " <> message
309+
Nothing -> error $ "missing \"bin-file\" key in plan component: " <> show component <> " in the plan in: " <> planJsonFile
310+
[] -> error $ "Cannot find \"component-name\" key with the value \"exe:" <> pkg <> "\" in the plan in: " <> planJsonFile
311+
Left message -> error $ "Cannot decode plan in " <> planJsonFile <> ": " <> message
307312
where matching :: Component -> Bool
308313
matching component = case componentName component of
309314
Just name -> name == "exe:" <> T.pack pkg

0 commit comments

Comments
 (0)