Skip to content

Commit

Permalink
Account for cpp-options and ghc-options (#73)
Browse files Browse the repository at this point in the history
Fixes #71
  • Loading branch information
martijnbastiaan authored Aug 3, 2023
1 parent 6dd8cb6 commit b793f25
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGES.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 0.3.1
* Dropped support for GHC 8.2
* Add `--ghc-arg` as a command line argument, allowing users to pass additional arguments to GHC used to parse Haddock.
* Add `--ghc-arg` as a command line argument, allowing users to pass additional arguments to GHC used to parse Haddock. ([#71](https://github.com/martijnbastiaan/doctest-parallel/issues/71))
* CPP options and GHC options (Cabal fields: `cpp-options` and `ghc-options`) are now passed to GHC when parsing source files ([#71](https://github.com/martijnbastiaan/doctest-parallel/issues/71))

# 0.3.0.1
* Add support for GHC 9.6
Expand Down
1 change: 1 addition & 0 deletions doctest-parallel.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ test-suite spectests
MainSpec
OptionsSpec
ParseSpec
ProjectsSpec
PropertySpec
Runner.ExampleSpec
RunnerSpec
Expand Down
9 changes: 7 additions & 2 deletions src/Test/DocTest/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import Distribution.Types.UnqualComponentName ( unUnqualComponentName )
import Distribution.PackageDescription
( GenericPackageDescription (condLibrary)
, exposedModules, libBuildInfo, hsSourceDirs, defaultExtensions, package
, packageDescription, condSubLibraries, includeDirs, autogenModules, ConfVar(..) )
, packageDescription, condSubLibraries, includeDirs, autogenModules
, ConfVar(..), BuildInfo (cppOptions), hcOptions )

import Distribution.Compiler (CompilerFlavor(GHC))
import Distribution.Pretty (prettyShow)
Expand Down Expand Up @@ -69,6 +70,8 @@ data Library = Library
-- ^ Exposed modules
, libDefaultExtensions :: [Extension]
-- ^ Extensions enabled by default
, libFlags :: [String]
-- ^ Flags found in 'cpp-options' and 'gcc-options'
}
deriving (Show)

Expand All @@ -79,6 +82,7 @@ mergeLibraries libs = Library
, libCSourceDirectories = concatMap libCSourceDirectories libs
, libModules = concatMap libModules libs
, libDefaultExtensions = concatMap libDefaultExtensions libs
, libFlags = concatMap libFlags libs
}

-- | Convert a "Library" to arguments suitable to be passed to GHCi.
Expand All @@ -88,7 +92,7 @@ libraryToGhciArgs Library{..} = (hsSrcArgs <> cSrcArgs, modArgs, extArgs)
hsSrcArgs = map ("-i" <>) libSourceDirectories
cSrcArgs = map ("-I" <>) libCSourceDirectories
modArgs = map prettyShow libModules
extArgs = map showExt libDefaultExtensions
extArgs = map showExt libDefaultExtensions <> libFlags

showExt = \case
EnableExtension ext -> "-X" <> show ext
Expand Down Expand Up @@ -230,6 +234,7 @@ extractSpecificCabalLibrary maybeLibName pkgPath = do
, libCSourceDirectories = map (root </>) cSourceDirs
, libModules = exposedModules lib `rmList` autogenModules buildInfo
, libDefaultExtensions = defaultExtensions buildInfo
, libFlags = cppOptions buildInfo <> hcOptions GHC buildInfo
}
where
buildInfo = libBuildInfo lib
Expand Down
49 changes: 49 additions & 0 deletions test/ProjectsSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module ProjectsSpec where

import qualified Test.Hspec
import Test.Hspec hiding (it)
import Test.HUnit

import Data.List (isInfixOf)
import Data.Maybe
import System.Environment
import System.Exit
import System.FilePath
import System.IO.Unsafe
import System.Process
import Text.Printf

main :: IO ()
main = hspec spec

type LibName = String
type NumberOfTestsExpected = Word

-- | Run 'cabal run doctests' in a project in projects/, expect a number of
-- succeeded tests.
runProject :: LibName -> NumberOfTestsExpected -> Assertion
runProject libName nTests = do
(exitCode, _stdout, stderr) <- readCreateProcessWithExitCode process ""
assertEqual ("'cabal run doctests' succeeded for " <> libName) exitCode ExitSuccess
assertBool ("expected\n\n" <> expect <> "\n\nin\n\n" <> stderr) (expect `isInfixOf` stderr)
where
expect = printf "Examples: %d Tried: %d Errors: 0 Unexpected output: 0" nTests nTests
process = (proc "cabal" ["run", "doctests"])
{ cwd = Just ("test" </> "projects" </> "cpp-options")
}

-- | 'it' or 'xit', depending on whether we run in a Nix/Stack context. Nix
-- doesn't have Cabal available, so the tests will fail. This is mostly a
-- workaround for CI.
ignoreIfNixOrStack :: Example a => String -> a -> SpecWith (Arg a)
ignoreIfNixOrStack = unsafePerformIO $ do
stack <- fmap isJust (lookupEnv "STACK_EXE")
nix <- fmap isJust (lookupEnv "NIX_BUILD_TOP")
if stack || nix
then pure Test.Hspec.xit
else pure Test.Hspec.it


spec :: Spec
spec = do
ignoreIfNixOrStack "cpp-projects" (runProject "cpp-options" 1)
2 changes: 2 additions & 0 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import qualified LocationSpec
import qualified MainSpec
import qualified OptionsSpec
import qualified ParseSpec
import qualified ProjectsSpec
import qualified PropertySpec
import qualified RunnerSpec
import qualified RunSpec
Expand All @@ -26,6 +27,7 @@ spec = do
describe "MainSpec" MainSpec.spec
describe "OptionsSpec" OptionsSpec.spec
describe "ParseSpec" ParseSpec.spec
describe "ProjectsSpec" ProjectsSpec.spec
describe "PropertySpec" PropertySpec.spec
describe "RunnerSpec" RunnerSpec.spec
describe "RunSpec" RunSpec.spec
Expand Down
6 changes: 6 additions & 0 deletions test/projects/cpp-options/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
packages:
.
../../..

write-ghc-environment-files: always

23 changes: 23 additions & 0 deletions test/projects/cpp-options/cpp-options.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cabal-version: 2.0
name: cpp-options
version: 0.1.0.0
license: MIT
author: Martijn Bastiaan
maintainer: [email protected]
build-type: Simple

library
exposed-modules: MyLib
ghc-options: -Wall
cpp-options: -DADD=add
build-depends: base
hs-source-dirs: src
default-language: Haskell2010

test-suite doctests
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: doctests.hs
ghc-options: -threaded
build-depends: base, cpp-options, doctest-parallel >= 0.1
default-language: Haskell2010
13 changes: 13 additions & 0 deletions test/projects/cpp-options/src/MyLib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{-# LANGUAGE CPP #-}

module MyLib (add, main) where

-- | Adds two 'Int's
--
-- >>> ADD 3 5
-- 8
add :: Int -> Int -> Int
add = (+)

main :: IO ()
main = print (ADD 3 5)
7 changes: 7 additions & 0 deletions test/projects/cpp-options/test/doctests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Main where

import Test.DocTest (mainFromCabal)
import System.Environment (getArgs)

main :: IO ()
main = mainFromCabal "cpp-options" =<< getArgs

0 comments on commit b793f25

Please sign in to comment.