-
Notifications
You must be signed in to change notification settings - Fork 701
/
Copy pathIntegrationTests2.hs
2126 lines (1887 loc) · 89.1 KB
/
IntegrationTests2.hs
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{- FOURMOLU_DISABLE -}
{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
-- For the handy instance IsString PackageIdentifier
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Main where
import Distribution.Client.Compat.Prelude
import Prelude ()
import Distribution.Client.DistDirLayout
import Distribution.Client.ProjectConfig
import Distribution.Client.HttpUtils
import Distribution.Client.TargetSelector hiding (DirActions(..))
import qualified Distribution.Client.TargetSelector as TS (DirActions(..))
import Distribution.Client.ProjectPlanning
import Distribution.Client.ProjectPlanning.Types
import Distribution.Client.ProjectBuilding
import Distribution.Client.ProjectOrchestration
( resolveTargets, distinctTargetComponents )
import Distribution.Client.TargetProblem
( TargetProblem', TargetProblem (..) )
import Distribution.Client.Types
( PackageLocation(..), UnresolvedSourcePackage
, PackageSpecifier(..) )
import Distribution.Client.Targets
( UserConstraint(..), UserConstraintScope(UserAnyQualifier) )
import qualified Distribution.Client.InstallPlan as InstallPlan
import Distribution.Solver.Types.SourcePackage as SP
import Distribution.Solver.Types.ConstraintSource
( ConstraintSource(ConstraintSourceUnknown) )
import Distribution.Solver.Types.PackageConstraint
( PackageProperty(PackagePropertySource) )
import qualified Distribution.Client.CmdBuild as CmdBuild
import qualified Distribution.Client.CmdRepl as CmdRepl
import qualified Distribution.Client.CmdRun as CmdRun
import qualified Distribution.Client.CmdTest as CmdTest
import qualified Distribution.Client.CmdBench as CmdBench
import qualified Distribution.Client.CmdHaddock as CmdHaddock
import qualified Distribution.Client.CmdListBin as CmdListBin
import Distribution.Package
import Distribution.PackageDescription
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
import Distribution.Simple.Setup (toFlag, HaddockFlags(..), defaultHaddockFlags)
import Distribution.Client.Setup (globalCommand)
import Distribution.Client.Config (loadConfig, SavedConfig(savedGlobalFlags), createDefaultConfigFile)
import Distribution.Simple.Compiler
import Distribution.Simple.Command
import qualified Distribution.Simple.Flag as Flag
import Distribution.System
import Distribution.Version
import Distribution.Text
import qualified Distribution.Client.CmdHaddockProject as CmdHaddockProject
import Distribution.Client.Setup (globalStoreDir)
import Distribution.Client.GlobalFlags (defaultGlobalFlags)
import Distribution.Simple.Setup (HaddockProjectFlags(..), defaultHaddockProjectFlags)
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.List (isInfixOf)
import Control.Monad
import Control.Concurrent (threadDelay)
import Control.Exception hiding (assert)
import System.FilePath
import System.Directory
import System.IO (hPutStrLn, stderr)
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.Options
import Data.Tagged (Tagged(..))
import qualified Data.ByteString as BS
import Distribution.Client.GlobalFlags (GlobalFlags, globalNix)
import Distribution.Simple.Flag (Flag (Flag, NoFlag))
import Distribution.Types.ParStrat
import Data.Maybe (fromJust)
#if !MIN_VERSION_directory(1,2,7)
removePathForcibly :: FilePath -> IO ()
removePathForcibly = removeDirectoryRecursive
#endif
main :: IO ()
main =
defaultMainWithIngredients
(defaultIngredients ++ [includingOptions projectConfigOptionDescriptions])
(withProjectConfig $ \config ->
testGroup "Integration tests (internal)"
(tests config))
tests :: ProjectConfig -> [TestTree]
tests config =
--TODO: tests for:
-- * normal success
-- * dry-run tests with changes
[ testGroup "Discovery and planning" $
[ testCase "no package" (testExceptionInFindingPackage config)
, testCase "no package2" (testExceptionInFindingPackage2 config)
, testCase "proj conf1" (testExceptionInProjectConfig config)
]
, testGroup "Target selectors" $
[ testCaseSteps "valid" testTargetSelectors
, testCase "bad syntax" testTargetSelectorBadSyntax
, testCaseSteps "ambiguous syntax" testTargetSelectorAmbiguous
, testCase "no current pkg" testTargetSelectorNoCurrentPackage
, testCase "no targets" testTargetSelectorNoTargets
, testCase "project empty" testTargetSelectorProjectEmpty
, testCase "canonicalized path" testTargetSelectorCanonicalizedPath
, testCase "problems (common)" (testTargetProblemsCommon config)
, testCaseSteps "problems (build)" (testTargetProblemsBuild config)
, testCaseSteps "problems (repl)" (testTargetProblemsRepl config)
, testCaseSteps "problems (run)" (testTargetProblemsRun config)
, testCaseSteps "problems (list-bin)" (testTargetProblemsListBin config)
, testCaseSteps "problems (test)" (testTargetProblemsTest config)
, testCaseSteps "problems (bench)" (testTargetProblemsBench config)
, testCaseSteps "problems (haddock)" (testTargetProblemsHaddock config)
]
, testGroup "Exceptions during building (local inplace)" $
[ testCase "configure" (testExceptionInConfigureStep config)
, testCase "build" (testExceptionInBuildStep config)
-- , testCase "register" testExceptionInRegisterStep
]
--TODO: need to repeat for packages for the store
--TODO: need to check we can build sub-libs, foreign libs and exes
-- components for non-local packages / packages in the store.
, testGroup "Successful builds" $
[ testCaseSteps "Setup script styles" (testSetupScriptStyles config)
, testCase "keep-going" (testBuildKeepGoing config)
#ifndef mingw32_HOST_OS
-- disabled because https://github.com/haskell/cabal/issues/6272
, testCase "local tarball" (testBuildLocalTarball config)
#endif
]
, testGroup "Regression tests" $
[ testCase "issue #3324" (testRegressionIssue3324 config)
, testCase "program options scope all" (testProgramOptionsAll config)
, testCase "program options scope local" (testProgramOptionsLocal config)
, testCase "program options scope specific" (testProgramOptionsSpecific config)
]
, testGroup "Flag tests" $
[
testCase "Test Nix Flag" testNixFlags,
testCase "Test Config options for commented options" testConfigOptionComments,
testCase "Test Ignore Project Flag" testIgnoreProjectFlag
]
, testGroup "haddock-project"
[ testCase "dependencies" (testHaddockProjectDependencies config)
]
]
testTargetSelectors :: (String -> IO ()) -> Assertion
testTargetSelectors reportSubCase = do
(_, _, _, localPackages, _) <- configureProject testdir config
let readTargetSelectors' = readTargetSelectorsWith (dirActions testdir)
localPackages
Nothing
reportSubCase "cwd"
do Right ts <- readTargetSelectors' []
ts @?= [TargetPackage TargetImplicitCwd ["p-0.1"] Nothing]
reportSubCase "all"
do Right ts <- readTargetSelectors'
["all", ":all"]
ts @?= replicate 2 (TargetAllPackages Nothing)
reportSubCase "filter"
do Right ts <- readTargetSelectors'
[ "libs", ":cwd:libs"
, "flibs", ":cwd:flibs"
, "exes", ":cwd:exes"
, "tests", ":cwd:tests"
, "benchmarks", ":cwd:benchmarks"]
zipWithM_ (@?=) ts
[ TargetPackage TargetImplicitCwd ["p-0.1"] (Just kind)
| kind <- concatMap (replicate 2) [LibKind .. ]
]
reportSubCase "all:filter"
do Right ts <- readTargetSelectors'
[ "all:libs", ":all:libs"
, "all:flibs", ":all:flibs"
, "all:exes", ":all:exes"
, "all:tests", ":all:tests"
, "all:benchmarks", ":all:benchmarks"]
zipWithM_ (@?=) ts
[ TargetAllPackages (Just kind)
| kind <- concatMap (replicate 2) [LibKind .. ]
]
reportSubCase "pkg"
do Right ts <- readTargetSelectors'
[ ":pkg:p", ".", "./", "p.cabal"
, "q", ":pkg:q", "q/", "./q/", "q/q.cabal"]
ts @?= replicate 4 (mkTargetPackage "p-0.1")
++ replicate 5 (mkTargetPackage "q-0.1")
reportSubCase "pkg:filter"
do Right ts <- readTargetSelectors'
[ "p:libs", ".:libs", ":pkg:p:libs"
, "p:flibs", ".:flibs", ":pkg:p:flibs"
, "p:exes", ".:exes", ":pkg:p:exes"
, "p:tests", ".:tests", ":pkg:p:tests"
, "p:benchmarks", ".:benchmarks", ":pkg:p:benchmarks"
, "q:libs", "q/:libs", ":pkg:q:libs"
, "q:flibs", "q/:flibs", ":pkg:q:flibs"
, "q:exes", "q/:exes", ":pkg:q:exes"
, "q:tests", "q/:tests", ":pkg:q:tests"
, "q:benchmarks", "q/:benchmarks", ":pkg:q:benchmarks"]
zipWithM_ (@?=) ts $
[ TargetPackage TargetExplicitNamed ["p-0.1"] (Just kind)
| kind <- concatMap (replicate 3) [LibKind .. ]
] ++
[ TargetPackage TargetExplicitNamed ["q-0.1"] (Just kind)
| kind <- concatMap (replicate 3) [LibKind .. ]
]
reportSubCase "component"
do Right ts <- readTargetSelectors'
[ "p", "lib:p", "p:lib:p", ":pkg:p:lib:p"
, "lib:q", "q:lib:q", ":pkg:q:lib:q" ]
ts @?= replicate 4 (TargetComponent "p-0.1" (CLibName LMainLibName))
++ replicate 3 (TargetComponent "q-0.1" (CLibName LMainLibName))
cleanProject testdir
where
testdir = "targets/simple"
config = mempty
testTargetSelectorBadSyntax :: Assertion
testTargetSelectorBadSyntax = do
(_, _, _, localPackages, _) <- configureProject testdir config
let targets = [ "foo bar", " foo"
, "foo:", "foo::bar"
, "foo: ", "foo: :bar"
, "a:b:c:d:e:f", "a:b:c:d:e:f:g:h" ]
Left errs <- readTargetSelectors localPackages Nothing targets
zipWithM_ (@?=) errs (map TargetSelectorUnrecognised targets)
cleanProject testdir
where
testdir = "targets/empty"
config = mempty
testTargetSelectorAmbiguous :: (String -> IO ()) -> Assertion
testTargetSelectorAmbiguous reportSubCase = do
-- 'all' is ambiguous with packages and cwd components
reportSubCase "ambiguous: all vs pkg"
assertAmbiguous "all"
[mkTargetPackage "all", mkTargetAllPackages]
[mkpkg "all" []]
reportSubCase "ambiguous: all vs cwd component"
assertAmbiguous "all"
[mkTargetComponent "other" (CExeName "all"), mkTargetAllPackages]
[mkpkg "other" [mkexe "all"]]
-- but 'all' is not ambiguous with non-cwd components, modules or files
reportSubCase "unambiguous: all vs non-cwd comp, mod, file"
assertUnambiguous "All"
mkTargetAllPackages
[ mkpkgAt "foo" [mkexe "All"] "foo"
, mkpkg "bar" [ mkexe "bar" `withModules` ["All"]
, mkexe "baz" `withCFiles` ["All"] ]
]
-- filters 'libs', 'exes' etc are ambiguous with packages and
-- local components
reportSubCase "ambiguous: cwd-pkg filter vs pkg"
assertAmbiguous "libs"
[ mkTargetPackage "libs"
, TargetPackage TargetImplicitCwd ["libs"] (Just LibKind) ]
[mkpkg "libs" []]
reportSubCase "ambiguous: filter vs cwd component"
assertAmbiguous "exes"
[ mkTargetComponent "other" (CExeName "exes")
, TargetPackage TargetImplicitCwd ["other"] (Just ExeKind) ]
[mkpkg "other" [mkexe "exes"]]
-- but filters are not ambiguous with non-cwd components, modules or files
reportSubCase "unambiguous: filter vs non-cwd comp, mod, file"
assertUnambiguous "Libs"
(TargetPackage TargetImplicitCwd ["bar"] (Just LibKind))
[ mkpkgAt "foo" [mkexe "Libs"] "foo"
, mkpkg "bar" [ mkexe "bar" `withModules` ["Libs"]
, mkexe "baz" `withCFiles` ["Libs"] ]
]
-- local components shadow packages and other components
reportSubCase "unambiguous: cwd comp vs pkg, non-cwd comp"
assertUnambiguous "foo"
(mkTargetComponent "other" (CExeName "foo"))
[ mkpkg "other" [mkexe "foo"]
, mkpkgAt "other2" [mkexe "foo"] "other2" -- shadows non-local foo
, mkpkg "foo" [] ] -- shadows package foo
-- local components shadow modules and files
reportSubCase "unambiguous: cwd comp vs module, file"
assertUnambiguous "Foo"
(mkTargetComponent "bar" (CExeName "Foo"))
[ mkpkg "bar" [mkexe "Foo"]
, mkpkg "other" [ mkexe "other" `withModules` ["Foo"]
, mkexe "other2" `withCFiles` ["Foo"] ]
]
-- packages shadow non-local components
reportSubCase "unambiguous: pkg vs non-cwd comp"
assertUnambiguous "foo"
(mkTargetPackage "foo")
[ mkpkg "foo" []
, mkpkgAt "other" [mkexe "foo"] "other" -- shadows non-local foo
]
-- packages shadow modules and files
reportSubCase "unambiguous: pkg vs module, file"
assertUnambiguous "Foo"
(mkTargetPackage "Foo")
[ mkpkgAt "Foo" [] "foo"
, mkpkg "other" [ mkexe "other" `withModules` ["Foo"]
, mkexe "other2" `withCFiles` ["Foo"] ]
]
-- non-exact case packages and components are ambiguous
reportSubCase "ambiguous: non-exact-case pkg names"
assertAmbiguous "Foo"
[ mkTargetPackage "foo", mkTargetPackage "FOO" ]
[ mkpkg "foo" [], mkpkg "FOO" [] ]
reportSubCase "ambiguous: non-exact-case comp names"
assertAmbiguous "Foo"
[ mkTargetComponent "bar" (CExeName "foo")
, mkTargetComponent "bar" (CExeName "FOO") ]
[ mkpkg "bar" [mkexe "foo", mkexe "FOO"] ]
where
assertAmbiguous :: String
-> [TargetSelector]
-> [SourcePackage (PackageLocation a)]
-> Assertion
assertAmbiguous str tss pkgs = do
res <- readTargetSelectorsWith
fakeDirActions
(map SpecificSourcePackage pkgs)
Nothing
[str]
case res of
Left [TargetSelectorAmbiguous _ tss'] ->
sort (map snd tss') @?= sort tss
_ -> assertFailure $ "expected Left [TargetSelectorAmbiguous _ _], "
++ "got " ++ show res
assertUnambiguous :: String
-> TargetSelector
-> [SourcePackage (PackageLocation a)]
-> Assertion
assertUnambiguous str ts pkgs = do
res <- readTargetSelectorsWith
fakeDirActions
(map SpecificSourcePackage pkgs)
Nothing
[str]
case res of
Right [ts'] -> ts' @?= ts
_ -> assertFailure $ "expected Right [Target...], "
++ "got " ++ show res
fakeDirActions = TS.DirActions {
TS.doesFileExist = \_p -> return True,
TS.doesDirectoryExist = \_p -> return True,
TS.canonicalizePath = \p -> return ("/" </> p), -- FilePath.Unix.</> ?
TS.getCurrentDirectory = return "/"
}
mkpkg :: String -> [Executable] -> SourcePackage (PackageLocation a)
mkpkg pkgidstr exes = mkpkgAt pkgidstr exes ""
mkpkgAt :: String -> [Executable] -> FilePath
-> SourcePackage (PackageLocation a)
mkpkgAt pkgidstr exes loc =
SourcePackage {
srcpkgPackageId = pkgid,
srcpkgSource = LocalUnpackedPackage loc,
srcpkgDescrOverride = Nothing,
srcpkgDescription = GenericPackageDescription {
packageDescription = emptyPackageDescription { package = pkgid },
gpdScannedVersion = Nothing,
genPackageFlags = [],
condLibrary = Nothing,
condSubLibraries = [],
condForeignLibs = [],
condExecutables = [ ( exeName exe, CondNode exe [] [] )
| exe <- exes ],
condTestSuites = [],
condBenchmarks = []
}
}
where
pkgid = fromMaybe (error $ "failed to parse " ++ pkgidstr) $ simpleParse pkgidstr
mkexe :: String -> Executable
mkexe name = mempty { exeName = fromString name }
withModules :: Executable -> [String] -> Executable
withModules exe mods =
exe { buildInfo = (buildInfo exe) { otherModules = map fromString mods } }
withCFiles :: Executable -> [FilePath] -> Executable
withCFiles exe files =
exe { buildInfo = (buildInfo exe) { cSources = files } }
mkTargetPackage :: PackageId -> TargetSelector
mkTargetPackage pkgid =
TargetPackage TargetExplicitNamed [pkgid] Nothing
mkTargetComponent :: PackageId -> ComponentName -> TargetSelector
mkTargetComponent pkgid cname =
TargetComponent pkgid cname
mkTargetAllPackages :: TargetSelector
mkTargetAllPackages = TargetAllPackages Nothing
instance IsString PackageIdentifier where
fromString pkgidstr = pkgid
where pkgid = fromMaybe (error $ "fromString @PackageIdentifier " ++ show pkgidstr) $ simpleParse pkgidstr
testTargetSelectorNoCurrentPackage :: Assertion
testTargetSelectorNoCurrentPackage = do
(_, _, _, localPackages, _) <- configureProject testdir config
let readTargetSelectors' = readTargetSelectorsWith (dirActions testdir)
localPackages
Nothing
targets = [ "libs", ":cwd:libs"
, "flibs", ":cwd:flibs"
, "exes", ":cwd:exes"
, "tests", ":cwd:tests"
, "benchmarks", ":cwd:benchmarks"]
Left errs <- readTargetSelectors' targets
zipWithM_ (@?=) errs
[ TargetSelectorNoCurrentPackage ts
| target <- targets
, let ts = fromMaybe (error $ "failed to parse target string " ++ target) $ parseTargetString target
]
cleanProject testdir
where
testdir = "targets/complex"
config = mempty
testTargetSelectorNoTargets :: Assertion
testTargetSelectorNoTargets = do
(_, _, _, localPackages, _) <- configureProject testdir config
Left errs <- readTargetSelectors localPackages Nothing []
errs @?= [TargetSelectorNoTargetsInCwd True]
cleanProject testdir
where
testdir = "targets/complex"
config = mempty
testTargetSelectorProjectEmpty :: Assertion
testTargetSelectorProjectEmpty = do
(_, _, _, localPackages, _) <- configureProject testdir config
Left errs <- readTargetSelectors localPackages Nothing []
errs @?= [TargetSelectorNoTargetsInProject]
cleanProject testdir
where
testdir = "targets/empty"
config = mempty
-- | Ensure we don't miss primary package and produce
-- TargetSelectorNoTargetsInCwd error due to symlink or
-- drive capitalisation mismatch when no targets are given
testTargetSelectorCanonicalizedPath :: Assertion
testTargetSelectorCanonicalizedPath = do
(_, _, _, localPackages, _) <- configureProject testdir config
cwd <- getCurrentDirectory
let virtcwd = cwd </> basedir </> symlink
-- Check that the symlink is there before running test as on Windows
-- some versions/configurations of git won't pull down/create the symlink
canRunTest <- doesDirectoryExist virtcwd
when canRunTest (do
let dirActions' = (dirActions symlink) { TS.getCurrentDirectory = return virtcwd }
Right ts <- readTargetSelectorsWith dirActions' localPackages Nothing []
ts @?= [TargetPackage TargetImplicitCwd ["p-0.1"] Nothing])
cleanProject testdir
where
testdir = "targets/simple"
symlink = "targets/symbolic-link-to-simple"
config = mempty
testTargetProblemsCommon :: ProjectConfig -> Assertion
testTargetProblemsCommon config0 = do
(_,elaboratedPlan,_) <- planProject testdir config
let pkgIdMap :: Map.Map PackageName PackageId
pkgIdMap = Map.fromList
[ (packageName p, packageId p)
| p <- InstallPlan.toList elaboratedPlan ]
cases :: [( TargetSelector -> TargetProblem'
, TargetSelector
)]
cases =
[ -- Cannot resolve packages outside of the project
( \_ -> TargetProblemNoSuchPackage "foobar"
, mkTargetPackage "foobar" )
-- We cannot currently build components like testsuites or
-- benchmarks from packages that are not local to the project
, ( \_ -> TargetComponentNotProjectLocal
(pkgIdMap Map.! "filepath") (CTestName "filepath-tests")
, mkTargetComponent (pkgIdMap Map.! "filepath")
(CTestName "filepath-tests") )
-- Components can be explicitly @buildable: False@
, ( \_ -> TargetComponentNotBuildable "q-0.1" (CExeName "buildable-false")
, mkTargetComponent "q-0.1" (CExeName "buildable-false") )
-- Testsuites and benchmarks can be disabled by the solver if it
-- cannot satisfy deps
, ( \_ -> TargetOptionalStanzaDisabledBySolver "q-0.1" (CTestName "solver-disabled")
, mkTargetComponent "q-0.1" (CTestName "solver-disabled") )
-- Testsuites and benchmarks can be disabled explicitly by the
-- user via config
, ( \_ -> TargetOptionalStanzaDisabledByUser
"q-0.1" (CBenchName "user-disabled")
, mkTargetComponent "q-0.1" (CBenchName "user-disabled") )
-- An unknown package. The target selector resolution should only
-- produce known packages, so this should not happen with the
-- output from 'readTargetSelectors'.
, ( \_ -> TargetProblemNoSuchPackage "foobar"
, mkTargetPackage "foobar" )
-- An unknown component of a known package. The target selector
-- resolution should only produce known packages, so this should
-- not happen with the output from 'readTargetSelectors'.
, ( \_ -> TargetProblemNoSuchComponent "q-0.1" (CExeName "no-such")
, mkTargetComponent "q-0.1" (CExeName "no-such") )
]
assertTargetProblems
elaboratedPlan
CmdBuild.selectPackageTargets
CmdBuild.selectComponentTarget
cases
where
testdir = "targets/complex"
config = config0 {
projectConfigLocalPackages = (projectConfigLocalPackages config0) {
packageConfigBenchmarks = toFlag False
}
, projectConfigShared = (projectConfigShared config0) {
projectConfigConstraints =
[( UserConstraint (UserAnyQualifier "filepath") PackagePropertySource
, ConstraintSourceUnknown )]
}
}
testTargetProblemsBuild :: ProjectConfig -> (String -> IO ()) -> Assertion
testTargetProblemsBuild config reportSubCase = do
reportSubCase "empty-pkg"
assertProjectTargetProblems
"targets/empty-pkg" config
CmdBuild.selectPackageTargets
CmdBuild.selectComponentTarget
[ ( TargetProblemNoTargets, mkTargetPackage "p-0.1" )
]
reportSubCase "all-disabled"
assertProjectTargetProblems
"targets/all-disabled"
config {
projectConfigLocalPackages = (projectConfigLocalPackages config) {
packageConfigBenchmarks = toFlag False
}
}
CmdBuild.selectPackageTargets
CmdBuild.selectComponentTarget
[ ( flip TargetProblemNoneEnabled
[ AvailableTarget "p-0.1" (CBenchName "user-disabled")
TargetDisabledByUser True
, AvailableTarget "p-0.1" (CTestName "solver-disabled")
TargetDisabledBySolver True
, AvailableTarget "p-0.1" (CExeName "buildable-false")
TargetNotBuildable True
, AvailableTarget "p-0.1" (CLibName LMainLibName)
TargetNotBuildable True
]
, mkTargetPackage "p-0.1" )
]
reportSubCase "enabled component kinds"
-- When we explicitly enable all the component kinds then selecting the
-- whole package selects those component kinds too
do (_,elaboratedPlan,_) <- planProject "targets/variety" config {
projectConfigLocalPackages = (projectConfigLocalPackages config) {
packageConfigTests = toFlag True,
packageConfigBenchmarks = toFlag True
}
}
assertProjectDistinctTargets
elaboratedPlan
CmdBuild.selectPackageTargets
CmdBuild.selectComponentTarget
[ mkTargetPackage "p-0.1" ]
[ ("p-0.1-inplace", (CLibName LMainLibName))
, ("p-0.1-inplace-a-benchmark", CBenchName "a-benchmark")
, ("p-0.1-inplace-a-testsuite", CTestName "a-testsuite")
, ("p-0.1-inplace-an-exe", CExeName "an-exe")
, ("p-0.1-inplace-libp", CFLibName "libp")
]
reportSubCase "disabled component kinds"
-- When we explicitly disable all the component kinds then selecting the
-- whole package only selects the library, foreign lib and exes
do (_,elaboratedPlan,_) <- planProject "targets/variety" config {
projectConfigLocalPackages = (projectConfigLocalPackages config) {
packageConfigTests = toFlag False,
packageConfigBenchmarks = toFlag False
}
}
assertProjectDistinctTargets
elaboratedPlan
CmdBuild.selectPackageTargets
CmdBuild.selectComponentTarget
[ mkTargetPackage "p-0.1" ]
[ ("p-0.1-inplace", (CLibName LMainLibName))
, ("p-0.1-inplace-an-exe", CExeName "an-exe")
, ("p-0.1-inplace-libp", CFLibName "libp")
]
reportSubCase "requested component kinds"
-- When we selecting the package with an explicit filter then we get those
-- components even though we did not explicitly enable tests/benchmarks
do (_,elaboratedPlan,_) <- planProject "targets/variety" config
assertProjectDistinctTargets
elaboratedPlan
CmdBuild.selectPackageTargets
CmdBuild.selectComponentTarget
[ TargetPackage TargetExplicitNamed ["p-0.1"] (Just TestKind)
, TargetPackage TargetExplicitNamed ["p-0.1"] (Just BenchKind)
]
[ ("p-0.1-inplace-a-benchmark", CBenchName "a-benchmark")
, ("p-0.1-inplace-a-testsuite", CTestName "a-testsuite")
]
testTargetProblemsRepl :: ProjectConfig -> (String -> IO ()) -> Assertion
testTargetProblemsRepl config reportSubCase = do
reportSubCase "multiple-libs"
assertProjectTargetProblems
"targets/multiple-libs" config
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ ( flip (CmdRepl.matchesMultipleProblem (CmdRepl.MultiReplDecision Nothing False))
[ AvailableTarget "p-0.1" (CLibName LMainLibName)
(TargetBuildable () TargetRequestedByDefault) True
, AvailableTarget "q-0.1" (CLibName LMainLibName)
(TargetBuildable () TargetRequestedByDefault) True
]
, mkTargetAllPackages )
]
reportSubCase "multiple-exes"
assertProjectTargetProblems
"targets/multiple-exes" config
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ ( flip (CmdRepl.matchesMultipleProblem (CmdRepl.MultiReplDecision Nothing False))
[ AvailableTarget "p-0.1" (CExeName "p2")
(TargetBuildable () TargetRequestedByDefault) True
, AvailableTarget "p-0.1" (CExeName "p1")
(TargetBuildable () TargetRequestedByDefault) True
]
, mkTargetPackage "p-0.1" )
]
reportSubCase "multiple-tests"
assertProjectTargetProblems
"targets/multiple-tests" config
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ ( flip (CmdRepl.matchesMultipleProblem (CmdRepl.MultiReplDecision Nothing False))
[ AvailableTarget "p-0.1" (CTestName "p2")
(TargetBuildable () TargetNotRequestedByDefault) True
, AvailableTarget "p-0.1" (CTestName "p1")
(TargetBuildable () TargetNotRequestedByDefault) True
]
, TargetPackage TargetExplicitNamed ["p-0.1"] (Just TestKind) )
]
reportSubCase "multiple targets"
do (_,elaboratedPlan,_) <- planProject "targets/multiple-exes" config
assertProjectDistinctTargets
elaboratedPlan
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ mkTargetComponent "p-0.1" (CExeName "p1")
, mkTargetComponent "p-0.1" (CExeName "p2")
]
[ ("p-0.1-inplace-p1", CExeName "p1")
, ("p-0.1-inplace-p2", CExeName "p2")
]
reportSubCase "libs-disabled"
assertProjectTargetProblems
"targets/libs-disabled" config
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ ( flip TargetProblemNoneEnabled
[ AvailableTarget "p-0.1" (CLibName LMainLibName) TargetNotBuildable True ]
, mkTargetPackage "p-0.1" )
]
reportSubCase "exes-disabled"
assertProjectTargetProblems
"targets/exes-disabled" config
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ ( flip TargetProblemNoneEnabled
[ AvailableTarget "p-0.1" (CExeName "p") TargetNotBuildable True
]
, mkTargetPackage "p-0.1" )
]
reportSubCase "test-only"
assertProjectTargetProblems
"targets/test-only" config
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ ( flip TargetProblemNoneEnabled
[ AvailableTarget "p-0.1" (CTestName "pexe")
(TargetBuildable () TargetNotRequestedByDefault) True
]
, mkTargetPackage "p-0.1" )
]
reportSubCase "empty-pkg"
assertProjectTargetProblems
"targets/empty-pkg" config
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ ( TargetProblemNoTargets, mkTargetPackage "p-0.1" )
]
reportSubCase "requested component kinds"
do (_,elaboratedPlan,_) <- planProject "targets/variety" config
-- by default we only get the lib
assertProjectDistinctTargets
elaboratedPlan
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ TargetPackage TargetExplicitNamed ["p-0.1"] Nothing ]
[ ("p-0.1-inplace", (CLibName LMainLibName)) ]
-- When we select the package with an explicit filter then we get those
-- components even though we did not explicitly enable tests/benchmarks
assertProjectDistinctTargets
elaboratedPlan
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ TargetPackage TargetExplicitNamed ["p-0.1"] (Just TestKind) ]
[ ("p-0.1-inplace-a-testsuite", CTestName "a-testsuite") ]
assertProjectDistinctTargets
elaboratedPlan
(CmdRepl.selectPackageTargets (CmdRepl.MultiReplDecision Nothing False))
CmdRepl.selectComponentTarget
[ TargetPackage TargetExplicitNamed ["p-0.1"] (Just BenchKind) ]
[ ("p-0.1-inplace-a-benchmark", CBenchName "a-benchmark") ]
testTargetProblemsListBin :: ProjectConfig -> (String -> IO ()) -> Assertion
testTargetProblemsListBin config reportSubCase = do
reportSubCase "one-of-each"
do (_,elaboratedPlan,_) <- planProject "targets/one-of-each" config
assertProjectDistinctTargets
elaboratedPlan
CmdListBin.selectPackageTargets
CmdListBin.selectComponentTarget
[ TargetPackage TargetExplicitNamed ["p-0.1"] Nothing
]
[ ("p-0.1-inplace-p1", CExeName "p1")
]
reportSubCase "multiple-exes"
assertProjectTargetProblems
"targets/multiple-exes" config
CmdListBin.selectPackageTargets
CmdListBin.selectComponentTarget
[ ( flip CmdListBin.matchesMultipleProblem
[ AvailableTarget "p-0.1" (CExeName "p2")
(TargetBuildable () TargetRequestedByDefault) True
, AvailableTarget "p-0.1" (CExeName "p1")
(TargetBuildable () TargetRequestedByDefault) True
]
, mkTargetPackage "p-0.1" )
]
reportSubCase "multiple targets"
do (_,elaboratedPlan,_) <- planProject "targets/multiple-exes" config
assertProjectDistinctTargets
elaboratedPlan
CmdListBin.selectPackageTargets
CmdListBin.selectComponentTarget
[ mkTargetComponent "p-0.1" (CExeName "p1")
, mkTargetComponent "p-0.1" (CExeName "p2")
]
[ ("p-0.1-inplace-p1", CExeName "p1")
, ("p-0.1-inplace-p2", CExeName "p2")
]
reportSubCase "exes-disabled"
assertProjectTargetProblems
"targets/exes-disabled" config
CmdListBin.selectPackageTargets
CmdListBin.selectComponentTarget
[ ( flip TargetProblemNoneEnabled
[ AvailableTarget "p-0.1" (CExeName "p") TargetNotBuildable True
]
, mkTargetPackage "p-0.1" )
]
reportSubCase "empty-pkg"
assertProjectTargetProblems
"targets/empty-pkg" config
CmdListBin.selectPackageTargets
CmdListBin.selectComponentTarget
[ ( TargetProblemNoTargets, mkTargetPackage "p-0.1" )
]
reportSubCase "lib-only"
assertProjectTargetProblems
"targets/lib-only" config
CmdListBin.selectPackageTargets
CmdListBin.selectComponentTarget
[ (CmdListBin.noComponentsProblem, mkTargetPackage "p-0.1" )
]
testTargetProblemsRun :: ProjectConfig -> (String -> IO ()) -> Assertion
testTargetProblemsRun config reportSubCase = do
reportSubCase "one-of-each"
do (_,elaboratedPlan,_) <- planProject "targets/one-of-each" config
assertProjectDistinctTargets
elaboratedPlan
CmdRun.selectPackageTargets
CmdRun.selectComponentTarget
[ TargetPackage TargetExplicitNamed ["p-0.1"] Nothing
]
[ ("p-0.1-inplace-p1", CExeName "p1")
]
reportSubCase "multiple-exes"
assertProjectTargetProblems
"targets/multiple-exes" config
CmdRun.selectPackageTargets
CmdRun.selectComponentTarget
[ ( flip CmdRun.matchesMultipleProblem
[ AvailableTarget "p-0.1" (CExeName "p2")
(TargetBuildable () TargetRequestedByDefault) True
, AvailableTarget "p-0.1" (CExeName "p1")
(TargetBuildable () TargetRequestedByDefault) True
]
, mkTargetPackage "p-0.1" )
]
reportSubCase "multiple targets"
do (_,elaboratedPlan,_) <- planProject "targets/multiple-exes" config
assertProjectDistinctTargets
elaboratedPlan
CmdRun.selectPackageTargets
CmdRun.selectComponentTarget
[ mkTargetComponent "p-0.1" (CExeName "p1")
, mkTargetComponent "p-0.1" (CExeName "p2")
]
[ ("p-0.1-inplace-p1", CExeName "p1")
, ("p-0.1-inplace-p2", CExeName "p2")
]
reportSubCase "exes-disabled"
assertProjectTargetProblems
"targets/exes-disabled" config
CmdRun.selectPackageTargets
CmdRun.selectComponentTarget
[ ( flip TargetProblemNoneEnabled
[ AvailableTarget "p-0.1" (CExeName "p") TargetNotBuildable True
]
, mkTargetPackage "p-0.1" )
]
reportSubCase "empty-pkg"
assertProjectTargetProblems
"targets/empty-pkg" config
CmdRun.selectPackageTargets
CmdRun.selectComponentTarget
[ ( TargetProblemNoTargets, mkTargetPackage "p-0.1" )
]
reportSubCase "lib-only"
assertProjectTargetProblems
"targets/lib-only" config
CmdRun.selectPackageTargets
CmdRun.selectComponentTarget
[ (CmdRun.noExesProblem, mkTargetPackage "p-0.1" )
]
testTargetProblemsTest :: ProjectConfig -> (String -> IO ()) -> Assertion
testTargetProblemsTest config reportSubCase = do
reportSubCase "disabled by config"
assertProjectTargetProblems
"targets/tests-disabled"
config {
projectConfigLocalPackages = (projectConfigLocalPackages config) {
packageConfigTests = toFlag False
}
}
CmdTest.selectPackageTargets
CmdTest.selectComponentTarget
[ ( flip TargetProblemNoneEnabled
[ AvailableTarget "p-0.1" (CTestName "user-disabled")
TargetDisabledByUser True
, AvailableTarget "p-0.1" (CTestName "solver-disabled")
TargetDisabledByUser True
]
, mkTargetPackage "p-0.1" )
]
reportSubCase "disabled by solver & buildable false"
assertProjectTargetProblems
"targets/tests-disabled"
config
CmdTest.selectPackageTargets
CmdTest.selectComponentTarget
[ ( flip TargetProblemNoneEnabled
[ AvailableTarget "p-0.1" (CTestName "user-disabled")
TargetDisabledBySolver True
, AvailableTarget "p-0.1" (CTestName "solver-disabled")
TargetDisabledBySolver True
]
, mkTargetPackage "p-0.1" )
, ( flip TargetProblemNoneEnabled
[ AvailableTarget "q-0.1" (CTestName "buildable-false")
TargetNotBuildable True
]
, mkTargetPackage "q-0.1" )
]
reportSubCase "empty-pkg"
assertProjectTargetProblems
"targets/empty-pkg" config
CmdTest.selectPackageTargets
CmdTest.selectComponentTarget
[ ( TargetProblemNoTargets, mkTargetPackage "p-0.1" )
]
reportSubCase "no tests"
assertProjectTargetProblems
"targets/simple"
config
CmdTest.selectPackageTargets
CmdTest.selectComponentTarget
[ ( CmdTest.noTestsProblem, mkTargetPackage "p-0.1" )
, ( CmdTest.noTestsProblem, mkTargetPackage "q-0.1" )
]
reportSubCase "not a test"
assertProjectTargetProblems
"targets/variety"
config
CmdTest.selectPackageTargets
CmdTest.selectComponentTarget $
[ ( const (CmdTest.notTestProblem
"p-0.1" (CLibName LMainLibName))
, mkTargetComponent "p-0.1" (CLibName LMainLibName) )
, ( const (CmdTest.notTestProblem
"p-0.1" (CExeName "an-exe"))