atelier-typed-process (empty) → 0.2.13.0
raw patch · 7 files changed · show whitespace changes · browse atelier-typed-process-0.2.13.0
+1049/−0 lines, 7 filesdep +atelier-typed-processdep +basedep +bytestring
Dependencies added: atelier-typed-process, base, bytestring, effectful, effectful-core, effectful-plugin, tasty, tasty-hunit, typed-process
Files
- CHANGELOG.md +14/−0
- LICENSE +21/−0
- README.md +18/−0
- atelier-typed-process.cabal +109/−0
- src/Atelier/Process/Typed.hs +400/−0
- test/Atelier/Process/TypedTest.hs +485/−0 · folded
- test/Main.hs +2/−0 · folded
+ CHANGELOG.md view
@@ -0,0 +1,14 @@+# Changelog++All notable changes to `atelier-typed-process` will be documented in this file.++The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),+and this project adheres to the [PVP](https://pvp.haskell.org/).++## [Unreleased]++## [0.2.13.0]++### Added++- Initial implementation targeting `typed-process-0.2.13.0`.
+ LICENSE view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2026 Tweag++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ README.md view
@@ -0,0 +1,18 @@+# atelier-typed-process++[`typed-process`] lifted to use [the `Eff` monad] from [`effectful`].++## Overview++`atelier-typed-process` wraps `IO` functions from `typed-process` and lifts+them into the `Eff` monad, for use with the `effectful` effect system, as+operations on [the `Process` effect].++## License++MIT — see [LICENSE](LICENSE).++[the `Process` effect]: https://hackage-content.haskell.org/package/effectful-2.7.1.0/docs/Effectful-Process.html#t:Process+[`typed-process`]: https://hackage.haskell.org/package/typed-process+[the `Eff` monad]: https://hackage-content.haskell.org/package/effectful-core-2.7.1.2/docs/Effectful.html#g:8+[`effectful`]: https://hackage-content.haskell.org/package/effectful-2.7.1.0
+ atelier-typed-process.cabal view
@@ -0,0 +1,109 @@+cabal-version: 1.18++-- This file has been generated from package.yaml by hpack version 0.38.3.+--+-- see: https://github.com/sol/hpack++name: atelier-typed-process+version: 0.2.13.0+synopsis: typed-process lifted for use with effectful+description: Lifted functions from `typed-process` to use with the `effectful` effect system.+category: System+homepage: https://github.com/tweag/tricorder#readme+bug-reports: https://github.com/tweag/tricorder/issues+author: Christian Georgii+maintainer: victor.bakke@tweag.io+license: MIT+license-file: LICENSE+build-type: Simple+tested-with:+ GHC == 9.14.1+ , GHC == 9.6.7+ , GHC == 9.8.4+ , GHC == 9.10.3+ , GHC == 9.12.4+extra-doc-files:+ CHANGELOG.md+ README.md++source-repository head+ type: git+ location: https://github.com/tweag/tricorder++library+ exposed-modules:+ Atelier.Process.Typed+ other-modules:+ Paths_atelier_typed_process+ hs-source-dirs:+ src+ default-extensions:+ BlockArguments+ DataKinds+ DeriveAnyClass+ DerivingStrategies+ DerivingVia+ DuplicateRecordFields+ FlexibleContexts+ GADTs+ LambdaCase+ MultiWayIf+ OverloadedLabels+ OverloadedRecordDot+ OverloadedStrings+ StrictData+ TemplateHaskell+ TypeFamilies+ ghc-options: -Weverything -Wno-unsafe -Wno-missing-safe-haskell-mode -Wno-monomorphism-restriction -Wno-missing-kind-signatures -Wno-missing-local-signatures -Wno-missing-import-lists -Wno-implicit-prelude -Wno-unticked-promoted-constructors -Wno-unused-packages -Wno-all-missed-specialisations -Wno-missed-specialisations -fplugin=Effectful.Plugin -threaded+ build-depends:+ base >=4.18 && <4.23+ , bytestring >=0.11 && <0.13+ , effectful ==2.7.*+ , effectful-core ==2.7.*+ , effectful-plugin ==2.2.*+ , typed-process >=0.2 && <0.2.14+ default-language: GHC2021+ if impl(GHC >= 9.8)+ ghc-options: -Wno-missing-poly-kind-signatures -Wno-missing-role-annotations++test-suite atelier-test+ type: exitcode-stdio-1.0+ main-is: Main.hs+ other-modules:+ Atelier.Process.TypedTest+ Paths_atelier_typed_process+ hs-source-dirs:+ test+ default-extensions:+ BlockArguments+ DataKinds+ DeriveAnyClass+ DerivingStrategies+ DerivingVia+ DuplicateRecordFields+ FlexibleContexts+ GADTs+ LambdaCase+ MultiWayIf+ OverloadedLabels+ OverloadedRecordDot+ OverloadedStrings+ StrictData+ TemplateHaskell+ TypeFamilies+ ghc-options: -Weverything -Wno-unsafe -Wno-missing-safe-haskell-mode -Wno-monomorphism-restriction -Wno-missing-kind-signatures -Wno-missing-local-signatures -Wno-missing-import-lists -Wno-implicit-prelude -Wno-unticked-promoted-constructors -Wno-unused-packages -Wno-all-missed-specialisations -Wno-missed-specialisations -fplugin=Effectful.Plugin -threaded -Wno-prepositive-qualified-module+ build-tool-depends:+ tasty-discover:tasty-discover+ build-depends:+ atelier-typed-process+ , base >=4.18 && <4.23+ , bytestring >=0.11 && <0.13+ , effectful ==2.7.*+ , effectful-core ==2.7.*+ , effectful-plugin ==2.2.*+ , tasty ==1.5.*+ , tasty-hunit >=0.10.2 && <0.11+ , typed-process >=0.2 && <0.2.14+ default-language: GHC2021+ if impl(GHC >= 9.8)+ ghc-options: -Wno-missing-poly-kind-signatures -Wno-missing-role-annotations
+ src/Atelier/Process/Typed.hs view
@@ -0,0 +1,400 @@+{-# LANGUAGE BlockArguments #-}+{-# LANGUAGE DataKinds #-}++-- | @typed-process@ lifted to @effectful@'s 'Eff'.+--+-- This module aims to mirror `System.Process.Typed` as closely as possible.+module Atelier.Process.Typed+ ( -- * Effect+ TypedProcess+ , run++ -- * Types+ , ProcessConfig+ , StreamSpec+ , StreamType (..)+ , Process++ -- * ProcessConfig++ -- ** Smart constructors+ , proc+ , shell++ -- ** Setters+ , setStdin+ , setStdout+ , setStderr+ , setWorkingDir+ , setWorkingDirInherit+ , setEnv+ , setEnvInherit+ , setCloseFds+ , setCreateGroup+ , setDelegateCtlc+ , setDetachConsole+ , setCreateNewConsole+ , setNewSession+ , setChildGroup+ , setChildGroupInherit+ , setChildUser+ , setChildUserInherit++ -- * Stream specs++ -- ** Built-in stream specs+ , inherit+ , nullStream+ , closed+ , byteStringInput+ , byteStringOutput+ , createPipe+ , useHandleOpen+ , useHandleClose++ -- ** Create your own stream spec+ , mkStreamSpec+ , mkPipeStreamSpec++ -- * Launch a process+ , runProcess+ , readProcess+ , readProcessStdout+ , readProcessStderr+ , readProcessInterleaved+ , withProcessWait+ , withProcessTerm+ , startProcess+ , stopProcess++ -- ** Exception-throwing functions++ --+ -- The functions ending in underbar (_) are the same as their counterparts+ -- without underbar but instead of returning an ExitCode they throw+ -- ExitCodeException on failure.+ , runProcess_+ , readProcess_+ , readProcessStdout_+ , readProcessStderr_+ , readProcessInterleaved_+ , withProcessWait_+ , withProcessTerm_++ -- * Interact with a process++ -- ** Process exit code+ , waitExitCode+ , waitExitCodeSTM+ , getExitCode+ , getExitCodeSTM+ , checkExitCode+ , checkExitCodeSTM++ -- ** Process ID+ , getPid++ -- ** Process streams+ , getStdin+ , getStdout+ , getStderr++ -- * Exceptions+ , ExitCodeException (..)+ , exitCodeExceptionWithOutput+ , exitCodeExceptionNoOutput+ , ByteStringOutputException (..)++ -- * Re-exports+ , ExitCode (..)+ , StdStream (..)+ , Pid+ , Handle++ -- * Unsafe functions+ , unsafeProcessHandle+ )+where++import Data.ByteString.Lazy (ByteString)+import Effectful (Eff, IOE, Limit (..), Persistence (..), (:>))+import Effectful.Concurrent.STM (STM)+import Effectful.Dispatch.Static (StaticRep, concUnliftIO, getStaticRep, unsafeEff, unsafeEff_)+import System.IO (Handle)+import System.Process.Typed+ ( ByteStringOutputException (..)+ , ExitCode (..)+ , ExitCodeException (..)+ , Pid+ , Process+ , ProcessConfig+ , StdStream (..)+ , StreamSpec+ , StreamType+ , byteStringInput+ , byteStringOutput+ , checkExitCodeSTM+ , closed+ , createPipe+ , exitCodeExceptionNoOutput+ , getExitCodeSTM+ , getStderr+ , getStdin+ , getStdout+ , inherit+ , nullStream+ , proc+ , setChildGroup+ , setChildGroupInherit+ , setChildUser+ , setChildUserInherit+ , setCloseFds+ , setCreateGroup+ , setCreateNewConsole+ , setDelegateCtlc+ , setDetachConsole+ , setEnv+ , setEnvInherit+ , setNewSession+ , setStderr+ , setStdin+ , setStdout+ , setWorkingDir+ , setWorkingDirInherit+ , shell+ , unsafeProcessHandle+ , useHandleClose+ , useHandleOpen+ , waitExitCodeSTM+ )++import Effectful.Process qualified as Eff+import System.Process.Typed qualified as TP+++-- | Effectful's 'Eff.Process' effect clashes with "System.Process.Typed"'s+-- 'Process' type, so we define an alias for it.+type TypedProcess = Eff.Process+++run :: (IOE :> es) => Eff (TypedProcess ': es) a -> Eff es a+run = Eff.runProcess+++-- | Lifted 'System.Process.Typed.mkStreamSpec'.+mkStreamSpec+ :: (TypedProcess :> es)+ => StdStream+ -> (ProcessConfig () () () -> Maybe Handle -> Eff es (a, Eff es ()))+ -> Eff es (StreamSpec streamType a)+mkStreamSpec stream f = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff \env ->+ concUnliftIO env Persistent Unlimited \unlift ->+ pure $ TP.mkStreamSpec stream \cfg mHandle -> do+ (a, cont) <- unlift $ f cfg mHandle+ pure (a, unlift cont)+++-- | Lifted 'System.Process.Typed.mkPipeStreamSpec'.+mkPipeStreamSpec+ :: (TypedProcess :> es)+ => (ProcessConfig () () () -> Handle -> Eff es (a, Eff es ())) -> Eff es (StreamSpec streamType a)+mkPipeStreamSpec f = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff \env ->+ concUnliftIO env Persistent Unlimited \unlift ->+ pure $ TP.mkPipeStreamSpec \cfg mHandle -> do+ (a, cont) <- unlift $ f cfg mHandle+ pure (a, unlift cont)+++-- | Lifted 'System.Process.Typed.runProcess'.+runProcess :: (TypedProcess :> es) => ProcessConfig stdin stdout stderr -> Eff es ExitCode+runProcess cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.runProcess cfg+++-- | Lifted 'System.Process.Typed.readProcess'.+readProcess+ :: (TypedProcess :> es)+ => ProcessConfig stdin stdoutIgnored stderrIgnored -> Eff es (ExitCode, ByteString, ByteString)+readProcess cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.readProcess cfg+++-- | Lifted 'System.Process.Typed.readProcessStdout'.+readProcessStdout+ :: (TypedProcess :> es) => ProcessConfig stdin stdoutIgnored stderr -> Eff es (ExitCode, ByteString)+readProcessStdout cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.readProcessStdout cfg+++-- | Lifted 'System.Process.Typed.readProcessStderr'.+readProcessStderr+ :: (TypedProcess :> es) => ProcessConfig stdin stdout stderrIgnored -> Eff es (ExitCode, ByteString)+readProcessStderr cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.readProcessStderr cfg+++-- | Lifted 'System.Process.Typed.readProcessInterleaved'.+readProcessInterleaved+ :: (TypedProcess :> es)+ => ProcessConfig stdin stdoutIgnored stderrIgnored -> Eff es (ExitCode, ByteString)+readProcessInterleaved cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.readProcessInterleaved cfg+++-- | Lifted 'System.Process.Typed.withProcessWait'.+withProcessWait+ :: (TypedProcess :> es)+ => ProcessConfig stdin stdout stderr+ -> (Process stdin stdout stderr -> Eff es a)+ -> Eff es a+withProcessWait cfg f = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff \env ->+ concUnliftIO env Persistent Unlimited \unlift ->+ TP.withProcessWait cfg \p -> unlift $ f p+++-- | Lifted 'System.Process.Typed.withProcessTerm'.+withProcessTerm+ :: (TypedProcess :> es)+ => ProcessConfig stdin stdout stderr+ -> (Process stdin stdout stderr -> Eff es a)+ -> Eff es a+withProcessTerm cfg f = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff \env ->+ concUnliftIO env Persistent Unlimited \unlift ->+ TP.withProcessTerm cfg \p -> unlift $ f p+++-- | Lifted 'System.Process.Typed.startProcess'.+startProcess+ :: (TypedProcess :> es)+ => ProcessConfig stdin stdout stderr+ -> Eff es (Process stdin stdout stderr)+startProcess cfg = do+ _ :: StaticRep TypedProcess <- getStaticRep+ unsafeEff_ $ TP.startProcess cfg+++-- | Lifted 'System.Process.Typed.stopProcess'.+stopProcess+ :: (TypedProcess :> es)+ => Process stdin stdout stderr+ -> Eff es ()+stopProcess process = do+ _ :: StaticRep TypedProcess <- getStaticRep+ unsafeEff_ $ TP.stopProcess process+++-- | Lifted 'System.Process.Typed.runProcess_'.+runProcess_ :: (TypedProcess :> es) => ProcessConfig stdin stdout stderr -> Eff es ()+runProcess_ cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.runProcess_ cfg+++-- | Lifted 'System.Process.Typed.readProcess_'.+readProcess_+ :: (TypedProcess :> es)+ => ProcessConfig stdin stdoutIgnored stderrIgnored -> Eff es (ByteString, ByteString)+readProcess_ cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.readProcess_ cfg+++-- | Lifted 'System.Process.Typed.readProcessStdout_'.+readProcessStdout_+ :: (TypedProcess :> es) => ProcessConfig stdin stdoutIgnored stderr -> Eff es ByteString+readProcessStdout_ cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.readProcessStdout_ cfg+++-- | Lifted 'System.Process.Typed.readProcessStderr_'.+readProcessStderr_+ :: (TypedProcess :> es) => ProcessConfig stdin stdout stderrIgnored -> Eff es ByteString+readProcessStderr_ cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.readProcessStderr_ cfg+++-- | Lifted 'System.Process.Typed.readProcessInterleaved_'.+readProcessInterleaved_+ :: (TypedProcess :> es)+ => ProcessConfig stdin stdoutIgnored stderrIgnored -> Eff es ByteString+readProcessInterleaved_ cfg = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff_ $ TP.readProcessInterleaved_ cfg+++-- | Lifted 'System.Process.Typed.withProcessWait_'.+withProcessWait_+ :: (TypedProcess :> es)+ => ProcessConfig stdin stdout stderr+ -> (Process stdin stdout stderr -> Eff es a)+ -> Eff es a+withProcessWait_ cfg f = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff \env ->+ concUnliftIO env Persistent Unlimited \unlift ->+ TP.withProcessWait_ cfg \p -> unlift $ f p+++-- | Lifted 'System.Process.Typed.withProcessTerm_'.+withProcessTerm_+ :: (TypedProcess :> es)+ => ProcessConfig stdin stdout stderr+ -> (Process stdin stdout stderr -> Eff es a)+ -> Eff es a+withProcessTerm_ cfg f = do+ (_ :: StaticRep TypedProcess) <- getStaticRep+ unsafeEff \env ->+ concUnliftIO env Persistent Unlimited \unlift ->+ TP.withProcessTerm_ cfg \p -> unlift $ f p+++-- | Lifted 'System.Process.Typed.waitExitCode'.+waitExitCode :: (TypedProcess :> es) => Process stdin stdout stderr -> Eff es ExitCode+waitExitCode process = do+ _ :: StaticRep TypedProcess <- getStaticRep+ unsafeEff_ $ TP.waitExitCode process+++-- | Lifted 'System.Process.Typed.getExitCode'.+getExitCode :: (TypedProcess :> es) => Process stdin stdout stderr -> Eff es (Maybe ExitCode)+getExitCode process = do+ _ :: StaticRep TypedProcess <- getStaticRep+ unsafeEff_ $ TP.getExitCode process+++-- | Lifted 'System.Process.Typed.checkExitCode'.+checkExitCode :: (TypedProcess :> es) => Process stdin stdout stderr -> Eff es ()+checkExitCode process = do+ _ :: StaticRep TypedProcess <- getStaticRep+ unsafeEff_ $ TP.checkExitCode process+++-- | Lifted 'System.Process.Typed.getPid'.+getPid :: (TypedProcess :> es) => Process stdin stdout stderr -> Eff es (Maybe Pid)+getPid process = do+ _ :: StaticRep TypedProcess <- getStaticRep+ unsafeEff_ $ TP.getPid process+++-- | Lifted 'System.Process.Typed.exitCodeExceptionWithOutput'.+exitCodeExceptionWithOutput+ :: (TypedProcess :> es)+ => Process stdin (STM ByteString) (STM ByteString) -> Eff es ExitCodeException+exitCodeExceptionWithOutput process = do+ _ :: StaticRep TypedProcess <- getStaticRep+ unsafeEff_ $ TP.exitCodeExceptionWithOutput process
+ test/Atelier/Process/TypedTest.hs view
@@ -0,0 +1,485 @@+{-# LANGUAGE BlockArguments #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeApplications #-}++-- | Specs checking that the lifted functions behave like their+-- "System.Process.Typed" counterparts, and that they unlift 'Eff' correctly+-- when they take callbacks.+module Atelier.Process.TypedTest (test_Typed) where++import Control.Exception (SomeException)+import Effectful (Eff, IOE, liftIO, runEff, (:>))+import Effectful.Concurrent (runConcurrent)+import Effectful.Concurrent.STM (atomically)+import Effectful.Exception (throwIO, try)+import Effectful.Reader.Static (ask, runReader)+import Effectful.State.Static.Local (State, execState, get, modify, put, runState)+import System.IO (hClose, hGetContents, hPutStr)+import Test.Tasty (TestTree, testGroup)+import Test.Tasty.HUnit++import Control.Exception qualified as E+import Data.ByteString.Lazy.Char8 qualified as BLC+import System.Process.Typed qualified as TP++import Atelier.Process.Typed+++test_Typed :: TestTree+test_Typed =+ testGroup+ "Atelier.Process.Typed"+ [ runProcessTests+ , runProcessUTests+ , readProcessTests+ , readProcessUTests+ , readProcessStdoutTests+ , readProcessStdoutUTests+ , readProcessStderrTests+ , readProcessStderrUTests+ , readProcessInterleavedTests+ , readProcessInterleavedUTests+ , startStopProcessTests+ , getExitCodeTests+ , checkExitCodeTests+ , exitCodeExceptionWithOutputTests+ , withProcessWaitTests+ , withProcessWaitUTests+ , withProcessTermTests+ , withProcessTermUTests+ , mkStreamSpecTests+ , mkPipeStreamSpecTests+ , streamTests+ , setterTests+ ]+++runProcessTests :: TestTree+runProcessTests =+ testGroup+ "runProcess"+ [ testCase "returns exit codes" do+ (ok, bad) <- runP $ (,) <$> runProcess (sh "exit 0") <*> runProcess (sh "exit 3")+ ok @?= ExitSuccess+ bad @?= ExitFailure 3+ , testCase "matches typed-process" do+ expected <- TP.runProcess (sh "exit 7")+ actual <- runP $ runProcess (sh "exit 7")+ actual @?= expected+ , testCase "discards output with nullStream" do+ code <- runP $ runProcess (setStdout nullStream (setStderr nullStream failing))+ code @?= ExitFailure 3+ ]+++runProcessUTests :: TestTree+runProcessUTests =+ testGroup+ "runProcess_"+ [ testCase "succeeds on exit 0" $ runP $ runProcess_ (sh "exit 0")+ , testCase "throws ExitCodeException on failure" do+ r <- try' $ runP $ runProcess_ (sh "exit 3")+ case r of+ Left e -> eceExitCode e @?= ExitFailure 3+ Right () -> assertFailure "expected ExitCodeException"+ ]+++readProcessTests :: TestTree+readProcessTests =+ testGroup+ "readProcess"+ [ testCase "returns exit code, stdout and stderr" do+ r <- runP $ readProcess failing+ r @?= (ExitFailure 3, "out\n", "err\n")+ , testCase "matches typed-process" do+ expected <- TP.readProcess failing+ actual <- runP $ readProcess failing+ actual @?= expected+ ]+++readProcessUTests :: TestTree+readProcessUTests =+ testGroup+ "readProcess_"+ [ testCase "returns stdout and stderr on success" do+ r <- runP $ readProcess_ (sh "echo a; echo b >&2")+ r @?= ("a\n", "b\n")+ , testCase "throws with captured output on failure" do+ r <- try' $ runP $ readProcess_ failing+ case r of+ Left e -> do+ eceExitCode e @?= ExitFailure 3+ eceStdout e @?= "out\n"+ eceStderr e @?= "err\n"+ Right _ -> assertFailure "expected ExitCodeException"+ ]+++readProcessStdoutTests :: TestTree+readProcessStdoutTests =+ testGroup+ "readProcessStdout"+ [ testCase "returns exit code and stdout" do+ r <- runP $ readProcessStdout (setStderr nullStream failing)+ r @?= (ExitFailure 3, "out\n")+ , testCase "closed stdin behaves like typed-process" do+ -- A closed stdin is unusable by `cat`; whatever typed-process does, we do too.+ expected <-+ TP.readProcessStdout (TP.setStderr TP.nullStream (TP.setStdin TP.closed (proc "cat" [])))+ actual <- runP $ readProcessStdout (setStderr nullStream (setStdin closed (proc "cat" [])))+ actual @?= expected+ ]+++readProcessStdoutUTests :: TestTree+readProcessStdoutUTests =+ testGroup+ "readProcessStdout_"+ [ testCase "returns stdout on success" do+ r <- runP $ readProcessStdout_ (sh "echo a")+ r @?= "a\n"+ , testCase "throws on failure" do+ r <- try' $ runP $ readProcessStdout_ (setStderr nullStream failing)+ case r of+ Left e -> do+ eceExitCode e @?= ExitFailure 3+ eceStdout e @?= "out\n"+ Right _ -> assertFailure "expected ExitCodeException"+ ]+++readProcessStderrTests :: TestTree+readProcessStderrTests =+ testGroup+ "readProcessStderr"+ [ testCase "returns exit code and stderr" do+ r <- runP $ readProcessStderr (setStdout nullStream failing)+ r @?= (ExitFailure 3, "err\n")+ ]+++readProcessStderrUTests :: TestTree+readProcessStderrUTests =+ testGroup+ "readProcessStderr_"+ [ testCase "returns stderr on success" do+ r <- runP $ readProcessStderr_ (sh "echo b >&2")+ r @?= "b\n"+ , testCase "throws on failure" do+ r <- try' $ runP $ readProcessStderr_ (setStdout nullStream failing)+ case r of+ Left e -> do+ eceExitCode e @?= ExitFailure 3+ eceStderr e @?= "err\n"+ Right _ -> assertFailure "expected ExitCodeException"+ ]+++readProcessInterleavedTests :: TestTree+readProcessInterleavedTests =+ testGroup+ "readProcessInterleaved"+ [ testCase "captures both streams" do+ (code, out) <- runP $ readProcessInterleaved failing+ code @?= ExitFailure 3+ -- Ordering between the streams is not guaranteed, but both are present.+ assertBool "stdout present" (isInfixOf' "out\n" (BLC.unpack out))+ assertBool "stderr present" (isInfixOf' "err\n" (BLC.unpack out))+ ]+++readProcessInterleavedUTests :: TestTree+readProcessInterleavedUTests =+ testGroup+ "readProcessInterleaved_"+ [ testCase "returns output on success" do+ r <- runP $ readProcessInterleaved_ (sh "echo a")+ r @?= "a\n"+ , testCase "throws on failure" do+ r <- try' $ runP $ readProcessInterleaved_ failing+ case r of+ Left e -> eceExitCode e @?= ExitFailure 3+ Right _ -> assertFailure "expected ExitCodeException"+ ]+++startStopProcessTests :: TestTree+startStopProcessTests =+ testGroup+ "startProcess / stopProcess / waitExitCode / getPid"+ [ testCase "start, wait and stop" do+ (code, mpid) <- runP do+ p <- startProcess (sh "exit 4")+ pid <- getPid p+ code <- waitExitCode p+ stopProcess p+ pure (code, pid)+ code @?= ExitFailure 4+ assertBool "pid was available" (mpid /= Nothing)+ , testCase "pipes round trip" do+ r <- runP $ withProcessWait (setStdin createPipe (setStdout createPipe (proc "cat" []))) \p ->+ liftIO do+ hPutStr (getStdin p) "ping"+ hClose (getStdin p)+ hGetContents (getStdout p) >>= \s -> length s `seq` pure s+ r @?= "ping"+ ]+++getExitCodeTests :: TestTree+getExitCodeTests =+ testGroup+ "getExitCode"+ [ testCase "Nothing while running, Just when finished" do+ (running, finished) <- runP do+ p <- startProcess (setStdin createPipe (proc "cat" []))+ running <- getExitCode p+ liftIO $ hClose (getStdin p)+ _ <- waitExitCode p+ finished <- getExitCode p+ stopProcess p+ pure (running, finished)+ running @?= Nothing+ finished @?= Just ExitSuccess+ ]+++checkExitCodeTests :: TestTree+checkExitCodeTests =+ testGroup+ "checkExitCode"+ [ testCase "passes on success" do+ ok <- try' $ runP do+ p <- startProcess (sh "exit 0")+ _ <- waitExitCode p+ checkExitCode p+ either (assertFailure . show) pure ok+ , testCase "throws on failure" do+ bad <- try' $ runP do+ p <- startProcess (sh "exit 5")+ _ <- waitExitCode p+ checkExitCode p+ case bad of+ Left e -> eceExitCode e @?= ExitFailure 5+ Right () -> assertFailure "expected ExitCodeException"+ ]+++exitCodeExceptionWithOutputTests :: TestTree+exitCodeExceptionWithOutputTests =+ testGroup+ "exitCodeExceptionWithOutput"+ [ testCase "includes captured output" do+ e <- runEff . runConcurrent . run $ do+ let cfg = setStdout byteStringOutput (setStderr byteStringOutput failing)+ p <- startProcess cfg+ _ <- waitExitCode p+ exitCodeExceptionWithOutput p+ eceExitCode e @?= ExitFailure 3+ eceStdout e @?= "out\n"+ eceStderr e @?= "err\n"+ ]+++withProcessWaitTests :: TestTree+withProcessWaitTests =+ testGroup+ "withProcessWait"+ [ testCase "returns the callback result" do+ r <- runP $ withProcessWait (sh "exit 0") \_ -> pure (42 :: Int)+ r @?= 42+ , testCase "callback can use surrounding effects" do+ (a, s) <- runEff . run . runReader ("env" :: String) . runState (0 :: Int)+ $ withProcessWait (sh "exit 0") \_ -> do+ addI 1+ e <- ask @String+ addI (length e)+ getI+ a @?= 4+ s @?= 4+ , testCase "state is kept when exception is caught in callback" do+ s <- runEff . run . execState (0 :: Int)+ $ withProcessWait (sh "exit 0") \_ -> do+ _ <- try @SomeException (putI 5 >> throwIO (userError "x"))+ addI 1+ s @?= 6+ ]+++withProcessWaitUTests :: TestTree+withProcessWaitUTests =+ testGroup+ "withProcessWait_"+ [ testCase "runs callback on success" do+ s <- runEff . run . execState (0 :: Int)+ $ withProcessWait_ (sh "exit 0") \_ -> putI 2+ s @?= 2+ , testCase "throws after the callback on failure" do+ r <- try' . runEff . run . runState (0 :: Int)+ $ withProcessWait_ (sh "exit 3") \_ -> putI 1+ case r of+ Left e -> eceExitCode e @?= ExitFailure 3+ Right _ -> assertFailure "expected ExitCodeException"+ ]+++withProcessTermTests :: TestTree+withProcessTermTests =+ testGroup+ "withProcessTerm"+ [ testCase "callback can use surrounding effects" do+ s <- runEff . run . execState (0 :: Int)+ $ withProcessTerm (sh "exit 0") \_ -> putI 9+ s @?= 9+ , testCase "terminates the process" do+ -- If the process were not terminated this would block for 30s.+ p <- runP $ withProcessTerm (sh "sleep 30") pure+ code <- TP.getExitCode p+ assertBool "process was terminated" (code /= Just ExitSuccess && code /= Nothing)+ , testCase "propagates exceptions from the callback" do+ r <- E.try @SomeException . runP+ $ withProcessTerm (sh "sleep 30") \_ -> throwIO (userError "boom") :: Eff Es ()+ case r of+ Left e -> assertBool "original exception" ("boom" `isInfixOf'` show e)+ Right () -> assertFailure "expected exception"+ ]+++withProcessTermUTests :: TestTree+withProcessTermUTests =+ testGroup+ "withProcessTerm_"+ [ testCase "throws when the process is terminated" do+ -- Termination makes the process exit non-zero, so the underscore+ -- variant throws, exactly like typed-process.+ r <- try' . runEff . run . runState (0 :: Int)+ $ withProcessTerm_ (sh "sleep 30") \_ -> putI 2+ case r of+ Left e -> eceExitCode e @?= ExitFailure (-15)+ Right _ -> assertFailure "expected ExitCodeException"+ ]+++mkStreamSpecTests :: TestTree+mkStreamSpecTests =+ testGroup+ "mkStreamSpec"+ [ testCase "runs setup and cleanup in Eff" do+ (a, s) <- runEff . run . runState ([] :: [String]) $ do+ spec <- mkStreamSpec NoStream \_ _ -> do+ logL "setup"+ pure ("value" :: String, logL "cleanup")+ withProcessWait (setStdout spec (sh "exit 0")) \p -> do+ logL "body"+ pure (getStdout p)+ a @?= "value"+ s @?= ["setup", "body", "cleanup"]+ , testCase "matches typed-process" do+ expected <- do+ let spec = TP.mkStreamSpec CreatePipe \_ mh -> pure (mh /= Nothing, pure ())+ TP.withProcessWait (TP.setStdout spec (sh "exit 0")) (pure . TP.getStdout)+ actual <- runP do+ spec <- mkStreamSpec CreatePipe \_ mh -> pure (mh /= Nothing, pure ())+ withProcessWait (setStdout spec (sh "exit 0")) (pure . getStdout)+ actual @?= expected+ ]+++mkPipeStreamSpecTests :: TestTree+mkPipeStreamSpecTests =+ testGroup+ "mkPipeStreamSpec"+ [ testCase "passes the pipe handle and runs cleanup in Eff" do+ (r, s) <- runEff . run . runState (0 :: Int) $ do+ spec <- mkPipeStreamSpec \_ h -> do+ addI 1+ out <- liftIO $ hGetContents h >>= \c -> length c `seq` pure c+ pure (out, addI 10)+ withProcessWait (setStdout spec (sh "printf piped")) (pure . getStdout)+ r @?= "piped"+ s @?= 11+ ]+++-- | Built-in stream specs, observed through the lifted functions.+streamTests :: TestTree+streamTests =+ testGroup+ "stream specs"+ [ testCase "byteStringInput" do+ r <- runP $ readProcessStdout_ (setStdin (byteStringInput "hello") (proc "cat" []))+ r @?= "hello"+ , testCase "byteStringOutput via STM" do+ (out, err) <- runEff . runConcurrent . run $ do+ p <- startProcess (setStdout byteStringOutput (setStderr byteStringOutput failing))+ _ <- waitExitCode p+ (,) <$> atomically (getStdout p) <*> atomically (getStderr p)+ out @?= "out\n"+ err @?= "err\n"+ ]+++-- | Config smart constructors and setters, observed through the lifted functions.+setterTests :: TestTree+setterTests =+ testGroup+ "config"+ [ testCase "setEnv" do+ r <- runP $ readProcessStdout_ (setEnv [("EFF_TP_VAR", "bar")] (sh "printf %s \"$EFF_TP_VAR\""))+ r @?= "bar"+ , testCase "setWorkingDir" do+ r <- runP $ readProcessStdout_ (setWorkingDir "/" (sh "pwd"))+ r @?= "/\n"+ , testCase "shell" do+ r <- runP $ readProcessStdout_ (shell "echo $((1 + 2))")+ r @?= "3\n"+ ]+++-- * Helpers+++type Es = '[TypedProcess, IOE]+++runP :: Eff Es a -> IO a+runP = runEff . run+++sh :: String -> ProcessConfig () () ()+sh script = proc "sh" ["-c", script]+++-- | Exits with 'ExitFailure' @3@ with some output on both streams.+failing :: ProcessConfig () () ()+failing = sh "echo out; echo err >&2; exit 3"+++try' :: IO a -> IO (Either ExitCodeException a)+try' = E.try+++isInfixOf' :: String -> String -> Bool+isInfixOf' needle hay = any (\t -> take (length needle) t == needle) (tails' hay)+ where+ tails' [] = [[]]+ tails' xs@(_ : t) = xs : tails' t+++addI :: (State Int :> es) => Int -> Eff es ()+addI n = modify (+ n)+++putI :: (State Int :> es) => Int -> Eff es ()+putI = put+++getI :: (State Int :> es) => Eff es Int+getI = get+++logL :: (State [String] :> es) => String -> Eff es ()+logL s = modify (++ [s])
+ test/Main.hs view
@@ -0,0 +1,2 @@+{-# OPTIONS_GHC -F -pgmF tasty-discover -optF --tree-display #-}+