Tests.hs 31.3 KB
Newer Older
1
module PackageTests.Tests(tests) where
2
3
4

import PackageTests.PackageTester

fmaste's avatar
fmaste committed
5
6
import qualified PackageTests.AutogenModules.Package.Check
import qualified PackageTests.AutogenModules.SrcDist.Check
7
import qualified PackageTests.BenchmarkStanza.Check
8
import qualified PackageTests.CaretOperator.Check
9
10
11
import qualified PackageTests.TestStanza.Check
import qualified PackageTests.DeterministicAr.Check
import qualified PackageTests.TestSuiteTests.ExeV10.Check
12
import qualified PackageTests.ForeignLibs.Check
13

14
15
16
import Distribution.Types.TargetInfo
import Distribution.Types.LocalBuildInfo

17
import Distribution.Package
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
18
import Distribution.Simple.LocalBuildInfo
19
  ( absoluteComponentInstallDirs
Christiaan Baaij's avatar
Christiaan Baaij committed
20
  , InstallDirs (..)
21
  , ComponentLocalBuildInfo(componentUnitId), ComponentName(..) )
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
22
23
24
import Distribution.Simple.InstallDirs ( CopyDest(NoCopyDest) )
import Distribution.Simple.BuildPaths  ( mkLibName, mkSharedLibName )
import Distribution.Simple.Compiler    ( compilerId )
25
import Distribution.System (buildOS, OS(Windows))
26
import Distribution.Version
27

28
import Control.Monad
29
import System.Directory
30
import Test.Tasty (mkTimeout, localOption)
31

32
33
import qualified Data.Char as Char

34
35
tests :: SuiteConfig -> TestTreeM ()
tests config = do
36
37
38
39
40

  ---------------------------------------------------------------------
  -- * External tests

  -- Test that Cabal parses 'benchmark' sections correctly
41
  tc "BenchmarkStanza"  PackageTests.BenchmarkStanza.Check.suite
42
43

  -- Test that Cabal parses 'test' sections correctly
44
  tc "TestStanza"       PackageTests.TestStanza.Check.suite
45

46
47
48
49
50
  -- Test that Cabal parses '^>=' operator correctly.
  -- Don't run this for GHC 7.0/7.2, which doesn't have a recent
  -- enough version of pretty. (But this is pretty dumb.)
  tc "CaretOperator" . whenGhcVersion (>= mkVersion [7,3])$
    PackageTests.CaretOperator.Check.suite
51

52
  -- Test that Cabal determinstically generates object archives
53
  tc "DeterministicAr"  PackageTests.DeterministicAr.Check.suite
54

fmaste's avatar
fmaste committed
55
56
57
58
59
  -- Test that cabal shows all the 'autogen-modules' warnings.
  tc "AutogenModules/Package" PackageTests.AutogenModules.Package.Check.suite
  -- Test that Cabal parses and uses 'autogen-modules' fields correctly
  tc "AutogenModules/SrcDist" PackageTests.AutogenModules.SrcDist.Check.suite

60
  -- Test that foreign libraries work
61
  tc "ForeignLibs" PackageTests.ForeignLibs.Check.suite
62

63
64
65
  ---------------------------------------------------------------------
  -- * Test suite tests

Edward Z. Yang's avatar
Edward Z. Yang committed
66
67
68
69
70
  -- TODO: This shouldn't be necessary, but there seems to be some
  -- bug in the way the test is written.  Not going to lose sleep
  -- over this... but would be nice to fix.
  testWhen (hasCabalForGhc config)
   . groupTests "TestSuiteTests/ExeV10" $
71
      (PackageTests.TestSuiteTests.ExeV10.Check.tests config)
72

73
  -- Test if detailed-0.9 builds correctly
Edward Z. Yang's avatar
Edward Z. Yang committed
74
75
  testWhen (hasCabalForGhc config)
   . tcs "TestSuiteTests/LibV09" "Build" $ cabal_build ["--enable-tests"]
76
77

  -- Tests for #2489, stdio deadlock
Edward Z. Yang's avatar
Edward Z. Yang committed
78
79
  testWhen (hasCabalForGhc config)
   . mapTestTrees (localOption (mkTimeout $ 10 ^ (8 :: Int)))
80
81
82
   . tcs "TestSuiteTests/LibV09" "Deadlock" $ do
      cabal_build ["--enable-tests"]
      shouldFail $ cabal "test" []
83
84
85
86
87

  ---------------------------------------------------------------------
  -- * Inline tests

  -- Test if exitcode-stdio-1.0 benchmark builds correctly
88
  tc "BenchmarkExeV10" $ cabal_build ["--enable-benchmarks"]
89
90

  -- Test --benchmark-option(s) flags on ./Setup bench
