graphql-engine/server/tests-hspec/SpecHook.hs
Daniel Harvey 744c03a84e tests: create BigQuery datasets for each test
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5288
GitOrigin-RevId: 7caedacd280fc6f8ea71af16397bb2c9641effc8
2022-08-08 14:29:51 +00:00

41 lines
1.2 KiB
Haskell

module SpecHook
( hook,
setupTestEnvironment,
teardownTestEnvironment,
)
where
import Control.Exception.Safe (bracket)
import Data.UUID.V4 (nextRandom)
import Harness.GraphqlEngine (startServerThread)
import Harness.TestEnvironment (TestEnvironment (..), stopServer)
import Hasura.Prelude
import System.Environment (lookupEnv)
import System.Log.FastLogger qualified as FL
import Test.Hspec (Spec, SpecWith, aroundAllWith)
setupTestEnvironment :: IO TestEnvironment
setupTestEnvironment = do
murlPrefix <- lookupEnv "HASURA_TEST_URLPREFIX"
mport <- fmap (>>= readMaybe) (lookupEnv "HASURA_TEST_PORT")
server <- startServerThread ((,) <$> murlPrefix <*> mport)
let logType = FL.LogFileNoRotate "tests-hspec.log" 1024
(logger, loggerCleanup) <- FL.newFastLogger logType
uniqueTestId <- nextRandom
pure
TestEnvironment
{ server = server,
uniqueTestId = uniqueTestId,
backendType = Nothing,
logger = logger,
loggerCleanup = loggerCleanup
}
teardownTestEnvironment :: TestEnvironment -> IO ()
teardownTestEnvironment TestEnvironment {..} = do
stopServer server
loggerCleanup
hook :: SpecWith TestEnvironment -> Spec
hook = aroundAllWith (const . bracket setupTestEnvironment teardownTestEnvironment)