streamly-filesystem/test/Main.hs

53 lines
1.8 KiB
Haskell
Raw Normal View History

2020-01-29 20:02:45 +00:00
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
2020-01-29 20:26:45 +00:00
import GHC.IO.Handle ( Handle )
import System.IO ( openFile
, IOMode(ReadMode)
)
import System.IO.Temp ( withSystemTempFile )
import Test.Hspec
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Streamly.External.FileSystem.Handle.Posix
2020-01-29 20:34:03 +00:00
2020-01-29 20:02:45 +00:00
checkCopyLBS :: FilePath -> Handle -> IO ()
checkCopyLBS filename handle' = do
let content = "Some file content"
print $ "Checking " <> filename
2020-01-29 20:26:45 +00:00
copyLBS content handle'
handle <- openFile filename ReadMode
bsContent <- BS.hGetContents handle
bsContent `shouldBe` (BSL.toStrict content)
2020-01-29 20:02:45 +00:00
2020-01-29 20:26:45 +00:00
checkReadFileLBS :: FilePath -> Handle -> IO ()
checkReadFileLBS filename handle' = do
let content = "Some file content"
print $ "Checking " <> filename
copyLBS content handle'
handle <- openFile filename ReadMode
bsContent <- readFileLBS handle
(BSL.toStrict bsContent) `shouldBe` (BSL.toStrict content)
2020-01-29 20:02:45 +00:00
2020-01-29 20:26:45 +00:00
checkCopyFileHandle :: Handle -> Handle -> IO ()
checkCopyFileHandle from to = do
let content = "Some file content"
copyLBS' content from
copyFileHandle' from to
fromContent <- readFileLBS from
2020-01-29 20:34:03 +00:00
toContent <- readFileLBS to
2020-01-29 20:26:45 +00:00
(BSL.toStrict fromContent) `shouldBe` (BSL.toStrict toContent)
2020-01-29 20:02:45 +00:00
2020-01-29 20:26:45 +00:00
main :: IO ()
main = hspec $ do
describe "Streamly.External.FileSystem.Handle.Posix" $ do
it "copyLBS" $ withSystemTempFile "x" checkCopyLBS
it "readFileLBS" $ withSystemTempFile "x" checkReadFileLBS
it "copyFileHandle" $ withSystemTempFile
"x"
(\_ h1 -> withSystemTempFile "y" $ (\_ h2 -> checkCopyFileHandle h1 h2))