91
  tc "BenchmarkOptions" $ do
92
93
94
95
96
97
98
99
      cabal_build ["--enable-benchmarks"]
      cabal "bench" [ "--benchmark-options=1 2 3" ]
      cabal "bench" [ "--benchmark-option=1"
                    , "--benchmark-option=2"
                    , "--benchmark-option=3"
                    ]

  -- Test --test-option(s) flags on ./Setup test
100
  tc "TestOptions" $ do
101
102
103
104
105
106
107
108
109
      cabal_build ["--enable-tests"]
      cabal "test" ["--test-options=1 2 3"]
      cabal "test" [ "--test-option=1"
                   , "--test-option=2"
                   , "--test-option=3"
                   ]

  -- Test attempt to have executable depend on internal
  -- library, but cabal-version is too old.
110
  tc "BuildDeps/InternalLibrary0" $ do
111
      r <- shouldFail $ cabal' "configure" []
112
113
114
115
116
      -- Should tell you how to enable the desired behavior
      let sb = "library which is defined within the same package."
      assertOutputContains sb r

  -- Test executable depends on internal library.
117
  tc "BuildDeps/InternalLibrary1" $ cabal_build []
118
119
120

  -- Test that internal library is preferred to an installed on
  -- with the same name and version
121
  tc "BuildDeps/InternalLibrary2" $ internal_lib_test "internal"
122
123
124

  -- Test that internal library is preferred to an installed on
  -- with the same name and LATER version
125
  tc "BuildDeps/InternalLibrary3" $ internal_lib_test "internal"
126

127
128
129
130
131
132
133
134
135
136
137
  -- On old versions of Cabal, an explicit dependency constraint which
  -- doesn't match the internal library causes us to use external
  -- library.  We got rid of this functionality in 1.25; now, we always
  -- use internal, and just emit an error if you check.
  tcs "BuildDeps/InternalLibrary4" "external" $ internal_lib_test "internal"

  -- On a previous buggy version of my patch, the test above passed only
  -- because the installed library caused Cabal to think that the
  -- dependency was fulfilled.  Make sure we ignore the dependency
  -- entirely.
  tcs "BuildDeps/InternalLibrary4" "ignore" $ cabal_build []
138
139
140
141

  -- Test "old build-dep behavior", where we should get the
  -- same package dependencies on all targets if cabal-version
  -- is sufficiently old.
142
  tc "BuildDeps/SameDepsAllRound" $ cabal_build []
143
144
145
146

  -- Test "new build-dep behavior", where each target gets
  -- separate dependencies.  This tests that an executable
  -- dep does not leak into the library.
147
  tc "BuildDeps/TargetSpecificDeps1" $ do
148
      cabal "configure" []
149
      r <- shouldFail $ cabal' "build" []
150
151
152
      assertRegex "error should be in MyLibrary.hs" "^MyLibrary.hs:" r
      assertRegex
        "error should be \"Could not find module `Text\\.PrettyPrint\""
153
        "(Could not find module|Failed to load interface for).*Text\\.PrettyPrint" r
154
155
156

  -- This is a control on TargetSpecificDeps1; it should
  -- succeed.
157
  tc "BuildDeps/TargetSpecificDeps2" $ cabal_build []
158
159
160
161

  -- Test "new build-dep behavior", where each target gets
  -- separate dependencies.  This tests that an library
  -- dep does not leak into the executable.
162
  tc "BuildDeps/TargetSpecificDeps3" $ do
163
      cabal "configure" []
164
      r <- shouldFail $ cabal' "build" []
165
166
167
      assertRegex "error should be in lemon.hs" "^lemon.hs:" r
      assertRegex
        "error should be \"Could not find module `Text\\.PrettyPrint\""
168
        "(Could not find module|Failed to load interface for).*Text\\.PrettyPrint" r
169
170

  -- Test that Paths module is generated and available for executables.
171
  tc "PathsModule/Executable" $ cabal_build []
172
173

  -- Test that Paths module is generated and available for libraries.
174
  tc "PathsModule/Library" $ cabal_build []
175
176

  -- Check that preprocessors (hsc2hs) are run
177
  tc "PreProcess" $ cabal_build ["--enable-tests", "--enable-benchmarks"]
178
179

  -- Check that preprocessors that generate extra C sources are handled
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
180
181
  tc "PreProcessExtraSources" $ cabal_build ["--enable-tests",
                                             "--enable-benchmarks"]
182
183

  -- Test building a vanilla library/executable which uses Template Haskell
184
  tc "TemplateHaskell/vanilla" $ cabal_build []
