【问题标题】:Set up Haskell Project and run tests设置 Haskell 项目并运行测试
【发布时间】:2016-08-13 03:06:58
【问题描述】:

我正在尝试设置一个带有测试的 Haskell 项目(库),我可以使用这些测试来处理 The Haskell Road to Logic, Maths, and Programming。我想要三个部分:

  • 随书附赠的代码,在子目录中
  • 我为书中的练习编写的代码;每章一个文件
  • 我为测试编写的代码;每章一个文件

我尝试了一个项目设置here,但收到以下阴谋集团错误:

Resolving dependencies...
Configuring haskell-road-0.1.0.0...
Building haskell-road-0.1.0.0...
Failed to install haskell-road-0.1.0.0
Build log ( /Users/stuart/.cabal/logs/haskell-road-0.1.0.0.log ):
cabal: Entering directory '.'
Configuring haskell-road-0.1.0.0...
Building haskell-road-0.1.0.0...
Preprocessing library haskell-road-0.1.0.0...

src/Chapter1.hs:1:1:
    File name does not match module name:
    Saw: ‘Main’
    Expected: ‘Chapter1’
cabal: Leaving directory '.'
cabal: Error: some packages failed to install:
haskell-road-0.1.0.0 failed during the building phase. The exception was:
ExitFailure 1

我希望能够运行 $ cabal test 并运行所有测试,并让导入路径正常工作。任何帮助表示赞赏。我认为测试结构可能存在问题,但我很难找到有关实际设置的权威指南。

编辑:更多细节

src/
  Chapter1.hs
  Book/
    GS.hs
    etc....
test/
  Chapter1Test.hs
  MainTestSuite.hs
  TestHelper.hs

haskell-book.hs:

-- Initial haskell-road.cabal generated by cabal init.  For further
-- documentation, see http://haskell.org/cabal/users-guide/

-- The name of the package.
name:                haskell-road

-- The package version.  See the Haskell package versioning policy (PVP)
-- for standards guiding when and how versions should be incremented.
-- https://wiki.haskell.org/Package_versioning_policy
-- PVP summary:      +-+------- breaking API changes
--                   | | +----- non-breaking API additions
--                   | | | +--- code changes with no API change
version:             0.1.0.0

-- A short (one-line) description of the package.
-- synopsis:

-- A longer description of the package.
-- description:

-- The license under which the package is released.
license:             MIT

-- The file containing the license text.
license-file:        LICENSE

-- The package author(s).
author:              Stuart Terrett

-- An email address to which users can send suggestions, bug reports, and
-- patches.
maintainer:          shterrett@gmail.com

-- A copyright notice.
-- copyright:

-- category:

build-type:          Simple

-- Extra files to be distributed with the package, such as examples or a
-- README.
extra-source-files:  ChangeLog.md

-- Constraint on the version of Cabal needed to build this package.
cabal-version:       >=1.10


library
  -- Modules exported by the library.
  exposed-modules:     Chapter1, Book.COR, Book.DB, Book.FAIS, Book.FCT, Book.GS, Book.Hierarchy, Book.IAR, Book.Nats, Book.POL, Book.Polynomials, Book.PowerSeries, Book.Query, Book.REL, Book.SetEq, Book.SetOrd, Book.STAL, Book.TAMO, Book.TUOLP, Book.WWN

  -- Modules included in this library but not exported.
  -- other-modules:

  -- LANGUAGE extensions used by modules in this package.
  other-extensions:    FlexibleInstances

  -- Other library packages from which modules are imported.
  build-depends:       base, random >=1.1 && <1.2, HUnit >=1.3 && <1.4

  -- Directories containing source files.
  hs-source-dirs:      src

  -- Base language which the package is written in.
  default-language:    Haskell2010


test-suite haskell-road-tests
  type:              exitcode-stdio-1.0
  hs-source-dirs:    tests, src
  main-is:           MainTestSuite.hs
  build-depends:     base,
                     HUnit,
                     QuickCheck,
                     test-framework,
                     test-framework-hunit,
                     test-framework-quickcheck2

MainTestSuite.hs

import Chapter1Test

exitProperly :: IO Counts -> IO ()
exitProperly m = do
  counts <- m
  exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess

allTests::[Test]
allTests = [Chapter1Test.itRuns]

main :: IO ()
main = exitProperly (runTestTT (TestList allTests))

【问题讨论】:

  • 请在问题本身中包含所有相关信息。不保证 github 链接将来仍然有效。
  • @hugomg 添加了额外信息
  • 为我的答案添加了所有更改的差异。
  • 您是否考虑使用 Stack 作为您的构建系统?

标签: haskell cabal


【解决方案1】:

所有变化的差异:

http://lpaste.net/5997592404872396800

您需要进行的具体更改:

  1. 在 Chapter1.hs 中确保 module Chapter1 出现在 import 语句之前:

    module Chapter1 where
    import ...
    
  2. 在每个 Book 模块中,您需要将前缀 Book. 添加到 每个模块语句,例如在Book/COR.hs:

    change:  module COR
        to:  module Book.COR
    

    此外,任何导入语句也需要Book. 前缀,即Book/STAL.hs

    change:  import DB
        to:  import Book.DB
    

    (将本书的模块留在模块名称空间的顶层可能更容易。)

  3. 修复这个编译错误:

    src/Book/IAR.hs:131:7:
        No instance for (Foldable t3) arising from a use of ‘foldr’
    

只需将{-# LANGUAGE NoMonomorphismRestriction #-} 添加到Book/IAR.hs 的顶部(它应该是第一行。)

  1. 修复这个编译错误:

    src/Book/FAIS.hs:14:4: Parse error in pattern: n + 1
    

    将:f (n+1) = True : f n 更改为 f n = True : f (n-1)

    这称为n+k pattern,有关它的更多信息(以及它被弃用的原因)可在此处获得:What are "n+k patterns" and why are they banned from Haskell 2010?

  2. test-suite 部分你有:

    hs-source-dirs: tests, src
    

    要使用src 目录中的代码,您的测试应该依赖haskell-road 库而不是编译源代码。也就是说,在test-suite 部分中使用这些行:

    hs-source-dirs: tests
    build-depends: base, haskell-road, HUnit, ...
    
  3. 文件test/Chapter1Test.hs需要一个模块声明:

    module Chapter1Test where
    

    并修复此import 声明:

    -import TestHelper.testCase
    +import TestHelper (testCase)
    
  4. 文件test/MainTestSuite.hs 需要这些导入语句:

    import System.Exit
    import Test.HUnit
    
  5. 文件test/testHelper.hs需要重命名为test/TestHelper.hs 并且还需要这个import语句:

    import Test.HUnit
    

【讨论】:

  • 感谢您的详细回复!我还必须删除 cabal 中的 test-framework 行,但它现在正在构建和测试中。
【解决方案2】:

Cabaldeveloping packages link 和 cabal 文件内容结构在那里描述。

通过查看错误消息,您的 haskell library 源文件 Chapter1 似乎以 module Main where 开头。如错误消息所述,它应该包含module Chapter1 where

库不应包含 main 而测试可执行文件应包含,这就是为什么您在 cabal 文件中使用 main-is 声明测试可执行文件的原因。

希望这会有所帮助! (我没有看 github 源码,只看错误信息。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 2013-04-09
    • 1970-01-01
    • 2019-05-26
    • 2011-04-28
    • 1970-01-01
    • 2019-12-29
    • 2018-03-15
    相关资源
    最近更新 更多