Skip to content

Commit a372933

Browse files
committed
Add real tests for language extensions solving
This also includes modifications to the solver testing DSL and the testing functions. See haskell#2732.
1 parent b3f1a63 commit a372933

File tree

2 files changed

+76
-42
lines changed
  • cabal-install/tests/UnitTests/Distribution/Client/Dependency/Modular

2 files changed

+76
-42
lines changed

cabal-install/tests/UnitTests/Distribution/Client/Dependency/Modular/DSL.hs

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ exDbPkgs = map (either exInstName exAvName)
141141

142142
exAvSrcPkg :: ExampleAvailable -> SourcePackage
143143
exAvSrcPkg ex =
144-
let (libraryDeps, testSuites) = splitTopLevel (CD.libraryDeps (exAvDeps ex))
144+
let (libraryDeps, testSuites, exts) = splitTopLevel (CD.libraryDeps (exAvDeps ex))
145145
in SourcePackage {
146146
packageInfoId = exAvPkgId ex
147147
, packageSource = LocalTarballPackage "<<path>>"
@@ -160,27 +160,31 @@ exAvSrcPkg ex =
160160
}
161161
, C.genPackageFlags = concatMap extractFlags
162162
(CD.libraryDeps (exAvDeps ex))
163-
, C.condLibrary = Just $ mkCondTree libraryDeps
163+
, C.condLibrary = Just $ mkCondTree (extsLib exts) libraryDeps
164164
, C.condExecutables = []
165-
, C.condTestSuites = map (\(t, deps) -> (t, mkCondTree deps))
165+
, C.condTestSuites = map (\(t, deps) -> (t, mkCondTree mempty deps))
166166
testSuites
167167
, C.condBenchmarks = []
168168
}
169169
}
170170
where
171-
-- Split the set of dependencies into the set of dependencies of the library
172-
-- and the dependencies of the test suites.
171+
-- Split the set of dependencies into the set of dependencies of the library,
172+
-- the dependencies of the test suites and extensions.
173173
splitTopLevel :: [ExampleDependency]
174174
-> ( [ExampleDependency]
175175
, [(ExampleTestName, [ExampleDependency])]
176+
, [Extension]
176177
)
177-
splitTopLevel [] = ([], [])
178+
splitTopLevel [] = ([], [], [])
178179
splitTopLevel (ExTest t a:deps) =
179-
let (other, testSuites) = splitTopLevel deps
180-
in (other, (t, a):testSuites)
180+
let (other, testSuites, exts) = splitTopLevel deps
181+
in (other, (t, a):testSuites, exts)
182+
splitTopLevel (ExExt ext:deps) =
183+
let (other, testSuites, exts) = splitTopLevel deps
184+
in (other, testSuites, ext:exts)
181185
splitTopLevel (dep:deps) =
182-
let (other, testSuites) = splitTopLevel deps
183-
in (dep:other, testSuites)
186+
let (other, testSuites, exts) = splitTopLevel deps
187+
in (dep:other, testSuites, exts)
184188

185189
-- Extract the total set of flags used
186190
extractFlags :: ExampleDependency -> [C.Flag]
@@ -197,11 +201,11 @@ exAvSrcPkg ex =
197201
extractFlags (ExExt _) = []
198202
extractFlags (ExLang _) = []
199203

200-
mkCondTree :: Monoid a => [ExampleDependency] -> DependencyTree a
201-
mkCondTree deps =
204+
mkCondTree :: Monoid a => a -> [ExampleDependency] -> DependencyTree a
205+
mkCondTree x deps =
202206
let (directDeps, flaggedDeps) = splitDeps deps
203207
in C.CondNode {
204-
C.condTreeData = mempty -- irrelevant to the solver
208+
C.condTreeData = x -- Necessary for language extensions
205209
, C.condTreeConstraints = map mkDirect directDeps
206210
, C.condTreeComponents = map mkFlagged flaggedDeps
207211
}
@@ -217,14 +221,17 @@ exAvSrcPkg ex =
217221
-> (C.Condition C.ConfVar
218222
, DependencyTree a, Maybe (DependencyTree a))
219223
mkFlagged (f, a, b) = ( C.Var (C.Flag (C.FlagName f))
220-
, mkCondTree a
221-
, Just (mkCondTree b)
224+
, mkCondTree mempty a
225+
, Just (mkCondTree mempty b)
222226
)
223227

