Skip to content

Commit

Permalink
Add readVerbatimFile
Browse files Browse the repository at this point in the history
- Have readVerbatimFile read contents strictly
  • Loading branch information
philderbeast committed Jan 1, 2025
1 parent d2c6a43 commit 10bb996
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import System.Directory

main = cabalTest . recordMode RecordMarked $ do
let log = recordHeader . pure
cwd <- liftIO getCurrentDirectory
env <- getTestEnv
let testDir = testCurrentDir env
liftIO . putStrLn $ "Current working directory: " ++ cwd
msg <- liftIO . readFile $ testDir </> "msg.txt"

outElse <- fails $ cabal' "v2-build" [ "all", "--dry-run", "--project-file=else.project" ]

msg <- readVerbatimFile "msg.expect.txt"
let msgSingle = lineBreaksToSpaces msg

log "Multiline string marking:"
Expand Down
29 changes: 29 additions & 0 deletions cabal-testsuite/src/Test/Cabal/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,3 +1271,32 @@ findDependencyInStore pkgName = do
[] -> error $ "Could not find " <> pkgName' <> " when searching for " <> pkgName' <> " in\n" <> show packageDirs
(dir:_) -> dir
pure (storeDir </> storeDirForGhcVersion </> libDir)

-- | It can easier to paste expected output verbatim into a text file,
-- especially if it is a multiline string, rather than encoding it as a multiline
-- string in Haskell source code.
--
-- With `-XMultilineStrings` triple quoted strings with line breaks will be
-- easier to write in source code but then this will only work with ghc-9.12.1
-- and later, in which case we'd have to use CPP with test scripts to support
-- older GHC versions. CPP doesn't play nicely with multiline strings using
-- string gaps. None of our test script import other modules. That might be a
-- way to avoid CPP in a module that uses multiline strings.
--
-- In summary, it is easier to read multiline strings from a file. That is what
-- this function facilitates.
--
-- The contents of the file are read strictly to avoid problems seen on Windows
-- deleting the file:
--
-- > cabal.test.hs:
-- > C:\Users\<username>\AppData\Local\Temp\cabal-testsuite-8376\errors.expect.txt:
-- > removePathForcibly:DeleteFile
-- > "\\\\?\\C:\\Users\\<username>\\AppData\\Local\\Temp\\cabal-testsuite-8376\\errors.expect.txt":
-- > permission denied (The process cannot access the file because it is being
-- > used by another process.)
readVerbatimFile :: FilePath -> TestM String
readVerbatimFile filename = do
testDir <- testCurrentDir <$> getTestEnv
s <- liftIO . readFile $ testDir </> filename
length s `seq` return s

0 comments on commit 10bb996

Please sign in to comment.