forked from haskell/cabal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixup haskell#9614: make config file extraction from help text more r…
…obust (haskell#9667) * Fixup haskell#9614: make config file extraction from help text more robust The previous test (PR haskell#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 haskell#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
Showing
4 changed files
with
46 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# cabal --help | ||
# cabal --help |
53 changes: 40 additions & 13 deletions
53
cabal-testsuite/PackageTests/Help/HelpPrintsConfigFile/help-prints-config-file.test.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters