Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hack around wrong .mix directories for tests (backport #7493) #7570

Merged
merged 9 commits into from
Aug 24, 2021
24 changes: 23 additions & 1 deletion Cabal/src/Distribution/Simple/Hpc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,29 @@ mixDir :: FilePath -- ^ \"dist/\" prefix
-> Way
-> FilePath -- ^ Component name
-> FilePath -- ^ Directory containing test suite's .mix files
mixDir distPref way name = hpcDir distPref way </> "mix" </> name
mixDir distPref way name = hpcDir distPrefBuild way </> "mix" </> name
where
-- This is a hack for HPC over test suites, needed to match the directory
-- where HPC saves and reads .mix files when the main library of the same
-- package is being processed, perhaps in a previous cabal run (#5213).
-- E.g., @distPref@ may be
-- @./dist-newstyle/build/x86_64-linux/ghc-9.0.1/cabal-gh5213-0.1/t/tests@
-- but the path where library mix files reside has two less components
-- at the end (@t/tests@) and this reduced path needs to be passed to
-- both @hpc@ and @ghc@. For non-default optimization levels, the path
-- suffix is one element longer and the extra path element needs
-- to be preserved.
distPrefElements = splitDirectories distPref
distPrefBuild = case drop (length distPrefElements - 3) distPrefElements of
["t", _, "noopt"] ->
joinPath $ take (length distPrefElements - 3) distPrefElements
++ ["noopt"]
["t", _, "opt"] ->
joinPath $ take (length distPrefElements - 3) distPrefElements
++ ["opt"]
[_, "t", _] ->
joinPath $ take (length distPrefElements - 2) distPrefElements
_ -> distPref

tixDir :: FilePath -- ^ \"dist/\" prefix
-> Way
Expand Down
29 changes: 29 additions & 0 deletions cabal-testsuite/PackageTests/Regression/T5213/cabal-gh5213.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Initial cabal-gh5213.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/

name: cabal-gh5213
version: 0.1
-- synopsis:
-- description:
license: BSD3
author: Ryan Scott
maintainer: ryan.gl.scott@gmail.com
-- copyright:
category: Testing
build-type: Simple
cabal-version: >=1.10

library
exposed-modules: CabalGH5213Exposed
other-modules: CabalGH5213Other
-- other-extensions:
build-depends: base >= 4 && < 5
hs-source-dirs: src
default-language: Haskell2010

test-suite tests
main-is: Main.hs
type: exitcode-stdio-1.0
build-depends: base, cabal-gh5213
hs-source-dirs: tests
default-language: Haskell2010
19 changes: 19 additions & 0 deletions cabal-testsuite/PackageTests/Regression/T5213/cabal.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# cabal new-test
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- cabal-gh5213-0.1 (lib) (first run)
- cabal-gh5213-0.1 (test:tests) (first run)
Configuring library for cabal-gh5213-0.1..
Preprocessing library for cabal-gh5213-0.1..
Building library for cabal-gh5213-0.1..
Configuring test suite 'tests' for cabal-gh5213-0.1..
Preprocessing test suite 'tests' for cabal-gh5213-0.1..
Building test suite 'tests' for cabal-gh5213-0.1..
Running 1 test suites...
Test suite tests: RUNNING...
Test suite tests: PASS
Test suite logged to: <ROOT>/cabal.dist/work/./dist/build/<ARCH>/ghc-<GHCVER>/cabal-gh5213-0.1/t/tests/test/cabal-gh5213-0.1-tests.log
Test coverage report written to <ROOT>/cabal.dist/work/./dist/build/<ARCH>/ghc-<GHCVER>/cabal-gh5213-0.1/t/tests/hpc/vanilla/html/tests/hpc_index.html
1 of 1 test suites (1 of 1 test cases) passed.
Package coverage report written to <ROOT>/cabal.dist/work/./dist/build/<ARCH>/ghc-<GHCVER>/cabal-gh5213-0.1/t/tests/hpc/vanilla/html/cabal-gh5213-0.1/hpc_index.html
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/Regression/T5213/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
packages: .

package cabal-gh5213
library-coverage: true
2 changes: 2 additions & 0 deletions cabal-testsuite/PackageTests/Regression/T5213/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Test.Cabal.Prelude
main = cabalTest $ cabal "new-test" []
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module CabalGH5213Exposed where

import CabalGH5213Other

foo :: Int
foo = bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module CabalGH5213Other where

bar :: Int
bar = 42
6 changes: 6 additions & 0 deletions cabal-testsuite/PackageTests/Regression/T5213/tests/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main where

import CabalGH5213Exposed

main :: IO ()
main = print foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Initial cabal-gh5213.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/

name: cabal-gh5213
version: 0.1
-- synopsis:
-- description:
license: BSD3
author: Ryan Scott
maintainer: ryan.gl.scott@gmail.com
-- copyright:
category: Testing
build-type: Simple
cabal-version: >=1.10

library
exposed-modules: CabalGH5213Exposed
other-modules: CabalGH5213Other
-- other-extensions:
build-depends: base >= 4 && < 5
hs-source-dirs: src
default-language: Haskell2010

test-suite tests
main-is: Main.hs
type: exitcode-stdio-1.0
build-depends: base, cabal-gh5213
hs-source-dirs: tests
default-language: Haskell2010
19 changes: 19 additions & 0 deletions cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# cabal new-test
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- cabal-gh5213-0.1 (lib) (first run)
- cabal-gh5213-0.1 (test:tests) (first run)
Configuring library for cabal-gh5213-0.1..
Preprocessing library for cabal-gh5213-0.1..
Building library for cabal-gh5213-0.1..
Configuring test suite 'tests' for cabal-gh5213-0.1..
Preprocessing test suite 'tests' for cabal-gh5213-0.1..
Building test suite 'tests' for cabal-gh5213-0.1..
Running 1 test suites...
Test suite tests: RUNNING...
Test suite tests: PASS
Test suite logged to: <ROOT>/cabal.dist/work/./dist/build/<ARCH>/ghc-<GHCVER>/cabal-gh5213-0.1/t/tests/noopt/test/cabal-gh5213-0.1-tests.log
Test coverage report written to <ROOT>/cabal.dist/work/./dist/build/<ARCH>/ghc-<GHCVER>/cabal-gh5213-0.1/t/tests/noopt/hpc/vanilla/html/tests/hpc_index.html
1 of 1 test suites (1 of 1 test cases) passed.
Package coverage report written to <ROOT>/cabal.dist/work/./dist/build/<ARCH>/ghc-<GHCVER>/cabal-gh5213-0.1/t/tests/noopt/hpc/vanilla/html/cabal-gh5213-0.1/hpc_index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
packages: .

package cabal-gh5213
coverage: True
optimization: False
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Test.Cabal.Prelude
main = cabalTest $ cabal "new-test" []
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module CabalGH5213Exposed where

import CabalGH5213Other

foo :: Int
foo = bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module CabalGH5213Other where

bar :: Int
bar = 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main where

import CabalGH5213Exposed

main :: IO ()
main = print foo
10 changes: 10 additions & 0 deletions changelog.d/pr-7493
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
synopsis: Fix `cabal test --enable-library-coverage` for other-modules
packages: Cabal
prs: #7493
issues: #5213
description: {

- Fix `cabal test --enable-library-coverage` for libraries with nonempty other-modules field.
- Due to a hack, this breaks coverage whenever the used Haskell compiler is called 't' (for a non-hacky fix we should rework HPC directories, possibly enabling multilib in the process, see #6440 and #6397).

}