Skip to content

Commit 8e648ae

Browse files
authored
Merge pull request #8054 from Colton-Clemmer/cc/nix-help
Make enable/disable nix flag easier to read
2 parents ec6202f + 5777bd9 commit 8e648ae

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

cabal-install/src/Distribution/Client/Setup.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,14 @@ globalCommand commands = CommandUI {
357357
globalHttpTransport (\v flags -> flags { globalHttpTransport = v })
358358
(reqArgFlag "HttpTransport")
359359

360-
,option [] ["nix"]
361-
"Nix integration: run commands through nix-shell if a 'shell.nix' file exists"
362-
globalNix (\v flags -> flags { globalNix = v })
363-
(boolOpt [] [])
360+
,multiOption "nix"
361+
globalNix (\v flags -> flags { globalNix = v })
362+
[
363+
noArg (Flag True) [] ["enable-nix"]
364+
"Enable Nix integration: run commands through nix-shell if a 'shell.nix' file exists",
365+
noArg (Flag False) [] ["disable-nix"]
366+
"Disable Nix integration"
367+
]
364368

365369
,option [] ["store-dir", "storedir"]
366370
"The location of the build store"

cabal-install/tests/IntegrationTests2.hs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ import Distribution.Package
4949
import Distribution.PackageDescription
5050
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
5151
import Distribution.Simple.Setup (toFlag, HaddockFlags(..), defaultHaddockFlags)
52+
import Distribution.Client.Setup (globalCommand)
5253
import Distribution.Simple.Compiler
54+
import Distribution.Simple.Command
5355
import Distribution.System
5456
import Distribution.Version
5557
import Distribution.ModuleName (ModuleName)
@@ -71,6 +73,9 @@ import Test.Tasty.Options
7173
import Data.Tagged (Tagged(..))
7274

7375
import 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)
7681
removePathForcibly :: 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-
145153
testFindProjectRoot :: Assertion
146154
testFindProjectRoot = 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

changelog.d/issue-8036

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
synopsis: Make enable/disable nix flags easier to read
2+
packages: cabal-install
3+
issues: #8036
4+
prs: #8054

0 commit comments

Comments
 (0)