sydtest-process (empty) → 0.0.0.0
raw patch · 5 files changed
+115/−0 lines, 5 filesdep +basedep +bytestringdep +process
Dependencies added: base, bytestring, process, sydtest, sydtest-process
Files
- LICENSE.md +5/−0
- src/Test/Syd/Process.hs +34/−0
- sydtest-process.cabal +54/−0
- test/Spec.hs +1/−0
- test/Test/Syd/ProcessSpec.hs +21/−0
+ LICENSE.md view
@@ -0,0 +1,5 @@+# Sydtest License++Copyright (c) 2021 Tom Sydney Kerckhove++See the Sydtest License at https://github.com/NorfairKing/sydtest/blob/master/sydtest/LICENSE.md for the full license text.
+ src/Test/Syd/Process.hs view
@@ -0,0 +1,34 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE TypeOperators #-}++module Test.Syd.Process where++import System.IO+import System.Process+import Test.Syd++-- | Run a given process while a test is running and give access to a process handle as an inner resource.+--+-- See 'processSetupFunc'.+processSpec :: CreateProcess -> TestDefM outers (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) result -> TestDefM outers () result+processSpec cp = setupAround $ processSetupFunc cp++-- | Run a given process while a group of tests is running and give access to a process handle as an outer resource.+--+-- See 'processSetupFunc'.+--+-- == __FOOTGUN__+--+-- The process will be shared accross multiple tests.+-- This may well be a good idea because starting the process can be prohibitively expensive to do around every test.+-- However, sharing the process means that tests could be sharing state.+-- When using this function, it is important to implement some form of cleaning the state before every test.+-- It is also important to use 'sequential' if some such state is maintained (and cleaned) so that the state is not cleaned while another test is running.+outerProcessSpec :: CreateProcess -> TestDefM ((Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) ': outers) inner result -> TestDefM outers inner result+outerProcessSpec cp = setupAroundAll $ processSetupFunc cp++-- | Set up a process beforehand and stop it afterwards.+--+-- The process will be terminated using 'cleanupProcess'.+processSetupFunc :: CreateProcess -> SetupFunc (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)+processSetupFunc cp = bracketSetupFunc (createProcess cp) cleanupProcess
+ sydtest-process.cabal view
@@ -0,0 +1,54 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.4.+--+-- see: https://github.com/sol/hpack++name: sydtest-process+version: 0.0.0.0+synopsis: A typed-process companion library for sydtest+category: Testing+homepage: https://github.com/NorfairKing/sydtest#readme+bug-reports: https://github.com/NorfairKing/sydtest/issues+author: Tom Sydney Kerckhove+maintainer: syd@cs-syd.eu+copyright: Copyright (c) 2021 Tom Sydney Kerckhove+license: OtherLicense+license-file: LICENSE.md+build-type: Simple++source-repository head+ type: git+ location: https://github.com/NorfairKing/sydtest++library+ exposed-modules:+ Test.Syd.Process+ other-modules:+ Paths_sydtest_process+ hs-source-dirs:+ src+ build-depends:+ base >=4.7 && <5+ , process+ , sydtest+ default-language: Haskell2010++test-suite sydtest-process-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ Test.Syd.ProcessSpec+ Paths_sydtest_process+ hs-source-dirs:+ test+ ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall+ build-tool-depends:+ sydtest-discover:sydtest-discover+ build-depends:+ base >=4.7 && <5+ , bytestring+ , process+ , sydtest+ , sydtest-process+ default-language: Haskell2010
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF sydtest-discover #-}
+ test/Test/Syd/ProcessSpec.hs view
@@ -0,0 +1,21 @@+{-# LANGUAGE OverloadedStrings #-}+{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}++module Test.Syd.ProcessSpec (spec) where++import qualified Data.ByteString as SB+import System.IO+import System.Process+import Test.Syd+import Test.Syd.Process++spec :: Spec+spec = processSpec ((proc "cat" []) {std_in = CreatePipe, std_out = CreatePipe, std_err = Inherit}) $+ it "can communicate with the process" $ \(Just inHandle, Just outHandle, _, _) -> do+ let contents = "Hello world"+ hSetBuffering inHandle NoBuffering+ hSetBuffering outHandle NoBuffering+ SB.hPut inHandle contents+ hFlush inHandle+ output <- SB.hGet outHandle (SB.length contents)+ output `shouldBe` contents