diff --git a/Cabal/src/Distribution/Simple/Hpc.hs b/Cabal/src/Distribution/Simple/Hpc.hs index b678772c1e6..4e292509d78 100644 --- a/Cabal/src/Distribution/Simple/Hpc.hs +++ b/Cabal/src/Distribution/Simple/Hpc.hs @@ -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 diff --git a/cabal-testsuite/PackageTests/Regression/T5213/cabal-gh5213.cabal b/cabal-testsuite/PackageTests/Regression/T5213/cabal-gh5213.cabal new file mode 100644 index 00000000000..1f27196421e --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213/cabal-gh5213.cabal @@ -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 diff --git a/cabal-testsuite/PackageTests/Regression/T5213/cabal.out b/cabal-testsuite/PackageTests/Regression/T5213/cabal.out new file mode 100644 index 00000000000..096ac64545c --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213/cabal.out @@ -0,0 +1,19 @@ +# cabal new-test +Resolving dependencies... +Build profile: -w ghc- -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: /cabal.dist/work/./dist/build//ghc-/cabal-gh5213-0.1/t/tests/test/cabal-gh5213-0.1-tests.log +Test coverage report written to /cabal.dist/work/./dist/build//ghc-/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 /cabal.dist/work/./dist/build//ghc-/cabal-gh5213-0.1/t/tests/hpc/vanilla/html/cabal-gh5213-0.1/hpc_index.html diff --git a/cabal-testsuite/PackageTests/Regression/T5213/cabal.project b/cabal-testsuite/PackageTests/Regression/T5213/cabal.project new file mode 100644 index 00000000000..e1c33e00303 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213/cabal.project @@ -0,0 +1,4 @@ +packages: . + +package cabal-gh5213 + library-coverage: true diff --git a/cabal-testsuite/PackageTests/Regression/T5213/cabal.test.hs b/cabal-testsuite/PackageTests/Regression/T5213/cabal.test.hs new file mode 100644 index 00000000000..cbb92ca7473 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213/cabal.test.hs @@ -0,0 +1,2 @@ +import Test.Cabal.Prelude +main = cabalTest $ cabal "new-test" [] \ No newline at end of file diff --git a/cabal-testsuite/PackageTests/Regression/T5213/src/CabalGH5213Exposed.hs b/cabal-testsuite/PackageTests/Regression/T5213/src/CabalGH5213Exposed.hs new file mode 100644 index 00000000000..becd65eff8b --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213/src/CabalGH5213Exposed.hs @@ -0,0 +1,6 @@ +module CabalGH5213Exposed where + +import CabalGH5213Other + +foo :: Int +foo = bar diff --git a/cabal-testsuite/PackageTests/Regression/T5213/src/CabalGH5213Other.hs b/cabal-testsuite/PackageTests/Regression/T5213/src/CabalGH5213Other.hs new file mode 100644 index 00000000000..b1194f103cb --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213/src/CabalGH5213Other.hs @@ -0,0 +1,4 @@ +module CabalGH5213Other where + +bar :: Int +bar = 42 diff --git a/cabal-testsuite/PackageTests/Regression/T5213/tests/Main.hs b/cabal-testsuite/PackageTests/Regression/T5213/tests/Main.hs new file mode 100644 index 00000000000..ee2b00a4d8a --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213/tests/Main.hs @@ -0,0 +1,6 @@ +module Main where + +import CabalGH5213Exposed + +main :: IO () +main = print foo diff --git a/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal-gh5213.cabal b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal-gh5213.cabal new file mode 100644 index 00000000000..1f27196421e --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal-gh5213.cabal @@ -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 diff --git a/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.out b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.out new file mode 100644 index 00000000000..3b888a6899d --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.out @@ -0,0 +1,19 @@ +# cabal new-test +Resolving dependencies... +Build profile: -w ghc- -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: /cabal.dist/work/./dist/build//ghc-/cabal-gh5213-0.1/t/tests/noopt/test/cabal-gh5213-0.1-tests.log +Test coverage report written to /cabal.dist/work/./dist/build//ghc-/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 /cabal.dist/work/./dist/build//ghc-/cabal-gh5213-0.1/t/tests/noopt/hpc/vanilla/html/cabal-gh5213-0.1/hpc_index.html diff --git a/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.project b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.project new file mode 100644 index 00000000000..ee157cb0f02 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.project @@ -0,0 +1,5 @@ +packages: . + +package cabal-gh5213 + coverage: True + optimization: False \ No newline at end of file diff --git a/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.test.hs b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.test.hs new file mode 100644 index 00000000000..cbb92ca7473 --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/cabal.test.hs @@ -0,0 +1,2 @@ +import Test.Cabal.Prelude +main = cabalTest $ cabal "new-test" [] \ No newline at end of file diff --git a/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/src/CabalGH5213Exposed.hs b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/src/CabalGH5213Exposed.hs new file mode 100644 index 00000000000..becd65eff8b --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/src/CabalGH5213Exposed.hs @@ -0,0 +1,6 @@ +module CabalGH5213Exposed where + +import CabalGH5213Other + +foo :: Int +foo = bar diff --git a/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/src/CabalGH5213Other.hs b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/src/CabalGH5213Other.hs new file mode 100644 index 00000000000..b1194f103cb --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/src/CabalGH5213Other.hs @@ -0,0 +1,4 @@ +module CabalGH5213Other where + +bar :: Int +bar = 42 diff --git a/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/tests/Main.hs b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/tests/Main.hs new file mode 100644 index 00000000000..ee2b00a4d8a --- /dev/null +++ b/cabal-testsuite/PackageTests/Regression/T5213ExeCoverage/tests/Main.hs @@ -0,0 +1,6 @@ +module Main where + +import CabalGH5213Exposed + +main :: IO () +main = print foo diff --git a/changelog.d/pr-7493 b/changelog.d/pr-7493 new file mode 100644 index 00000000000..00f3033c1c2 --- /dev/null +++ b/changelog.d/pr-7493 @@ -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). + +}