Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make enable/disable nix flag easier to read #8054

Merged
merged 5 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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