From 8a83b635a3f28e4de3e483f10f2c4aef43120685 Mon Sep 17 00:00:00 2001 From: Colton Clemmer Date: Sun, 20 Mar 2022 14:36:41 -0500 Subject: [PATCH 1/5] Make enble/disable nix flag easier to read --- cabal-install/src/Distribution/Client/Setup.hs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cabal-install/src/Distribution/Client/Setup.hs b/cabal-install/src/Distribution/Client/Setup.hs index daf42d11739..7bf16edb4c3 100644 --- a/cabal-install/src/Distribution/Client/Setup.hs +++ b/cabal-install/src/Distribution/Client/Setup.hs @@ -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" From 1f42579a29748cbd63ff6b93a8f67e3cb1578877 Mon Sep 17 00:00:00 2001 From: Colton Clemmer Date: Mon, 21 Mar 2022 21:36:03 -0500 Subject: [PATCH 2/5] Test WIP --- cabal-install/tests/IntegrationTests2.hs | 34 +++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/cabal-install/tests/IntegrationTests2.hs b/cabal-install/tests/IntegrationTests2.hs index 6403f14fbf3..d38f36bdaab 100644 --- a/cabal-install/tests/IntegrationTests2.hs +++ b/cabal-install/tests/IntegrationTests2.hs @@ -49,6 +49,7 @@ 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.System import Distribution.Version @@ -71,6 +72,9 @@ import Test.Tasty.Options import Data.Tagged (Tagged(..)) import qualified Data.ByteString as BS +import Distribution.Client.GlobalFlags (GlobalFlags) +import Distribution.Simple.Command +import Data.Maybe (fromJust) #if !MIN_VERSION_directory(1,2,7) removePathForcibly :: FilePath -> IO () @@ -92,7 +96,8 @@ tests config = -- * normal success -- * dry-run tests with changes [ testGroup "Discovery and planning" $ - [ testCase "find root" testFindProjectRoot + [ testCase "test nix flags" testNixFlags + , testCase "find root" testFindProjectRoot , testCase "find root fail" testExceptionFindProjectRoot , testCase "no package" (testExceptionInFindingPackage config) , testCase "no package2" (testExceptionInFindingPackage2 config) @@ -142,6 +147,33 @@ tests config = ] +testNixFlags :: Assertion +testNixFlags = do + let argsFn = commandOptions . globalCommand $ [] + let defaultFlags = commandDefaultFlags . globalCommand $ [] + let args = argsFn ShowArgs + let mNixFlag = find (\(OptionField name _) -> name == "nix") args + True @=? isJust mNixFlag -- Found the nix flag (--enable-nix, --disable-nix) + let nixFlags = optionDescr . fromJust $ mNixFlag + let enableNixFlag = findNixFlags defaultFlags True nixFlags + let disableNixFlag = findNixFlags defaultFlags False nixFlags + True @=? isJust enableNixFlag + True @=? isJust disableNixFlag + + where + findNixFlags :: GlobalFlags -> Bool -> [OptDescr GlobalFlags] -> Maybe (OptDescr GlobalFlags) + findNixFlags _ _ [] = Nothing + findNixFlags gf b [opt@(BoolOpt _ _ _ _ fn)] + | fn gf == Just b = Just opt + | otherwise = Nothing + findNixFlags gf b (opt@(BoolOpt _ _ _ _ fn):xs) + | fn gf == Just b = Just opt + | otherwise = findNixFlags gf b xs + findNixFlags gf b (opt@(ChoiceOpt [(_, _, _, fn)]):xs) + | fn gf == b = Just opt + | otherwise = findNixFlags gf b xs + findNixFlags _ _ (_:_) = Nothing + testFindProjectRoot :: Assertion testFindProjectRoot = do Left (BadProjectRootExplicitFile file) <- findProjectRoot (Just testdir) From 63546e74d589c86a190621f920167678faee236d Mon Sep 17 00:00:00 2001 From: Colton Clemmer Date: Mon, 21 Mar 2022 21:39:21 -0500 Subject: [PATCH 3/5] Add changelog file --- changelog.d/issue-8036 | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog.d/issue-8036 diff --git a/changelog.d/issue-8036 b/changelog.d/issue-8036 new file mode 100644 index 00000000000..28f6ad18678 --- /dev/null +++ b/changelog.d/issue-8036 @@ -0,0 +1,4 @@ +synopsis: Make enable/disable nix flags easier to read +packages: cabal-install +issues: #8036 +prs: #8054 \ No newline at end of file From 33bda9a220ee4a0ee9245875794591f82cebc9df Mon Sep 17 00:00:00 2001 From: Colton Clemmer Date: Wed, 23 Mar 2022 19:39:42 -0500 Subject: [PATCH 4/5] Test WIP --- cabal-install/tests/IntegrationTests2.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cabal-install/tests/IntegrationTests2.hs b/cabal-install/tests/IntegrationTests2.hs index d38f36bdaab..8ad6d77c0a9 100644 --- a/cabal-install/tests/IntegrationTests2.hs +++ b/cabal-install/tests/IntegrationTests2.hs @@ -150,8 +150,9 @@ tests config = testNixFlags :: Assertion testNixFlags = do let argsFn = commandOptions . globalCommand $ [] - let defaultFlags = commandDefaultFlags . globalCommand $ [] let args = argsFn ShowArgs + + let defaultFlags = commandDefaultFlags . globalCommand $ [] let mNixFlag = find (\(OptionField name _) -> name == "nix") args True @=? isJust mNixFlag -- Found the nix flag (--enable-nix, --disable-nix) let nixFlags = optionDescr . fromJust $ mNixFlag @@ -166,6 +167,9 @@ testNixFlags = do findNixFlags gf b [opt@(BoolOpt _ _ _ _ fn)] | fn gf == Just b = Just opt | otherwise = Nothing + findNixFlags gf b [opt@(ChoiceOpt [(_, _, _, fn)])] + | fn gf == b = Just opt + | otherwise = Nothing findNixFlags gf b (opt@(BoolOpt _ _ _ _ fn):xs) | fn gf == Just b = Just opt | otherwise = findNixFlags gf b xs From 5777bd987c786a347f6bed8eb4116c8e6273a43b Mon Sep 17 00:00:00 2001 From: Colton Clemmer Date: Wed, 23 Mar 2022 21:57:50 -0500 Subject: [PATCH 5/5] Test should pass --- cabal-install/tests/IntegrationTests2.hs | 66 +++++++++++------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/cabal-install/tests/IntegrationTests2.hs b/cabal-install/tests/IntegrationTests2.hs index 8ad6d77c0a9..43917db1852 100644 --- a/cabal-install/tests/IntegrationTests2.hs +++ b/cabal-install/tests/IntegrationTests2.hs @@ -51,6 +51,7 @@ 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) @@ -72,8 +73,8 @@ import Test.Tasty.Options import Data.Tagged (Tagged(..)) import qualified Data.ByteString as BS -import Distribution.Client.GlobalFlags (GlobalFlags) -import Distribution.Simple.Command +import Distribution.Client.GlobalFlags (GlobalFlags, globalNix) +import Distribution.Simple.Flag (Flag (Flag, NoFlag)) import Data.Maybe (fromJust) #if !MIN_VERSION_directory(1,2,7) @@ -96,8 +97,7 @@ tests config = -- * normal success -- * dry-run tests with changes [ testGroup "Discovery and planning" $ - [ testCase "test nix flags" testNixFlags - , testCase "find root" testFindProjectRoot + [ testCase "find root" testFindProjectRoot , testCase "find root fail" testExceptionFindProjectRoot , testCase "no package" (testExceptionInFindingPackage config) , testCase "no package2" (testExceptionInFindingPackage2 config) @@ -144,40 +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 + ] ] - -testNixFlags :: Assertion -testNixFlags = do - let argsFn = commandOptions . globalCommand $ [] - let args = argsFn ShowArgs - - let defaultFlags = commandDefaultFlags . globalCommand $ [] - let mNixFlag = find (\(OptionField name _) -> name == "nix") args - True @=? isJust mNixFlag -- Found the nix flag (--enable-nix, --disable-nix) - let nixFlags = optionDescr . fromJust $ mNixFlag - let enableNixFlag = findNixFlags defaultFlags True nixFlags - let disableNixFlag = findNixFlags defaultFlags False nixFlags - True @=? isJust enableNixFlag - True @=? isJust disableNixFlag - - where - findNixFlags :: GlobalFlags -> Bool -> [OptDescr GlobalFlags] -> Maybe (OptDescr GlobalFlags) - findNixFlags _ _ [] = Nothing - findNixFlags gf b [opt@(BoolOpt _ _ _ _ fn)] - | fn gf == Just b = Just opt - | otherwise = Nothing - findNixFlags gf b [opt@(ChoiceOpt [(_, _, _, fn)])] - | fn gf == b = Just opt - | otherwise = Nothing - findNixFlags gf b (opt@(BoolOpt _ _ _ _ fn):xs) - | fn gf == Just b = Just opt - | otherwise = findNixFlags gf b xs - findNixFlags gf b (opt@(ChoiceOpt [(_, _, _, fn)]):xs) - | fn gf == b = Just opt - | otherwise = findNixFlags gf b xs - findNixFlags _ _ (_:_) = Nothing - testFindProjectRoot :: Assertion testFindProjectRoot = do Left (BadProjectRootExplicitFile file) <- findProjectRoot (Just testdir) @@ -1963,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 \ No newline at end of file