Merge pull request #10709 from Kleidukos/cabal-3.14.1.1-release-notes
[cabal.git] / cabal-testsuite / PackageTests / LinkerOptions / NonignoredConfigs / cabal.test.hs
blob55ec5ee614403cb2f8f0ca826ae00e15b3f03be1
1 import Test.Cabal.Prelude
2 import Distribution.Simple.Utils
4 -- This test ensures the following fix holds:
5 -- > Fix project-local build flags being ignored.
6 -- >
7 -- > I noticed that running ‘cabal install’ with two separate sets of dynamic /
8 -- > static build flags (e.g. one with none, and one with ‘--enable-shared
9 -- > --enable-executable-dynamic --disable-library-vanilla’) produced packages with
10 -- > the same hash, instead of different hashes.
11 -- >
12 -- > After debugging this issue I found that this command (with no explicit cabal
13 -- > project file) was resulting in these build configuration flags being ignored,
14 -- > because in ProjectPlanning.hs, the sdist was not considered a local package, so
15 -- > the (non-shared) local-package-only configuration was being dropped.
16 -- >
17 -- > This fix ensures that these command-line arguments properly make it through to
18 -- > where they belong in cases like this.
20 -- Basically, take a simple package, build it under two sets of build flags:
21 -- > (nothing)
22 -- > --enable-shared --enable-executable-dynamic --disable-library-vanilla
24 -- And ensure that whereas before they produced the same hash, now the package
25 -- hashes produced are different. (And also supplementarily ensure that
26 -- re-running the same build with the same flags a second time produces a
27 -- deterministic hash too; the hash should differ only when we change these
28 -- flags.)
30 -- Based on the UniqueIPID test.
32 import Control.Monad (forM, foldM_)
33 import Data.List (isPrefixOf, tails)
35 data Linking = Static | Dynamic deriving (Eq, Ord, Show)
37 links :: [Linking]
38 links = [Static, Dynamic]
40 linkConfigFlags :: Linking -> [String]
41 linkConfigFlags Static =
44 linkConfigFlags Dynamic =
46 "--enable-shared",
47 "--enable-executable-dynamic",
48 "--disable-library-vanilla"
51 lrun :: [Linking]
52 lrun = [Static, Dynamic, Static, Dynamic]
54 main = cabalTest $ do
55 skipIfNoSharedLibraries
56 env <- getTestEnv
57 withPackageDb $ do
58 -- Phase 1: get 4 hashes according to config flags.
59 results <- forM (zip [0..] lrun) $ \(idx, linking) -> do
60 liftIO $ copyDirectoryRecursive minBound (testCurrentDir env </> "basic") (testCurrentDir env </> "basic" ++ show idx)
61 withDirectory ("basic" ++ show idx) $ do
62 packageEnv <- (</> ("basic" ++ show idx ++ ".env")) . testWorkDir <$> getTestEnv
63 let installOptions = ["--disable-deterministic", "--lib", "--package-env=" ++ packageEnv] ++ linkConfigFlags linking ++ ["basic"]
64 recordMode RecordMarked $ do
65 recordHeader $ "install options:" : installOptions
66 cabal "v2-install" installOptions
67 recordHeader $ "install options:" : installOptions
68 fails $ cabal "v2-install" installOptions
69 recordHeader $ "install options:" : "--force-reinstalls" : installOptions
70 cabal "v2-install" $ "--force-reinstalls" : installOptions
71 let exIPID s = takeWhile (/= '\n') . head . filter (\t -> any (`isPrefixOf` t) ["basic-0.1-", "bsc-0.1-"]) $ tails s
72 hashedIpid <- exIPID <$> liftIO (readFile packageEnv)
73 return $ ((idx, linking), hashedIpid)
74 -- Phase 2: make sure we have different hashes iff we have different config flags.
75 -- In particular make sure the dynamic config flags weren't silently
76 -- dropped and ignored, since this is the bug that prompted this test.
77 (\step -> foldM_ step (const $ return ()) results) $ \acc x -> do
78 acc x
79 return $ \future -> acc future >> do
80 let
81 ((thisIdx, thisLinking), thisHashedIpid) = x
82 ((futureIdx, futureLinking), futureHashedIpid) = future
83 when ((thisHashedIpid == futureHashedIpid) /= (thisLinking == futureLinking)) $ do
84 assertFailure . unlines $
85 if thisLinking /= futureLinking
86 then
87 -- What we are primarily concerned with testing
88 -- here.
90 "Error: static and dynamic config flags produced an IPID with the same hash; were the dynamic flags silently dropped?",
91 "\thashed IPID: " ++ thisHashedIpid
93 else
94 -- Help test our test can also make equal
95 -- hashes.
97 "Error: config flags were equal, yet a different IPID hash was produced.",
98 "\thashed IPID 1 : " ++ thisHashedIpid,
99 "\thashed IPID 2 : " ++ futureHashedIpid,
100 "\tlinking flags : " ++ show thisLinking