packages feed

casa-reoc-0.1.0.0: test/doctest_tests.hs

{-# LANGUAGE CPP #-}

-- \| Runs doctest on every module with examples, through @cabal exec@ with the
-- compiler and build directory that this test was built with, so that
-- @cabal test doctest -w ghc-9.6.7 --builddir=dist-newstyle-9.6.7@ uses
-- GHC 9.6.7 throughout. The doctest executable must be built with the same GHC.
import Control.Monad (unless)
import Data.List (isPrefixOf, tails)
import System.Environment (getExecutablePath)
import System.Exit (ExitCode (..), exitFailure)
import System.Process (rawSystem)

-- The build directory: the part of the path of this executable before its /build/.
buildDirectory :: FilePath -> FilePath
buildDirectory path =
  case [i | (i, t) <- zip [0 ..] (tails path), "/build/" `isPrefixOf` t] of
    i : _ -> take i path
    [] -> "dist-newstyle"

main :: IO ()
main = do
  builddir <- buildDirectory <$> getExecutablePath
  results <-
    mapM
      ( \f ->
          rawSystem
            "cabal"
            ["exec", "-v0", "-w", "ghc-" <> __GLASGOW_HASKELL_FULL_VERSION__, "--builddir=" <> builddir, "--", "doctest", "-isrc", "-iexamples/src", "-XHaskell2010", f]
      )
      [ "examples/src/ReOC/Example/Draft.hs"
      , "src/ReOC/Amendment.hs"
      , "src/ReOC/Primitive/ABN.hs"
      , "src/ReOC/Primitive/ARN.hs"
      , "src/ReOC/Primitive/AtMost.hs"
      , "src/ReOC/Primitive/EmailAddress.hs"
      , "src/ReOC/Primitive/FormText.hs"
      , "src/ReOC/Primitive/GrossWeight.hs"
      , "src/ReOC/Primitive/PassMark.hs"
      , "src/ReOC/Primitive/Percentage.hs"
      , "src/ReOC/Primitive/PhoneNumber.hs"
      , "src/ReOC/Primitive/Positive.hs"
      , "src/ReOC/Primitive/Postcode.hs"
      , "src/ReOC/Primitive/ReviewMonths.hs"
      , "src/ReOC/Primitive/Some.hs"
      , "src/ReOC/Primitive/Text1.hs"
      , "src/ReOC/Render/Base64.hs"
      , "src/ReOC/Render/BuildInfo.hs"
      , "src/ReOC/Render/Error.hs"
      , "src/ReOC/Render/Markdown.hs"
      , "src/ReOC/Render/OperationsManual/Holes.hs"
      , "src/ReOC/Render/Template.hs"
      , "src/ReOC/Render/Text.hs"
      , "src/ReOC/Render/TrainingManual/Markup.hs"
      ]
  unless (all (== ExitSuccess) results) exitFailure