{-# 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) whereimport 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.HUnitimport Control.Exception qualified as Eimport Data.ByteString.Lazy.Char8 qualified as BLCimport System.Process.Typed qualified as TPimport Atelier.Process.Typedtest_Typed :: TestTreetest_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 :: TestTreerunProcessTests = 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 :: TestTreerunProcessUTests = 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 :: TestTreereadProcessTests = 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 :: TestTreereadProcessUTests = 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 :: TestTreereadProcessStdoutTests = 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 :: TestTreereadProcessStdoutUTests = 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 :: TestTreereadProcessStderrTests = testGroup "readProcessStderr" [ testCase "returns exit code and stderr" do r <- runP $ readProcessStderr (setStdout nullStream failing) r @?= (ExitFailure 3, "err\n") ]readProcessStderrUTests :: TestTreereadProcessStderrUTests = 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 :: TestTreereadProcessInterleavedTests = 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 :: TestTreereadProcessInterleavedUTests = 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 :: TestTreestartStopProcessTests = 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 :: TestTreegetExitCodeTests = 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 :: TestTreecheckExitCodeTests = 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 :: TestTreeexitCodeExceptionWithOutputTests = 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 :: TestTreewithProcessWaitTests = 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 :: TestTreewithProcessWaitUTests = 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 :: TestTreewithProcessTermTests = 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 :: TestTreewithProcessTermUTests = 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 :: TestTreemkStreamSpecTests = 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 :: TestTreemkPipeStreamSpecTests = 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 :: TestTreestreamTests = 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 :: TestTreesetterTests = 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" ]-- * Helperstype Es = '[TypedProcess, IOE]runP :: Eff Es a -> IO arunP = runEff . runsh :: 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.tryisInfixOf' :: String -> String -> BoolisInfixOf' needle hay = any (\t -> take (length needle) t == needle) (tails' hay) where tails' [] = [[]] tails' xs@(_ : t) = xs : tails' taddI :: (State Int :> es) => Int -> Eff es ()addI n = modify (+ n)putI :: (State Int :> es) => Int -> Eff es ()putI = putgetI :: (State Int :> es) => Eff es IntgetI = getlogL :: (State [String] :> es) => String -> Eff es ()logL s = modify (++ [s])