@@ -17,7 +17,6 @@ module Testnet.Process.Run
1717 , mkExecConfig
1818 , mkExecConfigOffline
1919 , ProcessError (.. )
20- , ExecutableError (.. )
2120 , addEnvVarsToConfig
2221 ) where
2322
@@ -31,29 +30,21 @@ import Control.Monad.Trans.Except (ExceptT)
3130import Control.Monad.Trans.Except.Extra
3231import Control.Monad.Trans.Resource
3332import qualified Data.Aeson as Aeson
34- import qualified Data.ByteString.Lazy as LBS
35- import Data.Function
36- import qualified Data.List as List
37- import Data.Maybe (fromMaybe )
33+ import Data.Maybe
3834import Data.Monoid (Last (.. ))
3935import Data.String (fromString )
40- import qualified Data.Text as Text
4136import GHC.Stack (HasCallStack )
4237import qualified GHC.Stack as GHC
43- import qualified System.Directory as IO
4438import qualified System.Environment as IO
4539import System.Exit (ExitCode )
46- import System.FilePath
4740import System.IO
4841import qualified System.IO.Unsafe as IO
4942import qualified System.Process as IO
5043import System.Process
5144
5245import Hedgehog (MonadTest )
5346import qualified Hedgehog.Extras as H
54- import Hedgehog.Extras.Internal.Plan (Component (.. ), Plan (.. ))
5547import qualified Hedgehog.Extras.Stock.IO.Network.Sprocket as IO
56- import qualified Hedgehog.Extras.Stock.OS as OS
5748import Hedgehog.Extras.Test.Process (ExecConfig )
5849import qualified Hedgehog.Internal.Property as H
5950
@@ -146,13 +137,12 @@ procCli = GHC.withFrozenCallStack $ H.procFlex "cardano-cli" "CARDANO_CLI"
146137-- | Create a 'CreateProcess' describing how to start the cardano-node process
147138-- and an argument list.
148139procNode
149- :: HasCallStack
150- => MonadIO m
140+ :: (MonadTest m , MonadCatch m , MonadIO m , HasCallStack )
151141 => [String ]
152142 -- ^ Arguments to the CLI command
153- -> ExceptT ExecutableError m CreateProcess
143+ -> m CreateProcess
154144 -- ^ Captured stdout
155- procNode = GHC. withFrozenCallStack $ procFlexNew " cardano-node" " CARDANO_NODE"
145+ procNode = GHC. withFrozenCallStack $ H. procFlex " cardano-node" " CARDANO_NODE"
156146
157147-- | Create a 'CreateProcess' describing how to start the cardano-submit-api process
158148-- and an argument list.
@@ -248,119 +238,3 @@ resourceAndIOExceptionHandlers = [ Handler $ pure . ProcessIOException
248238 , Handler $ pure . ResourceException
249239 ]
250240
251- procFlexNew
252- :: MonadIO m
253- => String
254- -- ^ Cabal package name corresponding to the executable
255- -> String
256- -- ^ Environment variable pointing to the binary to run
257- -> [String ]
258- -- ^ Arguments to the CLI command
259- -> ExceptT ExecutableError m CreateProcess
260- -- ^ Captured stdout
261- procFlexNew = procFlexNew' H. defaultExecConfig
262-
263- procFlexNew'
264- :: MonadIO m
265- => H. ExecConfig
266- -> String
267- -- ^ Cabal package name corresponding to the executable
268- -> String
269- -- ^ Environment variable pointing to the binary to run
270- -> [String ]
271- -- ^ Arguments to the CLI command
272- -> ExceptT ExecutableError m CreateProcess
273- -- ^ Captured stdout
274- procFlexNew' execConfig pkg binaryEnv arguments = GHC. withFrozenCallStack $ do
275- bin <- binFlexNew pkg binaryEnv
276- pure (IO. proc bin arguments)
277- { IO. env = getLast $ H. execConfigEnv execConfig
278- , IO. cwd = getLast $ H. execConfigCwd execConfig
279- -- this allows sending signals to the created processes, without killing the test-suite process
280- , IO. create_group = True
281- }
282-
283- -- | Compute the path to the binary given a package name or an environment variable override.
284- binFlexNew
285- :: MonadIO m
286- => String
287- -- ^ Package name
288- -> String
289- -- ^ Environment variable pointing to the binary to run
290- -> ExceptT ExecutableError m FilePath
291- -- ^ Path to executable
292- binFlexNew pkg binaryEnv = do
293- maybeEnvBin <- liftIO $ IO. lookupEnv binaryEnv
294- case maybeEnvBin of
295- Just envBin -> return envBin
296- Nothing -> binDist pkg
297-
298- -- | Find the nearest plan.json going upwards from the current directory.
299- findDefaultPlanJsonFile :: IO FilePath
300- findDefaultPlanJsonFile = IO. getCurrentDirectory >>= go
301- where go :: FilePath -> IO FilePath
302- go d = do
303- let file = d </> " dist-newstyle/cache/plan.json"
304- exists <- IO. doesFileExist file
305- if exists
306- then return file
307- else do
308- let parent = takeDirectory d
309- if parent == d
310- then return " dist-newstyle/cache/plan.json"
311- else go parent
312-
313-
314- -- | Discover the location of the plan.json file.
315- planJsonFile :: IO FilePath
316- planJsonFile = do
317- maybeBuildDir <- liftIO $ IO. lookupEnv " CABAL_BUILDDIR"
318- case maybeBuildDir of
319- Just buildDir -> return $ " .." </> buildDir </> " cache/plan.json"
320- Nothing -> findDefaultPlanJsonFile
321- {-# NOINLINE planJsonFile #-}
322-
323- data ExecutableError
324- = CannotDecodePlanJSON FilePath String
325- | RetrievePlanJsonFailure IOException
326- | ReadFileFailure IOException
327- | ExecutableMissingInComponent FilePath String
328- -- ^ Component with key @component-name@ is found, but it is missing
329- -- the @bin-file@ key.
330- | ExecutableNotFoundInPlan String
331- -- ^ Component with key @component-name@ cannot be found
332- deriving Show
333-
334-
335- -- | Consult the "plan.json" generated by cabal to get the path to the executable corresponding.
336- -- to a haskell package. It is assumed that the project has already been configured and the
337- -- executable has been built.
338- binDist
339- :: MonadIO m
340- => String
341- -- ^ Package name
342- -> ExceptT ExecutableError m FilePath
343- -- ^ Path to executable
344- binDist pkg = do
345- pJsonFp <- handleIOExceptT RetrievePlanJsonFailure planJsonFile
346- contents <- handleIOExceptT ReadFileFailure $ LBS. readFile pJsonFp
347-
348- case Aeson. eitherDecode contents of
349- Right plan -> case List. filter matching (plan & installPlan) of
350- (component: _) -> case component & binFile of
351- Just bin -> return $ addExeSuffix (Text. unpack bin)
352- Nothing -> left $ ExecutableMissingInComponent pJsonFp $ " missing \" bin-file\" key in plan component: " <> show component
353- [] -> left $ ExecutableNotFoundInPlan $ " Cannot find \" component-name\" key with value \" exe:" <> pkg <> " \" "
354- Left message -> left $ CannotDecodePlanJSON pJsonFp $ " Cannot decode plan: " <> message
355- where matching :: Component -> Bool
356- matching component = case componentName component of
357- Just name -> name == Text. pack (" exe:" <> pkg)
358- Nothing -> False
359-
360- addExeSuffix :: String -> String
361- addExeSuffix s = if " .exe" `List.isSuffixOf` s
362- then s
363- else s <> exeSuffix
364-
365- exeSuffix :: String
366- exeSuffix = if OS. isWin32 then " .exe" else " "
0 commit comments