@@ -49,7 +49,9 @@ import Distribution.Package
4949import Distribution.PackageDescription
5050import Distribution.InstalledPackageInfo (InstalledPackageInfo )
5151import Distribution.Simple.Setup (toFlag , HaddockFlags (.. ), defaultHaddockFlags )
52+ import Distribution.Client.Setup (globalCommand )
5253import Distribution.Simple.Compiler
54+ import Distribution.Simple.Command
5355import Distribution.System
5456import Distribution.Version
5557import Distribution.ModuleName (ModuleName )
@@ -71,6 +73,9 @@ import Test.Tasty.Options
7173import Data.Tagged (Tagged (.. ))
7274
7375import qualified Data.ByteString as BS
76+ import Distribution.Client.GlobalFlags (GlobalFlags , globalNix )
77+ import Distribution.Simple.Flag (Flag (Flag , NoFlag ))
78+ import Data.Maybe (fromJust )
7479
7580#if !MIN_VERSION_directory(1,2,7)
7681removePathForcibly :: FilePath -> IO ()
@@ -139,9 +144,12 @@ tests config =
139144 , testCase " program options scope local" (testProgramOptionsLocal config)
140145 , testCase " program options scope specific" (testProgramOptionsSpecific config)
141146 ]
147+ , testGroup " Flag tests" $
148+ [
149+ testCase " Test Nix Flag" testNixFlags
150+ ]
142151 ]
143152
144-
145153testFindProjectRoot :: Assertion
146154testFindProjectRoot = do
147155 Left (BadProjectRootExplicitFile file) <- findProjectRoot (Just testdir)
@@ -1927,3 +1935,25 @@ tryFewTimes action = go (3 :: Int) where
19271935 hPutStrLn stderr $ " Trying " ++ show n ++ " after " ++ show e
19281936 threadDelay 10000
19291937 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