Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including data files only in cabal test suites

Tags:

haskell

cabal

I have some data files that only belong with my test program and would be confusing to distribute with the main library.

Unfortunately, the .cabal data-files field only applies to an entire cabal file, not a single section like the test-suite. If I try to use it in a test-suite stanza I just get:

Warning: ssh.cabal: Unknown fields: data-files (line 71)

Is there a natural way to associate these files with my test program so that they get copied around only if --enable-tests is specified?

If there's no better way I'll probably use Template Haskell to embed them into the executable and then write them out again at runtime or something like that, but it feels pretty ugly.

like image 699
GS - Apologise to Monica Avatar asked Mar 31 '15 05:03

GS - Apologise to Monica


1 Answers

There is no Cabal attribute that allows you to associate data files with a test suite (or any non-global stanza of your build).

The common solution to this problem is to declare those files in the extra-source-files attribute. That attribute is global, too, and it ensures that the files listed are included in the release tarball so that the test suite can find them when run. Unlike data-files, however, extra-source-files are not installed — which is probably what you want since your test suite is not installed either. So the files are available during the build (and test suite run), but they won't take up space in the installation.

Build drivers like cabal-install and stack will execute the test suite with the current working directory pointing into the top-level directory of your project, i.e. in the directory that contains your project's Cabal file. Thus, if you have a data file at test/my-data-file.txt, then your test suite can access the file using exactly that path.

like image 182
Peter Simons Avatar answered Nov 04 '22 08:11

Peter Simons