185
186
187

  -- Test building a profiled library/executable which uses Template Haskell
  -- (Cabal has to build the non-profiled version first)
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
188
189
  tc "TemplateHaskell/profiling" $ cabal_build ["--enable-library-profiling",
                                                "--enable-profiling"]
190

191
192
  -- Test building a dynamic library/executable which uses Template
  -- Haskell
193
  testWhen (hasSharedLibraries config) $
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
194
195
    tc "TemplateHaskell/dynamic" $ cabal_build ["--enable-shared",
                                                "--enable-executable-dynamic"]
196

197
198
  -- Test building an executable whose main() function is defined in a C
  -- file
199
  tc "CMain" $ cabal_build []
200
201

  -- Test build when the library is empty, for #1241
202
  tc "EmptyLib" $
203
204
205
      withPackage "empty" $ cabal_build []

  -- Test that "./Setup haddock" works correctly
206
  tc "Haddock" $ do
207
208
209
210
211
212
213
214
215
      dist_dir <- distDir
      let haddocksDir = dist_dir </> "doc" </> "html" </> "Haddock"
      cabal "configure" []
      cabal "haddock" []
      let docFiles
              = map (haddocksDir </>)
                    ["CPP.html", "Literate.html", "NoCPP.html", "Simple.html"]
      mapM_ (assertFindInFile "For hiding needles.") docFiles

Edward Z. Yang's avatar
Edward Z. Yang committed
216
  -- Test that Haddock with a newline in synopsis works correctly, #3004
217
  tc "HaddockNewline" $ do
Edward Z. Yang's avatar
Edward Z. Yang committed
218
219
220
        cabal "configure" []
        cabal "haddock" []

221
222
  -- Test that Cabal properly orders GHC flags passed to GHC (when
  -- there are multiple ghc-options fields.)
223
  tc "OrderFlags" $ cabal_build []
224
225

  -- Test that reexported modules build correctly
226
  tcs "ReexportedModules" "p" . whenGhcVersion (>= mkVersion [7,9]) $ do
227
      withPackageDb $ do
228
        withPackage "p" $ cabal_install ["--cabal-file", "p.cabal"]
229
230
        withPackage "q" $ do
            cabal_build []
231
  tcs "ReexportedModules" "fail-other" . whenGhcVersion (>= mkVersion [7,9]) $ do
232
233
234
      withPackage "p" $ do
          r <- shouldFail $ cabal' "configure" ["--cabal-file", "fail-other.cabal"]
          assertOutputContains "Private" r
235
  tcs "ReexportedModules" "fail-ambiguous" . whenGhcVersion (>= mkVersion [7,9]) $ do
236
237
238
239
240
      withPackageDb $ do
        withPackage "containers-dupe" $ cabal_install []
        withPackage "p" $ do
            r <- shouldFail $ cabal' "configure" ["--cabal-file", "fail-ambiguous.cabal"]
            assertOutputContains "Data.Map" r
241
  tcs "ReexportedModules" "fail-missing" . whenGhcVersion (>= mkVersion [7,9]) $ do
242
243
244
      withPackage "p" $ do
          r <- shouldFail $ cabal' "configure" ["--cabal-file", "fail-missing.cabal"]
          assertOutputContains "Missing" r
245

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  -- Test that module name ambiguity can be resolved using package
  -- qualified imports.  (Paper Backpack doesn't natively support
  -- this but we must!)
  tcs "Ambiguity" "package-import" $ do
    withPackageDb $ do
        withPackage "p" $ cabal_install []
        withPackage "q" $ cabal_install []
        withPackage "package-import" $ do
            cabal_build []
            runExe' "package-import" [] >>= assertOutputContains "p q"

  -- Test that we can resolve a module name ambiguity when reexporting
  -- by explicitly specifying what package we want.
  tcs "Ambiguity" "reexport" . whenGhcVersion (>= mkVersion [7,9]) $ do
    withPackageDb $ do
        withPackage "p" $ cabal_install []
        withPackage "q" $ cabal_install []
        withPackage "reexport" $ cabal_install []
        withPackage "reexport-test" $ do
            cabal_build []
            runExe' "reexport-test" [] >>= assertOutputContains "p q"

268
  -- Test that Cabal computes different IPIDs when the source changes.
269
  tc "UniqueIPID" . withPackageDb $ do
270
271
272
273
      withPackage "P1" $ cabal "configure" []
      withPackage "P2" $ cabal "configure" []
      withPackage "P1" $ cabal "build" []
      withPackage "P1" $ cabal "build" [] -- rebuild should work
274
      r1 <- withPackage "P1" $ cabal' "register" ["--print-ipid", "--inplace"]
275
      withPackage "P2" $ cabal "build" []
276
      r2 <- withPackage "P2" $ cabal' "register" ["--print-ipid", "--inplace"]
