Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce UTF-8 encoding on reading and writing files #75

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Hedgehog/Extras/Test/File.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}

module Hedgehog.Extras.Test.File
( createDirectoryIfMissing
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Copy link
Contributor

@carbolymer carbolymer Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evalIO is needed for proper reporting of IO exceptions

Suggested change
liftIO $ IO.withFile filePath IO.ReadMode $ \handle -> do
H.evalIO $ IO.withFile filePath IO.ReadMode $ \handle -> do

Why this change is only in those two places ? I think we should use utf8 in all functions using string / text when accessing files:
appendFile, textWriteFile, textReadFile, fileContains

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right. However, it makes sense to accept this PR as an improvement to the library and do the other fixes in a future PR.

Copy link
Contributor

@carbolymer carbolymer Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do the other fixes in a future PR

Fine but let's do them before making a new release... And the version of hedgehog-extras with this change was tagged already. Can we try to limit technical debt we're introducing?

Or at least put an effort into making technical debt manageable and traceable by creating follow-up tickets/prs?

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 ()
Expand Down
Loading