Skip to content

Commit

Permalink
Fix HaddockKeepTmpsCustom test for merge skew
Browse files Browse the repository at this point in the history
The `HaddockKeepTmpsCustom` test added in haskell#10392 skewed against haskell#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.
  • Loading branch information
9999years authored and Mikolaj committed Oct 5, 2024
1 parent 44e12fd commit 82357a8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 20 additions & 14 deletions cabal-testsuite/PackageTests/HaddockKeepsTmps/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions cabal-testsuite/src/Test/Cabal/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ".",
Expand Down

0 comments on commit 82357a8

Please sign in to comment.