Skip to content

Commit

Permalink
Convert to expression level coverage (incomplete), refactoring and in…
Browse files Browse the repository at this point in the history
…troduction of Json module
  • Loading branch information
killy971 committed Jan 20, 2015
1 parent 4dbb0e5 commit a420f6f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 33 deletions.
7 changes: 5 additions & 2 deletions codecov-haskell.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ library
hs-source-dirs: src
exposed-modules:
Trace.Hpc.Codecov,
Trace.Hpc.Codecov.Json,
Trace.Hpc.Codecov.Lix,
Trace.Hpc.Codecov.Types,
Trace.Hpc.Codecov.Util
Expand All @@ -62,7 +63,8 @@ library
hpc >= 0.6,
retry >= 0.5,
safe >= 0.3,
split
split,
vector

executable codecov-haskell
hs-source-dirs: src
Expand All @@ -77,7 +79,8 @@ executable codecov-haskell
hpc >= 0.6,
retry >= 0.5,
safe >= 0.3,
split
split,
vector
ghc-options: -Wall -fwarn-tabs -fwarn-incomplete-uni-patterns

executable run-cabal-test
Expand Down
2 changes: 1 addition & 1 deletion src/CodecovHaskellCmdLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ codecovHaskellArgs = CmdMain
, printResponse = False &= explicit &= name "print-response" &= help "Prints the json reponse received from codecov.io"
, dontSend = False &= explicit &= name "dont-send" &= help "Do not send the report to codecov.io"
, testSuites = [] &= typ "TEST-SUITE" &= args
} &= summary ("codecov-haskell-" ++ versionString version ++ ", (C) Guillaume Nargeot 2014")
} &= summary ("codecov-haskell-" ++ versionString version ++ ", (C) Guillaume Nargeot 2014-2015")
&= program "codecov-haskell"
where versionString = intercalate "." . map show . versionBranch
18 changes: 2 additions & 16 deletions src/Trace/Hpc/Codecov.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-- |
-- Module: Trace.Hpc.Codecov
-- Copyright: (c) 2014 Guillaume Nargeot
-- Copyright: (c) 2014-2015 Guillaume Nargeot
-- License: BSD3
-- Maintainer: Guillaume Nargeot <guillaume+hackage@nargeot.com>
-- Stability: experimental
Expand All @@ -18,6 +18,7 @@ import Data.List
import qualified Data.Map.Strict as M
import System.Exit (exitFailure)
import Trace.Hpc.Codecov.Config
import Trace.Hpc.Codecov.Json
import Trace.Hpc.Codecov.Lix
import Trace.Hpc.Codecov.Paths
import Trace.Hpc.Codecov.Types
Expand All @@ -33,21 +34,6 @@ type ModuleCoverageData = (

type TestSuiteCoverageData = M.Map FilePath ModuleCoverageData

-- single file coverage data in the format defined by codecov.io
type SimpleCoverage = [CoverageValue]

-- Is there a way to restrict this to only Number and Null?
type CoverageValue = Value

type LixConverter = Lix -> SimpleCoverage

defaultConverter :: LixConverter
defaultConverter = map $ \lix -> case lix of
Full -> Number 1
Partial -> Bool True
None -> Number 0
Irrelevant -> Null

toSimpleCoverage :: LixConverter -> Int -> [CoverageEntry] -> SimpleCoverage
toSimpleCoverage convert lineCount = (:) Null . convert . toLix lineCount

Expand Down
41 changes: 41 additions & 0 deletions src/Trace/Hpc/Codecov/Json.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{-# LANGUAGE OverloadedStrings #-}

-- |
-- Module: Trace.Hpc.Json
-- Copyright: (c) 2014-2015 Guillaume Nargeot
-- License: BSD3
-- Maintainer: Guillaume Nargeot <guillaume+hackage@nargeot.com>
-- Stability: experimental
--
-- Functions for generating the core coverage data in the json format defined by codecov.io

module Trace.Hpc.Codecov.Json ( defaultConverter ) where

import Control.Arrow (first)
import Data.Aeson
import Data.Aeson.Types ()
import Data.Maybe
import qualified Data.Vector as V
import Trace.Hpc.Codecov.Types

convHit :: Hit -> Value
convHit hit = case hit of
Full -> Number 1
Partial -> Bool True
None -> Number 0
Irrelevant -> Null

convArray :: [Value] -> Value
convArray = Array . V.fromList

convTuple :: (Value, (Int, Int)) -> Value
convTuple (hit, (start, end)) = convArray [toVal start, toVal end, hit]
where toVal = Number . fromIntegral

convExpr :: [ExprHit] -> Value
convExpr = convArray . map (convTuple . first convHit)

defaultConverter :: LixConverter
defaultConverter = map (convLine . fmap convExpr)
where convLine = fromMaybe Null

22 changes: 10 additions & 12 deletions src/Trace/Hpc/Codecov/Lix.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- |
-- Module: Trace.Hpc.Codecov.Lix
-- Copyright: (c) 2014 Guillaume Nargeot
-- Copyright: (c) 2014-2015 Guillaume Nargeot
-- License: BSD3
-- Maintainer: Guillaume Nargeot <guillaume+hackage@nargeot.com>
-- Stability: experimental
Expand All @@ -12,7 +12,7 @@ module Trace.Hpc.Codecov.Lix where

import Data.List
import Data.Ord
import Prelude hiding (getLine)
import Prelude
import Trace.Hpc.Codecov.Types
import Trace.Hpc.Codecov.Util
import Trace.Hpc.Mix
Expand All @@ -26,12 +26,10 @@ toHit xs
| or xs = Partial
| otherwise = None

getLine :: MixEntry -> Int
getLine = fffst . fromHpcPos . fst
where fffst (x, _, _, _) = x

toLineHit :: CoverageEntry -> (Int, Bool)
toLineHit (entries, counts, _source) = (getLine (head entries) - 1, all (> 0) counts)
toExprHit :: CoverageEntry -> (Int, ExprHit)
toExprHit (entries, counts, _) = (line - 1, (hit, (start, end)))
where (line, start, _, end) = fromHpcPos $ fst $ head entries
hit = toHit $ map (> 0) counts

isOtherwiseEntry :: CoverageEntry -> Bool
isOtherwiseEntry (mixEntries, _, source) =
Expand All @@ -50,8 +48,8 @@ adjust coverageEntry@(mixEntries, tixs, source) =

-- | Convert hpc coverage entries into a line based coverage format
toLix :: Int -- ^ Source line count
-> [CoverageEntry] -- ^ Mix entries and associated hit count
-> [CoverageEntry] -- ^ Coverage entries
-> Lix -- ^ Line coverage
toLix lineCount entries = map toHit (groupByIndex lineCount sortedLineHits)
where sortedLineHits = sortBy (comparing fst) lineHits
lineHits = map (toLineHit . adjust) entries
toLix lineCount entries = map listToMaybe (groupByIndex lineCount sortedExprHits)
where sortedExprHits = sortBy (comparing fst) exprHits
exprHits = map (toExprHit . adjust) entries
12 changes: 10 additions & 2 deletions src/Trace/Hpc/Codecov/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-- |
-- Module: Trace.Hpc.Codecov.Types
-- Copyright: (c) 2014 Guillaume Nargeot
-- Copyright: (c) 2014-2015 Guillaume Nargeot
-- License: BSD3
-- Maintainer: Guillaume Nargeot <guillaume+hackage@nargeot.com>
-- Stability: experimental
Expand All @@ -11,9 +11,15 @@

module Trace.Hpc.Codecov.Types where

import Data.Aeson
import Network.Curl
import Trace.Hpc.Mix

-- single file coverage data in the format defined by codecov.io
type SimpleCoverage = [Value]

type LixConverter = Lix -> SimpleCoverage

type CoverageEntry = (
[MixEntry], -- mix entries
[Integer], -- tix values
Expand All @@ -25,7 +31,9 @@ data Hit = Full
| Irrelevant
deriving (Eq, Show)

type Lix = [Hit]
type ExprHit = (Hit, (Int, Int))

type Lix = [Maybe [ExprHit]]

-- | Result to the POST request to codecov.io
data PostResult =
Expand Down

0 comments on commit a420f6f

Please sign in to comment.