Skip to content

Commit

Permalink
Merge pull request #8054 from Colton-Clemmer/cc/nix-help
Browse files Browse the repository at this point in the history
Make enable/disable nix flag easier to read
  • Loading branch information
jneira authored Mar 24, 2022
2 parents ec6202f + 5777bd9 commit 8e648ae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
12 changes: 8 additions & 4 deletions cabal-install/src/Distribution/Client/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,14 @@ globalCommand commands = CommandUI {
globalHttpTransport (\v flags -> flags { globalHttpTransport = v })
(reqArgFlag "HttpTransport")

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

,option [] ["store-dir", "storedir"]
"The location of the build store"
Expand Down
32 changes: 31 additions & 1 deletion cabal-install/tests/IntegrationTests2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ import Distribution.Package
import Distribution.PackageDescription
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
import Distribution.Simple.Setup (toFlag, HaddockFlags(..), defaultHaddockFlags)
import Distribution.Client.Setup (globalCommand)
import Distribution.Simple.Compiler
import Distribution.Simple.Command
import Distribution.System
import Distribution.Version
import Distribution.ModuleName (ModuleName)
Expand All @@ -71,6 +73,9 @@ import Test.Tasty.Options
import Data.Tagged (Tagged(..))

import qualified Data.ByteString as BS
import Distribution.Client.GlobalFlags (GlobalFlags, globalNix)
import Distribution.Simple.Flag (Flag (Flag, NoFlag))
import Data.Maybe (fromJust)

#if !MIN_VERSION_directory(1,2,7)
removePathForcibly :: FilePath -> IO ()
Expand Down Expand Up @@ -139,9 +144,12 @@ tests config =
, testCase "program options scope local" (testProgramOptionsLocal config)
, testCase "program options scope specific" (testProgramOptionsSpecific config)
]
, testGroup "Flag tests" $
[
testCase "Test Nix Flag" testNixFlags
]
]


testFindProjectRoot :: Assertion
testFindProjectRoot = do
Left (BadProjectRootExplicitFile file) <- findProjectRoot (Just testdir)
Expand Down Expand Up @@ -1927,3 +1935,25 @@ tryFewTimes action = go (3 :: Int) where
hPutStrLn stderr $ "Trying " ++ show n ++ " after " ++ show e
threadDelay 10000
go (n - 1)

testNixFlags :: Assertion
testNixFlags = do
let gc = globalCommand []
-- changing from the v1 to v2 build command does not change whether the "--enable-nix" flag
-- sets the globalNix param of the GlobalFlags type to True even though the v2 command doesn't use it
let nixEnabledFlags = getFlags gc . commandParseArgs gc True $ ["--enable-nix", "build"]
let nixDisabledFlags = getFlags gc . commandParseArgs gc True $ ["--disable-nix", "build"]
let nixDefaultFlags = getFlags gc . commandParseArgs gc True $ ["build"]
True @=? isJust nixDefaultFlags
True @=? isJust nixEnabledFlags
True @=? isJust nixDisabledFlags
Just True @=? (fromFlag . globalNix . fromJust $ nixEnabledFlags)
Just False @=? (fromFlag . globalNix . fromJust $ nixDisabledFlags)
Nothing @=? (fromFlag . globalNix . fromJust $ nixDefaultFlags)
where
fromFlag :: Flag Bool -> Maybe Bool
fromFlag (Flag x) = Just x
fromFlag NoFlag = Nothing
getFlags :: CommandUI GlobalFlags -> CommandParse (GlobalFlags -> GlobalFlags, [String]) -> Maybe GlobalFlags
getFlags cui (CommandReadyToGo (mkflags, _)) = Just . mkflags . commandDefaultFlags $ cui
getFlags _ _ = Nothing
4 changes: 4 additions & 0 deletions changelog.d/issue-8036
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
synopsis: Make enable/disable nix flags easier to read
packages: cabal-install
issues: #8036
prs: #8054

0 comments on commit 8e648ae

Please sign in to comment.