tmp-postgres 0.1.0.7 → 0.1.0.8
raw patch · 4 files changed
+27/−28 lines, 4 filesdep ~process
Dependency ranges changed: process
Files
- README.md +2/−2
- src/Database/Postgres/Temp/Internal.hs +19/−21
- test/Database/Postgres/Temp/InternalSpec.hs +2/−1
- tmp-postgres.cabal +4/−4
README.md view
@@ -1,7 +1,7 @@ [](http://travis-ci.org/jfischoff/tmp-postgres) # tmp-postgres -`tmp-postgres` is a libary for greating a temporary `postgres` instance on a random port for testing.+`tmp-postgres` is a library for creating a temporary `postgres` instance on a random port for testing. ```haskell result <- start []@@ -12,7 +12,7 @@ stop tempDB ``` -#Installation+# Installation ## macOS ```
src/Database/Postgres/Temp/Internal.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE RecordWildCards, LambdaCase, ScopedTypeVariables #-}+{-# LANGUAGE RecordWildCards, LambdaCase, ScopedTypeVariables, DeriveDataTypeable #-} module Database.Postgres.Temp.Internal where import System.IO.Temp import System.Process@@ -10,6 +10,7 @@ import Network.Socket import Control.Exception import Data.Typeable+import System.Posix.Signals openFreePort :: IO Int openFreePort = bracket (socket AF_INET Stream defaultProtocol) close $ \s -> do@@ -46,9 +47,9 @@ fourth :: (a, b, c, d) -> d fourth (_, _, _, x) = x -shellWith :: Handle -> Handle -> String -> CreateProcess-shellWith stdOut stdErr cmd =- (shell cmd)+procWith :: Handle -> Handle -> String -> [String] -> CreateProcess+procWith stdOut stdErr cmd args =+ (proc cmd args) { std_err = UseHandle stdErr , std_out = UseHandle stdOut }@@ -85,9 +86,9 @@ ClosedHandle _ -> return "" ) -runProcessWith :: Handle -> Handle -> String -> String -> IO ExitCode-runProcessWith stdOut stdErr name cmd- = createProcess_ name (shellWith stdOut stdErr cmd)+runProcessWith :: Handle -> Handle -> String -> String -> [String] -> IO ExitCode+runProcessWith stdOut stdErr name cmd args+ = createProcess_ name (procWith stdOut stdErr cmd args) >>= waitForProcess . fourth -- | Start postgres and pass in handles for stdout and stderr@@ -134,7 +135,7 @@ logger InitDB initDBExitCode <- runProcessWith stdOut stdErr "initdb"- ("initdb --nosync -E UNICODE -A trust -D " ++ dataDir)+ "initdb" ["-E", "UNICODE", "-A", "trust", "-D", dataDir] throwIfError InitDBFailed initDBExitCode logger WriteConfig@@ -145,16 +146,12 @@ -- slight race here, the port might not be free anymore! let connectionString = "postgresql:///test?host=" ++ mainDir ++ "&port=" ++ show port logger StartPostgres- let extraOptions = unwords $ map (\(key, value) -> "--" ++ key ++ "=" ++ value) options+ let extraOptions = map (\(key, value) -> "--" ++ key ++ "=" ++ value) options bracketOnError ( fmap (DB mainDir connectionString . fourth) $ createProcess_ "postgres"- ( shellWith stdOut stdErr- $ "postgres -D "- ++ dataDir- ++ " -p "- ++ show port- ++ " "- ++ extraOptions+ ( procWith stdOut stdErr+ "postgres"+ $ ["-D", dataDir, "-p", show port] ++ extraOptions ) ) stop@@ -165,10 +162,7 @@ logger CreateDB throwIfError CreateDBFailed =<< runProcessWith stdOut stdErr "createDB"- ( "createDB --host=" ++ mainDir- ++ " --port=" ++ show port- ++ " test"- )+ "createdb" ["-h", mainDir, "-p", show port, "test"] logger Finished return result@@ -188,7 +182,11 @@ -- | Stop postgres and clean up the temporary database folder. stop :: DB -> IO ExitCode stop DB {..} = do- terminateProcess pid+ withProcessHandle pid (\case+ OpenHandle p -> signalProcess sigINT p+ ClosedHandle _ -> return ()+ )+ result <- waitForProcess pid removeDirectoryRecursive mainDir return result
test/Database/Postgres/Temp/InternalSpec.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-} module Database.Postgres.Temp.InternalSpec (main, spec) where import Test.Hspec import System.IO.Temp@@ -12,6 +12,7 @@ import Database.PostgreSQL.Simple import qualified Data.ByteString.Char8 as BSC import System.Exit+import Control.Applicative ((<$>)) main :: IO () main = hspec spec
tmp-postgres.cabal view
@@ -1,8 +1,8 @@ name: tmp-postgres-version: 0.1.0.7+version: 0.1.0.8 synopsis: Start and stop a temporary postgres for testing description:- This module provides functions greating a temporary postgres instance on a random port for testing.+ This module provides functions creating a temporary postgres instance on a random port for testing. . > result <- start [] > case result of@@ -15,7 +15,7 @@ methods of dealing with @stdout@ and @stderr@. . The start methods use a config based on the one used by pg_tmp (http://ephemeralpg.org/), but can be overriden- by in different values to the first argument of the start functions.+ by different values to the first argument of the start functions. . WARNING!! Ubuntu's PostgreSQL installation does not put @initdb@ on the @PATH@. We need to add it manually. The necessary binaries are in the @\/usr\/lib\/postgresql\/VERSION\/bin\/@ directory, and should be added to the @PATH@@@ -39,7 +39,7 @@ , Database.Postgres.Temp.Internal build-depends: base >= 4.6 && < 5 , temporary- , process+ , process >= 1.2.0.0 , unix , directory , network