Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Depend on test from bench

Tags:

haskell

cabal

I have a test project

test-suite spec
  ...

benchmark bench
  build-depends:
    library-test-spec

... how do I depend on the test-suite code from benchmark? The above doesn't work, because the package with name library-test-spec doesn't exist.

like image 282
Reactormonk Avatar asked Jan 12 '18 13:01

Reactormonk


2 Answers

From Cabal 2.0 onwards, you can put the common code in a named "internal library" on which both the test suite and the benchmark can depend. According to the documentation:

Cabal 2.0 and later support “internal libraries”, which are extra named libraries (as opposed to the usual unnamed library section). For example, suppose that your test suite needs access to some internal modules in your library, which you do not otherwise want to export. You could put these modules in an internal library, which the main library and the test suite build-depends upon.

A convenience internal library looks like this:

library foo-internal
    exposed-modules: Foo.Internal
    build-depends: base

When depending on it, you don't need to put version constraints because same-package dependencies implicitly refer to the same package instance.

With internal libraries you can avoid both double compilation (as when you include the same sources twice) and encumbering your main library (as when you put the common code there).

Remember to include cabal-version: >=2 in your .cabal file.

like image 87
danidiaz Avatar answered Oct 02 '22 02:10

danidiaz


You can't depend on test-suite inside benchmarks. The general solution to this problem is to move functions you want to use in test-suite and in benchmarks into your library target. This, unfortunately, implies that if common functionality needs some testing libraries, your library target will have those testing frameworks in dependencies. If you don't want such behavior then you can move these functions into separate package and depend on this package in your tests and benchmarks.

Or just copy-paste code from tests to benchmarks.

like image 25
Shersh Avatar answered Oct 02 '22 01:10

Shersh