Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions Cabal-tests/tests/ParserTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ import Control.Monad (unless, void)
import Data.Algorithm.Diff (PolyDiff (..), getGroupedDiff)
import Data.Maybe (isNothing)
import Distribution.Fields (pwarning)
import Distribution.PackageDescription (GenericPackageDescription)
import Distribution.PackageDescription
( GenericPackageDescription
( packageDescription
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it's more robust to import Distribution.PackageDescription (GenericPackageDescription, fooField, bazField) than import Distribution.PackageDescription (GenericPackageDescription (fooField, bazField)). If in future the fields of GPD are changed and old accessors become simple functions (retained for backwards compatibility), the first form survives without a change, while the second would have to be amended.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know this is possible, I'll change this :)

, gpdScannedVersion
, genPackageFlags
, condLibrary
, condSubLibraries
, condForeignLibs
, condExecutables
, condTestSuites
, condBenchmarks
)
)
import Distribution.PackageDescription.Parsec (parseGenericPackageDescription)
import Distribution.PackageDescription.PrettyPrint (showGenericPackageDescription)
import Distribution.Parsec (PWarnType (..), PWarning (..), showPErrorWithSource, showPWarningWithSource)
Expand Down Expand Up @@ -237,7 +249,16 @@ treeDiffGoldenTest fp = ediffGolden goldenTest "expr" exprFile $ do
let res = withSource (PCabalFile (fp, contents)) $ parseGenericPackageDescription contents
let (_, x) = runParseResult res
case x of
Right gpd -> pure (toExpr gpd)
Right gpd -> pure $ toExpr
( gpd
-- Test accessors because they encapsulate the merging behaviour
, condLibrary gpd
, condSubLibraries gpd
, condForeignLibs gpd
, condExecutables gpd
, condTestSuites gpd
, condBenchmarks gpd
)
Left (_, errs) -> fail $ unlines $ "ERROR" : map (showPErrorWithSource . fmap renderCabalFileSource) (NE.toList errs)
where
input = "tests" </> "ParserTests" </> "regressions" </> fp
Expand All @@ -250,24 +271,38 @@ formatRoundTripTest fp = testCase "roundtrip" $ do
x <- parse contents
let contents' = showGenericPackageDescription x
y <- parse (toUTF8BS contents')
-- previously we mangled licenses a bit
let y' = y

let checkField field =
unless (field x == field y) $
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While at it, could you possible use (@?) or assertBool instead of raw assertFailure?

{- FOURMOLU_DISABLE -}
unless (x == y') $
#ifdef MIN_VERSION_tree_diff
assertFailure $ unlines
[ "re-parsed doesn't match"
, show $ ansiWlEditExpr $ ediff x y
]
assertFailure $ unlines
[ "re-parsed doesn't match"
, show $ ansiWlEditExpr $ ediff x y
]
#else
assertFailure $ unlines
[ "re-parsed doesn't match"
, "expected"
, show x
, "actual"
, show y
]
assertFailure $ unlines
[ "re-parsed doesn't match"
, "expected"
, show x
, "actual"
, show y
]
#endif
-- Due to the imports being merged, the structural comparison will fail
-- Instead, we check the equality after merging
sequence_
[ checkField packageDescription
, checkField gpdScannedVersion
, checkField genPackageFlags
, checkField condLibrary
, checkField condSubLibraries
, checkField condForeignLibs
, checkField condExecutables
, checkField condTestSuites
, checkField condBenchmarks
]

where
parse :: BS.ByteString -> IO GenericPackageDescription
parse c = do
Expand Down
Loading
Loading