Skip to content

Commit

Permalink
Do not depend on directory (#321)
Browse files Browse the repository at this point in the history
* Use c_unlink instead of removeFile

* Freshen up LazyHClose tests

* Avoid directory package in builder tests

* Do not depend on directory package

* Review suggestions
  • Loading branch information
Bodigrim authored Nov 12, 2020
1 parent 059af04 commit 59f8292
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 96 deletions.
75 changes: 36 additions & 39 deletions tests/LazyHClose.hs
Original file line number Diff line number Diff line change
@@ -1,67 +1,64 @@
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
module Main (main) where

import Control.Monad (void, forM_)
import Data.ByteString.Internal (toForeignPtr)
import Foreign.C.String (withCString)
import Foreign.ForeignPtr (finalizeForeignPtr)
import System.IO (openFile, openTempFile, hClose, hPutStrLn, IOMode(..))
import System.Posix.Internals (c_unlink)

import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as L8

import Control.Monad
import System.Directory
import System.Mem
import System.IO

import Data.ByteString.Internal
import Foreign.ForeignPtr

main :: IO ()
main = do
writeFile "a" "x"
let n = 1000
(fn, h) <- openTempFile "." "lazy-hclose-test.tmp"
hPutStrLn h "x"
hClose h

------------------------------------------------------------------------
-- readFile tests

print "Testing resource leaks for Strict.readFile"
putStrLn "Testing resource leaks for Strict.readFile"
forM_ [1..n] $ const $ do
r <- S.readFile "a"
S.writeFile "b" (S8.pack "abc")
renameFile "b" "a"
r <- S.readFile fn
appendFile fn "" -- will fail, if fn has not been closed yet

print "Testing resource leaks for Lazy.readFile"
putStrLn "Testing resource leaks for Lazy.readFile"
forM_ [1..n] $ const $ do
r <- L.readFile "a"
L.length r `seq` return () -- force the input, and done with 'r' now.
L.writeFile "b" (L8.pack "abc") -- but we still need the finalizers to run
renameFile "b" "a"
r <- L.readFile fn
L.length r `seq` return ()
appendFile fn "" -- will fail, if fn has not been closed yet

-- manage the resources explicitly.
print "Testing resource leaks when converting lazy to strict"
putStrLn "Testing resource leaks when converting lazy to strict"
forM_ [1..n] $ const $ do
let release c = finalizeForeignPtr fp where (fp,_,_) = toForeignPtr c
r <- L.readFile "a"
mapM_ release (L.toChunks r) -- should close it.
L.writeFile "b" (L8.pack "abc")
renameFile "b" "a"
r <- L.readFile fn
mapM_ release (L.toChunks r)
appendFile fn "" -- will fail, if fn has not been closed yet

------------------------------------------------------------------------
-- hGetContents tests

-- works now
print "Testing strict hGetContents"
putStrLn "Testing strict hGetContents"
forM_ [1..n] $ const $ do
h <- openFile "a" ReadMode
r <- S.hGetContents h -- should be strict, and hClosed.
h <- openFile fn ReadMode
r <- S.hGetContents h
S.last r `seq` return ()
S.writeFile "b" (S8.pack "abc")
renameFile "b" "a"
appendFile fn "" -- will fail, if fn has not been closed yet

-- works now
print "Testing lazy hGetContents"
putStrLn "Testing lazy hGetContents"
forM_ [1..n] $ const $ do
h <- openFile "a" ReadMode
r <- L.hGetContents h -- should be strict, and hClosed.
h <- openFile fn ReadMode
r <- L.hGetContents h
L.last r `seq` return ()
L.writeFile "b" (L8.pack "abc")
renameFile "b" "a"
appendFile fn "" -- will fail, if fn has not been closed yet

removeFile "a"
removeFile fn

n = 1000
removeFile :: String -> IO ()
removeFile fn = void $ withCString fn c_unlink
94 changes: 49 additions & 45 deletions tests/Properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
-- -fhpc interferes with rewrite rules firing.
--

import Foreign.C.String (withCString)
import Foreign.Storable
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc
Expand All @@ -21,7 +22,7 @@ import Control.Applicative
import Control.Monad
import Control.Concurrent
import Control.Exception
import System.Directory
import System.Posix.Internals (c_unlink)

import Data.List
import Data.Char
Expand Down Expand Up @@ -1668,73 +1669,73 @@ prop_fromForeignPtr x = (let (a,b,c) = (P.toForeignPtr x)
-- IO

prop_read_write_file_P x = ioProperty $ do
tid <- myThreadId
let f = "qc-test-" ++ show tid
P.writeFile f x
y <- P.readFile f
removeFile f
(fn, h) <- openTempFile "." "prop-compiled.tmp"
hClose h
P.writeFile fn x
y <- P.readFile fn
removeFile fn
return (x == y)

prop_read_write_file_C x = ioProperty $ do
tid <- myThreadId
let f = "qc-test-" ++ show tid
C.writeFile f x
y <- C.readFile f
removeFile f
(fn, h) <- openTempFile "." "prop-compiled.tmp"
hClose h
C.writeFile fn x
y <- C.readFile fn
removeFile fn
return (x == y)

prop_read_write_file_L x = ioProperty $ do
tid <- myThreadId
let f = "qc-test-" ++ show tid
L.writeFile f x
y <- L.readFile f
L.length y `seq` removeFile f
(fn, h) <- openTempFile "." "prop-compiled.tmp"
hClose h
L.writeFile fn x
y <- L.readFile fn
L.length y `seq` removeFile fn
return (x == y)

prop_read_write_file_D x = ioProperty $ do
tid <- myThreadId
let f = "qc-test-" ++ show tid
D.writeFile f x
y <- D.readFile f
D.length y `seq` removeFile f
(fn, h) <- openTempFile "." "prop-compiled.tmp"
hClose h
D.writeFile fn x
y <- D.readFile fn
D.length y `seq` removeFile fn
return (x == y)

------------------------------------------------------------------------

prop_append_file_P x y = ioProperty $ do
tid <- myThreadId
let f = "qc-test-" ++ show tid
P.writeFile f x
P.appendFile f y
z <- P.readFile f
removeFile f
(fn, h) <- openTempFile "." "prop-compiled.tmp"
hClose h
P.writeFile fn x
P.appendFile fn y
z <- P.readFile fn
removeFile fn
return (z == x `P.append` y)

prop_append_file_C x y = ioProperty $ do
tid <- myThreadId
let f = "qc-test-" ++ show tid
C.writeFile f x
C.appendFile f y
z <- C.readFile f
removeFile f
(fn, h) <- openTempFile "." "prop-compiled.tmp"
hClose h
C.writeFile fn x
C.appendFile fn y
z <- C.readFile fn
removeFile fn
return (z == x `C.append` y)

prop_append_file_L x y = ioProperty $ do
tid <- myThreadId
let f = "qc-test-" ++ show tid
L.writeFile f x
L.appendFile f y
z <- L.readFile f
L.length z `seq` removeFile f
(fn, h) <- openTempFile "." "prop-compiled.tmp"
hClose h
L.writeFile fn x
L.appendFile fn y
z <- L.readFile fn
L.length y `seq` removeFile fn
return (z == x `L.append` y)

prop_append_file_D x y = ioProperty $ do
tid <- myThreadId
let f = "qc-test-" ++ show tid
D.writeFile f x
D.appendFile f y
z <- D.readFile f
D.length z `seq` removeFile f
(fn, h) <- openTempFile "." "prop-compiled.tmp"
hClose h
D.writeFile fn x
D.appendFile fn y
z <- D.readFile fn
D.length y `seq` removeFile fn
return (z == x `D.append` y)

prop_packAddress = C.pack "this is a test"
Expand Down Expand Up @@ -2609,3 +2610,6 @@ findIndexEnd p = go . findIndices p

elemIndexEnd :: Eq a => a -> [a] -> Maybe Int
elemIndexEnd = findIndexEnd . (==)

removeFile :: String -> IO ()
removeFile fn = void $ withCString fn c_unlink
16 changes: 8 additions & 8 deletions tests/builder/Data/ByteString/Builder/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
module Data.ByteString.Builder.Tests (tests) where

import Control.Applicative
import Control.Monad (unless)
import Control.Monad (unless, void)
import Control.Monad.Trans.State (StateT, evalStateT, evalState, put, get)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Writer (WriterT, execWriterT, tell)
Expand All @@ -40,10 +40,10 @@ import qualified Data.ByteString.Builder.Prim as BP
import Data.ByteString.Builder.Prim.TestUtils

import Control.Exception (evaluate)
import System.IO (openTempFile, hPutStr, hClose, hSetBinaryMode, hSetNewlineMode, noNewlineTranslation)
import System.IO (hSetEncoding, utf8)
import System.Directory
import System.IO (openTempFile, hPutStr, hClose, hSetBinaryMode, hSetEncoding, utf8, hSetNewlineMode, noNewlineTranslation)
import Foreign (ForeignPtr, withForeignPtr, castPtr)
import Foreign.C.String (withCString)
import System.Posix.Internals (c_unlink)

import Test.Framework
import Test.Framework.Providers.QuickCheck2
Expand Down Expand Up @@ -110,8 +110,7 @@ testHandlePutBuilder =
between = filter safeChr a2
after = filter safeChr a3
#endif
tempDir <- getTemporaryDirectory
(tempFile, tempH) <- openTempFile tempDir "TestBuilder"
(tempFile, tempH) <- openTempFile "." "test-builder.tmp"
-- switch to UTF-8 encoding
hSetEncoding tempH utf8
hSetNewlineMode tempH noNewlineTranslation
Expand Down Expand Up @@ -147,8 +146,7 @@ testHandlePutBuilderChar8 =
where
testRecipe :: (String, String, String, Recipe) -> Property
testRecipe args@(before, between, after, recipe) = ioProperty $ do
tempDir <- getTemporaryDirectory
(tempFile, tempH) <- openTempFile tempDir "TestBuilder"
(tempFile, tempH) <- openTempFile "." "TestBuilder"
-- switch to binary / latin1 encoding
hSetBinaryMode tempH True
-- output recipe with intermediate direct writing to handle
Expand Down Expand Up @@ -177,6 +175,8 @@ testHandlePutBuilderChar8 =
unless success (error msg)
return success

removeFile :: String -> IO ()
removeFile fn = void $ withCString fn c_unlink

-- Recipes with which to test the builder functions
---------------------------------------------------
Expand Down
7 changes: 3 additions & 4 deletions tests/bytestring-tests.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test-suite prop-compiled
Data.ByteString.Short.Internal
Data.ByteString.Unsafe
hs-source-dirs: . ..
build-depends: base, ghc-prim, deepseq, random, directory,
build-depends: base, ghc-prim, deepseq, random,
test-framework, test-framework-quickcheck2,
QuickCheck >= 2.10 && < 2.15
c-sources: ../cbits/fpstring.c
Expand All @@ -55,7 +55,7 @@ test-suite lazy-hclose
Data.ByteString.Lazy.Internal
Data.ByteString.Unsafe
hs-source-dirs: . ..
build-depends: base, ghc-prim, deepseq, random, directory,
build-depends: base, ghc-prim, deepseq, random,
test-framework, test-framework-quickcheck2,
QuickCheck >= 2.10 && < 2.15
c-sources: ../cbits/fpstring.c
Expand All @@ -71,7 +71,7 @@ executable regressions
Data.ByteString.Lazy.Internal
Data.ByteString.Unsafe
hs-source-dirs: . ..
build-depends: base, ghc-prim, deepseq, random, directory,
build-depends: base, ghc-prim, deepseq, random,
test-framework, test-framework-hunit, HUnit
c-sources: ../cbits/fpstring.c
include-dirs: ../include
Expand Down Expand Up @@ -110,7 +110,6 @@ test-suite test-builder
QuickCheck >= 2.10 && < 2.15,
byteorder == 1.0.*,
dlist >= 0.5 && < 0.9,
directory,
transformers >= 0.3,
HUnit,
test-framework,
Expand Down

0 comments on commit 59f8292

Please sign in to comment.