1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
module RIO.FileSpec where
import Test.Hspec
import System.FilePath ((</>))
import UnliftIO.Temporary (withSystemTempDirectory)
import RIO
import qualified RIO.ByteString as BS
import qualified RIO.File as SUT
spec :: Spec
spec = do
describe "ensureFileDurable" $ do
it "ensures a file is durable with an fsync" $
withSystemTempDirectory "rio" $ \dir -> do
let fp = dir </> "ensure_file_durable"
writeFileUtf8 fp "Hello World"
SUT.ensureFileDurable fp
contents <- BS.readFile fp
contents `shouldBe` "Hello World"
describe "withBinaryFileDurableAtomic" $ do
context "read/write" $ do
it "works correctly" $ do
withSystemTempDirectory "rio" $ \dir -> do
let fp = dir </> "ensure_file_durable_atomic"
writeFileUtf8 fp "Hello World"
SUT.withBinaryFileDurableAtomic fp ReadWriteMode $ \h -> do
input <- BS.hGetLine h
input `shouldBe` "Hello World"
BS.hPut h "Goodbye World"
context "happy path" $ do
it "works the same as withFile" $ do
withSystemTempDirectory "rio" $ \dir -> do
let fp = dir </> "with_file_durable_atomic"
SUT.withBinaryFileDurableAtomic fp WriteMode $ \h ->
BS.hPut h "Hello World"
contents <- BS.readFile fp
contents `shouldBe` "Hello World"
describe "withBinaryFileDurable" $ do
context "happy path" $ do
it "works the same as withFile" $ do
withSystemTempDirectory "rio" $ \dir -> do
let fp = dir </> "with_file_durable"
SUT.withBinaryFileDurable fp WriteMode $ \h ->
BS.hPut h "Hello World"
contents <- BS.readFile fp
contents `shouldBe` "Hello World"
|