277
278
279
280
281
282
      let exIPID s = takeWhile (/= '\n') $
               head . filter (isPrefixOf $ "UniqueIPID-0.1-") $ (tails s)
      when ((exIPID $ resultOutput r1) == (exIPID $ resultOutput r2)) $
        assertFailure $ "cabal has not calculated different Installed " ++
          "package ID when source is changed."

283
284
  -- Test that if two components have the same module name, they do not
  -- clobber each other.
Edward Z. Yang's avatar
Edward Z. Yang committed
285
286
  testWhen (hasCabalForGhc config) $ -- uses library test suite
    tc "DuplicateModuleName" $ do
287
288
289
290
291
292
293
294
      cabal_build ["--enable-tests"]
      r1 <- shouldFail $ cabal' "test" ["foo"]
      assertOutputContains "test B" r1
      assertOutputContains "test A" r1
      r2 <- shouldFail $ cabal' "test" ["foo2"]
      assertOutputContains "test C" r2
      assertOutputContains "test A" r2

295
296
297
  -- Test that if test suite has a name which conflicts with a package
  -- which is in the database, we can still use the test case (they
  -- should NOT shadow).
Edward Z. Yang's avatar
Edward Z. Yang committed
298
299
  testWhen (hasCabalForGhc config)
   . tc "TestNameCollision" $ do
300
301
302
303
304
305
        withPackageDb $ do
          withPackage "parent" $ cabal_install []
          withPackage "child" $ do
            cabal_build ["--enable-tests"]
            cabal "test" []

Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
306
  -- Test that '--allow-newer' works via the 'Setup.hs configure' interface.
307
  tc "AllowNewer" $ do
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
308
309
310
311
        shouldFail $ cabal "configure" []
        cabal "configure" ["--allow-newer"]
        shouldFail $ cabal "configure" ["--allow-newer=baz,quux"]
        cabal "configure" ["--allow-newer=base", "--allow-newer=baz,quux"]
312
313
        cabal "configure" ["--allow-newer=bar", "--allow-newer=base,baz"
                          ,"--allow-newer=quux"]
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
314
315
316
317
318
319
320
        shouldFail $ cabal "configure" ["--enable-tests"]
        cabal "configure" ["--enable-tests", "--allow-newer"]
        shouldFail $ cabal "configure" ["--enable-benchmarks"]
        cabal "configure" ["--enable-benchmarks", "--allow-newer"]
        shouldFail $ cabal "configure" ["--enable-benchmarks", "--enable-tests"]
        cabal "configure" ["--enable-benchmarks", "--enable-tests"
                          ,"--allow-newer"]
321
322
323
324
325
326
327
328
329
        shouldFail $ cabal "configure" ["--allow-newer=Foo:base"]
        shouldFail $ cabal "configure" ["--allow-newer=Foo:base"
                                       ,"--enable-tests", "--enable-benchmarks"]
        cabal "configure" ["--allow-newer=AllowNewer:base"]
        cabal "configure" ["--allow-newer=AllowNewer:base"
                          ,"--allow-newer=Foo:base"]
        cabal "configure" ["--allow-newer=AllowNewer:base"
                          ,"--allow-newer=Foo:base"
                          ,"--enable-tests", "--enable-benchmarks"]
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
330

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  -- Test that '--allow-older' works via the 'Setup.hs configure' interface.
  tc "AllowOlder" $ do
        shouldFail $ cabal "configure" []
        cabal "configure" ["--allow-older"]
        shouldFail $ cabal "configure" ["--allow-older=baz,quux"]
        cabal "configure" ["--allow-older=base", "--allow-older=baz,quux"]
        cabal "configure" ["--allow-older=bar", "--allow-older=base,baz"
                          ,"--allow-older=quux"]
        shouldFail $ cabal "configure" ["--enable-tests"]
        cabal "configure" ["--enable-tests", "--allow-older"]
        shouldFail $ cabal "configure" ["--enable-benchmarks"]
        cabal "configure" ["--enable-benchmarks", "--allow-older"]
        shouldFail $ cabal "configure" ["--enable-benchmarks", "--enable-tests"]
        cabal "configure" ["--enable-benchmarks", "--enable-tests"
                          ,"--allow-older"]
        shouldFail $ cabal "configure" ["--allow-older=Foo:base"]
        shouldFail $ cabal "configure" ["--allow-older=Foo:base"
                                       ,"--enable-tests", "--enable-benchmarks"]
        cabal "configure" ["--allow-older=AllowOlder:base"]
        cabal "configure" ["--allow-older=AllowOlder:base"
                          ,"--allow-older=Foo:base"]
        cabal "configure" ["--allow-older=AllowOlder:base"
                          ,"--allow-older=Foo:base"
                          ,"--enable-tests", "--enable-benchmarks"]

