@@ -5,10 +5,11 @@ set -euo pipefail
55# Packages to run doctests for; defaults to all packages if none are specified
66PACKAGES=(" $@ " )
77
8- # Install doctest's Cabal integration , if it's not present already
9- if [[ -z " $( type -t cabal- doctest) " ]]
8+ # Install doctest, if it's not present already
9+ if [[ -z " $( type -t doctest) " ]]
1010then
11- cabal install doctest --flag cabal-doctest --ignore-project --overwrite-policy=always
11+ cabal install doctest --ignore-project --overwrite-policy=always
12+ PATH=$( cabal path --installdir) :$PATH
1213fi
1314
1415# Ensure doctest and PATH are using the same ghc version
@@ -18,8 +19,18 @@ getExecutablePath()
1819 " $1 " -package-env - -e ' import System.Environment' -e ' putStrLn =<< getExecutablePath'
1920}
2021
22+ lookupVar ()
23+ {
24+ ghc -e ' interact $ maybe "" id . lookup "' " $1 " ' " . read'
25+ }
26+
27+ getPackageDb ()
28+ {
29+ readlink -f " $( " $1 " --info | lookupVar ' Global Package DB' ) "
30+ }
31+
2132default_ghc=$( type -p ghc)
22- doctest_ghc=$( doctest --info | ghc -e ' interact $ maybe "" id . lookup " ghc" . read ' )
33+ doctest_ghc=$( doctest --info | lookupVar ' ghc' )
2334
2435if [[ " $( getExecutablePath " $default_ghc " ) " != " $( getExecutablePath " $doctest_ghc " ) " ]]
2536then
2940 exit 1
3041fi
3142
32- # Ensure the cabal-doctest executable can be found
33- PATH=$( cabal path --installdir) :$PATH
43+ if [[ " $( getPackageDb ghc) " != " $( getPackageDb doctest) " ]]
44+ then
45+ echo " Incompatible package DBs:" >&2
46+ echo " Default DB: $( getPackageDb ghc) " >&2
47+ echo " Doctest DB: $( getPackageDb doctest) " >&2
48+ exit 1
49+ fi
3450
35- # Ensure other scripts can be found
36- case " $0 " in
37- * /* ) PATH=$( dirname " $( which " $0 " ) " ) :$PATH ;;
38- esac
51+ # Find packages potentially containing doctests
52+ if [[ ${# PACKAGES[@]} -eq 0 ]]
53+ then
54+ for CABAL_FILE in $( git ls-files ' *.cabal' )
55+ do
56+ if git grep -qIEe ' (>>>|prop>)' " ${CABAL_FILE%/* } /*.hs"
57+ then
58+ PACKAGES+=(" $( basename " $CABAL_FILE " .cabal) " )
59+ fi
60+ done
61+ fi
3962
4063# Specify additional arguments needed for specific modules
4164EXTRA_ARGS=$( mktemp)
@@ -44,11 +67,18 @@ cat <<EOF >"$EXTRA_ARGS"
4467cardano-ledger-api:lib:cardano-ledger-api --build-depends=cardano-ledger-babbage:testlib
4568EOF
4669
70+ RESULT=0
71+ shopt -s lastpipe # Run the while loop in the current process, to enable setting RESULT=1
72+
4773# Run the doctests for some or all packages
4874cleret cabal targets " ${PACKAGES[@]} " |
4975sort | join -t' ' -a1 -j1 - " $EXTRA_ARGS " |
5076while read -ra ARGS
5177do
5278 echo " ***** cabal doctest ${ARGS[0]} *****"
53- cabal doctest --repl-options=' -w -Wdefault' " ${ARGS[@]} "
79+ cabal repl --with-repl=doctest --repl-options=' -w -Wdefault' \
80+ --build-depends=QuickCheck --build-depends=template-haskell \
81+ " ${ARGS[@]} " || RESULT=1
5482done
83+
84+ exit " $RESULT "
0 commit comments