• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/Distribution/Extra/H03-May-2022-543350

ChangeLog.mdH A D09-Sep-20011.3 KiB5129

LICENSEH A D09-Sep-20011.5 KiB3124

README.mdH A D09-Sep-20018.9 KiB256199

Setup.hsH A D09-Sep-200146 32

cabal-doctest.cabalH A D03-May-20221.5 KiB4841

README.md

1cabal-doctest
2=============
3
4[![Hackage](https://img.shields.io/hackage/v/cabal-doctest.svg)](https://hackage.haskell.org/package/cabal-doctest) [![Build Status](https://travis-ci.org/phadej/cabal-doctest.svg?branch=master)](https://travis-ci.org/phadej/cabal-doctest)
5
6A `Setup.hs` helper for running `doctests`.
7
8Simple example
9--------------
10
11For most use cases—a `.cabal` file with a single library containing
12doctests—adapting the simple example located
13[here](https://github.com/phadej/cabal-doctest/tree/master/simple-example)
14will be sufficient. (Note that this example requires `Cabal-1.24` or later, but
15you can relax this bound safely, although running doctests won't be supported
16on versions of `Cabal` older than 1.24.)
17
18To use this library in your `Setup.hs`, you should specify a `custom-setup`
19section in your `.cabal` file. For example:
20
21```
22custom-setup
23 setup-depends:
24   base >= 4 && <5,
25   Cabal,
26   cabal-doctest >= 1 && <1.1
27```
28
29/Note:/ `Cabal` dependency is needed because of
30[Cabal/GH-4288](https://github.com/haskell/cabal/issues/4288) bug.
31
32You'll also need to specify `build-type: Custom` at the top of the `.cabal`
33file. Now put this into your `Setup.hs` file:
34
35```haskell
36module Main where
37
38import Distribution.Extra.Doctest (defaultMainWithDoctests)
39
40main :: IO ()
41main = defaultMainWithDoctests "doctests"
42```
43
44When you build your project, this `Setup` will generate a `Build_doctests`
45module. To use it in a testsuite, simply do this:
46
47```haskell
48module Main where
49
50import Build_doctests (flags, pkgs, module_sources)
51import Data.Foldable (traverse_)
52import System.Environment.Compat (unsetEnv)
53import Test.DocTest (doctest)
54
55main :: IO ()
56main = do
57    traverse_ putStrLn args -- optionally print arguments
58    unsetEnv "GHC_ENVIRONMENT" -- see 'Notes'; you may not need this
59    doctest args
60  where
61    args = flags ++ pkgs ++ module_sources
62```
63
64(The `System.Environment.Compat` module is from the `base-compat`
65package. That's already in the transitive closure of `doctest`'s
66dependencies. `System.Environment.unsetEnv` was added with GHC 7.8 so,
67if you don't need to support versions of GHC older than 7.8, you can
68use `System.Environment` from `base` instead.)
69
70Example with multiple .cabal components
71---------------------------------------
72
73`cabal-doctest` also supports more exotic use cases where a `.cabal` file
74contains more components with doctests than just the main library, including:
75
76* Doctests in executables
77* Doctests in Internal libraries (if using `Cabal-2.0` or later)
78
79Unlike the simple example shown above, these examples involve _named_
80components. You don't need to change the `Setup.hs` script to support
81this use case. However, in this scenario `Build_doctests` will generate extra
82copies of the `flags`, `pkgs`, and `module_sources` values for each additional
83named component.
84
85Simplest approach is to use `x-doctest-components` field, for example
86```
87x-doctest-components: lib lib:internal exe:example
88```
89
90In that case, the testdrive is general:
91
92```haskell
93module Main where
94
95import Build_doctests (Component (..), components)
96import Data.Foldable (for_)
97import System.Environment.Compat (unsetEnv)
98import Test.DocTest (doctest)
99
100main :: IO ()
101main = for_ components $ \(Component name flags pkgs sources) -> do
102    print name
103    putStrLn "----------------------------------------"
104    let args = flags ++ pkgs ++ sources
105    for_ args putStrLn
106    unsetEnv "GHC_ENVIRONMENT"
107    doctest args
108```
109
110There's also a more explicit approach: if you have an executable named `foo`,
111then separate values named `flags_exe_foo`, `pkgs_exe_foo`, and `module_sources_exe_foo` will
112be generated in `Build_doctests`. If the name has hyphens in it
113(e.g., `my-exe`), then `cabal-doctest` will convert those hyphens to
114underscores (e.g., you'd get `flags_my_exe`, `pkgs_my_exe`, and
115`module_sources_my_exe`).
116Internal library `bar` values will have a `_lib_bar` suffix.
117
118An example testsuite driver for this use case might look like this:
119
120```haskell
121module Main where
122
123import Build_doctests
124       (flags,            pkgs,            module_sources,
125        flags_exe_my_exe, pkgs_exe_my_exe, module_sources_exe_my_exe)
126import Data.Foldable (traverse_)
127import System.Environment.Compat (unsetEnv)
128import Test.DocTest
129
130main :: IO ()
131main = do
132    unsetEnv "GHC_ENVRIONMENT"
133    -- doctests for library
134    traverse_ putStrLn libArgs
135    doctest libArgs
136
137    -- doctests for executable
138    traverse_ putStrLn exeArgs
139    doctest exeArgs
140  where
141    libArgs = flags            ++ pkgs            ++ module_sources
142    exeArgs = flags_exe_my_exe ++ pkgs_exe_my_exe ++ module_sources_exe_my_exe
143```
144
145See
146[this example](https://github.com/phadej/cabal-doctest/tree/master/multiple-components-example)
147for more details.
148
149Additional configuration
150------------------------
151
152The `cabal-doctest` based `Setup.hs` supports few extensions fields
153in `pkg.cabal` files to customise the `doctest` runner behaviour, without
154customising the default `doctest.hs`.
155
156```
157test-suite doctests:
158  if impl(ghc >= 8.0)
159    x-doctest-options: -fdiagnostics-color=never
160  x-doctest-source-dirs: test
161  x-doctest-modules: Servant.Utils.LinksSpec
162
163  ...
164```
165
166* `x-doctest-options` Additional arguments passed into `doctest` command.
167* `x-doctest-modules` Additional modules to `doctest`. May be useful if you
168  have `doctest` in test or executables (i.e not default library complonent).
169* `x-doctest-src-dirs` Additional source directories to look for the modules.
170
171Notes
172-----
173
174* Recent versions of `Cabal` (for instance, 2.0) can choose to build a
175  package's `doctest` test suite _before_ the library. However, in order for
176  `cabal-doctest` to work correctly, the library _must_ be built first, as
177  `doctest` relies on the presence of generated files that are only created
178  when the library is built. See
179  [#19](https://github.com/phadej/cabal-doctest/issues/19).
180
181  A hacky workaround for this problem is to depend on the library itself in a
182  `doctests` test suite. See
183  [the simple example's .cabal file](https://github.com/phadej/cabal-doctest/blob/master/simple-example/simple-example.cabal)
184  for a demonstration. (This assumes that the test suite has the ability to
185  read build artifacts from the library, a separate build component. In
186  practice, this assumption holds, which is why this library works at all.)
187
188* `custom-setup` section is supported starting from `cabal-install-1.24`.
189  For older `cabal-install's` you have to install custom setup dependencies
190  manually.
191
192* `stack` respects `custom-setup` starting from version 1.3.3. Before that
193  you have to use `explicit-setup-deps` setting in your `stack.yaml`.
194  ([stack/GH-2094](https://github.com/commercialhaskell/stack/issues/2094))
195
196* There is [an issue in the Cabal issue tracker](https://github.com/haskell/cabal/issues/2327)
197  about adding `cabal doctest` command. After that command is implemented,
198  this library will be deprecated.
199
200* You can use `x-doctest-options` field in `test-suite doctests` to
201  pass additional flags to the `doctest`.
202
203* For `build-type: Configure` packages, you can use
204  `defaultMainAutoconfWithDoctests` function to make custom `Setup.hs` script.
205
206* If you use the default `.` in `hs-source-dirs`, then running `doctests`
207  might fail with weird errors (ambiguous module errors). Workaround is
208  to move sources under `src/` or some non-top-level directory.
209
210* `extensions:` field isn't supported. Upgrade your `.cabal` file to use at least
211  `cabal-version: >= 1.10` and use `default-extensions` or `other-extensions`.
212
213* If you use QuickCheck properties (`prop>`) in your doctests,
214  the `test-suite doctest` should depend on `QuickCheck` and `template-haskell`.
215  This is a little HACK: These dependencies aren't needed to build the
216  `doctests` test-suite executable.  However, as we let `Cabal` resolve
217  dependencies, we can pass the resolved (and installed!) package identifiers to
218  to the `doctest` command.  This way, `QuickCheck` and `template-haskell` are
219  available to `doctest`, otherwise you'll get errors like:
220
221```
222    Variable not in scope:
223      mkName
224        :: [Char]
225           -> template-haskell-2.11.1.0:Language.Haskell.TH.Syntax.Name
226```
227
228or
229
230```
231    Variable not in scope:
232      polyQuickCheck
233        :: Language.Haskell.TH.Syntax.Name -> Language.Haskell.TH.Lib.ExpQ
234```
235
236* From version 2, Stack sets the `GHC_ENVRIONMENT` variable, and GHC
237  (as invoked by `doctest`) will pick that up. This is undesirable:
238  `cabal-doctest` passes all the necessary information on the command
239  line already, and can lead to ambiguous module errors as GHC will
240  load the environment in addition to what `cabal-doctest` instructs
241  it to.
242
243  Hence, `cabal-doctest` tells GHC to ignore package environments
244  altogether on the command line. However, this is only possible since
245  GHC 8.2. If you are using `cabal-doctest` with Stack 2 and GHC 8.0
246  or earlier and seeing ambiguous module errors or other mysterious
247  failures, try manually unsetting `GHC_ENVIRONMENT` before invoking
248  `doctest`.
249
250Copyright
251---------
252
253Copyright 2017 Oleg Grenrus.
254
255Available under the BSD 3-clause license.
256