From edaef11a43e1ec05c1143a182412a272133de86e Mon Sep 17 00:00:00 2001 From: Simon Hengel Date: Fri, 12 Jul 2024 01:54:04 +0700 Subject: [PATCH] Add `cabal-doctest` --- .gitattributes | 3 +-- doctest.cabal | 30 ++++++++++++++++++++++++++++++ driver/cabal-doctest.hs | 36 ++++++++++++++++++++++++++++++++++++ package.yaml | 18 +++++++++++++----- 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 driver/cabal-doctest.hs diff --git a/.gitattributes b/.gitattributes index 1f70750d..e0c9dafa 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -/doctest.cabal linguist-generated=true -/ghci-wrapper/ghci-wrapper.cabal linguist-generated=true +doctest.cabal linguist-generated=true diff --git a/doctest.cabal b/doctest.cabal index eafb6e02..4537c6e6 100644 --- a/doctest.cabal +++ b/doctest.cabal @@ -155,6 +155,36 @@ library if impl(ghc >= 9.8) ghc-options: -fno-warn-x-partial +executable cabal-doctest + main-is: cabal-doctest.hs + other-modules: + Main + Paths_doctest + hs-source-dirs: + driver + default-extensions: + NamedFieldPuns + RecordWildCards + DeriveFunctor + NoImplicitPrelude + ghc-options: -Wall -threaded + build-depends: + base >=4.5 && <5 + , code-page >=0.1 + , deepseq + , directory + , exceptions + , filepath + , ghc >=8.0 && <9.11 + , ghc-paths >=0.1.0.9 + , process + , syb >=0.3 + , temporary + , transformers + default-language: Haskell2010 + if impl(ghc >= 9.8) + ghc-options: -fno-warn-x-partial + executable doctest main-is: Main.hs other-modules: diff --git a/driver/cabal-doctest.hs b/driver/cabal-doctest.hs new file mode 100644 index 00000000..5ab343ad --- /dev/null +++ b/driver/cabal-doctest.hs @@ -0,0 +1,36 @@ +{-# LANGUAGE LambdaCase #-} +module Main (main) where + +import Prelude + +import Data.Version +import System.Environment +import System.Exit +import System.FilePath +import System.IO.Temp (withSystemTempDirectory) +import System.Process + +import Paths_doctest (version) + +main :: IO () +main = do + args <- getArgs + lookupEnv "CABAL" >>= \ case + Nothing -> run "cabal" args + Just cabal -> run cabal (drop 1 args) + +run :: String -> [String] -> IO () +run cabal args = withSystemTempDirectory "doctest" $ \ dir -> do + let + doctest = dir "doctest" + script = dir "init-ghci" + callProcess cabal ["install", "--ignore-project", "--installdir=" <> dir, "--install-method=symlink", "doctest-" <> showVersion version] + callProcess (dir "doctest") ["--version"] + callProcess cabal ("build" : "--only-dependencies" : args) + writeFile script ":seti -w -Wdefault" + spawnProcess cabal ("repl" + : "--build-depends=QuickCheck" + : "--build-depends=template-haskell" + : "--repl-options=-ghci-script=" <> script + : "--with-ghc" : doctest + : args) >>= waitForProcess >>= exitWith diff --git a/package.yaml b/package.yaml index 60b2e363..39d67d89 100644 --- a/package.yaml +++ b/package.yaml @@ -59,11 +59,19 @@ library: - Test.DocTest.Internal.Parse - Test.DocTest.Internal.Run -executable: - main: Main.hs - ghc-options: -threaded - source-dirs: driver - dependencies: doctest +executables: + doctest: + main: Main.hs + ghc-options: -threaded + source-dirs: driver + dependencies: doctest + + cabal-doctest: + main: cabal-doctest.hs + ghc-options: -threaded + source-dirs: driver + dependencies: + - temporary tests: spec: