diff --git a/cabal-install/src/Distribution/Client/Main.hs b/cabal-install/src/Distribution/Client/Main.hs index d7cf0ea49b4..6ef0a673717 100644 --- a/cabal-install/src/Distribution/Client/Main.hs +++ b/cabal-install/src/Distribution/Client/Main.hs @@ -397,6 +397,10 @@ mainWorker args = do pname <- getProgName configFile <- defaultConfigFile putStr (help pname) + -- Andreas Abel, 2024-01-28: https://github.com/haskell/cabal/pull/9614 + -- See cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/ + -- Third-party tools may rely on the specific wording + -- to find the config file in the help text, so do not change! putStr $ "\nYou can edit the cabal configuration file to set defaults:\n" ++ " " diff --git a/cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.out b/cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.out index 8f859e4c506..9b5b68a2db9 100644 --- a/cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.out +++ b/cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.out @@ -1 +1,2 @@ # cabal --help +# cabal --help diff --git a/cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.test.hs b/cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.test.hs index e725142bc24..171560419c3 100644 --- a/cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.test.hs +++ b/cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.test.hs @@ -1,25 +1,52 @@ --- Andreas Abel, 2024-01-13 +-- Andreas Abel, 2024-01-13, 2024-01-28 -- --- Ensure that the last line of the help text is the name of the config file. +-- Ensure that the help text prints the name of the config file, following a fixed text. -- This invariant is used by clients such as the Haskell setup github action. -- See: https://github.com/haskell-actions/setup/pull/63 +-- The end of the help text should look like: +-- +-- > You can edit the cabal configuration file to set defaults: +-- > <>/.cabal/config +-- > This file will be generated with sensible defaults if you run 'cabal update'. +-- +-- The last line is only present when the file does *not* exist. +-- +-- So while usually the last line is the name of the config file, +-- the correct way is to take the line after the fixed text "You can edit...". + import Distribution.Utils.String (trim) +import System.Directory (removeFile) import Test.Cabal.Prelude main = cabalTest $ do env <- getTestEnv - res <- cabal' "--help" [] + let configFile = testUserCabalConfigFile env - -- The end of the help text should be something like: - -- - -- > You can edit the cabal configuration file to set defaults: - -- > <>/.cabal/config - -- - -- So trimming the last line will give us the name of the config file. - let configFile = trim . last . lines . resultOutput $ res + -- Test 1: with config file present. + test configFile "Case: config file exists." - -- Verify that this is indeed the config file. - assertEqual "Last line of help text should be name of the config file" - (testUserCabalConfigFile env) + -- Test 2: with config file absent. + liftIO $ removeFile configFile + test configFile "Case: config file does not exist." + +test configFile info = do + res <- cabal' "--help" [] + assertEqual (unwords ["Help text contains name of the config file after the marker.", info]) configFile + (configFileFromHelpText $ resultOutput res) + +-- | The config file is the line following the fixed text: +-- "You can edit the cabal configuration file to set defaults:" +-- +-- If this marker is not found, return the empty string. +configFileFromHelpText :: String -> FilePath +configFileFromHelpText txt = + case found of + _marker : l : _ -> l + _ -> "" + where + marker = "You can edit the cabal configuration file to set defaults:" + (_before, found) = break (marker ==) $ map trim $ lines txt + -- Note: 'trim' lines to get rid of CR on Windows; + -- 'lines' does not seem to remove it. diff --git a/cabal-testsuite/src/Test/Cabal/Monad.hs b/cabal-testsuite/src/Test/Cabal/Monad.hs index 1f7b0237018..c1ecf3dfecb 100644 --- a/cabal-testsuite/src/Test/Cabal/Monad.hs +++ b/cabal-testsuite/src/Test/Cabal/Monad.hs @@ -330,7 +330,7 @@ runTestM mode m = withSystemTempDirectory "cabal-testsuite" $ \tmp_dir -> do -- Set CABAL_DIR in addition to HOME, since HOME has no -- effect on Windows. , ("CABAL_DIR", Just (testCabalDir env)) - , ("CABAL_CONFIG", Just $ testCabalDir env "config") + , ("CABAL_CONFIG", Just (testUserCabalConfigFile env)) ], testShouldFail = False, testRelativeCurrentDir = ".",