Skip to main content

HMock Compatibility (Part 3)

I am testing HMock compatibility using hr, which has trivial tests and allows me to focus on HMock. Using my quick fix to make HMock work with GHC 8.6 and 8.8, I experimented with configuring the tests so that the HMock tests are only run when using a supported GHC version. There are various ways to do this, and I decided to put the HMock tests in a separate module in order to minimize use of CPP macros. It works without issue.

The test suite is configured in hr.cabal as follows:

test-suite hr-test
  type: exitcode-stdio-1.0
  hs-source-dirs: test
  main-is: Spec.hs
  other-modules:
      HR.Test
  build-depends:
      base
    , hr
    , tasty >=1.0 && <1.5
    , tasty-hunit >=0.10 && <0.11
  if impl(ghc >= 8.6.1)
    other-modules:
      HR.HMock.Test
    build-depends:
      HMock
  default-language: Haskell2010
  ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N

The conditional adds the test module that uses HMock as well as the HMock dependency when using GHC 8.6 or newer.

The only other change is in Spec.hs:

{-# LANGUAGE CPP #-}

module Main (main) where

-- https://hackage.haskell.org/package/tasty
import Test.Tasty (defaultMain, testGroup)

-- (hr:test)
import qualified HR.Test
#if __GLASGOW_HASKELL__ >= 806
import qualified HR.HMock.Test
#endif

------------------------------------------------------------------------------

main :: IO ()
main = defaultMain $ testGroup "test"
    [ HR.Test.tests
#if __GLASGOW_HASKELL__ >= 806
    , HR.HMock.Test.tests
#endif
    ]

CPP macros are used to import the module that uses HMock and include those tests only when using GHC 8.6 or newer.

My current test uses my local fork of HMock that includes the MonadUnliftIO instance fix, so I am not pushing it yet. When the issue is really fixed, either by figuring out how to configure Stack to avoid the coercion error or when my fix is merged upstream, I plan on releasing these changes in hr.