Skip to content

Commit

Permalink
Fixup #9614: make config file extraction from help text more robust (#…
Browse files Browse the repository at this point in the history
…9667)

* Fixup #9614: make config file extraction from help text more robust

The previous test (PR #9614) did not work when the config file was
absent, because then the help text would add one more line at the
end (see issue #535).

The new test looks for the exact line printed before the line with the
config file.

We test both scenarios, with config file present or absent.

* Trim lines before searching the marker to work around CRLF issues.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
andreasabel and mergify[bot] authored Jan 31, 2024
1 parent 03d7b42 commit 46a7bfc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
4 changes: 4 additions & 0 deletions cabal-install/src/Distribution/Client/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
++ " "
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# cabal --help
# cabal --help
Original file line number Diff line number Diff line change
@@ -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:
-- > <<HOME>>/.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:
-- > <<HOME>>/.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.
2 changes: 1 addition & 1 deletion cabal-testsuite/src/Test/Cabal/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ".",
Expand Down

0 comments on commit 46a7bfc

Please sign in to comment.