Skip to content

Commit a4ccd53

Browse files
committed
Add a cabal coverage test
1 parent a675618 commit a4ccd53

File tree

15 files changed

+248
-0
lines changed

15 files changed

+248
-0
lines changed

test/cabal-coverage/cabal.project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
packages: pkga
2+
pkgb

test/cabal-coverage/conduit.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- https://github.com/snoyberg/conduit#readme
2+
3+
import Conduit
4+
import System.Directory (removeFile)
5+
6+
main = do
7+
-- Pure operations: summing numbers.
8+
print $ runConduitPure $ yieldMany [1..10] .| sumC
9+
10+
-- Exception safe file access: copy a file.
11+
writeFile "input.txt" "This is a test." -- create the source file
12+
runConduitRes $ sourceFileBS "input.txt" .| sinkFile "output.txt" -- actual copying
13+
readFile "output.txt" >>= putStrLn -- prove that it worked
14+
15+
-- Perform transformations.
16+
print $ runConduitPure $ yieldMany [1..10] .| mapC (+ 1) .| sinkList
17+
18+
removeFile "input.txt"
19+
removeFile "output.txt"

test/cabal-coverage/default.nix

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{ stdenv, cabal-install, cabalProject', recurseIntoAttrs, runCommand, testSrc }:
2+
3+
with stdenv.lib;
4+
5+
let
6+
project = cabalProject' {
7+
src = testSrc "cabal-coverage";
8+
modules = [{
9+
# Package has no exposed modules which causes
10+
# haddock: No input file(s)
11+
packages.bytestring-builder.doHaddock = false;
12+
13+
packages.pkga.components.library.doCoverage = true;
14+
packages.pkgb.components.library.doCoverage = true;
15+
}];
16+
};
17+
18+
in recurseIntoAttrs ({
19+
run = stdenv.mkDerivation {
20+
name = "cabal-coverage-test";
21+
22+
buildCommand = ''
23+
########################################################################
24+
# test coverage reports with an example project
25+
26+
fileExistsNonEmpty() {
27+
local file=$1
28+
if [ ! -f "$file" ]; then
29+
echo "Missing: $file"
30+
exit 1
31+
fi
32+
local filesize=$(command stat --format '%s' "$file")
33+
if [ $filesize -eq 0 ]; then
34+
echo "File must not be empty: $file"
35+
exit 1
36+
fi
37+
}
38+
dirExistsEmpty() {
39+
local dir=$1
40+
if [ ! -d "$dir" ]; then
41+
echo "Missing: $dir"
42+
exit 1
43+
fi
44+
if [ "$(ls -A $dir)" ]; then
45+
echo "Dir should be empty: $dir"
46+
exit 1
47+
fi
48+
}
49+
50+
pkga_basedir="${project.hsPkgs.pkga.coverageReport}/share/hpc"
51+
fileExistsNonEmpty "$pkga_basedir/mix/pkga-0.1.0.0/pkga-0.1.0.0-11jDRXSQ52N8utWAAuExyI/PkgA.mix"
52+
dirExistsEmpty "$pkga_basedir/tix/pkga-0.1.0.0"
53+
dirExistsEmpty "$pkga_basedir/html/pkga-0.1.0.0"
54+
55+
pkgb_basedir="${project.hsPkgs.pkgb.coverageReport}/share/hpc"
56+
testTix="$pkgb_basedir/tix/tests/tests.tix"
57+
libTix="$pkgb_basedir/tix/pkgb-0.1.0.0/pkgb-0.1.0.0.tix"
58+
fileExistsNonEmpty "$testTix"
59+
fileExistsNonEmpty "$libTix"
60+
fileExistsNonEmpty "$pkgb_basedir/mix/pkgb-0.1.0.0/pkgb-0.1.0.0-FGa0ceCE8juF7fHx4pGWSi/ConduitExample.mix"
61+
fileExistsNonEmpty "$pkgb_basedir/mix/pkgb-0.1.0.0/pkgb-0.1.0.0-FGa0ceCE8juF7fHx4pGWSi/PkgB.mix"
62+
fileExistsNonEmpty "$pkgb_basedir/mix/tests/Main.mix"
63+
fileExistsNonEmpty "$pkgb_basedir/html/pkgb-0.1.0.0/hpc_index.html"
64+
fileExistsNonEmpty "$pkgb_basedir/html/tests/hpc_index.html"
65+
66+
filesizeTestsTix=$(command stat --format '%s' "$testTix")
67+
filesizeLibTix=$(command stat --format '%s' "$libTix")
68+
if (( filesizeTestsTix <= filesizeLibTix )); then
69+
echo "Filesize of \"$testTix\" ($filesizeTestsTix) should be greather than that of \"$libTix\" ($filesizeLibTix). Did you forget to exclude test modules when creating \"$libTix\"?"
70+
exit 1
71+
fi
72+
73+
touch $out
74+
'';
75+
76+
meta.platforms = platforms.all;
77+
78+
passthru = {
79+
# Used for debugging with nix repl
80+
inherit project;
81+
};
82+
};
83+
})

test/cabal-coverage/pkga/MainA.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Main where
2+
3+
main :: IO ()
4+
main = putStrLn "This is MainA"

