Skip to content

Commit ca2ea6b

Browse files
Put Arbitrary instances for Cabal types in their own package.
This is with the intention of the new package, cabal-quickcheck-instances, being the blessed location for these orphans, as QuickCheck acquiring a Cabal dependency or vice-versa would be unsuitable. This reduces some duplication (some presumably deliberate, and some apparently accidental) and then some drift between the versions of these instances. Due to haskell#1575, some tests have had to move from the Cabal package to cabal-install.
1 parent bba31e6 commit ca2ea6b

File tree

16 files changed

+459
-340
lines changed

16 files changed

+459
-340
lines changed

Cabal/Cabal.cabal

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,21 +490,17 @@ test-suite unit-tests
490490
hs-source-dirs: tests
491491
other-modules:
492492
Test.Laws
493-
Test.QuickCheck.Utils
494493
UnitTests.Distribution.Compat.CreatePipe
495494
UnitTests.Distribution.Compat.ReadP
496495
UnitTests.Distribution.Compat.Time
497496
UnitTests.Distribution.Compat.Graph
498497
UnitTests.Distribution.Simple.Glob
499498
UnitTests.Distribution.Simple.Program.Internal
500499
UnitTests.Distribution.Simple.Utils
501-
UnitTests.Distribution.SPDX
502-
UnitTests.Distribution.System
503500
UnitTests.Distribution.Types.GenericPackageDescription
504501
UnitTests.Distribution.Utils.Generic
505502
UnitTests.Distribution.Utils.NubList
506503
UnitTests.Distribution.Utils.ShortText
507-
UnitTests.Distribution.Version
508504
main-is: UnitTests.hs
509505
build-depends:
510506
array,

Cabal/tests/Test/QuickCheck/Utils.hs

Lines changed: 0 additions & 29 deletions
This file was deleted.

Cabal/tests/UnitTests.hs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ import qualified UnitTests.Distribution.Compat.Graph
2020
import qualified UnitTests.Distribution.Simple.Glob
2121
import qualified UnitTests.Distribution.Simple.Program.Internal
2222
import qualified UnitTests.Distribution.Simple.Utils
23-
import qualified UnitTests.Distribution.System
2423
import qualified UnitTests.Distribution.Utils.Generic
2524
import qualified UnitTests.Distribution.Utils.NubList
2625
import qualified UnitTests.Distribution.Utils.ShortText
27-
import qualified UnitTests.Distribution.Version (versionTests)
28-
import qualified UnitTests.Distribution.SPDX (spdxTests)
2926
import qualified UnitTests.Distribution.Types.GenericPackageDescription
3027

3128
tests :: Int -> TestTree
@@ -56,14 +53,8 @@ tests mtimeChangeCalibrated =
5653
UnitTests.Distribution.Utils.NubList.tests
5754
, testGroup "Distribution.Utils.ShortText"
5855
UnitTests.Distribution.Utils.ShortText.tests
59-
, testGroup "Distribution.System"
60-
UnitTests.Distribution.System.tests
6156
, testGroup "Distribution.Types.GenericPackageDescription"
6257
UnitTests.Distribution.Types.GenericPackageDescription.tests
63-
, testGroup "Distribution.Version"
64-
UnitTests.Distribution.Version.versionTests
65-
, testGroup "Distribution.SPDX"
66-
UnitTests.Distribution.SPDX.spdxTests
6758
]
6859

6960
extraOptions :: [OptionDescription]

cabal-install/cabal-install.cabal.pp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,23 @@
525525
UnitTests.Distribution.Solver.Modular.WeightedPSQ
526526
UnitTests.Options
527527
UnitTests.TempTestDir
528+
529+
-- These are tests that would live in the Cabal package, except
530+
-- that they need Arbitrary instances from
531+
-- cabal-quickcheck-instances and then we run into #1575. If/when
532+
-- that's fixed, these modules can go back there.
533+
UnitTests.Distribution.SPDX
534+
UnitTests.Distribution.System
535+
UnitTests.Distribution.Version
536+
528537
build-depends:
529538
array,
530539
base,
531540
async,
532541
bytestring,
533542
cabal-lib-client,
534543
cabal-install-solver-dsl,
544+
cabal-quickcheck-instances,
535545
Cabal,
536546
containers,
537547
deepseq,

