@@ -14,8 +14,18 @@ import Control.Monad (unless, void)
1414import Data.Algorithm.Diff (PolyDiff (.. ), getGroupedDiff )
1515import Data.Maybe (isNothing )
1616import Distribution.Fields (pwarning )
17- import Distribution.PackageDescription (GenericPackageDescription )
18- import Distribution.Types.GenericPackageDescription (condLibrary )
17+ import Distribution.PackageDescription
18+ ( GenericPackageDescription
19+ , packageDescription
20+ , gpdScannedVersion
21+ , genPackageFlags
22+ , condLibrary
23+ , condSubLibraries
24+ , condForeignLibs
25+ , condExecutables
26+ , condTestSuites
27+ , condBenchmarks
28+ )
1929import Distribution.PackageDescription.Parsec (parseGenericPackageDescription )
2030import Distribution.PackageDescription.PrettyPrint (showGenericPackageDescription )
2131import Distribution.Parsec (PWarnType (.. ), PWarning (.. ), showPErrorWithSource , showPWarningWithSource )
@@ -35,15 +45,13 @@ import qualified Distribution.InstalledPackageInfo as IPI
3545
3646#ifdef MIN_VERSION_tree_diff
3747import Data.TreeDiff (ansiWlEditExpr , ediff , toExpr )
38- import Data.TreeDiff.Class (ToExpr )
3948import Data.TreeDiff.Golden (ediffGolden )
4049import Data.TreeDiff.Instances.Cabal ()
4150#endif
4251
4352tests :: TestTree
4453tests = testGroup " parsec tests"
4554 [ regressionTests
46- , accessorsTests
4755 , warningTests
4856 , errorTests
4957 , ipiTests
@@ -153,40 +161,6 @@ errorTest fp = cabalGoldenTest fp correct $ do
153161 input = " tests" </> " ParserTests" </> " errors" </> fp
154162 correct = replaceExtension input " errors"
155163
156- -------------------------------------------------------------------------------
157- -- Merging accessors tests
158- -------------------------------------------------------------------------------
159-
160- accessorsTests :: TestTree
161- accessorsTests = testGroup " accessors"
162- [
163- #ifdef MIN_VERSION_tree_diff
164- accessorsGoldenTestCondLibrary
165- [ " library-merging.cabal"
166- ]
167- #endif
168- ]
169-
170- #ifdef MIN_VERSION_tree_diff
171- accessorsGoldenTestCondLibrary :: [FilePath ] -> TestTree
172- accessorsGoldenTestCondLibrary = testGroup " condLibrary" . map (accessorsGoldenTest condLibrary)
173-
174- accessorsGoldenTest
175- :: ToExpr a
176- => (GenericPackageDescription -> a )
177- -> FilePath -> TestTree
178- accessorsGoldenTest f fp = ediffGolden goldenTest fp exprFile $ do
179- contents <- BS. readFile input
180- let res = withSource (PCabalFile (fp, contents)) $ parseGenericPackageDescription contents
181- let (_, x) = runParseResult res
182- case x of
183- Right gpd -> pure . toExpr $ f gpd
184- Left (_, errs) -> fail $ unlines $ " ERROR" : map (showPErrorWithSource . fmap renderCabalFileSource) (NE. toList errs)
185- where
186- input = " tests" </> " ParserTests" </> " accessors" </> fp
187- exprFile = replaceExtension input " expr"
188- #endif
189-
190164-------------------------------------------------------------------------------
191165-- Regressions
192166-------------------------------------------------------------------------------
@@ -270,16 +244,29 @@ formatGoldenTest fp = cabalGoldenTest "format" correct $ do
270244
271245#ifdef MIN_VERSION_tree_diff
272246treeDiffGoldenTest :: FilePath -> TestTree
273- treeDiffGoldenTest fp = ediffGolden goldenTest " expr" exprFile $ do
274- contents <- BS. readFile input
275- let res = withSource (PCabalFile (fp, contents)) $ parseGenericPackageDescription contents
276- let (_, x) = runParseResult res
277- case x of
278- Right gpd -> pure (toExpr gpd)
279- Left (_, errs) -> fail $ unlines $ " ERROR" : map (showPErrorWithSource . fmap renderCabalFileSource) (NE. toList errs)
280- where
281- input = " tests" </> " ParserTests" </> " regressions" </> fp
282- exprFile = replaceExtension input " expr"
247+ treeDiffGoldenTest fp =
248+ let go label f = ediffGolden goldenTest label exprFile $ do
249+ contents <- BS. readFile input
250+ let res = withSource (PCabalFile (fp, contents)) $ parseGenericPackageDescription contents
251+ let (_, x) = runParseResult res
252+ case x of
253+ Right gpd -> pure (toExpr $ f gpd)
254+ Left (_, errs) -> fail $ unlines $ " ERROR" : map (showPErrorWithSource . fmap renderCabalFileSource) (NE. toList errs)
255+ where
256+ input = " tests" </> " ParserTests" </> " regressions" </> fp
257+ exprFile = replaceExtension input (label <> " .expr" )
258+ in testGroup " expr"
259+ [ go " packageDescription" packageDescription
260+ , go " gpdScannedVersion" gpdScannedVersion
261+ , go " genPackageFlags" genPackageFlags
262+ -- Test accessors because they encapsulate the merging behaviour
263+ , go " condLibrary" condLibrary
264+ , go " condSubLibraries" condSubLibraries
265+ , go " condForeignLibs" condForeignLibs
266+ , go " condExecutables" condExecutables
267+ , go " condTestSuites" condTestSuites
268+ , go " condBenchmarks" condBenchmarks
269+ ]
283270#endif
284271
285272formatRoundTripTest :: FilePath -> TestTree
@@ -288,24 +275,38 @@ formatRoundTripTest fp = testCase "roundtrip" $ do
288275 x <- parse contents
289276 let contents' = showGenericPackageDescription x
290277 y <- parse (toUTF8BS contents')
291- -- previously we mangled licenses a bit
292- let y' = y
278+
279+ let checkField field =
280+ unless (field x == field y) $
293281{- FOURMOLU_DISABLE -}
294- unless (x == y') $
295282#ifdef MIN_VERSION_tree_diff
296- assertFailure $ unlines
297- [ " re-parsed doesn't match"
298- , show $ ansiWlEditExpr $ ediff x y
299- ]
283+ assertFailure $ unlines
284+ [ " re-parsed doesn't match"
285+ , show $ ansiWlEditExpr $ ediff x y
286+ ]
300287#else
301- assertFailure $ unlines
302- [ " re-parsed doesn't match"
303- , " expected"
304- , show x
305- , " actual"
306- , show y
307- ]
288+ assertFailure $ unlines
289+ [ " re-parsed doesn't match"
290+ , " expected"
291+ , show x
292+ , " actual"
293+ , show y
294+ ]
308295#endif
296+ -- Due to the imports being merged, the structural comparison will fail
297+ -- Instead, we check the equality after merging
298+ sequence_
299+ [ checkField packageDescription
300+ , checkField gpdScannedVersion
301+ , checkField genPackageFlags
302+ , checkField condLibrary
303+ , checkField condSubLibraries
304+ , checkField condForeignLibs
305+ , checkField condExecutables
306+ , checkField condTestSuites
307+ , checkField condBenchmarks
308+ ]
309+
309310 where
310311 parse :: BS. ByteString -> IO GenericPackageDescription
311312 parse c = do
0 commit comments