1
1
{-# LANGUAGE DeriveGeneric #-}
2
+ {-# LANGUAGE LambdaCase #-}
3
+ {-# LANGUAGE NamedFieldPuns #-}
2
4
{-# LANGUAGE OverloadedStrings #-}
3
5
{-# LANGUAGE ScopedTypeVariables #-}
4
6
{-# LANGUAGE TypeApplications #-}
@@ -25,20 +27,21 @@ module Hedgehog.Extras.Test.Process
25
27
, defaultExecConfig
26
28
) where
27
29
28
- import Control.Monad (Monad (.. ), MonadFail (fail ), void , unless )
30
+ import Control.Applicative (pure , (<|>) )
31
+ import Control.Monad (Monad (.. ), MonadFail (fail ), unless , void )
29
32
import Control.Monad.Catch (MonadCatch )
30
33
import Control.Monad.IO.Class (MonadIO , liftIO )
31
34
import Control.Monad.Trans.Resource (MonadResource , ReleaseKey , register )
32
35
import Data.Aeson (eitherDecode )
33
- import Data.Bool (Bool (.. ) )
36
+ import Data.Bool (Bool (True ), otherwise )
34
37
import Data.Either (Either (.. ))
35
38
import Data.Eq (Eq (.. ))
36
- import Data.Function (($) , (&) , ( .) )
39
+ import Data.Function (($) , (.) )
37
40
import Data.Functor ((<$>) )
38
41
import Data.Int (Int )
39
42
import Data.Maybe (Maybe (.. ))
40
43
import Data.Monoid (Last (.. ), mempty , (<>) )
41
- import Data.String (String )
44
+ import Data.String (IsString ( .. ), String )
42
45
import GHC.Generics (Generic )
43
46
import GHC.Stack (HasCallStack )
44
47
import Hedgehog (MonadTest )
@@ -55,6 +58,7 @@ import Text.Show (Show (show))
55
58
56
59
import qualified Data.ByteString.Lazy as LBS
57
60
import qualified Data.List as L
61
+ import Data.Text (Text )
58
62
import qualified Data.Text as T
59
63
import qualified GHC.Stack as GHC
60
64
import qualified Hedgehog as H
@@ -308,19 +312,26 @@ binDist pkg binaryEnv = do
308
312
<> " \" if you are working with sources. Otherwise define "
309
313
<> binaryEnv
310
314
<> " and have it point to the executable you want."
311
- contents <- H. evalIO . LBS. readFile $ planJsonFile
312
-
313
- case eitherDecode contents of
314
- Right plan -> case L. filter matching (plan & installPlan) of
315
- (component: _) -> case component & binFile of
316
- Just bin -> return $ addExeSuffix (T. unpack bin)
317
- Nothing -> error $ " missing \" bin-file\" key in plan component: " <> show component <> " in the plan in: " <> planJsonFile
318
- [] -> error $ " Cannot find \" component-name\" key with the value \" exe:" <> pkg <> " \" in the plan in: " <> planJsonFile
319
- Left message -> error $ " Cannot decode plan in " <> planJsonFile <> " : " <> message
320
- where matching :: Component -> Bool
321
- matching component = case componentName component of
322
- Just name -> name == " exe:" <> T. pack pkg
323
- Nothing -> False
315
+
316
+ Plan {installPlan} <- eitherDecode <$> H. evalIO (LBS. readFile planJsonFile)
317
+ >>= \ case
318
+ Left message -> error $ " Cannot decode plan in " <> planJsonFile <> " : " <> message
319
+ Right plan -> pure plan
320
+
321
+ let componentName = " exe:" <> fromString pkg
322
+ case findComponent componentName installPlan of
323
+ Just Component {binFile= Just binFilePath} -> pure . addExeSuffix $ T. unpack binFilePath
324
+ Just component@ Component {binFile= Nothing } ->
325
+ error $ " missing \" bin-file\" key in plan component: " <> show component <> " in the plan in: " <> planJsonFile
326
+ Nothing ->
327
+ error $ " Cannot find \" component-name\" key with the value \" exe:" <> pkg <> " \" in the plan in: " <> planJsonFile
328
+ where
329
+ findComponent :: Text -> [Component ] -> Maybe Component
330
+ findComponent _ [] = Nothing
331
+ findComponent needle (c@ Component {componentName, components}: topLevelComponents)
332
+ | componentName == Just needle = Just c
333
+ | otherwise = findComponent needle topLevelComponents <|> findComponent needle components
334
+
324
335
325
336
-- | Create a 'CreateProcess' describing how to start a process given the Cabal package name
326
337
-- corresponding to the executable, an environment variable pointing to the executable,
0 commit comments