|
1 |
| --- Andreas Abel, 2024-01-13 |
| 1 | +-- Andreas Abel, 2024-01-13, 2024-01-28 |
2 | 2 | --
|
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. |
4 | 4 | -- This invariant is used by clients such as the Haskell setup github action.
|
5 | 5 | -- See: https://github.com/haskell-actions/setup/pull/63
|
6 | 6 |
|
| 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 | + |
7 | 18 | import Distribution.Utils.String (trim)
|
| 19 | +import System.Directory (removeFile) |
8 | 20 | import Test.Cabal.Prelude
|
9 | 21 |
|
10 | 22 | main = cabalTest $ do
|
11 | 23 | env <- getTestEnv
|
12 |
| - res <- cabal' "--help" [] |
| 24 | + let configFile = testUserCabalConfigFile env |
13 | 25 |
|
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." |
21 | 28 |
|
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]) |
25 | 36 | 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. |
0 commit comments