1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
4 module UnitTests
.Distribution
.Client
.Get
(tests
) where
6 import Distribution
.Client
.Get
8 import Distribution
.Client
.Types
.SourceRepo
(SourceRepositoryPackage
(..))
9 import Distribution
.Types
.PackageId
10 import Distribution
.Types
.PackageName
11 import Distribution
.Types
.SourceRepo
(KnownRepoType
(..), RepoKind
(..), RepoType
(..), SourceRepo
(..), emptySourceRepo
)
12 import Distribution
.Verbosity
as Verbosity
13 import Distribution
.Version
15 import Control
.Exception
18 import System
.Directory
20 import System
.FilePath
21 import System
.IO.Error
24 import Test
.Tasty
.HUnit
25 import Test
.Utils
.TempTestDir
(withTestDir
)
26 import UnitTests
.Options
(RunNetworkTests
(..))
32 [ testCase
"no repos" testNoRepos
33 , testCase
"no repos of requested kind" testNoReposOfKind
34 , testCase
"no repo type specified" testNoRepoType
35 , testCase
"unsupported repo type" testUnsupportedRepoType
36 , testCase
"no repo location specified" testNoRepoLocation
37 , testCase
"correct repo kind selection" testSelectRepoKind
38 , testCase
"repo destination exists" testRepoDestinationExists
39 , testCase
"git fetch failure" testGitFetchFailed
41 , askOption
$ \(RunNetworkTests doRunNetTests
) ->
42 testGroup
"forkPackages, network tests" $
43 includeTestsIf doRunNetTests
$
44 [ testCase
"git clone" testNetworkGitClone
48 includeTestsIf
True xs
= xs
49 includeTestsIf
False _
= []
51 verbosity
:: Verbosity
52 verbosity
= Verbosity
.silent
-- for debugging try verbose
55 pkgidfoo
= PackageIdentifier
(mkPackageName
"foo") (mkVersion
[1, 0])
57 -- ------------------------------------------------------------
61 -- ------------------------------------------------------------
63 testNoRepos
:: Assertion
67 clonePackagesFromSourceRepo verbosity
"." Nothing
[] pkgrepos
68 e
@?
= ClonePackageNoSourceRepos pkgidfoo
70 pkgrepos
= [(pkgidfoo
, [])]
72 testNoReposOfKind
:: Assertion
73 testNoReposOfKind
= do
76 clonePackagesFromSourceRepo verbosity
"." repokind
[] pkgrepos
77 e
@?
= ClonePackageNoSourceReposOfKind pkgidfoo repokind
79 pkgrepos
= [(pkgidfoo
, [repo
])]
80 repo
= emptySourceRepo RepoHead
81 repokind
= Just RepoThis
83 testNoRepoType
:: Assertion
87 clonePackagesFromSourceRepo verbosity
"." Nothing
[] pkgrepos
88 e
@?
= ClonePackageNoRepoType pkgidfoo repo
90 pkgrepos
= [(pkgidfoo
, [repo
])]
91 repo
= emptySourceRepo RepoHead
93 testUnsupportedRepoType
:: Assertion
94 testUnsupportedRepoType
= do
97 clonePackagesFromSourceRepo verbosity
"." Nothing
[] pkgrepos
98 e
@?
= ClonePackageUnsupportedRepoType pkgidfoo repo
' repotype
100 pkgrepos
= [(pkgidfoo
, [repo
])]
102 (emptySourceRepo RepoHead
)
103 { repoType
= Just repotype
104 , repoLocation
= Just
"loc"
107 SourceRepositoryPackage
109 , srpLocation
= "loc"
111 , srpBranch
= Nothing
115 repotype
= OtherRepoType
"baz"
117 testNoRepoLocation
:: Assertion
118 testNoRepoLocation
= do
121 clonePackagesFromSourceRepo verbosity
"." Nothing
[] pkgrepos
122 e
@?
= ClonePackageNoRepoLocation pkgidfoo repo
124 pkgrepos
= [(pkgidfoo
, [repo
])]
126 (emptySourceRepo RepoHead
)
127 { repoType
= Just repotype
129 repotype
= KnownRepoType Darcs
131 testSelectRepoKind
:: Assertion
135 e
<- test requestedRepoType pkgrepos
136 e
@?
= ClonePackageNoRepoType pkgidfoo expectedRepo
138 e
' <- test requestedRepoType
(reverse pkgrepos
)
139 e
' @?
= ClonePackageNoRepoType pkgidfoo expectedRepo
142 clonePackagesFromSourceRepo verbosity
"." rt
[] rs
143 , (requestedRepoType
, expectedRepo
) <- cases
146 pkgrepos
= [(pkgidfoo
, [repo1
, repo2
, repo3
])]
147 repo1
= emptySourceRepo RepoThis
148 repo2
= emptySourceRepo RepoHead
149 repo3
= emptySourceRepo
(RepoKindUnknown
"bar")
152 , (Just RepoThis
, repo1
)
153 , (Just RepoHead
, repo2
)
154 , (Just
(RepoKindUnknown
"bar"), repo3
)
157 testRepoDestinationExists
:: Assertion
158 testRepoDestinationExists
=
159 withTestDir verbosity
"repos" $ \tmpdir
-> do
160 let pkgdir
= tmpdir
</> "foo"
161 createDirectory pkgdir
164 clonePackagesFromSourceRepo verbosity tmpdir Nothing
[] pkgrepos
165 e1
@?
= ClonePackageDestinationExists pkgidfoo pkgdir
True {- isdir -}
166 removeDirectory pkgdir
171 clonePackagesFromSourceRepo verbosity tmpdir Nothing
[] pkgrepos
172 e2
@?
= ClonePackageDestinationExists pkgidfoo pkgdir
False {- isfile -}
174 pkgrepos
= [(pkgidfoo
, [repo
])]
176 (emptySourceRepo RepoHead
)
177 { repoType
= Just
(KnownRepoType Darcs
)
178 , repoLocation
= Just
""
181 testGitFetchFailed
:: Assertion
183 withTestDir verbosity
"repos" $ \tmpdir
-> do
184 let srcdir
= tmpdir
</> "src"
186 (emptySourceRepo RepoHead
)
187 { repoType
= Just
(KnownRepoType Git
)
188 , repoLocation
= Just srcdir
191 SourceRepositoryPackage
192 { srpType
= KnownRepoType Git
193 , srpLocation
= srcdir
195 , srpBranch
= Nothing
199 pkgrepos
= [(pkgidfoo
, [repo
])]
202 clonePackagesFromSourceRepo verbosity tmpdir Nothing
[] pkgrepos
203 e1
@?
= ClonePackageFailedWithExitCode pkgidfoo repo
' "git" (ExitFailure
128)
205 testNetworkGitClone
:: Assertion
206 testNetworkGitClone
=
207 withTestDir verbosity
"repos" $ \tmpdir
-> do
209 (emptySourceRepo RepoHead
)
210 { repoType
= Just
(KnownRepoType Git
)
211 , repoLocation
= Just
"https://github.com/haskell/zlib.git"
213 clonePackagesFromSourceRepo
218 [(mkpkgid
"zlib1", [repo1
])]
219 assertFileContains
(tmpdir
</> "zlib1/zlib/zlib.cabal") ["name:", "zlib"]
222 (emptySourceRepo RepoHead
)
223 { repoType
= Just
(KnownRepoType Git
)
224 , repoLocation
= Just
(tmpdir
</> "zlib1")
226 clonePackagesFromSourceRepo
231 [(mkpkgid
"zlib2", [repo2
])]
232 assertFileContains
(tmpdir
</> "zlib2/zlib/zlib.cabal") ["name:", "zlib"]
235 (emptySourceRepo RepoHead
)
236 { repoType
= Just
(KnownRepoType Git
)
237 , repoLocation
= Just
(tmpdir
</> "zlib1")
238 , repoTag
= Just
"0.5.0.0"
240 clonePackagesFromSourceRepo
245 [(mkpkgid
"zlib3", [repo3
])]
246 assertFileContains
(tmpdir
</> "zlib3/zlib.cabal") ["version:", "0.5.0.0"]
248 mkpkgid nm
= PackageIdentifier
(mkPackageName nm
) (mkVersion
[])
250 -- ------------------------------------------------------------
254 -- ------------------------------------------------------------
256 assertException
:: forall e a
. (Exception e
, HasCallStack
) => IO a
-> IO e
257 assertException action
= do
263 "expected exception of type "
264 ++ show (typeOf
(undefined :: e
))
266 -- | Expect that one line in a file matches exactly the given words (i.e. at
267 -- least insensitive to whitespace)
268 assertFileContains
:: HasCallStack
=> FilePath -> [String] -> Assertion
269 assertFileContains file expected
= do
271 readFile file `
catch`
\e
->
272 if isDoesNotExistError e
273 then assertFailure
$ "expected a file to exist: " ++ file
275 unless (expected `
elem`
map words (lines c
)) $
280 ++ show (take 100 expected
)