Skip to content

Commit 5777bd9

Browse files
committed
Test should pass
1 parent 33bda9a commit 5777bd9

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

cabal-install/tests/IntegrationTests2.hs

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import Distribution.InstalledPackageInfo (InstalledPackageInfo)
5151
import Distribution.Simple.Setup (toFlag, HaddockFlags(..), defaultHaddockFlags)
5252
import Distribution.Client.Setup (globalCommand)
5353
import Distribution.Simple.Compiler
54+
import Distribution.Simple.Command
5455
import Distribution.System
5556
import Distribution.Version
5657
import Distribution.ModuleName (ModuleName)
@@ -72,8 +73,8 @@ import Test.Tasty.Options
7273
import Data.Tagged (Tagged(..))
7374

7475
import qualified Data.ByteString as BS
75-
import Distribution.Client.GlobalFlags (GlobalFlags)
76-
import Distribution.Simple.Command
76+
import Distribution.Client.GlobalFlags (GlobalFlags, globalNix)
77+
import Distribution.Simple.Flag (Flag (Flag, NoFlag))
7778
import Data.Maybe (fromJust)
7879

7980
#if !MIN_VERSION_directory(1,2,7)
@@ -96,8 +97,7 @@ tests config =
9697
-- * normal success
9798
-- * dry-run tests with changes
9899
[ testGroup "Discovery and planning" $
99-
[ testCase "test nix flags" testNixFlags
100-
, testCase "find root" testFindProjectRoot
100+
[ testCase "find root" testFindProjectRoot
101101
, testCase "find root fail" testExceptionFindProjectRoot
102102
, testCase "no package" (testExceptionInFindingPackage config)
103103
, testCase "no package2" (testExceptionInFindingPackage2 config)
@@ -144,40 +144,12 @@ tests config =
144144
, testCase "program options scope local" (testProgramOptionsLocal config)
145145
, testCase "program options scope specific" (testProgramOptionsSpecific config)
146146
]
147+
, testGroup "Flag tests" $
148+
[
149+
testCase "Test Nix Flag" testNixFlags
150+
]
147151
]
148152

149-
150-
testNixFlags :: Assertion
151-
testNixFlags = do
152-
let argsFn = commandOptions . globalCommand $ []
153-
let args = argsFn ShowArgs
154-
155-
let defaultFlags = commandDefaultFlags . globalCommand $ []
156-
let mNixFlag = find (\(OptionField name _) -> name == "nix") args
157-
True @=? isJust mNixFlag -- Found the nix flag (--enable-nix, --disable-nix)
158-
let nixFlags = optionDescr . fromJust $ mNixFlag
159-
let enableNixFlag = findNixFlags defaultFlags True nixFlags
160-
let disableNixFlag = findNixFlags defaultFlags False nixFlags
161-
True @=? isJust enableNixFlag
162-
True @=? isJust disableNixFlag
163-
164-
where
165-
findNixFlags :: GlobalFlags -> Bool -> [OptDescr GlobalFlags] -> Maybe (OptDescr GlobalFlags)
166-
findNixFlags _ _ [] = Nothing
167-
findNixFlags gf b [opt@(BoolOpt _ _ _ _ fn)]
168-
| fn gf == Just b = Just opt
169-
| otherwise = Nothing
170-
findNixFlags gf b [opt@(ChoiceOpt [(_, _, _, fn)])]
171-
| fn gf == b = Just opt
172-
| otherwise = Nothing
173-
findNixFlags gf b (opt@(BoolOpt _ _ _ _ fn):xs)
174-
| fn gf == Just b = Just opt
175-
| otherwise = findNixFlags gf b xs
176-
findNixFlags gf b (opt@(ChoiceOpt [(_, _, _, fn)]):xs)
177-
| fn gf == b = Just opt
178-
| otherwise = findNixFlags gf b xs
179-
findNixFlags _ _ (_:_) = Nothing
180-
181153
testFindProjectRoot :: Assertion
182154
testFindProjectRoot = do
183155
Left (BadProjectRootExplicitFile file) <- findProjectRoot (Just testdir)
@@ -1963,3 +1935,25 @@ tryFewTimes action = go (3 :: Int) where
19631935
hPutStrLn stderr $ "Trying " ++ show n ++ " after " ++ show e
19641936
threadDelay 10000
19651937
go (n - 1)
1938+
1939+
testNixFlags :: Assertion
1940+
testNixFlags = do
1941+
let gc = globalCommand []
1942+
-- changing from the v1 to v2 build command does not change whether the "--enable-nix" flag
1943+
-- sets the globalNix param of the GlobalFlags type to True even though the v2 command doesn't use it
1944+
let nixEnabledFlags = getFlags gc . commandParseArgs gc True $ ["--enable-nix", "build"]
1945+
let nixDisabledFlags = getFlags gc . commandParseArgs gc True $ ["--disable-nix", "build"]
1946+
let nixDefaultFlags = getFlags gc . commandParseArgs gc True $ ["build"]
1947+
True @=? isJust nixDefaultFlags
1948+
True @=? isJust nixEnabledFlags
1949+
True @=? isJust nixDisabledFlags
1950+
Just True @=? (fromFlag . globalNix . fromJust $ nixEnabledFlags)
1951+
Just False @=? (fromFlag . globalNix . fromJust $ nixDisabledFlags)
1952+
Nothing @=? (fromFlag . globalNix . fromJust $ nixDefaultFlags)
1953+
where
1954+
fromFlag :: Flag Bool -> Maybe Bool
1955+
fromFlag (Flag x) = Just x
1956+
fromFlag NoFlag = Nothing
1957+
getFlags :: CommandUI GlobalFlags -> CommandParse (GlobalFlags -> GlobalFlags, [String]) -> Maybe GlobalFlags
1958+
getFlags cui (CommandReadyToGo (mkflags, _)) = Just . mkflags . commandDefaultFlags $ cui
1959+
getFlags _ _ = Nothing

0 commit comments

Comments
 (0)