Skip to content

Commit

Permalink
Fixes mix file read failure when using cabal >= 1.22 / ghc >= 7.10 (f…
Browse files Browse the repository at this point in the history
…ixes #44)
  • Loading branch information
killy971 committed Aug 30, 2015
1 parent 1c6ea13 commit 91924d1
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist/
*.ps
*.prof
*.json
*.local
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[1.0.0](https://github.com/guillaume-nargeot/hpc-coveralls/issues?q=milestone:v1.0.0+is:closed)
* Add support for cabal 1.22 / ghc 7.10 (issue #44)

[0.9.0](https://github.com/guillaume-nargeot/hpc-coveralls/issues?q=milestone:v0.9.0+is:closed)
-----
* Fix instructions and target hpc data directory for Cabal 1.22 / GHC 7.10 (issue #38)
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ For example, you can try various combinations of the other options and confirm t

This boolean option enables curl verbose mode and prints the raw json response received after posting the coverage report to coveralls.io.

#### --cabal-file

Use this option to specify the cabal file of the coverage report target package.
This might be required in some cases, especially when building with cabal >= 1.22 and ghc >= 7.10, although hpc-coveralls assumes the package cabal file to be the unique file of extension ".cabal" in the current directory if it exists.
For further details check [this issue](https://github.com/guillaume-nargeot/hpc-coveralls/issues/44).

# Limitations

Because of the way hpc works, coverage data is only generated for modules that are referenced directly or indirectly by the test suites.
Expand Down
4 changes: 2 additions & 2 deletions hpc-coveralls.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hpc-coveralls
version: 0.9.0
version: 1.0.0
synopsis: Coveralls.io support for Haskell.
description:
This utility converts and sends Haskell projects hpc code coverage to
Expand Down Expand Up @@ -28,7 +28,7 @@ category: Control
build-type: Simple
stability: experimental
cabal-version: >= 1.8
tested-with: GHC == 7.6, GHC == 7.8
tested-with: GHC == 7.6, GHC == 7.8, GHC == 7.10
homepage: https://github.com/guillaume-nargeot/hpc-coveralls
bug-reports: https://github.com/guillaume-nargeot/hpc-coveralls/issues

Expand Down
2 changes: 2 additions & 0 deletions src/HpcCoverallsCmdLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Trace.Hpc.Coveralls.Types
data HpcCoverallsArgs = CmdMain
{ optExcludeDirs :: [String]
, argTestSuites :: [String]
, optCabalFile :: Maybe String
, optRepoToken :: Maybe String
, optDisplayReport :: Bool
, optCurlVerbose :: Bool
Expand All @@ -25,6 +26,7 @@ hpcCoverallsArgs = CmdMain
, optCurlVerbose = False &= explicit &= name "curl-verbose" &= help "Enable curl verbose mode and prints the json response received from coveralls.io"
, optDontSend = False &= explicit &= name "dont-send" &= help "Do not send the report to coveralls.io"
, optCoverageMode = AllowPartialLines &= explicit &= typ "MODE" &= name "coverage-mode" &= help "Coverage conversion mode: AllowPartialLines (default), StrictlyFullLines"
, optCabalFile = Nothing &= explicit &= typ "FILE" &= name "cabal-file" &= help "Cabal file (ex.: module-name.cabal)"
, optRepoToken = Nothing &= explicit &= typ "TOKEN" &= name "repo-token" &= help "Coveralls repo token"
, argTestSuites = [] &= typ "TEST-SUITES" &= args
} &= summary ("hpc-coveralls-" ++ versionString version ++ ", (C) Guillaume Nargeot 2014-2015")
Expand Down
15 changes: 12 additions & 3 deletions src/HpcCoverallsMain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import System.Console.CmdArgs
import System.Environment (getEnv, getEnvironment)
import System.Exit (exitFailure)
import Trace.Hpc.Coveralls
import Trace.Hpc.Coveralls.Config (Config(Config))
import Trace.Hpc.Coveralls.Cabal
import Trace.Hpc.Coveralls.Config (Config(Config), cabalFile)
import Trace.Hpc.Coveralls.Curl
import Trace.Hpc.Coveralls.GitInfo (getGitInfo)
import Trace.Hpc.Coveralls.Util
Expand All @@ -39,7 +40,12 @@ writeJson :: String -> Value -> IO ()
writeJson filePath = BSL.writeFile filePath . encode

getConfig :: HpcCoverallsArgs -> Maybe Config
getConfig hca = Config (optExcludeDirs hca) (optCoverageMode hca) (optRepoToken hca) <$> listToMaybe (argTestSuites hca)
getConfig hca = Config
(optExcludeDirs hca)
(optCoverageMode hca)
(optCabalFile hca)
(optRepoToken hca)
<$> listToMaybe (argTestSuites hca)

main :: IO ()
main = do
Expand All @@ -48,8 +54,11 @@ main = do
Nothing -> putStrLn "Please specify a target test suite name"
Just config -> do
(serviceName, jobId) <- getServiceAndJobID
mPkgNameVer <- case cabalFile config of
Just cabalFilePath -> getPackageNameVersion cabalFilePath
Nothing -> currDirPkgNameVer
gitInfo <- getGitInfo
coverallsJson <- generateCoverallsFromTix serviceName jobId gitInfo config
coverallsJson <- generateCoverallsFromTix serviceName jobId gitInfo config mPkgNameVer
when (optDisplayReport hca) $ BSL.putStrLn $ encode coverallsJson
let filePath = serviceName ++ "-" ++ jobId ++ ".json"
writeJson filePath coverallsJson
Expand Down
26 changes: 14 additions & 12 deletions src/Trace/Hpc/Coveralls.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,22 @@ mergeModuleCoverageData (source, mix, tixs1) (_, _, tixs2) =
mergeCoverageData :: [TestSuiteCoverageData] -> TestSuiteCoverageData
mergeCoverageData = foldr1 (M.unionWith mergeModuleCoverageData)

readMix' :: String -> String -> TixModule -> IO Mix
readMix' hpcDir name tix = readMix [getMixPath hpcDir name tix] (Right tix)
readMix' :: Maybe String -> String -> String -> TixModule -> IO Mix
readMix' mPkgNameVer hpcDir name tix = readMix [getMixPath mPkgNameVer hpcDir name tix] (Right tix)

This comment has been minimized.

Copy link
@killy971

killy971 Aug 31, 2015

Author Owner

Might be good to populate the array with the results of two calls to getMixPath:

  • one call identical to the current version,
  • one with "Nothing" instead of mPkgNameVer.

-- | Create a list of coverage data from the tix input
readCoverageData :: String -- ^ hpc data directory
readCoverageData :: Maybe String -- ^ Package name-version
-> String -- ^ hpc data directory
-> [String] -- ^ excluded source folders
-> String -- ^ test suite name
-> IO TestSuiteCoverageData -- ^ coverage data list
readCoverageData hpcDir excludeDirPatterns testSuiteName = do
readCoverageData mPkgNameVer hpcDir excludeDirPatterns testSuiteName = do
let tixPath = getTixPath hpcDir testSuiteName
mTix <- readTix tixPath
case mTix of
Nothing -> putStrLn ("Couldn't find the file " ++ tixPath) >> dumpDirectoryTree hpcDir >> ioFailure
Just (Tix tixs) -> do
mixs <- mapM (readMix' hpcDir testSuiteName) tixs
mixs <- mapM (readMix' mPkgNameVer hpcDir testSuiteName) tixs
let files = map filePath mixs
sources <- mapM readFile files
let coverageDataList = zip4 files sources mixs (map tixModuleTixs tixs)
Expand All @@ -128,17 +129,18 @@ readCoverageData hpcDir excludeDirPatterns testSuiteName = do
sourceDirFilter = not . matchAny excludeDirPatterns . fst4

-- | Generate coveralls json formatted code coverage from hpc coverage data
generateCoverallsFromTix :: String -- ^ CI name
-> String -- ^ CI Job ID
-> GitInfo -- ^ Git repo information
-> Config -- ^ hpc-coveralls configuration
-> IO Value -- ^ code coverage result in json format
generateCoverallsFromTix serviceName jobId gitInfo config = do
generateCoverallsFromTix :: String -- ^ CI name
-> String -- ^ CI Job ID
-> GitInfo -- ^ Git repo information
-> Config -- ^ hpc-coveralls configuration
-> Maybe String -- ^ Package name-version
-> IO Value -- ^ code coverage result in json format
generateCoverallsFromTix serviceName jobId gitInfo config mPkgNameVer = do
mHpcDir <- firstExistingDirectory hpcDirs
case mHpcDir of
Nothing -> putStrLn "Couldn't find the hpc data directory" >> dumpDirectory distDir >> ioFailure
Just hpcDir -> do
testSuitesCoverages <- mapM (readCoverageData hpcDir excludedDirPatterns) testSuiteNames
testSuitesCoverages <- mapM (readCoverageData mPkgNameVer hpcDir excludedDirPatterns) testSuiteNames
let coverageData = mergeCoverageData testSuitesCoverages
return $ toCoverallsJson serviceName jobId repoTokenM gitInfo converter coverageData
where excludedDirPatterns = excludedDirs config
Expand Down
12 changes: 6 additions & 6 deletions src/Trace/Hpc/Coveralls/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
-- Maintainer: Guillaume Nargeot <[email protected]>
-- Stability: experimental
--
-- Functions for reading the cabal package name and version.
-- Functions for reading cabal package name and version.

module Trace.Hpc.Coveralls.Cabal (packageNameVersion) where
module Trace.Hpc.Coveralls.Cabal (currDirPkgNameVer, getPackageNameVersion) where

import Control.Applicative
import Control.Monad
Expand All @@ -21,8 +21,8 @@ import System.Directory

getCabalFile :: FilePath -> IO (Maybe FilePath)
getCabalFile dir = do
cnts <- (filter isCabal <$> getDirectoryContents dir) >>= filterM doesFileExist
case cnts of
files <- (filter isCabal <$> getDirectoryContents dir) >>= filterM doesFileExist
case files of
[file] -> return $ Just file
_ -> return Nothing
where isCabal filename = ".cabal" `isSuffixOf` filename && length filename > 6
Expand All @@ -38,7 +38,7 @@ getPackageNameVersion file = do
version = showVersion (pkgVersion pkg)
showVersion = intercalate "." . map show . versionBranch

packageNameVersion :: IO (Maybe String)
packageNameVersion = runMaybeT $ pkgNameVersion currentDir
currDirPkgNameVer :: IO (Maybe String)
currDirPkgNameVer = runMaybeT $ pkgNameVersion currentDir
where pkgNameVersion = MaybeT . getPackageNameVersion <=< MaybeT . getCabalFile
currentDir = "."
1 change: 1 addition & 0 deletions src/Trace/Hpc/Coveralls/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Trace.Hpc.Coveralls.Types (CoverageMode)
data Config = Config {
excludedDirs :: ![FilePath],
coverageMode :: !CoverageMode,
cabalFile :: !(Maybe FilePath),
repoToken :: !(Maybe String),
testSuites :: ![String]
}
11 changes: 8 additions & 3 deletions src/Trace/Hpc/Coveralls/Paths.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
module Trace.Hpc.Coveralls.Paths where

import Control.Monad
import Data.Maybe
import Data.Traversable (traverse)
import System.Directory (
doesDirectoryExist, getDirectoryContents
Expand All @@ -33,11 +34,15 @@ tixDir = (++ "tix/")
mixDir :: String -> FilePath
mixDir = (++ "mix/")

getMixPath :: String -> String -> TixModule -> FilePath
getMixPath hpcDir testSuiteName tix = mixDir hpcDir ++ dirName ++ "/"
getMixPath :: Maybe String -- ^ target package name-version
-> String -- ^ hpc output base directory
-> String -- ^ test suite name
-> TixModule -- ^ tix module
-> FilePath -- ^ mix file patch
getMixPath mPkgNameVer hpcDir testSuiteName tix = mixDir hpcDir ++ dirName ++ "/"
where dirName = case span (/= '/') modName of
(_, []) -> testSuiteName
(packageId, _) -> packageId
(packageId, _) -> fromMaybe packageId mPkgNameVer
TixModule modName _ _ _ = tix

getTixPath :: String -> String -> FilePath
Expand Down

0 comments on commit 91924d1

Please sign in to comment.