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