cabal - installing test dependencies

Update Dec 2017: Dear reader, this post was written in a time when Stack did not exist and Haskell users had to deal with cabal directly. For most intents and purposes, you will have a much better experience using Stack.

That being said, the content of this post should still be valid.

If there is one serious pain point I’ve always felt about Haskell, it is the lack of documentation, or if documentation available, it’s usually very terse and feels like it’s meant more for experts than for beginners.

This short article will show you how to install test dependencies for a cabalized project using cabal. The longer version is centred around Yesod, but should be generally applicable. There is a tldr version available below.

Software involved:

ghc 7.4.1

cabal 1.18.0.2

yesod 1.2.4

yesod-bin 1.2.5.1

tldr version

In short, suppose you have a cabalized project, and somehow when you run its test suite, cabal complains about missing dependencies. Now, inside the .cabal file of the project, you should find a section beginning with a line that looks something like this:

test-suite test

and below it, there are some build-depends that look like libraries meant for testing. If so, then you are in luck. Simply execute the following commands:

which was the exact same output I got from running yesod test !!! So it seems like we’re in a vicious cycle here, with no way out…

So I proceeded to ask a question on stack overflow. After a few minutes, no one responded, and I could not find similar questions. A lucky google search yielded the following link:

https://groups.google.com/forum/#!topic/haskell-cafe/SFoNwaq8wdc

Which is an announcement for the release of cabal 1.18.0, and seemingly has nothing to do with our problem, until the following lines in the announcement:

* GHCi support. It's now much easier to use ghci when developing your
packages, especially if those packages require preprocessors (e.g.
hsc2hs).
Here's how working on a package might look like using the new features:
# Only once:
cabal sandbox init
cabal install --only-dependencies --enable-tests
# Configure, build, and run tests:
cabal test # now implies configure and build
# Play around with the code in GHCi:
cabal repl

In particular, the cabal install --only-dependencies --enable-tests line. Could that be the magic one liner needed to solve our problems? Fortunately, yes.

So I proceeded with:

cabal install --only-dependencies --enable-tests

and cabal goes on to install the dependencies for the test-suite test section.