224-
-- Split a set of dependencies into the set of unconditional dependencies
225-
-- and the set of dependencies guarded by a flag.
228+
-- Split a set of dependencies into direct dependencies and flagged
229+
-- dependencies. A direct dependency is a tuple of the name of package and
230+
-- maybe its version (no version means any version) meant to be converted
231+
-- to a 'C.Dependency' with 'mkDirect' for example. A flagged dependency is
232+
-- the set of dependencies guarded by a flag.
226233
--
227-
-- TODO: Maybe Int indicates dependency on a particular package
234+
-- TODO: Take care of flagged language extensions
228235
splitDeps :: [ExampleDependency]
229236
-> ( [(ExamplePkgName, Maybe Int)]
230237
, [(ExampleFlagName, [ExampleDependency], [ExampleDependency])]
@@ -242,16 +249,18 @@ exAvSrcPkg ex =
242249
in (directDeps, (f, a, b):flaggedDeps)
243250
splitDeps (ExTest _ _:_) =
244251
error "Unexpected nested test"
245-
splitDeps (ExExt _ext:deps) =
246-
splitDeps deps
247-
splitDeps (ExLang _lang:deps) =
248-
splitDeps deps
252+
splitDeps (_:deps) = splitDeps deps
249253

250254
-- Currently we only support simple setup dependencies
251255
mkSetupDeps :: [ExampleDependency] -> [C.Dependency]
252256
mkSetupDeps deps =
253257
let (directDeps, []) = splitDeps deps in map mkDirect directDeps
254258

259+
-- A 'C.Library' with just the given extensions in its 'BuildInfo'
260+
extsLib :: [Extension] -> C.Library
261+
extsLib es = mempty { C.libBuildInfo = mempty { C.otherExtensions = es } }
262+
263+
255264
exAvPkgId :: ExampleAvailable -> C.PackageIdentifier
256265
exAvPkgId ex = C.PackageIdentifier {
257266
pkgName = C.PackageName (exAvName ex)
@@ -279,15 +288,21 @@ exInstIdx :: [ExampleInstalled] -> C.PackageIndex.InstalledPackageIndex
279288
exInstIdx = C.PackageIndex.fromList . map exInstInfo
280289

281290
exResolve :: ExampleDb
291+
-- List of extensions supported by the compiler.
292+
-> [Extension]
282293
-> [ExamplePkgName]
283294
-> Bool
284295
-> ([String], Either String CI.InstallPlan.InstallPlan)
285-
exResolve db targets indepGoals = runProgress $
296+
exResolve db exts targets indepGoals = runProgress $
286297
resolveDependencies C.buildPlatform
287-
(C.unknownCompilerInfo C.buildCompilerId C.NoAbiTag)
298+
compiler
288299
Modular
289300
params
290301
where
302+
defaultCompiler = C.unknownCompilerInfo C.buildCompilerId C.NoAbiTag
303+
compiler = if null exts
304+
then defaultCompiler
305+
else defaultCompiler { C.compilerInfoExtensions = Just exts }
291306
(inst, avai) = partitionEithers db
292307
instIdx = exInstIdx inst
293308
avaiIdx = SourcePackageDb {

cabal-install/tests/UnitTests/Distribution/Client/Dependency/Modular/Solver.hs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Test.Tasty.HUnit (testCase, assertEqual, assertBool)
1414
import Test.Tasty.Options
1515

1616
-- Cabal
17-
import Language.Haskell.Extension -- (Extension(..), UnknownExtension, Language)
17+
import Language.Haskell.Extension (Extension(..), KnownExtension(..))
1818

1919
-- cabal-install
2020
import UnitTests.Distribution.Client.Dependency.Modular.DSL
@@ -71,7 +71,13 @@ tests = [
7171
, runTest $ mkTest db12 "baseShim6" ["E"] (Just [("E", 1), ("syb", 2)])
7272
]
7373
, testGroup "Extensions" [
74-
runTest $ mkTest dbExt1 "unsupportedExtension" ["A"] Nothing
74+
runTest $ mkTestExts [EnableExtension CPP] dbExts1 "unsupported" ["A"] Nothing
75+
, runTest $ mkTestExts [EnableExtension CPP] dbExts1 "unsupportedTransitive" ["B"] Nothing
76+
, runTest $ mkTestExts [EnableExtension RankNTypes] dbExts1 "supported" ["A"] (Just [("A",1)])
77+
, runTest $ mkTestExts (map EnableExtension [CPP,RankNTypes]) dbExts1 "supportedTransitive" ["C"] (Just [("A",1),("B",1), ("C",1)])
78+
, runTest $ mkTestExts [EnableExtension CPP] dbExts1 "disabledExtension" ["D"] Nothing
79+
, runTest $ mkTestExts (map EnableExtension [CPP,RankNTypes]) dbExts1 "disabledExtension" ["D"] Nothing
80+
, runTest $ mkTestExts (UnknownExtension "custom" : map EnableExtension [CPP,RankNTypes]) dbExts1 "unknownExtension" ["E"] (Just [("A",1),("B",1),("C",1),("E",1)])
7581
]
7682
]
7783
where
@@ -82,30 +88,40 @@ tests = [
8288
-------------------------------------------------------------------------------}
8389

8490
data SolverTest = SolverTest {
85-
testLabel :: String
86-
, testTargets :: [String]
87-
, testResult :: Maybe [(String, Int)]
88-
, testIndepGoals :: Bool
89-
, testDb :: ExampleDb
91+
testLabel :: String
92+
, testTargets :: [String]
93+
, testResult :: Maybe [(String, Int)]
94+
, testIndepGoals :: Bool
95+
, testDb :: ExampleDb
96+
, testSupportedExts :: [Extension]
9097
}
9198

9299
mkTest :: ExampleDb
93100
-> String
94101
-> [String]
95102
-> Maybe [(String, Int)]
96103
-> SolverTest
97-
mkTest db label targets result = SolverTest {
98-
testLabel = label
99-
, testTargets = targets
100-
, testResult = result
101-
, testIndepGoals = False
102-
, testDb = db
104+
mkTest = mkTestExts []
105+
106+
mkTestExts :: [Extension]
107+
-> ExampleDb
108+
-> String
109+
-> [String]
110+
-> Maybe [(String, Int)]
111+
-> SolverTest
112+
mkTestExts exts db label targets result = SolverTest {
113+
testLabel = label
114+
, testTargets = targets
115+
, testResult = result
116+
, testIndepGoals = False
117+
, testDb = db
118+
, testSupportedExts = exts
103119
}
104120

105121
runTest :: SolverTest -> TF.TestTree
106122
runTest SolverTest{..} = askOption $ \(OptionShowSolverLog showSolverLog) ->
107123
testCase testLabel $ do
108-
let (_msgs, result) = exResolve testDb testTargets testIndepGoals
124+
let (_msgs, result) = exResolve testDb testSupportedExts testTargets testIndepGoals
109125
when showSolverLog $ mapM_ putStrLn _msgs
110126
case result of
111127
Left err -> assertBool ("Unexpected error:\n" ++ err) (isNothing testResult)
@@ -346,10 +362,13 @@ db12 =
346362
, Right $ exAv "E" 1 [ExFix "base" 4, ExFix "syb" 2]
347363
]
348364

349-
350-
dbExt1 :: ExampleDb
351-
dbExt1 = [
352-
Right $ exAv "A" 1 [ExExt $ UnknownExtension "unknown"]
365+
dbExts1 :: ExampleDb
366+
dbExts1 = [
367+
Right $ exAv "A" 1 [ExExt (EnableExtension RankNTypes)]
368+
, Right $ exAv "B" 1 [ExExt (EnableExtension CPP), ExAny "A"]
369+
, Right $ exAv "C" 1 [ExAny "B"]
370+
, Right $ exAv "D" 1 [ExExt (DisableExtension CPP), ExAny "B"]
371+
, Right $ exAv "E" 1 [ExExt (UnknownExtension "custom"), ExAny "C"]
353372
]
354373

355374

0 commit comments

Comments
 (0)