on master help sign in
Factor out the useful withTempFile function
bf1e282Bryan O'Sullivan
tests/Makefile+3−1
@@ -49,7 +49,9 @@ qc-hpc: Properties.hs QuickCheckUtils.hs $(lib-srcs:%=../%)
49 $(ghc) $(ghc-hpc-flags) $(ghc-test-flags) $(ghc-opt-flags) -ihpcdir \
49 $(ghc) $(ghc-hpc-flags) $(ghc-test-flags) $(ghc-opt-flags) -ihpcdir \
50 --make -threaded -o $@ $<
50 --make -threaded -o $@ $<
51
51
52+Regressions.o: TestUtils.o
53+
52−regressions: Regressions.o
54+regressions: Regressions.o TestUtils.o
53 $(ghc) $(ghc-test-flags) -o $@ $^ $(lib)
55 $(ghc) $(ghc-test-flags) -o $@ $^ $(lib)
54
56
55coverage: qc-hpc-html/hpc_index.html
57coverage: qc-hpc-html/hpc_index.html
tests/Regressions.hs+2−10
@@ -2,9 +2,7 @@
2
2
3-- Regression tests for specific bugs.
3-- Regression tests for specific bugs.
4
4
5−import Control.Exception (SomeException, bracket, handle)
5+import Control.Exception (SomeException, handle)
6−import Control.Monad (when)
7−import System.Directory (removeFile)
8import System.IO
6import System.IO
9import qualified Data.ByteString as B
7import qualified Data.ByteString as B
10import qualified Data.ByteString.Lazy as LB
8import qualified Data.ByteString.Lazy as LB
@@ -14,13 +12,7 @@ import qualified Data.Text.Lazy.Encoding as LE
14import qualified Test.Framework as F
12import qualified Test.Framework as F
15import qualified Test.Framework.Providers.HUnit as F
13import qualified Test.Framework.Providers.HUnit as F
16import Test.HUnit (assertFailure)
14import Test.HUnit (assertFailure)
17−
15+import TestUtils (withTempFile)
18−withTempFile = bracket (openTempFile "." "crashy.txt") cleanupTemp . uncurry
19− where
20− cleanupTemp (path,h) = do
21− open <- hIsOpen h
22− when open (hClose h)
23− removeFile path
24
16
25-- Reported by Michael Snoyman: UTF-8 encoding a large lazy bytestring
17-- Reported by Michael Snoyman: UTF-8 encoding a large lazy bytestring
26-- caused either a segfault or attempt to allocate a negative number
18-- caused either a segfault or attempt to allocate a negative number
tests/TestUtils.hsnew file+17
@@ -0,0 +1,17 @@
1+module TestUtils
2+ (
3+ withTempFile
4+ ) where
5+
6+import Control.Exception (bracket)
7+import Control.Monad (when)
8+import System.Directory (removeFile)
9+import System.IO (Handle, hClose, hIsOpen, openTempFile)
10+
11+withTempFile :: (FilePath -> Handle -> IO a) -> IO a
12+withTempFile = bracket (openTempFile "." "crashy.txt") cleanupTemp . uncurry
13+ where
14+ cleanupTemp (path,h) = do
15+ open <- hIsOpen h
16+ when open (hClose h)
17+ removeFile path