validate dependabot configuration
[cabal.git] / cabal-install / tests / UnitTests / Distribution / Client / Init / Golden.hs
blob78387d404dac8a723db9382e0640fbdb06d1df6e
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE LambdaCase #-}
4 module UnitTests.Distribution.Client.Init.Golden
5 ( tests
6 ) where
8 import Test.Tasty
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 ((<>))
17 #endif
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 -- -------------------------------------------------------------------- --
37 -- golden test suite
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.
53 tests
54 :: Verbosity
55 -> InitFlags
56 -> InstalledPackageIndex
57 -> SourcePackageDb
58 -> TestTree
59 tests v initFlags pkgIx srcDb =
60 testGroup
61 "golden"
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
68 where
69 pkgDir =
70 evalPrompt (getPackageDir initFlags) $
71 fromList ["."]
72 pkgName =
73 evalPrompt (packageNamePrompt srcDb initFlags) $
74 fromList ["test-package", "test-package", "y"]
76 goldenPkgDescTests
77 :: Verbosity
78 -> SourcePackageDb
79 -> FilePath
80 -> PackageName
81 -> TestTree
82 goldenPkgDescTests v srcDb pkgDir pkgName =
83 testGroup
84 "package description golden tests"
85 [ goldenVsString
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
90 , goldenVsString
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
95 , goldenVsString
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
100 , goldenVsString
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
106 where
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
112 goldenExeTests
113 :: Verbosity
114 -> InstalledPackageIndex
115 -> FilePath
116 -> PackageName
117 -> TestTree
118 goldenExeTests v pkgIx pkgDir pkgName =
119 testGroup
120 "exe golden tests"
121 [ goldenVsString
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
126 , goldenVsString
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
131 , goldenVsString
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
136 , goldenVsString
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
141 , goldenVsString
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"]})
147 where
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
153 goldenLibTests
154 :: Verbosity
155 -> InstalledPackageIndex
156 -> FilePath
157 -> PackageName
158 -> TestTree
159 goldenLibTests v pkgIx pkgDir pkgName =
160 testGroup
161 "lib golden tests"
162 [ goldenVsString
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
167 , goldenVsString
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
172 , goldenVsString
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
177 , goldenVsString
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
182 , goldenVsString
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
187 , goldenVsString
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"]})
193 where
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
199 goldenTestTests
200 :: Verbosity
201 -> InstalledPackageIndex
202 -> FilePath
203 -> PackageName
204 -> TestTree
205 goldenTestTests v pkgIx pkgDir pkgName =
206 testGroup
207 "test golden tests"
208 [ goldenVsString
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
213 , goldenVsString
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
218 , goldenVsString
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
223 , goldenVsString
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
228 , goldenVsString
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"]})
233 , goldenVsString
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
238 , goldenVsString
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
244 where
245 runGoldenTest opts args flags =
246 case runPrompt (genTestTarget flags pkgIx) args of
247 Left e -> assertFailure $ show e
248 Right (Nothing, _) ->
249 assertFailure
250 "goldenTestTests: Tests not enabled."
251 Right (Just t, _) -> mkStanza [mkTestStanza opts $ t{_testDependencies = mangleBaseDep t _testDependencies}]
253 -- | Full cabal file golden tests
254 goldenCabalTests
255 :: Verbosity
256 -> InstalledPackageIndex
257 -> SourcePackageDb
258 -> TestTree
259 goldenCabalTests v pkgIx srcDb =
260 testGroup
261 ".cabal file golden tests"
262 [ goldenVsString
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
266 , goldenVsString
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
270 , goldenVsString
271 "Library, empty flags, not simple, with comments + no minimal"
272 (goldenCabal "cabal-lib-with-comments.golden")
273 $ runGoldenTest (libProjArgs "Y") emptyFlags
274 , goldenVsString
275 "Library, empty flags, not simple, no comments + no minimal"
276 (goldenCabal "cabal-lib-no-comments.golden")
277 $ runGoldenTest (libProjArgs "N") emptyFlags
278 , goldenVsString
279 "Test suite, empty flags, not simple, with comments + no minimal"
280 (goldenCabal "cabal-test-suite-with-comments.golden")
281 $ runGoldenTest (testProjArgs "Y") emptyFlags
282 , goldenVsString
283 "Test suite, empty flags, not simple, no comments + no minimal"
284 (goldenCabal "cabal-test-suite-no-comments.golden")
285 $ runGoldenTest (testProjArgs "N") emptyFlags
287 where
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, _)) ->
313 assertFailure $
314 show l ++ "\n" ++ show e ++ "\n" ++ show t
316 -- -------------------------------------------------------------------- --
317 -- utils
319 mkStanza :: [PrettyField FieldAnnotation] -> IO BS8.ByteString
320 mkStanza fields =
321 return . BS8.pack $
322 showFields'
323 annCommentLines
324 postProcessFieldLines
326 fields
328 golden :: FilePath
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
356 pkgArgs =
357 fromList
358 [ "5"
359 , "foo-package"
360 , "foo-package"
361 , "y"
362 , "0.1.0.0"
363 , "2"
364 , "git username"
365 , "foo-kmett"
366 , "git email"
367 , "foo-kmett@kmett.kmett"
368 , "home"
369 , "synopsis"
370 , "4"
373 testProjArgs :: String -> NonEmpty String
374 testProjArgs comments =
375 fromList ["4", "test-dir", "[]", "foo-package"]
376 <> pkgArgs
377 <> fromList (NEL.drop 1 testArgs)
378 <> fromList [comments]
380 libProjArgs :: String -> NonEmpty String
381 libProjArgs comments =
382 fromList ["1", "test-dir", "[]", "foo-package"]
383 <> pkgArgs
384 <> libArgs
385 <> testArgs
386 <> fromList [comments]
388 fullProjArgs :: String -> NonEmpty String
389 fullProjArgs comments =
390 fromList ["3", "test-dir", "[]", "foo-package"]
391 <> pkgArgs
392 <> libArgs
393 <> exeArgs
394 <> testArgs
395 <> fromList [comments]