356
357
358
  -- Test that Cabal can choose flags to disable building a component when that
  -- component's dependencies are unavailable. The build should succeed without
  -- requiring the component's dependencies or imports.
359
360
361
362
  tc "BuildableField" $ do
      r <- cabal' "configure" ["-v"]
      assertOutputContains "Flags chosen: build-exe=False" r
      cabal "build" []
363

364
365
366
367
368
  -- TODO: Enable these tests on Windows
  unlessWindows $ do
      tc "GhcPkgGuess/SameDirectory" $ ghc_pkg_guess "ghc"
      tc "GhcPkgGuess/SameDirectoryVersion" $ ghc_pkg_guess "ghc-7.10"
      tc "GhcPkgGuess/SameDirectoryGhcVersion" $ ghc_pkg_guess "ghc-7.10"
369

370
  unlessWindows $ do
371
372
373
      tc "GhcPkgGuess/Symlink" $ do
        -- We don't want to distribute a tarball with symlinks. See #3190.
        withSymlink "bin/ghc"
374
                    "PackageTests/GhcPkgGuess/Symlink/ghc" $
375
376
377
378
                    ghc_pkg_guess "ghc"

      tc "GhcPkgGuess/SymlinkVersion" $ do
        withSymlink "bin/ghc-7.10"
379
                    "PackageTests/GhcPkgGuess/SymlinkVersion/ghc" $
380
381
382
383
                    ghc_pkg_guess "ghc"

      tc "GhcPkgGuess/SymlinkGhcVersion" $ do
        withSymlink "bin/ghc-7.10"
384
                    "PackageTests/GhcPkgGuess/SymlinkGhcVersion/ghc" $
385
                    ghc_pkg_guess "ghc"
386

387
388
389
390
  -- Basic test for internal libraries (in p); package q is to make
  -- sure that the internal library correctly is used, not the
  -- external library.
  tc "InternalLibraries" $ do
391
392
393
394
      withPackageDb $ do
          withPackage "q" $ cabal_install []
          withPackage "p" $ do
              cabal_install []
395
396
              cabal "clean" []
              r <- runInstalledExe' "foo" []
397
398
              assertOutputContains "I AM THE ONE" r

399
400
401
402
403
404
405
406
407
  -- Test to see if --gen-script works.
  tcs "InternalLibraries" "gen-script" $ do
    withPackageDb $ do
      withPackage "p" $ do
        cabal_build []
        cabal "copy" []
        cabal "register" ["--gen-script"]
        _ <- if buildOS == Windows
                then shell "cmd" ["/C", "register.bat"]
Oleg Grenrus's avatar
Oleg Grenrus committed
408
                else shell "sh" ["register.sh"]
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
        return ()
      -- Make sure we can see p
      withPackage "r" $ cabal_install []

  -- Test to see if --gen-pkg-config works.
  tcs "InternalLibraries" "gen-pkg-config" $ do
    withPackageDb $ do
      withPackage "p" $ do
        cabal_build []
        cabal "copy" []
        let dir = "pkg-config.bak"
        cabal "register" ["--gen-pkg-config=" ++ dir]
        -- Infelicity! Does not respect CWD.
        pkg_dir <- packageDir
        let notHidden = not . isHidden
            isHidden name = "." `isPrefixOf` name
        confs <- fmap (sort . filter notHidden)
               . liftIO $ getDirectoryContents (pkg_dir </> dir)
        forM_ confs $ \conf -> ghcPkg "register" [pkg_dir </> dir </> conf]

      -- Make sure we can see p
      withPackage "r" $ cabal_install []

432
  -- Internal libraries used by a statically linked executable:
433
434
435
436
  -- no libraries should get installed or registered.  (Note,
  -- this does build shared libraries just to make sure they
  -- don't get installed, so this test doesn't work on Windows.)
  testWhen (hasSharedLibraries config) $
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
437
438
    tcs "InternalLibraries/Executable" "Static" $
      multiple_libraries_executable False
439
440
441

  -- Internal libraries used by a dynamically linked executable:
  -- ONLY the dynamic library should be installed, no registration
442
  testWhen (hasSharedLibraries config) $
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
443
444
    tcs "InternalLibraries/Executable" "Dynamic" $
      multiple_libraries_executable True
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459

  -- Internal library used by public library; it must be installed and
  -- registered.
  tc "InternalLibraries/Library" $ do
      withPackageDb $ do
          withPackage "foolib" $ cabal_install []
          withPackage "fooexe" $ do
              cabal_build []
              runExe' "fooexe" []
                  >>= assertOutputContains "25"

  -- Test to ensure that cabal_macros.h are computed per-component.
  tc "Macros" $ do
      cabal_build []
      runExe' "macros-a" []