cabal-install/tests/UnitTests.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import qualified UnitTests.Distribution.Client.IndexUtils.Timestamp
2828
import qualified UnitTests.Distribution.Client.InstallPlan
2929
import qualified UnitTests.Distribution.Client.VCS
3030
import qualified UnitTests.Distribution.Client.Get
31+
import qualified UnitTests.Distribution.SPDX
32+
import qualified UnitTests.Distribution.System
33+
import qualified UnitTests.Distribution.Version
3134

3235
import UnitTests.Options
3336

@@ -78,6 +81,12 @@ tests mtimeChangeCalibrated =
7881
UnitTests.Distribution.Client.VCS.tests mtimeChange
7982
, testGroup "UnitTests.Distribution.Client.Get"
8083
UnitTests.Distribution.Client.Get.tests
84+
, testGroup "UnitTests.Distribution.SPDX"
85+
UnitTests.Distribution.SPDX.spdxTests
86+
, testGroup "UnitTests.Distribution.System"
87+
UnitTests.Distribution.System.tests
88+
, testGroup "UnitTestsDistribution.Version"
89+
UnitTests.Distribution.Version.versionTests
8190
]
8291

8392
main :: IO ()

cabal-install/tests/UnitTests/Distribution/Client/ArbitraryInstances.hs

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
module UnitTests.Distribution.Client.ArbitraryInstances (
55
adjustSize,
6-
shortListOf,
7-
shortListOf1,
86
arbitraryFlag,
9-
ShortToken(..),
10-
arbitraryShortToken,
117
NonMEmpty(..),
128
NoShrink(..),
139
) where
@@ -39,144 +35,14 @@ import Distribution.Client.IndexUtils.Timestamp
3935
import Test.QuickCheck
4036

4137

