Skip to content

Commit ddd22b7

Browse files
LysxiaBodigrim
authored andcommitted
test: Use temporary to deal with temp files
The previous implementation was dumping temp files in the current directory
1 parent 9f5a41a commit ddd22b7

File tree

5 files changed

+13
-19
lines changed

5 files changed

+13
-19
lines changed

.github/workflows/emulated.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
githubToken: ${{ github.token }}
3131
install: |
3232
apt-get update -y
33-
apt-get install -y curl ghc libghc-tasty-quickcheck-dev libghc-tasty-hunit-dev
33+
apt-get install -y curl ghc libghc-tasty-quickcheck-dev libghc-tasty-hunit-dev libghc-temporary-dev
3434
run: |
3535
curl -s https://hackage.haskell.org/package/data-array-byte-0.1/data-array-byte-0.1.tar.gz | tar xz
3636
ghc --version

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 & 4 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 ()
@@ -33,7 +34,6 @@ import qualified Data.Text.Unsafe as T
3334
import qualified Test.Tasty as F
3435
import qualified Test.Tasty.HUnit as F
3536
import Test.Tasty.HUnit ((@?=))
36-
import System.Directory (removeFile)
3737

3838
import Tests.Utils (withTempFile)
3939

@@ -48,15 +48,13 @@ lazy_encode_crash = withTempFile $ \ _ h ->
4848
-- encoded file can result in a crash in the RTS (i.e. not merely an
4949
-- exception).
5050
hGetContents_crash :: IO ()
51-
hGetContents_crash = do
52-
(path, h) <- openTempFile "." "crashy.txt"
51+
hGetContents_crash = withSystemTempFile "crashy.txt" $ \path h -> do
5352
B.hPut h (B.pack [0x78, 0xc4 ,0x0a]) >> hClose h
5453
h' <- openFile path ReadMode
5554
hSetEncoding h' utf8
5655
handle (\(_::SomeException) -> return ()) $
5756
T.hGetContents h' >> assertFailure "T.hGetContents should crash"
5857
hClose h'
59-
removeFile path
6058

6159
-- Reported by Ian Lynagh: attempting to allocate a sufficiently large
6260
-- string (via either Array.new or Text.replicate) could result in an

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ test-suite tests
299299
binary,
300300
bytestring,
301301
deepseq,
302-
directory,
303302
ghc-prim,
304303
tasty,
305304
tasty-hunit,
306305
tasty-quickcheck,
307306
template-haskell,
307+
temporary,
308308
transformers,
309309
text
310310
if impl(ghc < 9.4)
@@ -313,7 +313,6 @@ test-suite tests
313313
-- ghc-9.2.1 library depends on parsec, which causes a circular dependency.
314314
if impl(ghc >= 8.2.1 && < 8.6 || >= 8.6.2 && < 9.2 || >= 9.2.2)
315315
build-depends: tasty-inspection-testing
316-
317316
default-language: Haskell2010
318317
default-extensions: NondecreasingIndentation
319318

@@ -333,6 +332,7 @@ benchmark text-benchmarks
333332
directory,
334333
filepath,
335334
tasty-bench >= 0.2,
335+
temporary,
336336
text,
337337
transformers
338338

0 commit comments

Comments
 (0)