Skip to content

Commit

Permalink
Use a temp file for test db name
Browse files Browse the repository at this point in the history
Don't hardcode 'dist/' as the working directory for direct-sqlite unit
tests temp database files.  Instead, just create a random temp file
and stick it into the current working directory.

Fix for #60
  • Loading branch information
nurpax committed Jan 31, 2016
1 parent 22cddea commit d757181
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
1 change: 1 addition & 0 deletions direct-sqlite.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ test-suite test
, directory
, HUnit
, direct-sqlite
, temporary
, text
40 changes: 15 additions & 25 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import System.Directory
import System.Exit (exitFailure)
import System.IO
import System.IO.Error (isDoesNotExistError, isUserError)
import System.IO.Temp (withTempFile)
import System.Timeout (timeout)
import Test.HUnit

Expand Down Expand Up @@ -875,16 +876,13 @@ testMultiRowInsert TestEnv{..} = TestCase $ do
return ()


sharedDBPath :: Text
sharedDBPath = "dist/test/direct-sqlite-test-database.db"

withTestEnv :: (TestEnv -> IO a) -> IO a
withTestEnv cb =
withTestEnv :: String -> (TestEnv -> IO a) -> IO a
withTestEnv tempDbName cb =
withConn $ \conn ->
cb TestEnv
{ conn = conn
, withConn = withConn
, withConnShared = withConnPath sharedDBPath
, withConnShared = withConnPath (T.pack tempDbName)
}
where
withConn = withConnPath ":memory:"
Expand All @@ -898,28 +896,20 @@ withTestEnv cb =
close conn
return r

runTestGroup :: [TestEnv -> Test] -> IO Bool
runTestGroup tests = do
runTestGroup :: String -> [TestEnv -> Test] -> IO Bool
runTestGroup tempDbName tests = do
Counts{cases, tried, errors, failures} <-
withTestEnv $ \env -> runTestTT $ TestList $ map ($ env) tests
withTestEnv tempDbName $ \env -> runTestTT $ TestList $ map ($ env) tests
return (cases == tried && errors == 0 && failures == 0)

main :: IO ()
main = do
mapM_ (`hSetBuffering` LineBuffering) [stdout, stderr]

T.putStrLn $ "Creating " `T.append` sharedDBPath
handleJust (\e -> if isDoesNotExistError e
then Just ()
else Nothing)
(\_ -> return ())
(removeFile $ T.unpack sharedDBPath)
open sharedDBPath >>= close

ok <- runTestGroup regressionTests
when (not ok) exitFailure

-- Signal failure if feature tests fail. I'd rather print a noisy warning
-- instead, but cabal redirects test output to log files by default.
ok <- runTestGroup featureTests
when (not ok) exitFailure
withTempFile "." "direct-sqlite-test-database" $ \tempDbName _hFile -> do
open (T.pack tempDbName) >>= close
ok <- runTestGroup tempDbName regressionTests
when (not ok) exitFailure
-- Signal failure if feature tests fail. I'd rather print a noisy warning
-- instead, but cabal redirects test output to log files by default.
ok <- runTestGroup tempDbName featureTests
when (not ok) exitFailure

0 comments on commit d757181

Please sign in to comment.