Repository: hasura/graphql-engine
File: server/src-test/Main.hs (Haskell)

Code before:
import           Test.Hspec

import qualified Data.Aeson                   as A
import qualified Data.ByteString.Lazy.Char8   as BL
import qualified Database.PG.Query            as Q
import qualified Network.HTTP.Client          as HTTP
import qualified Network.HTTP.Client.TLS      as HTTP
import qualified Test.Hspec.Runner            as Hspec

import           Hasura.Db                    (PGExecCtx (..))
import           Hasura.RQL.Types             (SQLGenCtx (..), adminUserInfo)
import           Hasura.RQL.Types.Run
import           Hasura.Server.Init           (RawConnInfo, mkConnInfo, mkRawConnInfo,
                                               parseRawConnInfo, runWithEnv)
import           Hasura.Server.Migrate

import qualified Data.Parser.CacheControlSpec as CacheControlParser
import qualified Hasura.IncrementalSpec       as IncrementalSpec
import qualified Hasura.RQL.MetadataSpec      as MetadataSpec
import qualified Hasura.Server.MigrateSpec    as MigrateSpec

data TestSuites
  = AllSuites !RawConnInfo
  -- ^ Run all test suites. It probably doesn't make sense to be able to specify additional 
  -- hspec args here.
  | SingleSuite ![String] !TestSuite
  -- ^ Args to pass through to hspec (as if from 'getArgs'), and the specific suite to run.

data TestSuite
  = UnitSuite
  | PostgresSuite !RawConnInfo

main :: IO ()
main = parseArgs >>= \case
  AllSuites pgConnOptions -> do
    postgresSpecs <- buildPostgresSpecs pgConnOptions
    runHspec [] (unitSpecs *> postgresSpecs)
  SingleSuite hspecArgs suite -> runHspec hspecArgs =<< case suite of
    UnitSuite                   -> pure unitSpecs
    PostgresSuite pgConnOptions -> buildPostgresSpecs pgConnOptions

unitSpecs :: Spec
unitSpecs = do
  describe "Data.Parser.CacheControl" CacheControlParser.spec
  describe "Hasura.Incremental" IncrementalSpec.spec
  describe "Hasura.RQL.Metadata" MetadataSpec.spec

buildPostgresSpecs :: RawConnInfo -> IO Spec
buildPostgresSpecs pgConnOptions = do
  env <- getEnvironment


Review comment (suggestion):
I’m not totally convinced by this comment! I think it makes as much sense to specify additional hspec arguments  when running all the test suites as it does when running just one.

Originally I considered making these truly different test suites, in the Cabal sense. We could have `test-suite graphql-engine-unit-tests` and `test-suite graphql-engine-postgres-tests`. However, working with multiple test suites can be a little awkward, especially with `stack`, so I decided to only have one test suite that can be configured via options. `cabal new-run` makes this a lot simpler, though, so maybe that would be a simpler solution now. I don’t feel strongly either way.

Code after:
import           Test.Hspec

import qualified Data.Aeson                   as A
import qualified Data.ByteString.Lazy.Char8   as BL
import qualified Database.PG.Query            as Q
import qualified Network.HTTP.Client          as HTTP
import qualified Network.HTTP.Client.TLS      as HTTP
import qualified Test.Hspec.Runner            as Hspec

import           Hasura.Db                    (PGExecCtx (..))
import           Hasura.RQL.Types             (SQLGenCtx (..), adminUserInfo)
import           Hasura.RQL.Types.Run
import           Hasura.Server.Init           (RawConnInfo, mkConnInfo, mkRawConnInfo,
                                               parseRawConnInfo, runWithEnv)
import           Hasura.Server.Migrate
import           Hasura.Server.Version

import qualified Data.Parser.CacheControlSpec as CacheControlParser
import qualified Hasura.IncrementalSpec       as IncrementalSpec
import qualified Hasura.RQL.MetadataSpec      as MetadataSpec
import qualified Hasura.Server.MigrateSpec    as MigrateSpec

data TestSuites
  = AllSuites !RawConnInfo
  -- ^ Run all test suites. It probably doesn't make sense to be able to specify additional 
  -- hspec args here.
  | SingleSuite ![String] !TestSuite
  -- ^ Args to pass through to hspec (as if from 'getArgs'), and the specific suite to run.

data TestSuite
  = UnitSuite
  | PostgresSuite !RawConnInfo

main :: IO ()
main = withVersion $$(getVersionFromEnvironment) $ parseArgs >>= \case
  AllSuites pgConnOptions -> do
    postgresSpecs <- buildPostgresSpecs pgConnOptions
    runHspec [] (unitSpecs *> postgresSpecs)
  SingleSuite hspecArgs suite -> runHspec hspecArgs =<< case suite of
    UnitSuite                   -> pure unitSpecs
    PostgresSuite pgConnOptions -> buildPostgresSpecs pgConnOptions

unitSpecs :: Spec
unitSpecs = do
  describe "Data.Parser.CacheControl" CacheControlParser.spec
  describe "Hasura.Incremental" IncrementalSpec.spec
  describe "Hasura.RQL.Metadata" MetadataSpec.spec

buildPostgresSpecs :: (HasVersion) => RawConnInfo -> IO Spec
buildPostgresSpecs pgConnOptions = do
  env <- getEnvironment