You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Haskell HUnit测试项目Cabal依赖解析失败求助

解决Haskell项目的依赖冲突与测试代码问题

一、核心问题:base版本依赖冲突

你的项目中库和测试套件指定了不兼容的base版本约束:

  • 库hs-collections-lib要求base ^>=4.17.2.0
  • 测试套件hs-collections-testsuite要求base ^>=4.14

而Haskell的base包是GHC自带的核心库,一个环境中只能存在一个base版本,因此Cabal无法解析这种冲突,导致依赖解析失败。

二、分步解决方案

1. 统一base版本约束

修改hs-collections.cabal文件中测试套件的base依赖,与库的版本保持一致:

Test-suite hs-collections-testsuite
  Type: exitcode-stdio-1.0
  Main-is: ListTest.hs
  Build-depends: base ^>=4.17.2.0,
               HUnit ^>=1.6,
               hs-collections-lib
  Hs-source-dirs: test
  Default-language: Haskell2010  

如果需要兼容多个GHC版本,也可以使用更宽松的约束(比如base >=4.14 && <4.22),但优先保持版本一致能避免额外问题。

2. 修复测试代码的逻辑错误

你的测试文件存在两处问题:

  • 未导入len函数所在的模块
  • 错误使用标准列表[],但len接受的是自定义的List类型(Nil/Cons构造器)

修改后的test/ListTest.hs:

module ListTest where

import Test.HUnit
import qualified System.Exit as Exit
import List.Boundary.List (len)
import List.Entity.List (Nil)

test1 :: Test
test1 = TestCase (assertEqual "should return 0" 0 (len Nil))

tests :: Test
tests = TestList [TestLabel "test1" test1]
 
main :: IO ()
main = do
  result <- runTestTT tests
  if failures result > 0 then Exit.exitFailure else Exit.exitSuccess

3. 构建并运行测试

执行以下命令验证修复效果:

cabal build
cabal test

三、额外建议

  • 始终保持库与测试套件的base版本一致,避免依赖冲突
  • 区分自定义类型与标准库类型(如Prelude的[]),确保测试用例使用正确的构造器
  • 若需支持多版本GHC,可使用Cabal条件语句指定不同base约束:
Library hs-collections-lib
  Build-depends:    base >=4.14 && <4.22,
  ...

Test-suite hs-collections-testsuite
  Build-depends: base >=4.14 && <4.22,
  ...

内容的提问来源于stack exchange,提问作者Yago

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 15:28:12