@@ -51,6 +51,7 @@ import Distribution.InstalledPackageInfo (InstalledPackageInfo)
51
51
import Distribution.Simple.Setup (toFlag , HaddockFlags (.. ), defaultHaddockFlags )
52
52
import Distribution.Client.Setup (globalCommand )
53
53
import Distribution.Simple.Compiler
54
+ import Distribution.Simple.Command
54
55
import Distribution.System
55
56
import Distribution.Version
56
57
import Distribution.ModuleName (ModuleName )
@@ -72,8 +73,8 @@ import Test.Tasty.Options
72
73
import Data.Tagged (Tagged (.. ))
73
74
74
75
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 ))
77
78
import Data.Maybe (fromJust )
78
79
79
80
#if !MIN_VERSION_directory(1,2,7)
@@ -96,8 +97,7 @@ tests config =
96
97
-- * normal success
97
98
-- * dry-run tests with changes
98
99
[ testGroup " Discovery and planning" $
99
- [ testCase " test nix flags" testNixFlags
100
- , testCase " find root" testFindProjectRoot
100
+ [ testCase " find root" testFindProjectRoot
101
101
, testCase " find root fail" testExceptionFindProjectRoot
102
102
, testCase " no package" (testExceptionInFindingPackage config)
103
103
, testCase " no package2" (testExceptionInFindingPackage2 config)
@@ -144,40 +144,12 @@ tests config =
144
144
, testCase " program options scope local" (testProgramOptionsLocal config)
145
145
, testCase " program options scope specific" (testProgramOptionsSpecific config)
146
146
]
147
+ , testGroup " Flag tests" $
148
+ [
149
+ testCase " Test Nix Flag" testNixFlags
150
+ ]
147
151
]
148
152
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
-
181
153
testFindProjectRoot :: Assertion
182
154
testFindProjectRoot = do
183
155
Left (BadProjectRootExplicitFile file) <- findProjectRoot (Just testdir)
@@ -1963,3 +1935,25 @@ tryFewTimes action = go (3 :: Int) where
1963
1935
hPutStrLn stderr $ " Trying " ++ show n ++ " after " ++ show e
1964
1936
threadDelay 10000
1965
1937
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