on master help sign in
Factor out the useful withTempFile function

--HG-- extra : convert_revision : 51e2815edfd288dd89130483676e3a0340b8186a

commitbf1e282dd75b5635e81d1d9b81dd566d6c4f1251 authorBryan O'Sullivan <bos@serpentine.com> dateThu 26 Aug 2010, 18:28 (16 years ago) parentcac8169
3 files changed+22−11
tests/Makefile+3−1 tests/Regressions.hs+2−10 tests/TestUtils.hs+17
tests/Makefile+3−1
@@ -49,7 +49,9 @@ qc-hpc: Properties.hs QuickCheckUtils.hs $(lib-srcs:%=../%)
4949 $(ghc) $(ghc-hpc-flags) $(ghc-test-flags) $(ghc-opt-flags) -ihpcdir \
5050 --make -threaded -o $@ $<
5151
52−regressions: Regressions.o
52+Regressions.o: TestUtils.o
53+
54+regressions: Regressions.o TestUtils.o
5355 $(ghc) $(ghc-test-flags) -o $@ $^ $(lib)
5456
5557coverage: qc-hpc-html/hpc_index.html
tests/Regressions.hs+2−10
@@ -2,9 +2,7 @@
22
33-- Regression tests for specific bugs.
44
5−import Control.Exception (SomeException, bracket, handle)
6−import Control.Monad (when)
7−import System.Directory (removeFile)
5+import Control.Exception (SomeException, handle)
86import System.IO
97import qualified Data.ByteString as B
108import qualified Data.ByteString.Lazy as LB
@@ -14,13 +12,7 @@ import qualified Data.Text.Lazy.Encoding as LE
1412import qualified Test.Framework as F
1513import qualified Test.Framework.Providers.HUnit as F
1614import Test.HUnit (assertFailure)
17−
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
15+import TestUtils (withTempFile)
2416
2517-- Reported by Michael Snoyman: UTF-8 encoding a large lazy bytestring
2618-- 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