8

I have a fresh installation of stack and ghci:

$ stack --version
Version 1.6.3, Git revision b27e629b8c4ce369e3b8273f04db193b060000db (5454 commits) x86_64 hpack-0.20.0
$ ghci --version
The Glorious Glasgow Haskell Compilation System, version 8.2.1

I make a new project:

$ stack new so-mve
Downloading template "new-template" to create project "so-mve" in so-mve/ 
... blah blah blah ...
Looking for .cabal or package.yaml files to use to init the project.
Using cabal packages:
- so-mve/
Selecting the best among 12 snapshots...
* Matches lts-10.3
Selected resolver: lts-10.3
Initialising configuration using resolver: lts-10.3
Total number of user packages considered: 1
Writing configuration to file: so-mve/stack.yaml
All done.

Looks pretty good:

$ tree so-mve
so-mve
├── ChangeLog.md
├── LICENSE
├── README.md
├── Setup.hs
├── app
│   └── Main.hs
├── package.yaml
├── so-mve.cabal
├── src
│   └── Lib.hs
├── stack.yaml
└── test
    └── Spec.hs

It builds and runs:

$ cd so-mve
$ stack build
so-mve-0.1.0.0: build (lib + exe)
Preprocessing library for so-mve-0.1.0.0..
Building library for so-mve-0.1.0.0..
Preprocessing executable 'so-mve-exe' for so-mve-0.1.0.0..
Building executable 'so-mve-exe' for so-mve-0.1.0.0..
so-mve-0.1.0.0: copy/register
Installing library in /...blah-blah.../so-mve/.stack-work/install/x86_64-osx/lts-10.3/8.2.2/lib/x86_64-osx-ghc-8.2.2/so-mve-0.1.0.0-5kG2WnHWwo99IiYYGoxrcC
Installing executable so-mve-exe in /...blah-blah.../so-mve/.stack-work/install/x86_64-osx/lts-10.3/8.2.2/bin
Registering library for so-mve-0.1.0.0..
$ stack exec so-mve-exe
someFunc

Tests run:

$ stack test
blah blah blah
[2 of 2] Compiling Main             ( test/Spec.hs, .stack-work/dist/x86_64-osx/Cabal-2.0.1.0/build/so-mve-test/so-mve-test-tmp/Main.o )
...blah-blah-blah...
Progress: 1/2Test suite not yet implemented
so-mve-0.1.0.0: Test suite so-mve-test passed
Completed 2 action(s).

I triple check that HUnit is installed

$ stack install HUnit
Populated index cache.

I add one line to test/Spec.hs

$ cat test/Spec.hs
import Test.HUnit
main :: IO ()
main = putStrLn "Test suite not yet implemented"

Doesn't work:

$ stack test
so-mve-0.1.0.0: unregistering (components added: test:so-mve-test)
so-mve-0.1.0.0: build (lib + exe + test)
Preprocessing library for so-mve-0.1.0.0..
Building library for so-mve-0.1.0.0..
Preprocessing executable 'so-mve-exe' for so-mve-0.1.0.0..
Building executable 'so-mve-exe' for so-mve-0.1.0.0..
Preprocessing test suite 'so-mve-test' for so-mve-0.1.0.0..
Building test suite 'so-mve-test' for so-mve-0.1.0.0..
[2 of 2] Compiling Main             ( test/Spec.hs, .stack-work/dist/x86_64-osx/Cabal-2.0.1.0/build/so-mve-test/so-mve-test-tmp/Main.o )
/...blah-blah.../so-mve/test/Spec.hs:1:1: error:
    Could not find module ‘Test.HUnit’
    Use -v to see a list of the files searched for.
  |          
1 | import Test.HUnit
  | ^^^^^^^^^^^^^^^^^

Progress: 1/2
--  While building custom Setup.hs for package so-mve-0.1.0.0 using:
      /Users/XXXXXXXX/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_2.0.1.0_ghc-8.2.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-2.0.1.0 build lib:so-mve exe:so-mve-exe test:so-mve-test --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

I don't have trouble importing other libraries, like Text.Read and Text.Printf. I googled around a bunch, but didn't find an answer. Any ideas for me?

hnefatl
  • 5,860
  • 2
  • 27
  • 49
Reb.Cabin
  • 5,426
  • 3
  • 35
  • 64
  • You've tagged this `cabal`, should it be tagged `stack`? And have you added `HUnit` to your test project's dependencies in `package.yaml`? – hnefatl Jan 16 '18 at 23:00
  • What does your `.cabal` file look like? You need to specify the `HUnit` dependency in the part of the cabal file that specifies the test build. – jkeuhlen Jan 16 '18 at 23:10
  • @hnefatl The tag `stack` would have to be something new because SO suggests that it means stack-the-data-structure, also suggesting a bunch of things having to do with stack-as-in-stackoverflow. I settled on `cabal` as a fallback. – Reb.Cabin Jan 17 '18 at 00:03
  • @Reb.Cabin Sorry, I meant `haskell-stack`. – hnefatl Jan 17 '18 at 00:04
  • @hnetfatl oh, cool! For the next one, i’ll know how to tag it. – Reb.Cabin Jan 17 '18 at 01:04

2 Answers2

11

You just need to add HUnit to the dependencies for your test project. When using stack, you should edit the package.yaml file to specify dependencies. In particular, your test configuration should look something like:

tests:
  so-mve-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - ...
    dependencies:
    - HUnit

This is documented in the latest Stack Guide, under the section Adding Dependencies.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
  • 1
    Both answers are spot on. I'm giving you the check-mark for the added reference to the stack documentation. – Reb.Cabin Jan 16 '18 at 23:59
4

You installed HUnit globally with stack, but that doesn't mean it is specified for your project.

Your cabal file for the project needs to specify a dependency on HUnit:

--so-mve.cabal
...
test-suite so-mve 
  type:                exitcode-stdio-1.0
  hs-source-dirs:      test
  main-is:             Spec.hs
  build-depends:       base
                     , HUnit
  ...

Text.Read and Text.Printf are both included in base, so you don't need to specify an additional dependency.


As pointed out in the comments, since you're using a package.yaml (as opposed to a stack.yaml) configuration with the newer version of stack, you'll need to specify the dependency there instead of the .cabal file:

tests:
  so-mve-test:
    main: Spec.hs
    source-dirs: test
    ghc-options:
    - -threaded
    dependencies:
    - HUnit
jkeuhlen
  • 4,401
  • 23
  • 36
  • From the actual content of the OP's post, it's clear they're using `stack` rather than `cabal`. – hnefatl Jan 16 '18 at 23:14
  • @hnefatl `stack` uses a `.cabal` file still. – jkeuhlen Jan 16 '18 at 23:15
  • @hnefatl And in fact, stack new creates that file for you, as you can see in OP's output of `tree` – jkeuhlen Jan 16 '18 at 23:16
  • 1
    Yes, but it shouldn't be edited - when using `stack`, the `package.yaml` file is the primary configuration file, while the `.cabal` file is frequently overwritten by `stack`. – hnefatl Jan 16 '18 at 23:17
  • Ah I forgot about that change with recent versions of stack, I'm still using `stack.yaml` files. – jkeuhlen Jan 16 '18 at 23:19