-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Allow user to specify "package directories" corresponding to the source directories of packages in a cabal.project. - Add functionality required to allow the user to specify the hpc data search directories explicitly, rather than searching for it in the usual places. This is primarily motivated by the Nix package manager, where hpc output is usally written to some folder outside of the current directory (e.g. to /nix/store/HASH-my-lib-0.1.0.0/share/hpc) as well as cabal.projects, where the hpc output directory might be "dist-newstyle/build/$platform/$compiler/$package/hpc". Multiple hpc directories can be specified, for e.g., one for each package in a cabal.project. - The filepath listed in a mix file doesn't include the sub-directory of the package in a cabal.project. So for cabal.projects, the filepath used as the index in the TestSuiteCoverageData is now prefixed with the sub-directory of the package. The behaviour when not using the "--package-dir" argument, or when using "--package-dir ./" is unchanged. - Gave a Monoid instance to TestSuiteCoverageData so we can easily use folds to sum the data up. - Separated the coveralls.io specific logic and the coverage data logic. The coverage data could be re-used for other coveralls-style tools. - Source files are now excluded before searching for them, so that if the file does not exist, it does not throw an error (primarily motivated by out-of-source builds such as Nix, where modules such as Path_library.hs can't be found in the build directory). - Changed the structure of getCoverageData a little, and added some commentary, so each of the steps required to read all the coverage data are a bit clearer.
- Loading branch information
1 parent
04fe42c
commit 80d8c82
Showing
11 changed files
with
459 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- Module: Trace.Hpc.Coveralls | ||
-- Copyright: (c) 2014-2015 Guillaume Nargeot | ||
-- License: BSD3 | ||
-- Maintainer: Guillaume Nargeot <[email protected]> | ||
-- Stability: experimental | ||
-- | ||
-- Functions for collection hpc data. | ||
|
||
module Trace.Hpc.Coverage ( getCoverageData ) where | ||
|
||
import Control.Applicative | ||
import Data.List | ||
import Data.Maybe (fromMaybe) | ||
import Data.Semigroup ((<>)) | ||
import qualified Data.Map.Strict as M | ||
import System.Directory (findFile) | ||
import Trace.Hpc.Coveralls.Paths | ||
import Trace.Hpc.Coveralls.Types | ||
import Trace.Hpc.Coveralls.Util | ||
import Trace.Hpc.Mix | ||
import Trace.Hpc.Tix | ||
|
||
readMix' :: [PackageIdentifier] -> [FilePath] -> String -> TixModule -> IO Mix | ||
readMix' pkgIds hpcDirs name tix = readMix dirs (Right tix) | ||
where | ||
dirs = nub $ (\p hpcDir -> getMixPath p hpcDir name tix) <$> (Nothing : (Just <$> pkgNameVers)) <*> hpcDirs | ||
pkgNameVers = asNameVer <$> pkgIds | ||
|
||
readTix' :: [FilePath] | ||
-- ^ HPC data directories | ||
-> String | ||
-- ^ Test suite name | ||
-> IO Tix | ||
-- ^ Tix | ||
readTix' hpcDirs testSuiteName = do | ||
let tixFileLocations = possibleTixFileLocations hpcDirs testSuiteName | ||
mTixPath <- firstExistingFile tixFileLocations | ||
|
||
case mTixPath of | ||
Nothing -> | ||
putStrLn ("Couldn't find any of the possible tix file locations: " ++ show tixFileLocations) >> ioFailure | ||
Just tixPath -> do | ||
mTix <- readTix tixPath | ||
case mTix of | ||
Nothing -> | ||
putStrLn ("Couldn't read the file " ++ tixPath) >> ioFailure | ||
Just tix -> pure tix | ||
|
||
getCoverageData | ||
:: [Package] | ||
-- ^ Packages | ||
-> [FilePath] | ||
-- ^ HPC data directories | ||
-> [String] | ||
-- ^ Excluded source folders | ||
-> [String] | ||
-- ^ Test suite names | ||
-> IO TestSuiteCoverageData | ||
getCoverageData pkgs hpcDirs excludedDirPatterns testSuiteNames = do | ||
-- For each test suite | ||
foldFor testSuiteNames $ \testSuiteName -> do | ||
|
||
-- Read the tix file for the test suite | ||
(Tix tixModules) <- readTix' hpcDirs testSuiteName | ||
|
||
-- For each TixModule in the tix file | ||
foldFor tixModules $ \tixModule@(TixModule _ _ _ tixs) -> do | ||
|
||
-- Read the mix file | ||
mix@(Mix filePath _ _ _ _) <- readMix' pkgIds hpcDirs testSuiteName tixModule | ||
|
||
-- Also read the source associated with the mix file, but only if it's not excluded | ||
if matchAny excludedDirPatterns filePath | ||
then mempty -- If excluded, we just return monoidal identity | ||
else do | ||
-- Find source relative to project sub-directory (e.g. "./", "./my-lib-01") | ||
projectFilePath <- findProjectSourceFile pkgDirs filePath | ||
source <- readFile projectFilePath | ||
|
||
-- Package source up with module mix and tix information, indexed by the file path. | ||
pure . TestSuiteCoverageData $ M.singleton projectFilePath (source, mix, tixs) | ||
|
||
-- Sum all this up using the Monoid instance for TestCoverageData. | ||
|
||
where | ||
pkgIds = pkgId <$> pkgs | ||
pkgDirs = pkgRootDir <$> pkgs | ||
|
||
findProjectSourceFile :: [FilePath] -> FilePath -> IO FilePath | ||
findProjectSourceFile pkgDirs fp = do | ||
mFile <- findFile pkgDirs fp | ||
case mFile of | ||
Nothing -> | ||
putStrLn ("Couldn't find the source file " ++ fp ++ " in directories: " <> show pkgDirs <> ".") >> ioFailure | ||
(Just actualFilePath) -> | ||
pure (removeLeading "./" $ -- To retain consistency with current reports | ||
actualFilePath) | ||
where | ||
-- Remove prefix from a string (if present, do nothing otherwise) | ||
removeLeading :: String -> String -> String | ||
removeLeading prefix path = fromMaybe path $ stripPrefix prefix path |
Oops, something went wrong.