From 10bb996a21f850efc0773134b13b1ef48bc32613 Mon Sep 17 00:00:00 2001 From: Phil de Joux Date: Sat, 21 Dec 2024 09:22:44 -0500 Subject: [PATCH] Add readVerbatimFile - Have readVerbatimFile read contents strictly --- .../ParseErrorProvenance/cabal.test.hs | 6 +--- .../{msg.txt => msg.expect.txt} | 0 cabal-testsuite/src/Test/Cabal/Prelude.hs | 29 +++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) rename cabal-testsuite/PackageTests/ProjectImport/ParseErrorProvenance/{msg.txt => msg.expect.txt} (100%) diff --git a/cabal-testsuite/PackageTests/ProjectImport/ParseErrorProvenance/cabal.test.hs b/cabal-testsuite/PackageTests/ProjectImport/ParseErrorProvenance/cabal.test.hs index 4d5d4c2811f..7981952a6a2 100644 --- a/cabal-testsuite/PackageTests/ProjectImport/ParseErrorProvenance/cabal.test.hs +++ b/cabal-testsuite/PackageTests/ProjectImport/ParseErrorProvenance/cabal.test.hs @@ -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:" diff --git a/cabal-testsuite/PackageTests/ProjectImport/ParseErrorProvenance/msg.txt b/cabal-testsuite/PackageTests/ProjectImport/ParseErrorProvenance/msg.expect.txt similarity index 100% rename from cabal-testsuite/PackageTests/ProjectImport/ParseErrorProvenance/msg.txt rename to cabal-testsuite/PackageTests/ProjectImport/ParseErrorProvenance/msg.expect.txt diff --git a/cabal-testsuite/src/Test/Cabal/Prelude.hs b/cabal-testsuite/src/Test/Cabal/Prelude.hs index 1bbdebbec25..9dacd120d1e 100644 --- a/cabal-testsuite/src/Test/Cabal/Prelude.hs +++ b/cabal-testsuite/src/Test/Cabal/Prelude.hs @@ -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\\AppData\Local\Temp\cabal-testsuite-8376\errors.expect.txt: +-- > removePathForcibly:DeleteFile +-- > "\\\\?\\C:\\Users\\\\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