Cabal: Building something from within a test hook

Hi, 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've got this far towards getting it working: """" main :: IO () main = defaultMainWithHooks (simpleUserHooks { runTests = buildAndRunTests }) buildAndRunTests :: Args -> Bool -> PackageDescription -> LocalBuildInfo -> IO () buildAndRunTests _ _ _ local_build_info = do test_package_description <- fmap flattenPackageDescription $ readPackageDescription normal "./tests/test-package.cabal" let [executable] = executables test_package_description build test_package_description local_build_info defaultBuildFlags [] system $ modulePath executable """ This kind of works, but the local_build_info is obviously wrong as it is for the package being compiled rather than the one I >want< to compile. What I'm really hoping Cabal provides or is able to provide is a way to configure the tests package using the old LBI such that the same tools (e.g. compiler, hc-pkg etc) that were used for this package are used for the tests one while leaving everything else at it's default value. I could then use the resulting LBI to build the tests as I desire. Any pointers or suggestions for alternative ways I should be going about this are gratefully recieved! Max

On Thu, 17 Jul 2008, Max Bolingbroke wrote:
Hi,
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 .

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@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 """
participants (2)
-
Henning Thielemann
-
Max Bolingbroke