460
          >>= assertOutputContains "macros-a"
461
      runExe' "macros-b" []
462
          >>= assertOutputContains "macros-b"
463

464
465
466
467
468
469
470
471
  -- Test for 'build-type: Configure' example from the Cabal manual.
  -- Disabled on Windows since MingW doesn't ship with autoreconf by
  -- default.
  unlessWindows $ do
      tc "Configure" $ do
          _ <- shell "autoreconf" ["-i"]
          cabal_build []

472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  tc "ConfigureComponent/Exe" $ do
    withPackageDb $ do
      cabal_install ["goodexe"]
      runExe' "goodexe" [] >>= assertOutputContains "OK"

  tcs "ConfigureComponent/SubLib" "sublib-explicit" $ do
    withPackageDb $ do
      cabal_install ["sublib", "--cid", "sublib-0.1-abc"]
      cabal_install ["exe", "--dependency", "sublib=sublib-0.1-abc"]
      runExe' "exe" [] >>= assertOutputContains "OK"

  tcs "ConfigureComponent/SubLib" "sublib" $ do
    withPackageDb $ do
      cabal_install ["sublib"]
      cabal_install ["exe"]
      runExe' "exe" [] >>= assertOutputContains "OK"

  tcs "ConfigureComponent/Test" "test" $ do
    withPackageDb $ do
      cabal_install ["test-for-cabal"]
      withPackage "testlib" $ cabal_install []
      cabal "configure" ["testsuite"]
      cabal "build" []
      cabal "test" []

497
498
499
500
501
502
503
504
505
506
507
508
509
510
  -- Test that per-component copy works, when only building library
  tc "CopyComponent/Lib" $
      withPackageDb $ do
          cabal "configure" []
          cabal "build" ["lib:p"]
          cabal "copy" ["lib:p"]

  -- Test that per-component copy works, when only building one executable
  tc "CopyComponent/Exe" $
      withPackageDb $ do
          cabal "configure" []
          cabal "build" ["myprog"]
          cabal "copy" ["myprog"]

511
512
513
514
515
516
  -- Test internal custom preprocessor
  tc "CustomPreProcess" $ do
      cabal_build []
      runExe' "hello-world" []
        >>= assertOutputContains "hello from A"

517
518
519
520
521
522
  -- Test PATH-munging
  tc "BuildToolsPath" $ do
      cabal_build []
      runExe' "hello-world" []
        >>= assertOutputContains "1111"

523
524
525
526
527
528
529
530
  -- Test that executable recompilation works
  -- https://github.com/haskell/cabal/issues/3294
  tc "Regression/T3294" $ do
    pkg_dir <- packageDir
    liftIO $ writeFile (pkg_dir </> "Main.hs") "main = putStrLn \"aaa\""
    cabal "configure" []
    cabal "build" []
    runExe' "T3294" [] >>= assertOutputContains "aaa"
531
    ghcFileModDelay
532
533
534
535
    liftIO $ writeFile (pkg_dir </> "Main.hs") "main = putStrLn \"bbb\""
    cabal "build" []
    runExe' "T3294" [] >>= assertOutputContains "bbb"

536
537
538
539
540
  -- Test that other-extensions of disabled component do not
  -- effect configure step.
  tc "Regression/T3847" $ do
    cabal "configure" ["--disable-tests"]

