Sventimir
Sventimir

Reputation: 2056

HUnit tests within cabal sandbox

After having some trouble I managed to configure my Cabal project so that it runs a simple stub test True @=? True. It works still if I substitute one value with a constant from tested package. However when I try to create an instance of a custom datatype to be tested, I get a linking error.

For now the project consists of two modules:

module TestedModule where

data MyDataType = MyData String Char Int deriving Show

boolVal = True

________________________________
module TestTestedModule where

import Test.HUnit
import TestedModule
import qualified Distribution.TestSuite as C
import qualified Distribution.TestSuite.HUnit as H

tests :: IO [C.Test]                                                            
tests = return $ map (uncurry H.test) testModule                              

testModule :: [(String, Test)]                                                
testModule = [{- ("Test Show", testShow), -}                        
                ("Test True", testTrue)
             ]

testTrue = TestCase $ boolVal @? "False!"

-- testShow = TestCase $ "blabla" @=? show (MyData "blabla" 'a' 1)

Now when testShow is commented out, everything is fine, but when I uncomment it, I get:

$ cabal test
Building argparse-0.1.0.0...
Preprocessing library argparse-0.1.0.0...
[1 of 1] Compiling TestedModule         ( TestedModule.hs, dist/build/TestedModule.o )
In-place registering package-0.1.0.0...
Preprocessing test suite 'test-module' for package-0.1.0.0...
[1 of 2] Compiling TestedModule         ( TestedModule.hs, dist/build/TestedModule.o )
[2 of 2] Compiling TestArgparse     ( TestTestedModule.hs, dist/build/TestTestedModule.o ) [TestedModule changed]
In-place registering test-module-0.1.0.0...
[1 of 1] Compiling Main             ( dist/build/test-moduleStub/test-moduleStub-tmp/test-moduleStub.hs, dist/build/test-moduleStub/test-moduleStub-tmp/Main.o )
Linking dist/build/test-moduleStub/test-moduleStub ...
/home/sven/Haskell/lib/package/dist/build/libtest-package.a(TestTestedModule.o):(.data+0x50): undefined reference to `testzmargparsezm0zi1zi0zi0_TestedModule_MyData_static_info'
collect2: error: ld returned 1 exit status

The error occurs even if testShow is still commented out in testModule list. package.cabal looks like this:

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

name:                   package
version:                0.1.0.0
synopsis:               Some description
-- description:
license:                Apache-2.0
license-file:           LICENSE
author:                 Sventimir
maintainer:             [email protected]
-- copyright:
-- category:
build-type:             Simple
-- extra-source-files:
cabal-version:          >=1.10

library
    exposed-modules:        TestedModule
    -- other-modules:
    -- other-extensions:
    -- hs-source-dirs:
    default-language:       Haskell2010
    build-depends:          base >=4.7 && <4.8,
                        HUnit >=1.2 && <1.3,
                        containers  >=0.5 && <0.6

Test-Suite test-module
    type:                   detailed-0.9
    test-module:            TestTestedModule
    default-language:       Haskell2010
    build-depends: base >=4.7 && <4.8,
               Cabal >=1.20.0,
               HUnit >=1.2 && <1.3,
               cabal-test-hunit,
               containers >=0.5

Files are all located in:

$ ls -la /home/sven/Haskell/lib/package
drwxr-xr-x 6 sven users  4096 Oct  5 10:57 .
drwxrwxrwx 6 sven users  4096 Sep 28 11:13 ..
drwxr-xr-x 7 sven users  4096 Oct  5 10:47 .cabal-sandbox
drwxr-xr-x 8 sven users  4096 Oct  5 09:27 .git
-rw-r--r-- 1 sven users    57 Oct  4 19:06 .gitignore
-rw-r--r-- 1 sven users   546 Oct  5 10:57 TestedModule.hs
-rw-r--r-- 1 sven users 11358 Oct  4 09:27 LICENSE
-rw-r--r-- 1 sven users    46 Oct  4 09:27 Setup.hs
-rw-r--r-- 1 sven users   650 Oct  5 10:56 TestTestedModule.hs
-rw-r--r-- 1 sven users  1190 Oct  5 09:39 package.cabal
-rw r--r-- 1 sven users   990 Oct  4 18:02 cabal.sandbox.config
drwxr-xr-x 5 sven users  4096 Oct  5 11:03 dist
drwxr-xr-x 2 sven users  4096 Oct  4 09:19 orid

Upvotes: 1

Views: 195

Answers (0)

Related Questions