11{-# LANGUAGE DeriveGeneric #-}
2- {-# LANGUAGE RecordWildCards #-}
32{-# LANGUAGE FlexibleContexts #-}
3+ {-# LANGUAGE RecordWildCards #-}
4+
45module Test.Cabal.DecodeShowBuildInfo where
56
6- import Test.Cabal.Prelude
7- import Test.Cabal.Plan
8- import Distribution.Compat.Stack
9- import Distribution.Text ( display )
10- import Distribution.Types.ComponentName
11- import Distribution.Types.LibraryName
12- import Distribution.Types.UnqualComponentName
13- import Distribution.Package
14- import Distribution.Pretty ( prettyShow )
15- import Control.Monad.Trans.Reader
16- import Data.Aeson
17- import GHC.Generics
18- import System.Exit
7+ import Control.Monad.Trans.Reader
8+ import Data.Aeson
9+ import Distribution.Compat.Stack
10+ import Distribution.Package
11+ import Distribution.Pretty ( prettyShow )
12+ import Distribution.Text ( display )
13+ import Distribution.Types.ComponentName
14+ import Distribution.Types.LibraryName
15+ import Distribution.Types.UnqualComponentName
16+ import GHC.Generics
17+ import System.Exit
18+ import Test.Cabal.Plan
19+ import Test.Cabal.Prelude
1920
2021-- | Execute 'cabal build --enable-build-info'.
2122--
2223-- Results can be read via 'withPlan', 'buildInfoFile' and 'decodeBuildInfoFile'.
2324runShowBuildInfo :: [String ] -> TestM ()
24- runShowBuildInfo args = noCabalPackageDb $ cabal " build" (" --enable-build-info" : args)
25+ runShowBuildInfo args = noCabalPackageDb $ cabal " build" (" --enable-build-info" : args)
2526
2627-- | Read 'build-info.json' for a given package and component
2728-- from disk and record the content. Helpful for defining test-cases
@@ -51,13 +52,15 @@ data BuildInfo = BuildInfo
5152 { cabalLibVersion :: String
5253 , compiler :: CompilerInfo
5354 , components :: [ComponentInfo ]
54- } deriving (Generic , Show )
55+ }
56+ deriving (Generic , Show )
5557
5658data CompilerInfo = CompilerInfo
5759 { flavour :: String
5860 , compilerId :: String
5961 , path :: String
60- } deriving (Generic , Show )
62+ }
63+ deriving (Generic , Show )
6164
6265data ComponentInfo = ComponentInfo
6366 { componentType :: String
@@ -68,22 +71,23 @@ data ComponentInfo = ComponentInfo
6871 , componentSrcFiles :: [FilePath ]
6972 , componentHsSrcDirs :: [FilePath ]
7073 , componentSrcDir :: FilePath
71- } deriving (Generic , Show )
74+ }
75+ deriving (Generic , Show )
7276
7377instance ToJSON BuildInfo where
7478 toEncoding = genericToEncoding defaultOptions
7579instance FromJSON BuildInfo where
76- parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = camelTo2 ' -' }
80+ parseJSON = genericParseJSON defaultOptions{ fieldLabelModifier = camelTo2 ' -' }
7781
7882instance ToJSON CompilerInfo where
7983 toEncoding = genericToEncoding defaultOptions
8084instance FromJSON CompilerInfo where
81- parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = camelTo2 ' -' }
85+ parseJSON = genericParseJSON defaultOptions{ fieldLabelModifier = camelTo2 ' -' }
8286
8387instance ToJSON ComponentInfo where
8488 toEncoding = genericToEncoding defaultOptions
8589instance FromJSON ComponentInfo where
86- parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = drop 10 . camelTo2 ' -' }
90+ parseJSON = genericParseJSON defaultOptions{ fieldLabelModifier = drop 10 . camelTo2 ' -' }
8791
8892-- -----------------------------------------------------------
8993-- Assertion Helpers to define succinct test cases
@@ -105,14 +109,15 @@ data ComponentAssertion = ComponentAssertion
105109 }
106110
107111defCompAssertion :: ComponentAssertion
108- defCompAssertion = ComponentAssertion
109- { unitIdPred = not . null
110- , compilerArgsPred = not . null
111- , modules = []
112- , sourceFiles = []
113- , sourceDirs = []
114- , compType = " "
115- }
112+ defCompAssertion =
113+ ComponentAssertion
114+ { unitIdPred = not . null
115+ , compilerArgsPred = not . null
116+ , modules = []
117+ , sourceFiles = []
118+ , sourceDirs = []
119+ , compType = " "
120+ }
116121
117122-- | Assert common build information, such as compiler location, compiler version
118123-- and cabal library version.
@@ -128,8 +133,8 @@ assertCommonBuildInfo buildInfo = do
128133assertComponentPure :: WithCallStack (ComponentInfo -> ComponentAssertion -> TestM () )
129134assertComponentPure component ComponentAssertion {.. } = do
130135 assertEqual " Component type" compType (componentType component)
131- assertBool " Component Unit Id" (unitIdPred $ componentUnitId component)
132- assertBool " Component compiler args" (compilerArgsPred $ componentCompilerArgs component)
136+ assertBool " Component Unit Id" (unitIdPred $ componentUnitId component)
137+ assertBool " Component compiler args" (compilerArgsPred $ componentCompilerArgs component)
133138 assertEqual " Component modules" modules (componentModules component)
134139 assertEqual " Component source files" sourceFiles (componentSrcFiles component)
135140 assertEqual " Component source directories" sourceDirs (componentHsSrcDirs component)
@@ -148,11 +153,11 @@ assertComponent pkgName cname assert = do
148153 assertCommonBuildInfo buildInfo
149154
150155 let component = findComponentInfo buildInfo
151- let assertWithCompType = assert { compType = compTypeStr cname }
156+ let assertWithCompType = assert{ compType = compTypeStr cname}
152157 assertComponentPure component assertWithCompType
153158 where
154159 compTypeStr :: ComponentName -> String
155- compTypeStr (CLibName _) = " lib"
160+ compTypeStr (CLibName _) = " lib"
156161 compTypeStr (CFLibName _) = " flib"
157162 compTypeStr (CExeName _) = " exe"
158163 compTypeStr (CTestName _) = " test"
@@ -162,10 +167,17 @@ assertComponent pkgName cname assert = do
162167 findComponentInfo buildInfo =
163168 case filter (\ c -> prettyShow cname == componentName c) (components buildInfo) of
164169 [x] -> x
165- [] -> error $ " findComponentInfo: component " ++ prettyShow cname ++ " does not"
166- ++ " exist in build info-file"
167- _ -> error $ " findComponentInfo: found multiple copies of component " ++ prettyShow cname
168- ++ " in build info plan"
170+ [] ->
171+ error $
172+ " findComponentInfo: component "
173+ ++ prettyShow cname
174+ ++ " does not"
175+ ++ " exist in build info-file"
176+ _ ->
177+ error $
178+ " findComponentInfo: found multiple copies of component "
179+ ++ prettyShow cname
180+ ++ " in build info plan"
169181
170182-- | Helper function to create an executable component name.
171183exe :: String -> ComponentName
0 commit comments