541
542
543
544
545
546
547
548
549
550
551
552
553
  -- Test that we don't pick up include-dirs from libraries
  -- we didn't actually depend on.
  tc "Regression/T2971" $ do
    withPackageDb $ do
      withPackage "p" $ cabal_install []
      withPackage "q" $ do
        cabal "configure" []
        assertOutputContains "T2971test.h"
            =<< shouldFail (cabal' "build" [])

  -- Test that we pick up include dirs from internal library
  tc "Regression/T2971a" $ cabal_build []

554
555
556
557
558
559
560
561
562
563
564
565
566
  -- Test that we don't accidentally add the inplace directory to
  -- an executable RPATH.  Don't test on Windows, which doesn't
  -- support RPATH.
  unlessWindows $ do
    tc "Regression/T4025" $ do
      cabal "configure" ["--enable-executable-dynamic"]
      cabal "build" []
      -- This should fail as it we should NOT be able to find the
      -- dynamic library for the internal library (since we didn't
      -- install it).  If we incorrectly encoded our local dist
      -- dir in the RPATH, this will succeed.
      shouldFail $ runExe' "exe" []

567
568
569
570
571
572
573
574
  -- Test error message we report when a non-buildable target is
  -- requested to be built
  -- TODO: We can give a better error message here, see #3858.
  tcs "BuildTargetErrors" "non-buildable" $ do
    cabal "configure" []
    assertOutputContains "There is no component"
        =<< shouldFail (cabal' "build" ["not-buildable-exe"])

575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
  tc "Backpack/Includes1" . whenGhcVersion (>= mkVersion [8,1]) $ do
      cabal "configure" []
      r <- shouldFail $ cabal' "build" []
      assertBool "error should be in B.hs" $
          resultOutput r =~ "^B.hs:"
      assertBool "error should be \"Could not find module Data.Set\"" $
          resultOutput r =~ "(Could not find module|Failed to load interface).*Data.Set"

  tcs "Backpack/Includes2" "internal" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      cabal_install ["--cabal-file", "Includes2.cabal"]
      -- TODO: haddock for internal method doesn't work
      runExe' "exe" [] >>= assertOutputContains "minemysql minepostgresql"

  tcs "Backpack/Includes2" "internal-fail" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      r <- shouldFail $ cabal' "configure" ["--cabal-file", "fail.cabal"]
      assertOutputContains "mysql" r

  tcs "Backpack/Includes2" "external" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      withPackage "mylib" $ cabal_install_with_docs ["--ipid", "mylib-0.1.0.0"]
      withPackage "mysql" $ cabal_install_with_docs ["--ipid", "mysql-0.1.0.0"]
      withPackage "postgresql" $ cabal_install_with_docs ["--ipid", "postgresql-0.1.0.0"]
      withPackage "mylib" $
        cabal_install_with_docs ["--ipid", "mylib-0.1.0.0",
                       "--instantiate-with", "Database=mysql-0.1.0.0:Database.MySQL"]
      withPackage "mylib" $
        cabal_install_with_docs ["--ipid", "mylib-0.1.0.0",
                       "--instantiate-with", "Database=postgresql-0.1.0.0:Database.PostgreSQL"]
      withPackage "src" $ cabal_install_with_docs []
      withPackage "exe" $ do
        cabal_install_with_docs []
        runExe' "exe" [] >>= assertOutputContains "minemysql minepostgresql"

  tcs "Backpack/Includes2" "per-component" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      let cabal_install' args = cabal_install_with_docs (["--cabal-file", "Includes2.cabal"] ++ args)
      cabal_install' ["mylib", "--cid", "mylib-0.1.0.0"]
      cabal_install' ["mysql", "--cid", "mysql-0.1.0.0"]
      cabal_install' ["postgresql", "--cid", "postgresql-0.1.0.0"]
      cabal_install' ["mylib", "--cid", "mylib-0.1.0.0",
                     "--instantiate-with", "Database=mysql-0.1.0.0:Database.MySQL"]
      cabal_install' ["mylib", "--cid", "mylib-0.1.0.0",
                     "--instantiate-with", "Database=postgresql-0.1.0.0:Database.PostgreSQL"]
      cabal_install' ["Includes2"]
      cabal_install' ["exe"]
      runExe' "exe" [] >>= assertOutputContains "minemysql minepostgresql"

  tcs "Backpack/Includes3" "internal" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      cabal_install []
      -- TODO: refactorize
      pkg_dir <- packageDir
      _ <- run (Just pkg_dir) "touch" ["indef/Foo.hs"]
      cabal "build" []
      runExe' "exe" [] >>= assertOutputContains "fromList [(0,2),(2,4)]"

  tcs "Backpack/Includes3" "external-fail" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      withPackage "sigs" $ cabal_install []
      withPackage "indef" $ cabal_install []
      -- Forgot to build the instantiated versions!
      withPackage "exe" $ do
        r <- shouldFail $ cabal' "configure" []
        assertOutputContains "indef-0.1.0.0" r
        return ()

  tcs "Backpack/Includes3" "external-ok" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      containers_result <- ghcPkg' "field" ["--global", "containers", "id"]
      containers_id <- case stripPrefix "id: " (resultOutput containers_result) of
        Just x -> return (takeWhile (not . Char.isSpace) x)
        Nothing -> error "could not determine id of containers"
      withPackage "sigs" $ cabal_install_with_docs ["--ipid", "sigs-0.1.0.0"]
      withPackage "indef" $ cabal_install_with_docs ["--ipid", "indef-0.1.0.0"]
      withPackage "sigs" $ do
        -- NB: this REUSES the dist directory that we typechecked
        -- indefinitely, but it's OK; the recompile checker should get it.
        cabal_install_with_docs ["--ipid", "sigs-0.1.0.0",
                       "--instantiate-with", "Data.Map=" ++ containers_id ++ ":Data.Map"]
      withPackage "indef" $ do
        -- Ditto.
        cabal_install_with_docs ["--ipid", "indef-0.1.0.0",
                       "--instantiate-with", "Data.Map=" ++ containers_id ++ ":Data.Map"]
      withPackage "exe" $ do
        cabal_install []
        runExe' "exe" [] >>= assertOutputContains "fromList [(0,2),(2,4)]"

  tcs "Backpack/Includes3" "external-explicit" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      withPackage "sigs" $ cabal_install_with_docs ["--cid", "sigs-0.1.0.0", "lib:sigs"]
      withPackage "indef" $ cabal_install_with_docs ["--cid", "indef-0.1.0.0", "--dependency=sigs=sigs-0.1.0.0", "lib:indef"]

  tc "Backpack/Includes4" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      cabal_install []
      runExe' "exe" [] >>= assertOutputContains "A (B (A (B"

  tc "Backpack/Includes5" . whenGhcVersion (>= mkVersion [8,1]) $ do
      cabal "configure" []
      r <- shouldFail $ cabal' "build" []
      assertOutputContains "Foobar" r
