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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
{-# LANGUAGE ScopedTypeVariables, FlexibleContexts #-}
module UnitTests.Distribution.Client.Get (tests) where
import Distribution.Client.Get
import Distribution.Types.PackageId
import Distribution.Types.PackageName
import Distribution.Types.SourceRepo (SourceRepo (..), emptySourceRepo, RepoKind (..), RepoType (..), KnownRepoType (..))
import Distribution.Client.Types.SourceRepo (SourceRepositoryPackage (..))
import Distribution.Verbosity as Verbosity
import Distribution.Version
import Control.Monad
import Control.Exception
import Data.Typeable
import System.FilePath
import System.Directory
import System.Exit
import System.IO.Error
import Test.Tasty
import Test.Tasty.HUnit
import UnitTests.Options (RunNetworkTests (..))
import UnitTests.TempTestDir (withTestDir)
tests :: [TestTree]
tests =
[ testGroup "forkPackages"
[ testCase "no repos" testNoRepos
, testCase "no repos of requested kind" testNoReposOfKind
, testCase "no repo type specified" testNoRepoType
, testCase "unsupported repo type" testUnsupportedRepoType
, testCase "no repo location specified" testNoRepoLocation
, testCase "correct repo kind selection" testSelectRepoKind
, testCase "repo destination exists" testRepoDestinationExists
, testCase "git fetch failure" testGitFetchFailed
]
, askOption $ \(RunNetworkTests doRunNetTests) ->
testGroup "forkPackages, network tests" $
includeTestsIf doRunNetTests $
[ testCase "git clone" testNetworkGitClone
]
]
where
includeTestsIf True xs = xs
includeTestsIf False _ = []
verbosity :: Verbosity
verbosity = Verbosity.silent -- for debugging try verbose
pkgidfoo :: PackageId
pkgidfoo = PackageIdentifier (mkPackageName "foo") (mkVersion [1,0])
-- ------------------------------------------------------------
-- * Unit tests
-- ------------------------------------------------------------
testNoRepos :: Assertion
testNoRepos = do
e <- assertException $
clonePackagesFromSourceRepo verbosity "." Nothing [] pkgrepos
e @?= ClonePackageNoSourceRepos pkgidfoo
where
pkgrepos = [(pkgidfoo, [])]
testNoReposOfKind :: Assertion
testNoReposOfKind = do
e <- assertException $
clonePackagesFromSourceRepo verbosity "." repokind [] pkgrepos
e @?= ClonePackageNoSourceReposOfKind pkgidfoo repokind
where
pkgrepos = [(pkgidfoo, [repo])]
repo = emptySourceRepo RepoHead
repokind = Just RepoThis
testNoRepoType :: Assertion
testNoRepoType = do
e <- assertException $
clonePackagesFromSourceRepo verbosity "." Nothing []pkgrepos
e @?= ClonePackageNoRepoType pkgidfoo repo
where
pkgrepos = [(pkgidfoo, [repo])]
repo = emptySourceRepo RepoHead
testUnsupportedRepoType :: Assertion
testUnsupportedRepoType = do
e <- assertException $
clonePackagesFromSourceRepo verbosity "." Nothing [] pkgrepos
e @?= ClonePackageUnsupportedRepoType pkgidfoo repo' repotype
where
pkgrepos = [(pkgidfoo, [repo])]
repo = (emptySourceRepo RepoHead)
{ repoType = Just repotype
, repoLocation = Just "loc"
}
repo' = SourceRepositoryPackage
{ srpType = repotype
, srpLocation = "loc"
, srpTag = Nothing
, srpBranch = Nothing
, srpSubdir = Proxy
, srpCommand = []
}
repotype = OtherRepoType "baz"
testNoRepoLocation :: Assertion
testNoRepoLocation = do
e <- assertException $
clonePackagesFromSourceRepo verbosity "." Nothing [] pkgrepos
e @?= ClonePackageNoRepoLocation pkgidfoo repo
where
pkgrepos = [(pkgidfoo, [repo])]
repo = (emptySourceRepo RepoHead) {
repoType = Just repotype
}
repotype = KnownRepoType Darcs
testSelectRepoKind :: Assertion
testSelectRepoKind =
sequence_
[ do e <- test requestedRepoType pkgrepos
e @?= ClonePackageNoRepoType pkgidfoo expectedRepo
e' <- test requestedRepoType (reverse pkgrepos)
e' @?= ClonePackageNoRepoType pkgidfoo expectedRepo
| let test rt rs = assertException $
clonePackagesFromSourceRepo verbosity "." rt [] rs
, (requestedRepoType, expectedRepo) <- cases
]
where
pkgrepos = [(pkgidfoo, [repo1, repo2, repo3])]
repo1 = emptySourceRepo RepoThis
repo2 = emptySourceRepo RepoHead
repo3 = emptySourceRepo (RepoKindUnknown "bar")
cases = [ (Nothing, repo1)
, (Just RepoThis, repo1)
, (Just RepoHead, repo2)
, (Just (RepoKindUnknown "bar"), repo3)
]
testRepoDestinationExists :: Assertion
testRepoDestinationExists =
withTestDir verbosity "repos" $ \tmpdir -> do
let pkgdir = tmpdir </> "foo"
createDirectory pkgdir
e1 <- assertException $
clonePackagesFromSourceRepo verbosity tmpdir Nothing [] pkgrepos
e1 @?= ClonePackageDestinationExists pkgidfoo pkgdir True {- isdir -}
removeDirectory pkgdir
writeFile pkgdir ""
e2 <- assertException $
clonePackagesFromSourceRepo verbosity tmpdir Nothing [] pkgrepos
e2 @?= ClonePackageDestinationExists pkgidfoo pkgdir False {- isfile -}
where
pkgrepos = [(pkgidfoo, [repo])]
repo = (emptySourceRepo RepoHead) {
repoType = Just (KnownRepoType Darcs),
repoLocation = Just ""
}
testGitFetchFailed :: Assertion
testGitFetchFailed =
withTestDir verbosity "repos" $ \tmpdir -> do
let srcdir = tmpdir </> "src"
repo = (emptySourceRepo RepoHead) {
repoType = Just (KnownRepoType Git),
repoLocation = Just srcdir
}
repo' = SourceRepositoryPackage
{ srpType = KnownRepoType Git
, srpLocation = srcdir
, srpTag = Nothing
, srpBranch = Nothing
, srpSubdir = Proxy
, srpCommand = []
}
pkgrepos = [(pkgidfoo, [repo])]
e1 <- assertException $
clonePackagesFromSourceRepo verbosity tmpdir Nothing [] pkgrepos
e1 @?= ClonePackageFailedWithExitCode pkgidfoo repo' "git" (ExitFailure 128)
testNetworkGitClone :: Assertion
testNetworkGitClone =
withTestDir verbosity "repos" $ \tmpdir -> do
let repo1 = (emptySourceRepo RepoHead) {
repoType = Just (KnownRepoType Git),
repoLocation = Just "https://github.com/haskell/zlib.git"
}
clonePackagesFromSourceRepo verbosity tmpdir Nothing []
[(mkpkgid "zlib1", [repo1])]
assertFileContains (tmpdir </> "zlib1/zlib.cabal") ["name:", "zlib"]
let repo2 = (emptySourceRepo RepoHead) {
repoType = Just (KnownRepoType Git),
repoLocation = Just (tmpdir </> "zlib1")
}
clonePackagesFromSourceRepo verbosity tmpdir Nothing []
[(mkpkgid "zlib2", [repo2])]
assertFileContains (tmpdir </> "zlib2/zlib.cabal") ["name:", "zlib"]
let repo3 = (emptySourceRepo RepoHead) {
repoType = Just (KnownRepoType Git),
repoLocation = Just (tmpdir </> "zlib1"),
repoTag = Just "0.5.0.0"
}
clonePackagesFromSourceRepo verbosity tmpdir Nothing []
[(mkpkgid "zlib3", [repo3])]
assertFileContains (tmpdir </> "zlib3/zlib.cabal") ["version:", "0.5.0.0"]
where
mkpkgid nm = PackageIdentifier (mkPackageName nm) (mkVersion [])
-- ------------------------------------------------------------
-- * HUnit utils
-- ------------------------------------------------------------
assertException :: forall e a. (Exception e, HasCallStack) => IO a -> IO e
assertException action = do
r <- try action
case r of
Left e -> return e
Right _ -> assertFailure $ "expected exception of type "
++ show (typeOf (undefined :: e))
-- | Expect that one line in a file matches exactly the given words (i.e. at
-- least insensitive to whitespace)
--
assertFileContains :: HasCallStack => FilePath -> [String] -> Assertion
assertFileContains file expected = do
c <- readFile file `catch` \e ->
if isDoesNotExistError e
then assertFailure $ "expected a file to exist: " ++ file
else throwIO e
unless (expected `elem` map words (lines c)) $
assertFailure $ "expected the file " ++ file ++ " to contain "
++ show (take 100 expected)
|