Skip to content

Commit 46a7bfc

Browse files
Fixup #9614: make config file extraction from help text more robust (#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>
1 parent 03d7b42 commit 46a7bfc

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

cabal-install/src/Distribution/Client/Main.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ mainWorker args = do
397397
pname <- getProgName
398398
configFile <- defaultConfigFile
399399
putStr (help pname)
400+
-- Andreas Abel, 2024-01-28: https://github.com/haskell/cabal/pull/9614
401+
-- See cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/
402+
-- Third-party tools may rely on the specific wording
403+
-- to find the config file in the help text, so do not change!
400404
putStr $
401405
"\nYou can edit the cabal configuration file to set defaults:\n"
402406
++ " "
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
# cabal --help
2+
# cabal --help
Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
1-
-- Andreas Abel, 2024-01-13
1+
-- Andreas Abel, 2024-01-13, 2024-01-28
22
--
3-
-- Ensure that the last line of the help text is the name of the config file.
3+
-- Ensure that the help text prints the name of the config file, following a fixed text.
44
-- This invariant is used by clients such as the Haskell setup github action.
55
-- See: https://github.com/haskell-actions/setup/pull/63
66

7+
-- The end of the help text should look like:
8+
--
9+
-- > You can edit the cabal configuration file to set defaults:
10+
-- > <<HOME>>/.cabal/config
11+
-- > This file will be generated with sensible defaults if you run 'cabal update'.
12+
--
13+
-- The last line is only present when the file does *not* exist.
14+
--
15+
-- So while usually the last line is the name of the config file,
16+
-- the correct way is to take the line after the fixed text "You can edit...".
17+
718
import Distribution.Utils.String (trim)
19+
import System.Directory (removeFile)
820
import Test.Cabal.Prelude
921

1022
main = cabalTest $ do
1123
env <- getTestEnv
12-
res <- cabal' "--help" []
24+
let configFile = testUserCabalConfigFile env
1325

14-
-- The end of the help text should be something like:
15-
--
16-
-- > You can edit the cabal configuration file to set defaults:
17-
-- > <<HOME>>/.cabal/config
18-
--
19-
-- So trimming the last line will give us the name of the config file.
20-
let configFile = trim . last . lines . resultOutput $ res
26+
-- Test 1: with config file present.
27+
test configFile "Case: config file exists."
2128

22-
-- Verify that this is indeed the config file.
23-
assertEqual "Last line of help text should be name of the config file"
24-
(testUserCabalConfigFile env)
29+
-- Test 2: with config file absent.
30+
liftIO $ removeFile configFile
31+
test configFile "Case: config file does not exist."
32+
33+
test configFile info = do
34+
res <- cabal' "--help" []
35+
assertEqual (unwords ["Help text contains name of the config file after the marker.", info])
2536
configFile
37+
(configFileFromHelpText $ resultOutput res)
38+
39+
-- | The config file is the line following the fixed text:
40+
-- "You can edit the cabal configuration file to set defaults:"
41+
--
42+
-- If this marker is not found, return the empty string.
43+
configFileFromHelpText :: String -> FilePath
44+
configFileFromHelpText txt =
45+
case found of
46+
_marker : l : _ -> l
47+
_ -> ""
48+
where
49+
marker = "You can edit the cabal configuration file to set defaults:"
50+
(_before, found) = break (marker ==) $ map trim $ lines txt
51+
-- Note: 'trim' lines to get rid of CR on Windows;
52+
-- 'lines' does not seem to remove it.

cabal-testsuite/src/Test/Cabal/Monad.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ runTestM mode m = withSystemTempDirectory "cabal-testsuite" $ \tmp_dir -> do
330330
-- Set CABAL_DIR in addition to HOME, since HOME has no
331331
-- effect on Windows.
332332
, ("CABAL_DIR", Just (testCabalDir env))
333-
, ("CABAL_CONFIG", Just $ testCabalDir env </> "config")
333+
, ("CABAL_CONFIG", Just (testUserCabalConfigFile env))
334334
],
335335
testShouldFail = False,
336336
testRelativeCurrentDir = ".",

0 commit comments

Comments
 (0)