From bdd89f779248140812424711fee6585c29757f6d Mon Sep 17 00:00:00 2001 From: Jordan Millar Date: Tue, 19 Nov 2024 15:22:56 -0400 Subject: [PATCH] Enforce UTF-8 encoding on reading and writing files --- src/Hedgehog/Extras/Test/File.hs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Hedgehog/Extras/Test/File.hs b/src/Hedgehog/Extras/Test/File.hs index 8bc089b1..13cf2cbd 100644 --- a/src/Hedgehog/Extras/Test/File.hs +++ b/src/Hedgehog/Extras/Test/File.hs @@ -1,6 +1,6 @@ {-# LANGUAGE BangPatterns #-} -{-# LANGUAGE TypeApplications #-} {-# LANGUAGE RankNTypes #-} +{-# LANGUAGE TypeApplications #-} module Hedgehog.Extras.Test.File ( createDirectoryIfMissing @@ -65,6 +65,8 @@ import Data.Maybe import Data.Semigroup import Data.String (String) import Data.Text (Text) +import qualified Data.Text as Text +import qualified Data.Text.IO as Text import Data.Time.Clock (UTCTime) import GHC.Stack (HasCallStack) import Hedgehog (MonadTest) @@ -158,7 +160,9 @@ appendFile filePath contents = GHC.withFrozenCallStack $ do writeFile :: (MonadTest m, MonadIO m, HasCallStack) => FilePath -> String -> m () writeFile filePath contents = GHC.withFrozenCallStack $ do void . H.annotate $ "Writing file: " <> filePath - H.evalIO $ IO.writeFile filePath contents + H.evalIO $ IO.withFile filePath IO.WriteMode $ \handle -> do + IO.hSetEncoding handle IO.utf8 + IO.hPutStr handle contents -- | Open a handle to the 'filePath' file. openFile :: (MonadTest m, MonadIO m, HasCallStack) => FilePath -> IOMode -> m Handle @@ -170,7 +174,9 @@ openFile filePath mode = GHC.withFrozenCallStack $ do readFile :: (MonadTest m, MonadIO m, HasCallStack) => FilePath -> m String readFile filePath = GHC.withFrozenCallStack $ do void . H.annotate $ "Reading file: " <> filePath - H.evalIO $ IO.readFile filePath + liftIO $ IO.withFile filePath IO.ReadMode $ \handle -> do + IO.hSetEncoding handle IO.utf8 + Text.unpack <$> Text.hGetContents handle -- | Write 'contents' to the 'filePath' file. lbsWriteFile :: (MonadTest m, MonadIO m, HasCallStack) => FilePath -> LBS.ByteString -> m ()