Skip to content

Commit 6c54ec6

Browse files
committed
test: Use temporary to deal with temp files
The previous implementation was dumping temp files in the current directory
1 parent 9f5a41a commit 6c54ec6

4 files changed

Lines changed: 13 additions & 16 deletions

File tree

benchmarks/haskell/Benchmarks.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import System.FilePath ((</>))
1010
import System.IO
1111

1212
#ifdef mingw32_HOST_OS
13+
import System.IO.Temp (emptySystemTempFile)
1314
import System.Directory (removeFile)
1415
#endif
1516

@@ -40,11 +41,11 @@ import qualified Benchmarks.Programs.Throughput as Programs.Throughput
4041
mkSink :: IO (FilePath, Handle)
4142
mkSink = do
4243
#ifdef mingw32_HOST_OS
43-
(sinkFn, sink) <- openTempFile "." "dev.null"
44+
sinkFn <- emptySystemTempFile "dev.null"
4445
#else
4546
let sinkFn = "/dev/null"
46-
sink <- openFile sinkFn WriteMode
4747
#endif
48+
sink <- openFile sinkFn WriteMode
4849
hSetEncoding sink utf8
4950
pure (sinkFn, sink)
5051

tests/Tests/Regressions.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Control.Exception (SomeException, handle)
1414
import Data.Char (isLetter, chr)
1515
import GHC.Exts (Int(..), sizeofByteArray#)
1616
import System.IO
17+
import System.IO.Temp (withSystemTempFile)
1718
import Test.Tasty.HUnit (assertBool, assertEqual, assertFailure)
1819
import qualified Data.ByteString as B
1920
import Data.ByteString.Char8 ()
@@ -48,8 +49,7 @@ lazy_encode_crash = withTempFile $ \ _ h ->
4849
-- encoded file can result in a crash in the RTS (i.e. not merely an
4950
-- exception).
5051
hGetContents_crash :: IO ()
51-
hGetContents_crash = do
52-
(path, h) <- openTempFile "." "crashy.txt"
52+
hGetContents_crash = withSystemTempFile "crashy.txt" $ \path h -> do
5353
B.hPut h (B.pack [0x78, 0xc4 ,0x0a]) >> hClose h
5454
h' <- openFile path ReadMode
5555
hSetEncoding h' utf8

tests/Tests/Utils.hs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ module Tests.Utils
88
, withTempFile
99
) where
1010

11-
import Control.Exception (SomeException, bracket, bracket_, evaluate, try)
12-
import Control.Monad (when, unless)
11+
import Control.Exception (SomeException, bracket_, evaluate, try)
12+
import Control.Monad (when)
13+
import System.IO.Temp (withSystemTempFile)
1314
import GHC.IO.Handle.Internals (withHandle)
14-
import System.Directory (removeFile)
15-
import System.IO (Handle, hClose, hFlush, hIsOpen, hIsClosed, hIsWritable, openTempFile)
15+
import System.IO (Handle, hFlush, hIsOpen, hIsWritable)
1616
import Test.QuickCheck (Property, ioProperty, property, (===), counterexample)
1717

1818
-- Ensure that two potentially bottom values (in the sense of crashing
@@ -31,12 +31,7 @@ infix 4 =^=
3131
{-# NOINLINE (=^=) #-}
3232

3333
withTempFile :: (FilePath -> Handle -> IO a) -> IO a
34-
withTempFile = bracket (openTempFile "." "crashy.txt") cleanupTemp . uncurry
35-
where
36-
cleanupTemp (path,h) = do
37-
closed <- hIsClosed h
38-
unless closed $ hClose h
39-
removeFile path
34+
withTempFile = withSystemTempFile "crashy.txt"
4035

4136
withRedirect :: Handle -> Handle -> IO a -> IO a
4237
withRedirect tmp h = bracket_ swap swap

text.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ test-suite tests
299299
binary,
300300
bytestring,
301301
deepseq,
302-
directory,
303302
ghc-prim,
304303
tasty,
305304
tasty-hunit,
@@ -313,7 +312,8 @@ test-suite tests
313312
-- ghc-9.2.1 library depends on parsec, which causes a circular dependency.
314313
if impl(ghc >= 8.2.1 && < 8.6 || >= 8.6.2 && < 9.2 || >= 9.2.2)
315314
build-depends: tasty-inspection-testing
316-
315+
if os(windows)
316+
build-depends: temporary
317317
default-language: Haskell2010
318318
default-extensions: NondecreasingIndentation
319319

@@ -333,6 +333,7 @@ benchmark text-benchmarks
333333
directory,
334334
filepath,
335335
tasty-bench >= 0.2,
336+
temporary,
336337
text,
337338
transformers
338339

0 commit comments

Comments
 (0)