test/cabal-coverage/pkga/PkgA.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module PkgA (decode) where
2+
3+
import Control.Lens
4+
import Data.Text.Lens
5+
import Data.Char
6+
import Data.Text (Text)
7+
8+
decode :: Text -> Text
9+
decode = unpacked . mapped %~ rot 13
10+
11+
rot :: Int -> Char -> Char
12+
rot n c | c >= 'a' && c <= 'z' = r 'a' 'z'
13+
| c >= 'A' && c <= 'Z' = r 'A' 'Z'
14+
| otherwise = c
15+
where
16+
r a b = chr $ ord a + ((ord c - ord a + n) `mod` (ord b - ord a + 1))

test/cabal-coverage/pkga/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

test/cabal-coverage/pkga/pkga.cabal

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cabal-version: 2.2
2+
-- Initial package description 'pkga.cabal' generated by 'cabal init'. For
3+
-- further documentation, see http://haskell.org/cabal/users-guide/
4+
5+
name: pkga
6+
version: 0.1.0.0
7+
-- synopsis:
8+
-- description:
9+
-- bug-reports:
10+
license: LicenseRef-PublicDomain
11+
author: Rodney Lorrimar
12+
maintainer: rodney.lorrimar@iohk.io
13+
category: Testing
14+
15+
library
16+
exposed-modules: PkgA
17+
build-depends: base
18+
, lens
19+
, text
20+
default-language: Haskell2010
21+
22+
executable pkga-exe
23+
main-is: MainA.hs
24+
build-depends: base
25+
hs-source-dirs: .
26+
default-language: Haskell2010

test/cabal-coverage/pkgb/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

test/cabal-coverage/pkgb/app/Main.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Main where
2+
3+
import ConduitExample (example)
4+
import PkgB (message)
5+
import qualified Data.Text.IO as T
6+
7+
main :: IO ()
8+
main = do
9+
T.putStrLn message
10+
example

test/cabal-coverage/pkgb/app/tests.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Main where
2+
3+
import System.Process
4+
import ConduitExample
5+
6+
main :: IO ()
7+
main = example

test/cabal-coverage/pkgb/pkgb.cabal

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
cabal-version: 2.2
2+
-- Initial package description 'pkgb.cabal' generated by 'cabal init'. For
3+
-- further documentation, see http://haskell.org/cabal/users-guide/
4+
5+
name: pkgb
6+
version: 0.1.0.0
7+
-- synopsis:
8+
-- description:
9+
-- bug-reports:
10+
license: LicenseRef-PublicDomain
11+
author: Rodney Lorrimar
12+
maintainer: rodney.lorrimar@iohk.io
13+
category: Testing
14+
15+
library
16+
exposed-modules: ConduitExample
17+
, PkgB
18+
build-depends: base
19+
, pkga
20+
, conduit
21+
, conduit-extra
22+
, directory
23+
, resourcet
24+
hs-source-dirs: src
25+
default-language: Haskell2010
26+
27+
executable pkgb
28+
main-is: Main.hs
29+
build-depends: base
30+
, pkgb
31+
, optparse-applicative
32+
, text
33+
hs-source-dirs: app
34+
default-language: Haskell2010
35+
36+
test-suite tests
37+
type: exitcode-stdio-1.0
38+
main-is: tests.hs
39+
hs-source-dirs: app
40+
build-depends: base
41+
, pkgb
42+
, process
43+
build-tools: pkga
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- https://github.com/snoyberg/conduit#readme
2+
3+
module ConduitExample (example) where
4+
5+
import Conduit
6+
import System.Directory (removeFile)
7+
8+
example = do
9+
-- Pure operations: summing numbers.
10+
print $ runConduitPure $ yieldMany [1..10] .| sumC
11+
12+
-- Exception safe file access: copy a file.
13+
writeFile "input.txt" "This is a test." -- create the source file
14+
runConduitRes $ sourceFileBS "input.txt" .| sinkFile "output.txt" -- actual copying
15+
readFile "output.txt" >>= putStrLn -- prove that it worked
16+
17+
-- Perform transformations.
18+
print $ runConduitPure $ yieldMany [1..10] .| mapC (+ 1) .| sinkList
19+
20+
removeFile "input.txt"
21+
removeFile "output.txt"

test/cabal-coverage/pkgb/src/PkgB.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module PkgB (message) where
4+
5+
import PkgA (decode)
6+
7+
message = decode "Guvf vf n pnony cebwrpg!"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Main where
2+
3+
import ConduitExample
4+
5+
main = example

test/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ let
178178
compiler-nix-name = callTest ./compiler-nix-name {};
179179
hls-cabal = callTest ./haskell-language-server/cabal.nix {};
180180
hls-stack = callTest ./haskell-language-server/stack.nix {};
181+
cabal-coverage = callTest ./cabal-coverage {};
181182

182183
unit = unitTests;
183184
} // lib.optionalAttrs (!stdenv.hostPlatform.isGhcjs && pkgs.haskell-nix.defaultCompilerNixName != "ghc8101" ) {

0 commit comments

Comments
 (0)