How to reduce duplication in fields depending on the assembly of the .cabal file?

Here is the .cabal file:

Name: myprogram Version: 0.1 -- blah blah blah Cabal-version: >=1.9.2 Executable myprogram HS-source-dirs: src Main-is: Main.hs Build-depends: attoparsec == 0.10.*, base == 4.3.*, -- long long list of packages Test-Suite test HS-source-dirs: test, src Type: exitcode-stdio-1.0 Main-is: Main.hs Build-depends: attoparsec == 0.10.*, base == 4.3.*, -- long long list of packages QuickCheck == 2.4.* 

Is there any way to replace the long list of build-specific packages for a test suite with "the same as for the executable, as well as QuickCheck"?

Edit: version information.

  • cabal-dev 0.9
  • cabal-install 0.10.2
  • Cabal Library 1.10.2.0
  • GHC 7.0.4
  • Haskell Platform 2011.4.0.0
+52
haskell cabal
Apr 15 2018-12-15T00:
source share
4 answers

Is there any way to replace the long list of build-specific packages for a test suite with "the same as for the executable, as well as QuickCheck"?

Not that I knew. However, there is a way to mention only one list of package-specific packages by structuring your project for three purposes:

  • a library that contains all your code and needs a long list, depending on the assembly.
  • executable file, which consists of only one file and depends on the base and library on top.
  • A test package that depends on the library on top and the test packages you use.

Perhaps this approach is what indygemma offers, but the bondage file proposed there will not achieve this, as Norman Ramsey notes in a comment. Here are the highlights of what you need in the cab file. For a complete example that works for me, you can look at this cabal file .

 name: my-program version: ... library hs-source-dirs: src-lib build-depends: base, containers, ... exposed-modules: My.Program.Main, ... executable my-program hs-source-dirs: src-exec main-is: my-program.hs Build-depends: base, my-program test-suite tests type: exitcode-stdio-1.0 hs-source-dirs: src-test main-is: tests.hs other-modules: ... build-depends: base, my-program, test-framework, ... 

Important points:

  • There are three separate source directories for three purposes. This is necessary to stop ghc from recompiling library files while building other targets.

  • All application code is in the library. The executable file is only a shell, for example:

     import My.Program.Main (realMain) main = realMain 
  • The library provides all the modules needed for testing.

The last point emphasizes the drawback of this approach: you need to expose the internal modules. The main advantage of this approach is that you have less duplication in the cabal file and, more importantly, less duplication in the build process: the library code will be created only once, and then it will be associated with both the executable and the test suite.

+31
Aug 19 '13 at 9:19
source share

You can also use hpack instead of writing the .cabal file manually:

In the hpack package.yaml format, you can specify the general field dependencies , whose entries are added to the field of all build-depends components when creating the .cabal file.

For example, see hpack package.yaml and the generated hpack.cabal .

To start using hpack with an existing package, you can use hpack-convert , which will generate package.yaml from an existing .cabal file.

To create a new package using hpack, you can use the stack simple-hpack as follows: stack new mypkg simple-hpack .

If you use stack for development, you do not need to manually call hpack to restore the .cabal file with the updated package.yaml package - stack will do this automatically.

+4
08 Oct '16 at 23:02
source share

There is no easy way:

  • you can use m4 and specify your dependencies once, but then you will need to process your Cabal file through m4 every time you change it.

  • you can move the code you are testing to a library and then specify the library in your build-depends for the test. This requires that you install the library even to run the test.

  • You simply cannot put the test into a cabal file. Build it with ghc -make, which will pull dependencies. But then you lose integration with the cab.

0
May 30 '13 at 2:38
source share

There is an optional library for .cabal files that solves your problem.

 name: myprogram version: 0.1 -- blah blah blah cabal-version: >=1.9.2 library build-depends: attoparsec == 0.10.* , base == 4.3.* -- long long list of packages executable myprogram hs-source-dirs: src main-is: Main.hs test-suite test hs-source-dirs: test, src type: exitcode-stdio-1.0 main-is: Main.hs build-depends: QuickCheck == 2.4.* 
-5
Apr 15 '12 at 16:58
source share



All Articles