找不到模块“Test.HUnit”
Could not find module ‘Test.HUnit’
我全新安装了 stack 和 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
我做了一个新项目:
$ 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.
看起来不错:
$ 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
它构建并且 运行s:
$ 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
测试运行:
$ 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).
我三次检查 HUnit
是否已安装
$ stack install HUnit
Populated index cache.
我在test/Spec.hs
中加一行
$ cat test/Spec.hs
import Test.HUnit
main :: IO ()
main = putStrLn "Test suite not yet implemented"
无效:
$ 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
我在导入其他库时没有遇到问题,例如 Text.Read
和 Text.Printf
。
我用谷歌搜索了一堆,但没有找到答案。对我有什么想法吗?
您使用 stack
全局安装了 HUnit
,但这并不意味着它是 。
项目的 cabal
文件需要指定对 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
和Text.Printf
都包含在base
中,所以你不需要指定额外的依赖。
正如评论中所指出的那样,由于您使用的是 package.yaml
(而不是 stack.yaml
)配置以及较新版本的 stack
,因此您需要在那里指定依赖项而不是 .cabal
文件:
tests:
so-mve-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
dependencies:
- HUnit
您只需将 HUnit
添加到测试项目的依赖项中。使用 stack
时,您应该编辑 package.yaml
文件以指定依赖项。特别是,您的测试配置应该类似于:
tests:
so-mve-test:
main: Spec.hs
source-dirs: test
ghc-options:
- ...
dependencies:
- HUnit
这在最新的 Stack Guide, under the section Adding Dependencies 中有记载。
我全新安装了 stack 和 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
我做了一个新项目:
$ 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.
看起来不错:
$ 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
它构建并且 运行s:
$ 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
测试运行:
$ 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).
我三次检查 HUnit
是否已安装
$ stack install HUnit
Populated index cache.
我在test/Spec.hs
$ cat test/Spec.hs
import Test.HUnit main :: IO () main = putStrLn "Test suite not yet implemented"
无效:
$ 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
我在导入其他库时没有遇到问题,例如 Text.Read
和 Text.Printf
。
我用谷歌搜索了一堆,但没有找到答案。对我有什么想法吗?
您使用 stack
全局安装了 HUnit
,但这并不意味着它是
项目的 cabal
文件需要指定对 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
和Text.Printf
都包含在base
中,所以你不需要指定额外的依赖。
正如评论中所指出的那样,由于您使用的是 package.yaml
(而不是 stack.yaml
)配置以及较新版本的 stack
,因此您需要在那里指定依赖项而不是 .cabal
文件:
tests:
so-mve-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
dependencies:
- HUnit
您只需将 HUnit
添加到测试项目的依赖项中。使用 stack
时,您应该编辑 package.yaml
文件以指定依赖项。特别是,您的测试配置应该类似于:
tests:
so-mve-test:
main: Spec.hs
source-dirs: test
ghc-options:
- ...
dependencies:
- HUnit
这在最新的 Stack Guide, under the section Adding Dependencies 中有记载。