1 cabal-testsuite is a suite of integration tests for Cabal-based
7 1. Build `cabal-tests` (`cabal build cabal-tests`)
8 2. Run the `cabal-tests` executable. It will scan for all tests
9 in your current directory and subdirectories and run them.
10 To run a specific set of tests, use `cabal-tests PATH ...`. You can
11 control parallelism using the `-j` flag.
13 There are a few useful flags:
15 * `--with-cabal PATH` can be used to specify the path of a
16 `cabal-install` executable. IF YOU DO NOT SPECIFY THIS FLAG,
17 CABAL INSTALL TESTS WILL NOT RUN.
19 * `--with-ghc PATH` can be used to specify an alternate version of
20 GHC to ask the tests to compile with.
22 * `--builddir DIR` can be used to manually specify the dist directory
23 that was used to build `cabal-tests`; this can be used if
24 the autodetection doesn't work correctly (which may be the
25 case for old versions of GHC.)
30 If you learn better by example, just look at the tests that live
31 in `cabal-testsuite/PackageTests`; if you `git log -p`, you can
32 see the full contents of various commits which added a test for
33 various functionality. See if you can find an existing test that
34 is similar to what you want to test.
36 Otherwise, here is a walkthrough:
38 1. Create the package(s) that you need for your test in a
40 (Currently (2021-10-06), tests are stored in `PackageTests`,
41 with the exception of one test stored in `tests`.)
43 2. Create one or more `.test.hs` scripts in your directory, using
46 import Test.Cabal.Prelude
47 main = setupAndCabalTest $ do
48 -- your test code here
51 `setupAndCabal` test indicates that invocations of `setup`
52 should work both for a raw `Setup` script, as well as
53 `cabal-install` (if your test works only for one or the
54 other, use `setupTest` or `cabalTest`).
56 Code runs in the `TestM` monad, which manages some administrative
57 environment (e.g., the test that is running, etc.).
58 `Test.Cabal.Prelude` contains a number of useful functions
59 for testing implemented in this monad, including the functions `cabal`
60 and `setup` which let you invoke those respective programs. You should
61 read through that file to get a sense for what capabilities
62 are possible (grep for use-sites of functions to see how they
63 are used). If you don't see something anywhere, that's probably
64 because it isn't implemented. Implement it!
66 To include parts that are supposed to fail (in the sense that a
67 non-zero exit code is returned), there is the `fails` combinator,
71 fails $ cabal "bad-command" [ "bad", "args" ]
72 cabal "good-command" [ "good", "args" ]
73 fails $ cabal "another-bad-one" [ ... ]
77 3. Run your tests using `cabal-tests` (no need to rebuild when
78 you add or modify a test; it is automatically picked up).
79 The first time you run a test, assuming everything else is
80 in order, it will complain that the actual output doesn't match
81 the expected output. Use the `--accept` flag to accept the
82 output if it makes sense!
84 We also support a `.multitest.hs` prefix; eventually this will
85 allow multiple tests to be defined in one file but run in parallel;
86 at the moment, these just indicate long running tests that should
87 be run early (to avoid straggling).
89 Frequently asked questions
90 --------------------------
92 For all of these answers, to see examples of the functions in
93 question, grep the test suite.
95 **Why isn't some output I added to Cabal showing up in the recorded
96 test output?** Only "marked" output is picked up by Cabal; currently,
97 only `notice`, `warn` and `die` produce marked output. Use those
98 combinators for your output.
100 **How do I safely let my test modify version-controlled source files?**
101 Use `withSourceCopy`. Note that you MUST `git add`
102 all files which are relevant to the test; otherwise they will not be
103 available when running the test.
105 **How can I add a dependency on a package from Hackage in a test?**
106 By default, the test suite is completely independent of the contents
107 of Hackage, to ensure that it keeps working across all GHC versions.
108 If possible, define the package locally. If the package needs
109 to be from Hackage (e.g., you are testing the global store code
110 in new-build), use `withRepo "repo"` to initialize a "fake" Hackage with
111 the packages placed in the `repo` directory.
113 **How do I run an executable that my test built?** The specific
114 function you should use depends on how you built the executable:
116 * If you built it using `Setup build`, use `runExe`
117 * If you installed it using `Setup install` or `cabal install`, use
119 * If you built it with `cabal build`, use `runPlanExe`; note
120 that you will need to run this inside of a `withPlan` that is
121 placed *after* you have invoked `build`. (Grep for an example!)
123 **How do I turn off accept tests? My test output wobbles too much.**
124 Use `recordMode DoNotRecord`. This should be a last resort; consider
125 modifying Cabal so that the output is stable. If you must do this, make
126 sure you add extra, manual tests to ensure the output looks like what
129 **How can I manually test for a string in output?** Use the primed
130 variants of a command (e.g., `cabal'` rather than `cabal`) and use
131 `assertOutputContains`. Note that this will search over BOTH stdout
134 **How do I skip running a test in some environments?** Use the
135 `skipIf` and `skipUnless` combinators. Useful parameters to test
136 these with include `hasSharedLibraries`, `hasProfiledLibraries`,
137 `hasCabalShared`, `isGhcVersion`, `isWindows`, `isLinux`, `isOSX`
138 and `hasCabalForGhc`.
140 **I programatically modified a file in my test suite, but Cabal/GHC
141 doesn't seem to be picking it up.** You need to sleep sufficiently
142 long before editing a file, in order for file system timestamp
143 resolution to pick it up. Use `withDelay` and `delay` prior to
144 making a modification.
146 **How do I mark a test as broken?** Use `expectBroken`, which takes
147 the ticket number as its first argument. Note that this does NOT
148 handle accept-test brokenness, so you will have to add a manual
149 string output test, if that is how your test is "failing."
154 By default, we run tests directly on the source code that is checked into the
155 source code repository. However, some tests require programatically
156 modifying source files, or interact with Cabal commands which are
157 not hermetic (e.g., `cabal freeze`). In this case, cabal-testsuite
158 supports opting into a hermetic test, where we first make copy of all
159 the relevant source code before starting the test. You can opt into
160 this mode using the `withSourceCopy` combinator (search for examples!)
161 This mode is subject to the following limitations:
163 * You must be running the test inside a valid Git checkout of the test
164 suite (`withSourceCopy` uses Git to determine which files should be copied.)
166 * You must `git add` all files which are relevant to the test, otherwise
167 they will not be copied.
169 * The source copy is still made at a well-known location, so running
170 a test is still not reentrant. (See also Known Limitations.)
175 This is the second rewrite of the integration testing framework. The
176 primary goal was to use Haskell as the test language (letting us take
177 advantage of a real programming language, and use utilities provided to
178 us by the Cabal library itself), while at the same time compensating for
179 two perceived problems of pure-Haskell test suites:
181 * Haskell test suites are generally compiled before they run
182 (for example, this is the modus operandi of `cabal test`).
183 In practice, this results in a long edit-recompile cycle
184 when working on tests. This hurts a lot when you would
185 like to experimentally edit a test when debugging an issue.
187 * Haskell's metaprogramming facilities (e.g., Template Haskell)
188 can't handle dynamically loading modules from the file system;
189 thus, there ends up being a considerable amount of boilerplate
190 needed to "wire" up test cases to the central test runner.
192 Our approach to address these issues is to maintain Haskell test scripts
193 as self-contained programs which are run by the GHCi interpreter.
194 This is not altogether trivial, and so there are a few important
195 technical innovations to make this work:
197 * Unlike a traditional test program which can be built by the Cabal
198 build system, these test scripts must be interpretable at
199 runtime (outside of the build system.) Our approach to handle
200 this is to link against the same version of Cabal that was
201 used to build the top-level test program (by way of a Custom
202 setup linked against the Cabal library under test) and then
203 use this library to compute the necessary GHC flags to pass
206 * The startup latency of `runghc` can be quite high, which adds up
207 when you have many tests. To solve this, in `Test.Cabal.Server`
208 we have an implementation an GHCi server, for which we can reuse
209 a GHCi instance as we are running test scripts. It took some
210 technical ingenuity to implement this, but the result is that
211 running scripts is essentially free.
213 Here is the general outline of how the `cabal-tests` program operates:
215 1. It first loads the cached `LocalBuildInfo` associated with the
216 host build system (which was responsible for building `cabal-tests`
217 in the first place.) This information lets us compute the
218 flags that we will use to subsequently invoke GHC.
220 2. We then recursively scan the current working directory, looking
221 for files suffixed `.test.hs`; these are the test scripts we
224 3. For every thread specified via the `-j`, we spawn a GHCi
225 server, and then use these to run the test scripts until all
226 test scripts have been run.
228 The new `cabal-tests` runner doesn't use Tasty because I couldn't
229 figure out how to get out the threading setting, and then spawn
230 that many GHCi servers to service the running threads. Improvements
236 An expect test (aka _golden test_)
237 is a test where we read out the output of the test
238 and compare it directly against a saved copy of the test output.
239 When test output changes, you can ask the test suite to "accept"
240 the new output, which automatically overwrites the old expected
241 test output with the new.
243 Supporting expect tests with Cabal is challenging, because Cabal
244 interacts with multiple versions of external components (most
245 prominently GHC) with different variants of their output, and no
246 one wants to rerun a test on four different versions of GHC to make
247 sure we've picked up the correct output in all cases.
249 Still, we'd like to take advantage of expect tests for Cabal's error
250 reporting. So here's our strategy:
252 1. We have a new verbosity flag `+markoutput` which lets you toggle the emission
253 of `-----BEGIN CABAL OUTPUT-----` and `-----END CABAL OUTPUT-----`
256 2. When someone requests an expect test, we ONLY consider output between
259 The expectation is that Cabal will only enclose output it controls
260 between these stanzas. In practice, this just means we wrap `die`,
261 `warn` and `notice` with these markers.
263 An added benefit of this strategy is that we can continue operating
264 at high verbosity by default (which is very helpful for having useful
265 diagnostic information immediately, e.g. in CI.)
267 We also need to deal with nondeterminism in test output in some
268 situations. Here are the most common ones:
270 * Dependency solving output on failure is still non-deterministic, due to
271 its dependence on the global package database. We're tracking this
272 in https://github.com/haskell/cabal/issues/4332 but for now, we're
273 not running expect tests on this output.
275 * Tests against Custom setup will build against the Cabal that shipped with
276 GHC, so you need to be careful NOT to record this output (since we
277 don't control that output.)
279 * We have some munging on the output, to remove common sources of
280 non-determinism: paths, GHC versions, boot package versions, etc.
281 Check `normalizeOutput` to see what we do. Note that we save
282 *normalized* output, so if you modify the normalizer you will
283 need to rerun the test suite accepting everything.
285 * The Setup interface gets a `--enable-deterministic` flag which we
286 pass by default. The intent is to make Cabal more deterministic;
287 for example, with this flag we no longer compute a hash when
288 computing IPIDs, but just use the tag `-inplace`. You can manually
289 disable this using `--disable-deterministic` (as is the case with
294 * It's good style to put `default-language` in all your stanzas, so
295 Cabal doesn't complain about it (that warning is marked!). Ditto
296 with `cabal-version` at the top of your Cabal file.
298 * If you can't get the output of a test to be deterministic, no
299 problem: just exclude it from recording and do a manual test
300 on the output for the string you're looking for. Try to be
301 deterministic, but sometimes it's not (easily) possible.
306 Here are some things we do not currently plan on supporting:
308 * A file format for specifying multiple packages and source files.
309 While in principle there is nothing wrong with making it easier
310 to write tests, tests stored in this manner are more difficult
311 to debug with, as they must first be "decompressed" into a full
312 folder hierarchy before they can be interacted with. (But some
313 of our tests need substantial setup; for example, tests that
314 have to setup a package repository. In this case, because there
315 already is a setup necessary, we might consider making things easier here.)
320 * Tests are NOT reentrant: test build products are always built into
321 the same location, and if you run the same test at the same time,
322 you will clobber each other. This is convenient for debugging and
323 doesn't seem to be a problem in practice.