Skip to content

Commit

Permalink
Add Parsec test for all and specific PackageConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
jgotoh committed Jan 29, 2024
1 parent c4ef63f commit 4ed5455
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cabal-install/src/Distribution/Client/ProjectConfig/Parsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ parseCondTree programDb source fields0 = do
-- TODO parse conditionals
return configSkeleton

-- Monad in which sections are parsed
-- | Monad in which sections are parsed
type SectionParser = StateT SectionS ParseResult

-- | State of section parser
-- | State of 'SectionParser'
newtype SectionS = SectionS
{ _stateConfig :: ProjectConfig
}
Expand Down Expand Up @@ -153,6 +153,7 @@ parsePackageName :: Position -> [SectionArg Position] -> ParseResult (Maybe Pack
parsePackageName pos args = case args of
[SecArgName _ secName] -> parseName secName
[SecArgStr _ secName] -> parseName secName
[SecArgOther _ secName] -> parseName secName
_ -> do
parseWarning pos PWTUnknownSection "target package name or * required"
return Nothing
Expand All @@ -161,7 +162,8 @@ parsePackageName pos args = case args of
Left _ -> return Nothing
Right cfgTarget -> return $ pure cfgTarget
parser :: ParsecParser PackageConfigTarget
parser = P.choice [P.try (P.char '*' >> return AllPackages), SpecificPackage <$> parsec] -- parse * or parsec :: m PackageName
parser =
P.choice [P.try (P.char '*' >> return AllPackages), SpecificPackage <$> parsec]

warnInvalidSubsection pos name = lift $ parseWarning pos PWTInvalidSubsection $ "invalid subsection " ++ show name

Expand Down
42 changes: 42 additions & 0 deletions cabal-testsuite/PackageTests/ProjectConfig/Parsec/cabal.test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ main = do
cabalTest' "read project-config-shared" testProjectConfigShared
cabalTest' "set explicit provenance" testProjectConfigProvenance
cabalTest' "read project-config-local-packages" testProjectConfigLocalPackages
cabalTest' "read project-config-all-packages" testProjectConfigAllPackages
cabalTest' "read project-config-specific-packages" testProjectConfigSpecificPackages

testPackages :: TestM ()
testPackages = do
Expand Down Expand Up @@ -279,6 +281,46 @@ testProjectConfigLocalPackages = do
packageConfigTestTestOptions = [toPathTemplate "--some-option", toPathTemplate "42"]
packageConfigBenchmarkOptions = [toPathTemplate "--some-benchmark-option", toPathTemplate "--another-option"]

testProjectConfigAllPackages :: TestM ()
testProjectConfigAllPackages = do
let rootFp = "project-config-all-packages"
(config, legacy) <- readConfigDefault rootFp
assertConfig expected config legacy (projectConfigAllPackages . condTreeData)
where
expected :: PackageConfig
expected =
mempty
{ packageConfigProfDetail = Flag ProfDetailAllFunctions
, packageConfigProfLibDetail = Flag ProfDetailExportedFunctions
}

testProjectConfigSpecificPackages :: TestM ()
testProjectConfigSpecificPackages = do
let rootFp = "project-config-specific-packages"
(config, legacy) <- readConfigDefault rootFp
assertConfig expected config legacy (projectConfigSpecificPackage . condTreeData)
where
expected = MapMappend $ Map.fromList [("foo", expectedFoo), ("bar", expectedBar), ("baz", expectedBaz)]
expectedFoo :: PackageConfig
expectedFoo =
mempty
{ packageConfigProfDetail = Flag ProfDetailAllFunctions
, packageConfigProfLibDetail = Flag ProfDetailExportedFunctions
, packageConfigVanillaLib = Flag True
}
expectedBar :: PackageConfig
expectedBar =
mempty
{ packageConfigProfDetail = Flag ProfDetailTopLate
, packageConfigProfLibDetail = Flag ProfDetailNone
, packageConfigProgPrefix = Flag $ toPathTemplate "prefix/path"
}
expectedBaz :: PackageConfig
expectedBaz =
mempty
{ packageConfigSharedLib = Flag True
}

readConfigDefault :: FilePath -> TestM (ProjectConfigSkeleton, ProjectConfigSkeleton)
readConfigDefault testSubDir = readConfig testSubDir "cabal.project"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package *
profiling-detail: all-functions
library-profiling-detail: exported-functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package foo
profiling-detail: all-functions
library-profiling-detail: exported-functions
library-vanilla: true

package bar
profiling-detail: late-toplevel
library-profiling-detail: none
program-prefix: prefix/path

package baz
shared: True

0 comments on commit 4ed5455

Please sign in to comment.