on master help sign in
Flexible tests for checkBuildPlan (#375)

@snoyberg Can you confirm that this simple test suite works for you? I've set this up so that I can then add some cyclic dependencies and add an occurs check for that, but figured I'd setup tests for some of the existing invariants first. Should be like: $ cabal test --ghc-options=-O0; cat dist/test/stackage-0.4.0-spec.log Building stackage-0.4.0... Preprocessing library stackage-0.4.0... In-place registering stackage-0.4.0... Preprocessing executable 'stackage' for stackage-0.4.0... Preprocessing test suite 'spec' for stackage-0.4.0... [3 of 4] Compiling Stackage.BuildPlanSpec ( test/Stackage/BuildPlanSpec.hs, dist/build/spec/spec-tmp/Stackage/BuildPlanSpec.o ) Linking dist/build/spec/spec ... Running 1 test suites... Test suite spec: RUNNING... Test suite spec: PASS Test suite logged to: dist/test/stackage-0.4.0-spec.log 1 of 1 test suites (1 of 1 test cases) passed. Test suite spec: RUNNING... Stackage.BuildPlan simple package set bad version range on depdendency fails nonexistent package fails to check default package set checks ok Stackage.CorePackages works contains known core packages getCoreExecutables includes known executables Stackage.PackageIndex works getLatestDescriptions gives reasonable results Finished in 14.3302 seconds 9 examples, 0 failures Test suite spec: PASS Test suite logged to: dist/test/stackage-0.4.0-spec.log

commit45b33ac54d795e69ef2625de1d9aae9628fe6327 authorChris Done <chrisdone@gmail.com> dateSun 4 Jan 2015, 22:26 (12 years ago) parent5da6e5c
4 files changed+131−15
Stackage/BuildConstraints.hs+1 Stackage/CheckBuildPlan.hs+1 test/Stackage/BuildPlanSpec.hs+109−15 test/test-build-constraints.yaml+20
Stackage/BuildConstraints.hs+1
@@ -10,6 +10,7 @@ module Stackage.BuildConstraints
1010 , SystemInfo (..)
1111 , getSystemInfo
1212 , defaultBuildConstraints
13+ , toBC
1314 ) where
1415
1516import Control.Monad.Writer.Strict (execWriter, tell)
Stackage/CheckBuildPlan.hs+1
@@ -7,6 +7,7 @@
77-- | Confirm that a build plan has a consistent set of dependencies.
88module Stackage.CheckBuildPlan
99 ( checkBuildPlan
10+ , BadBuildPlan
1011 ) where
1112
1213import Control.Monad.Writer.Strict (Writer, execWriter, tell)
test/Stackage/BuildPlanSpec.hs+109−15
@@ -1,22 +1,56 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
12{-# LANGUAGE OverloadedStrings, NoImplicitPrelude #-}
23module Stackage.BuildPlanSpec (spec) where
34
4−import Stackage.BuildPlan
5−import Stackage.Prelude
6−import Stackage.BuildConstraints
7−import Stackage.UpdateBuildPlan
8−import Test.Hspec
9−import qualified Data.Yaml as Y
10−import Distribution.Version (anyVersion)
115import qualified Data.Map as Map
12−import Network.HTTP.Client (withManager)
13−import Network.HTTP.Client.TLS (tlsManagerSettings)
6+import qualified Data.Map.Strict as M
7+import qualified Data.Set as S
8+import Data.Yaml
9+import qualified Data.Yaml as Y
10+import Distribution.Version
11+import Network.HTTP.Client
12+import Network.HTTP.Client.TLS (tlsManagerSettings)
13+import Stackage.BuildConstraints
14+import Stackage.BuildPlan
15+import Stackage.CheckBuildPlan
16+import Stackage.PackageDescription
17+import Stackage.Prelude
18+import Stackage.UpdateBuildPlan
19+import Test.Hspec
1420
1521spec :: Spec
16−spec = it "works" $ withManager tlsManagerSettings $ \man -> do
17− bc <- defaultBuildConstraints man
18− pkgs <- getLatestAllowedPlans bc
19− bp <- newBuildPlan pkgs bc
22+spec = do
23+ it "simple package set" $ check testBuildConstraints $ makePackageSet
24+ [("foo", [0, 0, 0], [("bar", thisV [0, 0, 0])])
25+ ,("bar", [0, 0, 0], [])]
26+ it "bad version range on depdendency fails" $ badBuildPlan $ makePackageSet
27+ [("foo", [0, 0, 0], [("bar", thisV [1, 1, 0])])
28+ ,("bar", [0, 0, 0], [])]
29+ it "nonexistent package fails to check" $ badBuildPlan $ makePackageSet
30+ [("foo", [0, 0, 0], [("nonexistent", thisV [0, 0, 0])])
31+ ,("bar", [0, 0, 0], [])]
32+ it "default package set checks ok" $ check defaultBuildConstraints getLatestAllowedPlans
33+
34+-- | Checking should be considered a bad build plan.
35+badBuildPlan :: (BuildConstraints -> IO (Map PackageName PackagePlan))
36+ -> void
37+ -> IO ()
38+badBuildPlan m _ = do
39+ mu <- try (check testBuildConstraints m)
40+ case mu of
41+ Left (_ :: BadBuildPlan) ->
42+ return ()
43+ Right () ->
44+ error "Expected bad build plan."
45+
46+-- | Check build plan with the given package set getter.
47+check :: (Manager -> IO BuildConstraints)
48+ -> (BuildConstraints -> IO (Map PackageName PackagePlan))
49+ -> IO ()
50+check readPlanFile getPlans = withManager tlsManagerSettings $ \man -> do
51+ bc <- readPlanFile man
52+ plans <- getPlans bc
53+ bp <- newBuildPlan plans bc
2054 let bs = Y.encode bp
2155 ebp' = Y.decodeEither bs
2256
@@ -26,14 +60,74 @@ spec = it "works" $ withManager tlsManagerSettings $ \man -> do
2660 forM_ allPackages $ \name ->
2761 (name, lookup name (bpPackages bp')) `shouldBe`
2862 (name, lookup name (bpPackages bp))
29−
3063 bpGithubUsers bp' `shouldBe` bpGithubUsers bp
64+
3165 when (bp' /= bp) $ error "bp' /= bp"
32− bp2 <- updateBuildPlan pkgs bp
66+ bp2 <- updateBuildPlan plans bp
3367 when (dropVersionRanges bp2 /= dropVersionRanges bp) $ error "bp2 /= bp"
68+ checkBuildPlan bp
3469 where
3570 dropVersionRanges bp =
3671 bp { bpPackages = map go $ bpPackages bp }
3772 where
3873 go pb = pb { ppConstraints = go' $ ppConstraints pb }
3974 go' pc = pc { pcVersionRange = anyVersion }
75+
76+-- | Make a package set from a convenient data structure.
77+makePackageSet
78+ :: [(String,[Int],[(String,VersionRange)])]
79+ -> BuildConstraints
80+ -> IO (Map PackageName PackagePlan)
81+makePackageSet ps _ =
82+ return $
83+ M.fromList $
84+ map
85+ (\(name,ver,deps) ->
86+ ( PackageName name
87+ , dummyPackage ver $
88+ M.fromList $
89+ map
90+ (\(dname,dver) ->
91+ ( PackageName dname
92+ , DepInfo {diComponents = S.fromList
93+ [CompLibrary]
94+ ,diRange = dver}))
95+ deps))
96+ ps
97+ where
98+ dummyPackage v deps =
99+ PackagePlan
100+ {ppVersion = Version v []
101+ ,ppGithubPings = mempty
102+ ,ppUsers = mempty
103+ ,ppConstraints =
104+ PackageConstraints
105+ {pcVersionRange = anyV
106+ ,pcMaintainer = Nothing
107+ ,pcTests = Don'tBuild
108+ ,pcHaddocks = Don'tBuild
109+ ,pcBuildBenchmarks = False
110+ ,pcFlagOverrides = mempty
111+ ,pcEnableLibProfile = False}
112+ ,ppDesc =
113+ SimpleDesc
114+ {sdPackages = deps
115+ ,sdTools = mempty
116+ ,sdProvidedExes = mempty
117+ ,sdModules = mempty}}
118+
119+-- | This exact version is required.
120+thisV :: [Int] -> VersionRange
121+thisV ver = thisVersion (Version ver [])
122+
123+-- | Accept any version.
124+anyV :: VersionRange
125+anyV = anyVersion
126+
127+-- | Test plan.
128+testBuildConstraints :: void -> IO BuildConstraints
129+testBuildConstraints _ =
130+ decodeFileEither
131+ (fpToString fp) >>=
132+ either throwIO toBC
133+ where fp = "test/test-build-constraints.yaml"
test/test-build-constraints.yamlnew file+20
@@ -0,0 +1,20 @@
1+packages:
2+ "Test":
3+ - foo
4+ - bar
5+
6+global-flags: []
7+
8+skipped-tests: []
9+expected-test-failures: []
10+expected-haddock-failures: []
11+skipped-benchmarks: []
12+skipped-profiling: []
13+
14+github-users:
15+ bar:
16+ - demo
17+
18+package-flags:
19+ foo:
20+ demo: true