42-
adjustSize :: (Int -> Int) -> Gen a -> Gen a
43-
adjustSize adjust gen = sized (\n -> resize (adjust n) gen)
44-
45-
shortListOf :: Int -> Gen a -> Gen [a]
46-
shortListOf bound gen =
47-
sized $ \n -> do
48-
k <- choose (0, (n `div` 2) `min` bound)
49-
vectorOf k gen
50-
51-
shortListOf1 :: Int -> Gen a -> Gen [a]
52-
shortListOf1 bound gen =
53-
sized $ \n -> do
54-
k <- choose (1, 1 `max` ((n `div` 2) `min` bound))
55-
vectorOf k gen
56-
57-
newtype ShortToken = ShortToken { getShortToken :: String }
58-
deriving Show
59-
60-
instance Arbitrary ShortToken where
61-
arbitrary =
62-
ShortToken <$>
63-
(shortListOf1 5 (choose ('#', '~'))
64-
`suchThat` (not . ("[]" `isPrefixOf`)))
65-
--TODO: [code cleanup] need to replace parseHaskellString impl to stop
66-
-- accepting Haskell list syntax [], ['a'] etc, just allow String syntax.
67-
-- Workaround, don't generate [] as this does not round trip.
68-
69-
70-
shrink (ShortToken cs) =
71-
[ ShortToken cs' | cs' <- shrink cs, not (null cs') ]
72-
73-
arbitraryShortToken :: Gen String
74-
arbitraryShortToken = getShortToken <$> arbitrary
75-
76-
instance Arbitrary Version where
77-
arbitrary = do
78-
branch <- shortListOf1 4 $
79-
frequency [(3, return 0)
80-
,(3, return 1)
81-
,(2, return 2)
82-
,(1, return 3)]
83-
return (mkVersion branch)
84-
where
85-
86-
shrink ver = [ mkVersion branch' | branch' <- shrink (versionNumbers ver)
87-
, not (null branch') ]
88-
89-
instance Arbitrary VersionRange where
90-
arbitrary = canonicaliseVersionRange <$> sized verRangeExp
91-
where
92-
verRangeExp n = frequency $
93-
[ (2, return anyVersion)
94-
, (1, liftM thisVersion arbitrary)
95-
, (1, liftM laterVersion arbitrary)
96-
, (1, liftM orLaterVersion arbitrary)
97-
, (1, liftM orLaterVersion' arbitrary)
98-
, (1, liftM earlierVersion arbitrary)
99-
, (1, liftM orEarlierVersion arbitrary)
100-
, (1, liftM orEarlierVersion' arbitrary)
101-
, (1, liftM withinVersion arbitrary)
102-
, (2, liftM VersionRangeParens arbitrary)
103-
] ++ if n == 0 then [] else
104-
[ (2, liftM2 unionVersionRanges verRangeExp2 verRangeExp2)
105-
, (2, liftM2 intersectVersionRanges verRangeExp2 verRangeExp2)
106-
]
107-
where
108-
verRangeExp2 = verRangeExp (n `div` 2)
109-
110-
orLaterVersion' v =
111-
unionVersionRanges (laterVersion v) (thisVersion v)
112-
orEarlierVersion' v =
113-
unionVersionRanges (earlierVersion v) (thisVersion v)
114-
115-
canonicaliseVersionRange = fromVersionIntervals . toVersionIntervals
116-
117-
instance Arbitrary PackageName where
118-
arbitrary = mkPackageName . intercalate "-" <$> shortListOf1 2 nameComponent
119-
where
120-
nameComponent = shortListOf1 5 (elements packageChars)
121-
`suchThat` (not . all isDigit)
122-
packageChars = filter isAlphaNum ['\0'..'\127']
123-
124-
instance Arbitrary Dependency where
125-
arbitrary = Dependency <$> arbitrary <*> arbitrary <*> fmap getNonMEmpty arbitrary
126-
12738
instance Arbitrary PackageVersionConstraint where
12839
arbitrary = PackageVersionConstraint <$> arbitrary <*> arbitrary
12940

130-
instance Arbitrary UnqualComponentName where
131-
-- same rules as package names
132-
arbitrary = packageNameToUnqualComponentName <$> arbitrary
133-
134-
instance Arbitrary LibraryName where
135-
arbitrary = elements =<< sequenceA [LSubLibName <$> arbitrary, pure LMainLibName]
136-
137-
instance Arbitrary OS where
138-
arbitrary = elements knownOSs
139-
140-
instance Arbitrary Arch where
141-
arbitrary = elements knownArches
142-
143-
instance Arbitrary Platform where
144-
arbitrary = Platform <$> arbitrary <*> arbitrary
145-
146-
instance Arbitrary a => Arbitrary (Flag a) where
147-
arbitrary = arbitraryFlag arbitrary
148-
shrink NoFlag = []
149-
shrink (Flag x) = NoFlag : [ Flag x' | x' <- shrink x ]
150-
151-
arbitraryFlag :: Gen a -> Gen (Flag a)
152-
arbitraryFlag genA =
153-
sized $ \sz ->
154-
case sz of
155-
0 -> pure NoFlag
156-
_ -> frequency [ (1, pure NoFlag)
157-
, (3, Flag <$> genA) ]
158-
159-
16041
instance (Arbitrary a, Ord a) => Arbitrary (NubList a) where
16142
arbitrary = toNubList <$> arbitrary
16243
shrink xs = [ toNubList [] | (not . null) (fromNubList xs) ]
16344
-- try empty, otherwise don't shrink as it can loop
16445

165-
instance Arbitrary Verbosity where
166-
arbitrary = elements [minBound..maxBound]
167-
168-
instance Arbitrary PathTemplate where
169-
arbitrary = toPathTemplate <$> arbitraryShortToken
170-
shrink t = [ toPathTemplate s | s <- shrink (show t), not (null s) ]
171-
172-
173-
newtype NonMEmpty a = NonMEmpty { getNonMEmpty :: a }
174-
deriving (Eq, Ord, Show)
175-
176-
instance (Arbitrary a, Monoid a, Eq a) => Arbitrary (NonMEmpty a) where
177-
arbitrary = NonMEmpty <$> (arbitrary `suchThat` (/= mempty))
178-
shrink (NonMEmpty x) = [ NonMEmpty x' | x' <- shrink x, x' /= mempty ]
179-
18046
newtype NoShrink a = NoShrink { getNoShrink :: a }
18147
deriving (Eq, Ord, Show)
18248

cabal-install/tests/UnitTests/Distribution/Client/ProjectConfig.hs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -790,13 +790,6 @@ instance Arbitrary PackageProperty where
790790
instance Arbitrary OptionalStanza where
791791
arbitrary = elements [minBound..maxBound]
792792

793-
instance Arbitrary FlagName where
794-
arbitrary = mkFlagName <$> flagident
795-
where
796-
flagident = lowercase <$> shortListOf1 5 (elements flagChars)
797-
`suchThat` (("-" /=) . take 1)
798-
flagChars = "-_" ++ ['a'..'z']
799-
800793
instance Arbitrary PreSolver where
801794
arbitrary = elements [minBound..maxBound]
802795

Cabal/tests/UnitTests/Distribution/SPDX.hs renamed to cabal-install/tests/UnitTests/Distribution/SPDX.hs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# OPTIONS_GHC -fno-warn-orphans -fno-warn-deprecations #-}
21
module UnitTests.Distribution.SPDX (spdxTests) where
32

43
import Distribution.Compat.Prelude.Internal
@@ -8,6 +7,8 @@ import Distribution.SPDX
87
import Distribution.Parsec.Class (eitherParsec)
98
import Distribution.Pretty (prettyShow)
109

10+
import Distribution.Instances.Arbitrary ()
11+
1112
import Test.Tasty
1213
import Test.Tasty.QuickCheck
1314

@@ -113,46 +114,3 @@ shouldAcceptProp = conjoin $
113114
shouldRejectProp :: Property
114115
shouldRejectProp = conjoin $
115116
map (\l -> counterexample (prettyShow l) (not $ isAcceptableLicense l)) shouldReject
116-
117-
-------------------------------------------------------------------------------
118-
-- Instances
119-
-------------------------------------------------------------------------------
120-
121-
instance Arbitrary LicenseId where
122-
arbitrary = elements $ licenseIdList LicenseListVersion_3_2
123-
124-
instance Arbitrary LicenseExceptionId where
125-
arbitrary = elements $ licenseExceptionIdList LicenseListVersion_3_2
126-
127-
instance Arbitrary LicenseRef where
128-
arbitrary = mkLicenseRef' <$> ids' <*> ids
129-
where
130-
ids = listOf1 $ elements $ ['a'..'z'] ++ ['A' .. 'Z'] ++ ['0'..'9'] ++ "_-"
131-
ids' = oneof [ pure Nothing, Just <$> ids ]
132-
133-
instance Arbitrary SimpleLicenseExpression where
134-
arbitrary = oneof
135-
[ ELicenseId <$> arbitrary
136-
, ELicenseIdPlus <$> arbitrary
137-
, ELicenseRef <$> arbitrary
138-
]
139-
140-
instance Arbitrary LicenseExpression where
141-
arbitrary = sized arb
142-
where
143-
arb n
144-
| n <= 0 = ELicense <$> arbitrary <*> pure Nothing
145-
| otherwise = oneof
146-
[ ELicense <$> arbitrary <*> arbitrary
147-
, EAnd <$> arbA <*> arbB
148-
, EOr <$> arbA <*> arbB
149-
]
150-
where
151-
m = n `div` 2
152-
arbA = arb m
153-
arbB = arb (n - m)
154-
155-
shrink (EAnd a b) = a : b : map (uncurry EAnd) (shrink (a, b))
156-
shrink (EOr a b) = a : b : map (uncurry EOr) (shrink (a, b))
157-
shrink _ = []
158-

0 commit comments

Comments
 (0)