2 {-# LANGUAGE LambdaCase #-}
4 module UnitTests
.Distribution
.Client
.Init
.Golden
9 import Test
.Tasty
.Golden
10 import Test
.Tasty
.HUnit
12 import qualified Data
.ByteString
.Lazy
.Char8
as BS8
13 import Data
.List
.NonEmpty
(fromList
)
14 import Data
.List
.NonEmpty
as NEL
(NonEmpty
, drop)
15 #if __GLASGOW_HASKELL__
< 804
16 import Data
.Semigroup
((<>))
19 import Distribution
.CabalSpecVersion
20 import Distribution
.Client
.Init
.FlagExtractors
21 import Distribution
.Client
.Init
.Format
22 import Distribution
.Client
.Init
.Interactive
.Command
23 import Distribution
.Client
.Init
.Types
24 import Distribution
.Client
.Types
.SourcePackageDb
25 import Distribution
.Fields
.Pretty
26 import Distribution
.Simple
.Flag
27 import Distribution
.Simple
.PackageIndex
hiding (fromList
)
28 import Distribution
.Types
.PackageName
(PackageName
)
29 import Distribution
.Verbosity
31 import System
.FilePath
33 import Distribution
.Client
.Init
.Defaults
34 import UnitTests
.Distribution
.Client
.Init
.Utils
36 -- -------------------------------------------------------------------- --
39 -- | Golden executable tests.
41 -- We test target generation against a golden file in @tests/fixtures/init/@ for
42 -- executables, libraries, and test targets with the following:
44 -- * Empty flags, non-simple target gen, no special options
45 -- * Empty flags, simple target gen, no special options
46 -- * Empty flags, non-simple target gen, with generated comments (no minimal setting)
47 -- * Empty flags, non-simple target gen, with minimal setting (no generated comments)
48 -- * Empty flags, non-simple target gen, minimal and generated comments set.
50 -- Additionally, we test whole @.cabal@ file generation for every combination
51 -- of library, lib + tests, exe, exe + tests, exe + lib, exe + lib + tests
52 -- and so on against the same options.
56 -> InstalledPackageIndex
59 tests v initFlags pkgIx srcDb
=
62 [ goldenLibTests v pkgIx pkgDir pkgName
63 , goldenExeTests v pkgIx pkgDir pkgName
64 , goldenTestTests v pkgIx pkgDir pkgName
65 , goldenPkgDescTests v srcDb pkgDir pkgName
66 , goldenCabalTests v pkgIx srcDb
70 evalPrompt
(getPackageDir initFlags
) $
73 evalPrompt
(packageNamePrompt srcDb initFlags
) $
74 fromList
["test-package", "test-package", "y"]
82 goldenPkgDescTests v srcDb pkgDir pkgName
=
84 "package description golden tests"
86 "Empty flags, non-simple, no comments"
87 (goldenPkgDesc
"pkg.golden")
88 $ let opts
= WriteOpts
False False False v pkgDir Library pkgName defaultCabalVersion
89 in runPkgDesc opts emptyFlags pkgArgs
91 "Empty flags, non-simple, with comments"
92 (goldenPkgDesc
"pkg-with-comments.golden")
93 $ let opts
= WriteOpts
False False False v pkgDir Library pkgName defaultCabalVersion
94 in runPkgDesc opts emptyFlags pkgArgs
96 "Dummy flags, >= cabal version syntax, with comments"
97 (goldenPkgDesc
"pkg-with-flags.golden")
98 $ let opts
= WriteOpts
False False False v pkgDir Library pkgName defaultCabalVersion
99 in runPkgDesc opts
(dummyFlags
{cabalVersion
= Flag CabalSpecV1_0
}) pkgArgs
101 "Dummy flags, old cabal version, with comments"
102 (goldenPkgDesc
"pkg-old-cabal-with-flags.golden")
103 $ let opts
= WriteOpts
False False False v pkgDir Library pkgName defaultCabalVersion
104 in runPkgDesc opts
(dummyFlags
{cabalVersion
= Flag CabalSpecV2_0
}) pkgArgs
107 runPkgDesc opts flags args
= do
108 case runPrompt
(genPkgDescription flags srcDb
) args
of
109 Left e
-> assertFailure
$ show e
110 Right
(pkg
, _
) -> mkStanza
$ mkPkgDescription opts pkg
114 -> InstalledPackageIndex
118 goldenExeTests v pkgIx pkgDir pkgName
=
122 "Empty flags, not simple, no options, no comments"
123 (goldenExe
"exe-no-comments.golden")
124 $ let opts
= WriteOpts
False False True v pkgDir Executable pkgName defaultCabalVersion
125 in runGoldenExe opts exeArgs emptyFlags
127 "Empty flags, not simple, with comments + no minimal"
128 (goldenExe
"exe-with-comments.golden")
129 $ let opts
= WriteOpts
False False False v pkgDir Executable pkgName defaultCabalVersion
130 in runGoldenExe opts exeArgs emptyFlags
132 "Empty flags, not simple, with minimal + no comments"
133 (goldenExe
"exe-minimal-no-comments.golden")
134 $ let opts
= WriteOpts
False True True v pkgDir Executable pkgName defaultCabalVersion
135 in runGoldenExe opts exeArgs emptyFlags
137 "Empty flags, not simple, with minimal + comments"
138 (goldenExe
"exe-simple-minimal-with-comments.golden")
139 $ let opts
= WriteOpts
False True False v pkgDir Executable pkgName defaultCabalVersion
140 in runGoldenExe opts exeArgs emptyFlags
142 "Build tools flag, not simple, with comments + no minimal"
143 (goldenExe
"exe-build-tools-with-comments.golden")
144 $ let opts
= WriteOpts
False False False v pkgDir Executable pkgName defaultCabalVersion
145 in runGoldenExe opts exeArgs
(emptyFlags
{buildTools
= Flag
["happy"]})
148 runGoldenExe opts args flags
=
149 case runPrompt
(genExeTarget flags pkgIx
) args
of
150 Right
(t
, _
) -> mkStanza
[mkExeStanza opts
$ t
{_exeDependencies
= mangleBaseDep t _exeDependencies
}]
151 Left e
-> assertFailure
$ show e
155 -> InstalledPackageIndex
159 goldenLibTests v pkgIx pkgDir pkgName
=
163 "Empty flags, not simple, no options, no comments"
164 (goldenLib
"lib-no-comments.golden")
165 $ let opts
= WriteOpts
False False True v pkgDir Library pkgName defaultCabalVersion
166 in runGoldenLib opts libArgs emptyFlags
168 "Empty flags, simple, no options, no comments"
169 (goldenLib
"lib-simple-no-comments.golden")
170 $ let opts
= WriteOpts
False False True v pkgDir Library pkgName defaultCabalVersion
171 in runGoldenLib opts libArgs emptyFlags
173 "Empty flags, not simple, with comments + no minimal"
174 (goldenLib
"lib-with-comments.golden")
175 $ let opts
= WriteOpts
False False False v pkgDir Library pkgName defaultCabalVersion
176 in runGoldenLib opts libArgs emptyFlags
178 "Empty flags, not simple, with minimal + no comments"
179 (goldenLib
"lib-minimal-no-comments.golden")
180 $ let opts
= WriteOpts
False True True v pkgDir Library pkgName defaultCabalVersion
181 in runGoldenLib opts libArgs emptyFlags
183 "Empty flags, not simple, with minimal + comments"
184 (goldenLib
"lib-simple-minimal-with-comments.golden")
185 $ let opts
= WriteOpts
False True False v pkgDir Library pkgName defaultCabalVersion
186 in runGoldenLib opts libArgs emptyFlags
188 "Build tools flag, not simple, with comments + no minimal"
189 (goldenLib
"lib-build-tools-with-comments.golden")
190 $ let opts
= WriteOpts
False False False v pkgDir Library pkgName defaultCabalVersion
191 in runGoldenLib opts libArgs
(emptyFlags
{buildTools
= Flag
["happy"]})
194 runGoldenLib opts args flags
=
195 case runPrompt
(genLibTarget flags pkgIx
) args
of
196 Right
(t
, _
) -> mkStanza
[mkLibStanza opts
$ t
{_libDependencies
= mangleBaseDep t _libDependencies
}]
197 Left e
-> assertFailure
$ show e
201 -> InstalledPackageIndex
205 goldenTestTests v pkgIx pkgDir pkgName
=
209 "Empty flags, not simple, no options, no comments"
210 (goldenTest
"test-no-comments.golden")
211 $ let opts
= WriteOpts
False False True v pkgDir Library pkgName defaultCabalVersion
212 in runGoldenTest opts testArgs emptyFlags
214 "Empty flags, not simple, with comments + no minimal"
215 (goldenTest
"test-with-comments.golden")
216 $ let opts
= WriteOpts
False False False v pkgDir Library pkgName defaultCabalVersion
217 in runGoldenTest opts testArgs emptyFlags
219 "Empty flags, not simple, with minimal + no comments"
220 (goldenTest
"test-minimal-no-comments.golden")
221 $ let opts
= WriteOpts
False True True v pkgDir Library pkgName defaultCabalVersion
222 in runGoldenTest opts testArgs emptyFlags
224 "Empty flags, not simple, with minimal + comments"
225 (goldenTest
"test-simple-minimal-with-comments.golden")
226 $ let opts
= WriteOpts
False True False v pkgDir Library pkgName defaultCabalVersion
227 in runGoldenTest opts testArgs emptyFlags
229 "Build tools flag, not simple, with comments + no minimal"
230 (goldenTest
"test-build-tools-with-comments.golden")
231 $ let opts
= WriteOpts
False False False v pkgDir Library pkgName defaultCabalVersion
232 in runGoldenTest opts testArgs
(emptyFlags
{buildTools
= Flag
["happy"]})
234 "Standalone tests, empty flags, not simple, no options, no comments"
235 (goldenTest
"standalone-test-no-comments.golden")
236 $ let opts
= WriteOpts
False False True v pkgDir TestSuite pkgName defaultCabalVersion
237 in runGoldenTest opts testArgs emptyFlags
239 "Standalone tests, empty flags, not simple, with comments + no minimal"
240 (goldenTest
"standalone-test-with-comments.golden")
241 $ let opts
= WriteOpts
False False False v pkgDir TestSuite pkgName defaultCabalVersion
242 in runGoldenTest opts testArgs emptyFlags
245 runGoldenTest opts args flags
=
246 case runPrompt
(genTestTarget flags pkgIx
) args
of
247 Left e
-> assertFailure
$ show e
248 Right
(Nothing
, _
) ->
250 "goldenTestTests: Tests not enabled."
251 Right
(Just t
, _
) -> mkStanza
[mkTestStanza opts
$ t
{_testDependencies
= mangleBaseDep t _testDependencies
}]
253 -- | Full cabal file golden tests
256 -> InstalledPackageIndex
259 goldenCabalTests v pkgIx srcDb
=
261 ".cabal file golden tests"
263 "Library and executable, empty flags, not simple, with comments + no minimal"
264 (goldenCabal
"cabal-lib-and-exe-with-comments.golden")
265 $ runGoldenTest
(fullProjArgs
"Y") emptyFlags
267 "Library and executable, empty flags, not simple, no comments + no minimal"
268 (goldenCabal
"cabal-lib-and-exe-no-comments.golden")
269 $ runGoldenTest
(fullProjArgs
"N") emptyFlags
271 "Library, empty flags, not simple, with comments + no minimal"
272 (goldenCabal
"cabal-lib-with-comments.golden")
273 $ runGoldenTest
(libProjArgs
"Y") emptyFlags
275 "Library, empty flags, not simple, no comments + no minimal"
276 (goldenCabal
"cabal-lib-no-comments.golden")
277 $ runGoldenTest
(libProjArgs
"N") emptyFlags
279 "Test suite, empty flags, not simple, with comments + no minimal"
280 (goldenCabal
"cabal-test-suite-with-comments.golden")
281 $ runGoldenTest
(testProjArgs
"Y") emptyFlags
283 "Test suite, empty flags, not simple, no comments + no minimal"
284 (goldenCabal
"cabal-test-suite-no-comments.golden")
285 $ runGoldenTest
(testProjArgs
"N") emptyFlags
288 runGoldenTest args flags
=
289 case runPrompt
(createProject v pkgIx srcDb flags
) args
of
290 Left e
-> assertFailure
$ show e
291 (Right
(ProjectSettings opts pkgDesc
(Just libTarget
) (Just exeTarget
) (Just testTarget
), _
)) -> do
292 let pkgFields
= mkPkgDescription opts pkgDesc
293 commonStanza
= mkCommonStanza opts
294 libStanza
= mkLibStanza opts
$ libTarget
{_libDependencies
= mangleBaseDep libTarget _libDependencies
}
295 exeStanza
= mkExeStanza opts
$ exeTarget
{_exeDependencies
= mangleBaseDep exeTarget _exeDependencies
}
296 testStanza
= mkTestStanza opts
$ testTarget
{_testDependencies
= mangleBaseDep testTarget _testDependencies
}
298 mkStanza
$ pkgFields
++ [commonStanza
, libStanza
, exeStanza
, testStanza
]
299 (Right
(ProjectSettings opts pkgDesc
(Just libTarget
) Nothing
(Just testTarget
), _
)) -> do
300 let pkgFields
= mkPkgDescription opts pkgDesc
301 commonStanza
= mkCommonStanza opts
302 libStanza
= mkLibStanza opts
$ libTarget
{_libDependencies
= mangleBaseDep libTarget _libDependencies
}
303 testStanza
= mkTestStanza opts
$ testTarget
{_testDependencies
= mangleBaseDep testTarget _testDependencies
}
305 mkStanza
$ pkgFields
++ [commonStanza
, libStanza
, testStanza
]
306 (Right
(ProjectSettings opts pkgDesc Nothing Nothing
(Just testTarget
), _
)) -> do
307 let pkgFields
= mkPkgDescription opts pkgDesc
308 commonStanza
= mkCommonStanza opts
309 testStanza
= mkTestStanza opts
$ testTarget
{_testDependencies
= mangleBaseDep testTarget _testDependencies
}
311 mkStanza
$ pkgFields
++ [commonStanza
, testStanza
]
312 (Right
(ProjectSettings _ _ l e t
, _
)) ->
314 show l
++ "\n" ++ show e
++ "\n" ++ show t
316 -- -------------------------------------------------------------------- --
319 mkStanza
:: [PrettyField FieldAnnotation
] -> IO BS8
.ByteString
324 postProcessFieldLines
329 golden
= "tests" </> "fixtures" </> "init" </> "golden"
331 goldenExe
:: FilePath -> FilePath
332 goldenExe file
= golden
</> "exe" </> file
334 goldenTest
:: FilePath -> FilePath
335 goldenTest file
= golden
</> "test" </> file
337 goldenLib
:: FilePath -> FilePath
338 goldenLib file
= golden
</> "lib" </> file
340 goldenCabal
:: FilePath -> FilePath
341 goldenCabal file
= golden
</> "cabal" </> file
343 goldenPkgDesc
:: FilePath -> FilePath
344 goldenPkgDesc file
= golden
</> "pkg-desc" </> file
346 libArgs
:: NonEmpty
String
347 libArgs
= fromList
["1", "2"]
349 exeArgs
:: NonEmpty
String
350 exeArgs
= fromList
["1", "2", "1"]
352 testArgs
:: NonEmpty
String
353 testArgs
= fromList
["y", "1", "test", "1"]
355 pkgArgs
:: NonEmpty
String
367 , "foo-kmett@kmett.kmett"
373 testProjArgs
:: String -> NonEmpty
String
374 testProjArgs comments
=
375 fromList
["4", "test-dir", "[]", "foo-package"]
377 <> fromList
(NEL
.drop 1 testArgs
)
378 <> fromList
[comments
]
380 libProjArgs
:: String -> NonEmpty
String
381 libProjArgs comments
=
382 fromList
["1", "test-dir", "[]", "foo-package"]
386 <> fromList
[comments
]
388 fullProjArgs
:: String -> NonEmpty
String
389 fullProjArgs comments
=
390 fromList
["3", "test-dir", "[]", "foo-package"]
395 <> fromList
[comments
]