1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
{-# LANGUAGE StandaloneDeriving #-}
module Test.Dependencies where
import Control.Arrow
import Test.HUnit
import Debian.Control.String
import Debian.Apt.Dependencies hiding (packageVersionParagraph)
import Debian.Relation
import Debian.Version
import Debian.Apt.Package
packageA =
[ ("Package", " a")
, ("Version", " 1.0")
, ("Depends", " b")
]
packageB =
[ ("Package", " b")
, ("Version", " 1.0")
]
packageC =
[ ("Package", " c")
, ("Version", " 1.0")
, ("Depends", " doesNotExist")
]
packageD =
[ ("Package", " d")
, ("Version", " 1.0")
, ("Depends", " e | f, g | h")
]
packageE =
[ ("Package", " e")
, ("Version", " 1.0")
]
packageF =
[ ("Package", " f")
, ("Version", " 1.0")
]
packageG =
[ ("Package", " g")
, ("Version", " 1.0")
]
packageH =
[ ("Package", " h")
, ("Version", " 1.0")
]
packageI =
[ ("Package", " i")
, ("Version", " 1.0")
, ("Depends", " k")
]
packageJ =
[ ("Package", " j")
, ("Version", " 1.0")
, ("Provides", " k")
]
packageK =
[ ("Package", " k")
, ("Version", " 1.0")
]
control
= [ packageA
, packageB
, packageC
, packageD
, packageE
, packageF
, packageG
, packageH
, packageI
, packageJ
, packageK
]
depends p =
case lookup "Depends" p of
Nothing -> []
(Just v) -> either (error . show) id (parseRelations v)
mkCSP :: [[(String, String)]] -> String -> ([(String, String)] -> Relations) -> CSP [(String, String)]
mkCSP paragraphs relStr depF =
CSP { pnm = addProvides providesF paragraphs $ packageNameMap getName paragraphs
, relations = either (error . show) id (parseRelations relStr)
, depFunction = depF
, conflicts = conflicts'
, packageVersion = packageVersionParagraph
}
where
getName :: [(String, String)] -> BinPkgName
getName p = case lookup "Package" p of Nothing -> error "Missing Package field" ; (Just n) -> BinPkgName (PkgName (stripWS n))
conflicts' :: [(String, String)] -> Relations
conflicts' p =
case lookup "Conflicts" p of
Nothing -> []
(Just c) -> either (error . show) id (parseRelations c)
providesF :: [(String, String)] -> [BinPkgName]
providesF p =
case lookup "Provides" p of
Nothing -> []
(Just v) ->
map (BinPkgName . PkgName) $ parseCommaList v
parseCommaList :: String -> [String]
parseCommaList str =
words $ map (\c -> if c == ',' then ' ' else c) str
packageVersionParagraph :: [(String, String)] -> (BinPkgName, DebianVersion)
packageVersionParagraph p =
case lookup "Package" p of
Nothing -> error $ "Could not find Package in " ++ show p
(Just n) ->
case lookup "Version" p of
Nothing -> error $ "Could not find Package in " ++ show p
(Just v) ->
(BinPkgName (PkgName (stripWS n)), parseDebianVersion v)
mapSnd :: (b -> c) -> [(a,b)] -> [(a,c)]
mapSnd f = map (second f)
instance Show DebianVersion where
show = show . prettyDebianVersion
deriving instance Show Status
deriving instance Show Relation
deriving instance Show VersionReq
deriving instance Show ArchitectureReq
test1 =
let csp = mkCSP control "a" depends
expected = [ (Complete, [packageB, packageA])]
in
TestCase (assertEqual "test1" expected (search bt csp))
missing1 =
let csp = mkCSP control "c" depends
expected = []
in
TestCase (assertEqual "missing1" expected (search bt csp))
ors1 =
let csp = mkCSP control "d" depends
expected = [ (Complete, [packageG, packageE, packageD])
, (Complete, [packageH, packageE, packageD])
, (Complete, [packageG, packageF, packageD])
, (Complete, [packageH, packageF, packageD])
]
in
TestCase (assertEqual "ors1" expected (search bt csp))
provides1 =
let csp = mkCSP control "i" depends
expected = [ (Complete, [packageK, packageI])
, (Complete, [packageJ, packageI])
]
in
TestCase (assertEqual "provides1" expected (search bt csp))
provides2 =
let csp = mkCSP control "k" depends
expected = [ (Complete, [packageK])
, (Complete, [packageJ])
]
in
TestCase (assertEqual "provides2" expected (search bt csp))
dependencyTests =
[ test1
, missing1
, ors1
, provides1
, provides2
]
-- runTestText putTextToShowS test1 >>= \(c,st) -> putStrLn (st "")
|