From 82357a81eaeca9a44223c4d4388b5993328227d0 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Fri, 4 Oct 2024 15:12:53 -0700 Subject: [PATCH] Fix `HaddockKeepTmpsCustom` test for merge skew The `HaddockKeepTmpsCustom` test added in #10392 skewed against #10366, which changes where temporary files are written. We can work around this by setting `$TMPDIR` and `$TEMP` while running tests, so that temporary files are written to the test's temporary directory rather than the system one. --- .../HaddockKeepTmpsCustom/cabal.test.hs | 6 ++-- .../HaddockKeepsTmps/cabal.test.hs | 34 +++++++++++-------- cabal-testsuite/src/Test/Cabal/Monad.hs | 4 +++ 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/cabal-testsuite/PackageTests/HaddockKeepTmpsCustom/cabal.test.hs b/cabal-testsuite/PackageTests/HaddockKeepTmpsCustom/cabal.test.hs index 39433896ce3..e2d819e44d6 100644 --- a/cabal-testsuite/PackageTests/HaddockKeepTmpsCustom/cabal.test.hs +++ b/cabal-testsuite/PackageTests/HaddockKeepTmpsCustom/cabal.test.hs @@ -11,11 +11,11 @@ main = cabalTest $ recordMode DoNotRecord $ withProjectFile "cabal.project" $ do -- "foobar.ext" will be "fooXXX.ext". let glob = if isWindows - then "**/had*.txt" - else "**/haddock-response*.txt" + then "had*.txt" + else "haddock-response*.txt" -- Check that there is a response file. - responseFiles <- assertGlobMatchesTestDir testDistDir glob + responseFiles <- assertGlobMatchesTestDir testTmpDir glob -- Check that the matched response file is not empty, and is indeed a Haddock -- response file. diff --git a/cabal-testsuite/PackageTests/HaddockKeepsTmps/cabal.test.hs b/cabal-testsuite/PackageTests/HaddockKeepsTmps/cabal.test.hs index 0f601d77363..3a76caadf6c 100644 --- a/cabal-testsuite/PackageTests/HaddockKeepsTmps/cabal.test.hs +++ b/cabal-testsuite/PackageTests/HaddockKeepsTmps/cabal.test.hs @@ -1,23 +1,29 @@ -{-# LANGUAGE LambdaCase #-} -import Test.Cabal.Prelude -import Data.List (sort, isPrefixOf) -import Distribution.Verbosity +import Data.List (isPrefixOf, sort) import Distribution.Simple.Glob import Distribution.Simple.Glob.Internal import Distribution.Simple.Utils +import Distribution.Verbosity import System.Directory +import Test.Cabal.Prelude -- Test that "cabal haddock" preserves temporary files -- We use haddock-keep-temp-file: True in the cabal.project. main = cabalTest $ recordMode DoNotRecord $ withProjectFile "cabal.project" $ do - pwd <- testTmpDir <$> getTestEnv - liftIO $ createDirectory (pwd "temp") - withEnv [(if isWindows then "TMP" else "TMPDIR", Just $ pwd "temp")] $ - cabal "haddock" [] - files <- liftIO $ listDirectory (pwd "temp") - case [ pwd "temp" f | f <- files, takeExtension f == ".txt" ] of - [] -> error "Expecting a response file being mentioned in the outcome" - files' -> - -- Assert the matched response file is not empty, and indeed a haddock rsp - assertAnyFileContains files' "--package-name" + cabal "haddock" [] + + -- From the docs for `System.IO.openTempFile`: + -- + -- On Windows, the template prefix may be truncated to 3 chars, e.g. + -- "foobar.ext" will be "fooXXX.ext". + let glob = + if isWindows + then "had*.txt" + else "haddock-response*.txt" + + -- Check that there is a response file. + responseFiles <- assertGlobMatchesTestDir testTmpDir glob + + -- Check that the matched response file is not empty, and is indeed a Haddock + -- response file. + assertAnyFileContains responseFiles "--package-name" diff --git a/cabal-testsuite/src/Test/Cabal/Monad.hs b/cabal-testsuite/src/Test/Cabal/Monad.hs index 63605768de7..31e1e07bf52 100644 --- a/cabal-testsuite/src/Test/Cabal/Monad.hs +++ b/cabal-testsuite/src/Test/Cabal/Monad.hs @@ -410,6 +410,10 @@ runTestM mode m = -- effect on Windows. , ("CABAL_DIR", Just (testCabalDir env)) , ("CABAL_CONFIG", Just (testUserCabalConfigFile env)) + -- Set `TMPDIR` so that temporary files aren't created in the global `TMPDIR`. + , ("TMPDIR", Just tmp_dir) + -- Windows uses `TMP` for the `TMPDIR`. + , ("TMP", Just tmp_dir) ], testShouldFail = False, testRelativeCurrentDir = ".",