Cabal: Building something from within a test hook

Max Bolingbroke batterseapower at hotmail.com
Fri Jul 18 06:56:14 EDT 2008


>> I'd like to provide tests for my Cabal project, and would like to do
>> so by having >another< Cabal project in a tests/ directory that is
>> built and run by "Cabal test".
>
> I used to define an Executable 'test' and run this manually. It's also
> possible to compile it conditionally with the flags of Cabal V1.2, e.g.
> ./Setup.lhs configure -f buildTest .
>

Henning,

Thanks for your suggestion: I've come up with a solution based on
this, but I'm not particularly happy with it and I'd like to see if
anyone has ideas on how to improve it.

My situation is a bit complicated because I'm trying to test a
compiler plugin. This means that I want to build my test executable in
two different ways for the tests. Presently I accomplish this by
copy-pasting the relevant Executable section, but this doesn't seem
very satisfactory. It's also a bit annoying I have to have essentially
two different projects folded into one file, which means as a user I
have to:
1) Build without -ftests, then ensure I install the resulting library
2) Build with -ftests (which builds an entirely different set of
executables, that depends on the library just built) and run those

It seems like it could be nicer if I could do step 1) and then just
run "./Setup tests" but I can't write a hook to do this because I need
some way to feed the configuration information from the build of step
1) to the configuration process of step 2).

Anyway, the .cabal file is as follows, please suggest any improvements
I could make:

"""
Name:           pan-plugin
Version:        1.0
Cabal-Version:  >= 1.2
License:        BSD3
Author:         Sean Seefried
Maintainer:     batterseapower at hotmail.com
Copyright:      (c) Sean Seefried 2005
Stability:      alpha
Build-Type:     Simple

Flag Tests
  Description:  Build the plugin tests rather than the plugin itself
  Default:      False

Library
  Buildable:            True
  Exposed-Modules:      Pan.Optimizer.Plugin
  Other-Modules:        Pan.Optimizer.LiftImage,
                        Pan.Optimizer.MonadUtils
  Build-Depends:        Pan>=0.1, pan-plugin-annotations>=1.0,
                        mtl, ghc, base, haskell98
  if flag(Tests)
    Buildable:          False

Executable pan-plugin-tests-optimized
  Ghc-Options:          -plgPan.Optimizer.Plugin
  Main-Is:              Pan/Optimizer/Tests.hs
  Hs-Source-Dirs:       .
  Extensions:           MagicHash
  if !flag(tests)
    Buildable:          False
  else
    Build-Depends:      Pan>=0.1, pan-plugin-annotations>=1.0,
                        ghc, base, haskell98, old-time, random,
                        pan-plugin

Executable pan-plugin-tests-unoptimized
  Main-Is:              Pan/Optimizer/Tests.hs
  Hs-Source-Dirs:       .
  Extensions:           MagicHash
  if !flag(tests)
    Buildable:          False
  else
    Build-Depends:      Pan>=0.1, pan-plugin-annotations>=1.0,
                        ghc, base, haskell98, old-time, random
"""


More information about the Libraries mailing list