678
      assertOutputContains "Could not find" r
679
680
681
682
683
684
685
686
687
      return ()

  tc "Backpack/Reexport1" . whenGhcVersion (>= mkVersion [8,1]) $ do
    withPackageDb $ do
      withPackage "p" $ cabal_install_with_docs []
      withPackage "q" $ do
        cabal_build []
        cabal "haddock" []

688
  where
689
690
691
692
693
694
695
696
    ghc_pkg_guess bin_name = do
        cwd <- packageDir
        with_ghc <- getWithGhcPath
        r <- withEnv [("WITH_GHC", Just with_ghc)]
           . shouldFail $ cabal' "configure" ["-w", cwd </> bin_name]
        assertOutputContains "is version 9999999" r
        return ()

697
698
699
700
    -- Shared test function for BuildDeps/InternalLibrary* tests.
    internal_lib_test expect = withPackageDb $ do
        withPackage "to-install" $ cabal_install []
        cabal_build []
701
        r <- runExe' "lemon" []
702
703
        assertEqual
            ("executable should have linked with the " ++ expect ++ " library")
704
705
            ("foo foo myLibFunc " ++ expect)
            (concatOutput (resultOutput r))
706

707
708
709
710
711
    assertRegex :: String -> String -> Result -> TestM ()
    assertRegex msg regex r = let out = resultOutput r
                              in assertBool (msg ++ ",\nactual output:\n" ++ out)
                                 (out =~ regex)

712
713
714
715
716
717
718
719
720
    multiple_libraries_executable is_dynamic =
        withPackageDb $ do
            cabal_install $ [ if is_dynamic then "--enable-executable-dynamic"
                                            else "--disable-executable-dynamic"
                            , "--enable-shared"]
            dist_dir <- distDir
            lbi <- liftIO $ getPersistBuildConfig dist_dir
            let pkg_descr = localPkgDescr lbi
                compiler_id = compilerId (compiler lbi)
721
                cname = CSubLibName $ mkUnqualComponentName "foo-internal"
722
                [target] = componentNameTargets' pkg_descr lbi cname
723
                uid = componentUnitId (targetCLBI target)
Christiaan Baaij's avatar
Christiaan Baaij committed
724
725
                InstallDirs{libdir=dir,dynlibdir=dyndir} =
                  absoluteComponentInstallDirs pkg_descr lbi uid NoCopyDest
726
            assertBool "interface files should be installed"
727
                =<< liftIO (doesFileExist (dir </> "Foo.hi"))
728
            assertBool "static library should be installed"
729
730
731
732
                =<< liftIO (doesFileExist (dir </> mkLibName uid))
            if is_dynamic
              then
                assertBool "dynamic library MUST be installed"
Christiaan Baaij's avatar
Christiaan Baaij committed
733
                    =<< liftIO (doesFileExist (dyndir </> mkSharedLibName
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
734
                                               compiler_id uid))
735
              else
736
                assertBool "dynamic library should be installed"
Christiaan Baaij's avatar
Christiaan Baaij committed
737
                    =<< liftIO (doesFileExist (dyndir </> mkSharedLibName
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
738
                                               compiler_id uid))
739
740
741
742
743
744
            shouldFail $ ghcPkg "describe" ["foo"]
            -- clean away the dist directory so that we catch accidental
            -- dependence on the inplace files
            cabal "clean" []
            runInstalledExe' "foo" [] >>= assertOutputContains "46"

745
    tc :: FilePath -> TestM a -> TestTreeM ()
746
747
748
749
750
    tc name = testTree config name

    tcs :: FilePath -> FilePath -> TestM a -> TestTreeM ()
    tcs name sub_name m
        = testTreeSub config name sub_name m