Search code examples
haskellcabalhspec

cabal misconfiguration for tests


My .cabal file contains the following hspec configuration:

-- The name of the package.
name:                MyModule

version:             0.1.0.0

cabal-version:       >=1.10
...
test-suite my-tests
  ghc-options:         -Wall -Werror
  cpp-options:         -DTEST
  default-extensions:  OverloadedStrings
  type:                exitcode-stdio-1.0
  main-is:             HSpecTests.hs
  hs-source-dirs:      tests
  build-depends:       MyModule, base >= 4.8 && < 4.9, containers >= 0.5 && <0.6, split >= 0.2 && < 0.3, hspec
  default-language:    Haskell2010

My directory structure is as follows:

MyProject
     | - src
          | - Main.hs
          | - Other.hs
          | - tests
             | -HSpecTests.hs
     | - dist
     | - MyProj.cabal

when I run cabal build, my source build succesfully to an executable ./dist/build/myproj/myproj.

cabal build then fails with:

cabal: can't find source for HSpecTests in
dist/build/my-tests/my-tests-tmp, tests

Inspecting the build directory shows that the my-tests directory is missing.

changing the hs-source-dirs to src/tests produces:

cabal: can't find source for HSpecTests in 
dist/build/my-tests/my-tests-tmp, src/tests

While moving all tests to the top level under root/tests, and changing the .cabal file to hs-source-dirs: tests produces:

<command line>: cannot satisfy -package-id MyModule-0.1.0.0-inplace
    (use -v for more information)

How have I misconfigured this?


Solution

  • Your hs-source-dirs is wrong:

     hs-source-dirs:      tests
    

    Given that HSpecTests's path is src/tests/HSpecTests.hs, it should be

     hs-source-dirs:      src/tests
    

    However, keep in mind that tests are usually in the top-level folder:

    MyProject
         | - src
              | - Main.hs
              | - Other.hs
         | - tests
              | -HSpecTests.hs
         | - dist
         | - MyProj.cabal