Skip to content

Commit

Permalink
Fix encoding issue on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Nov 19, 2024
1 parent 9fffaf9 commit 1cbd09a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ library gen
Test.Hedgehog.Roundtrip.CBOR

build-depends:
Diff,
QuickCheck,
aeson >=1.5.6.0,
base16-bytestring,
Expand Down
68 changes: 61 additions & 7 deletions cardano-api/gen/Test/Hedgehog/Golden/ErrorMessage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ module Test.Hedgehog.Golden.ErrorMessage where
import Cardano.Api (Error (..))
import Cardano.Api.Pretty

import qualified Control.Concurrent.QSem as IO
import Control.Exception (bracket_)
import Control.Monad
import Control.Monad.IO.Class
import Data.Algorithm.Diff (PolyDiff (Both), getGroupedDiff)
import Data.Algorithm.DiffOutput (ppDiff)
import Data.Data
import qualified Data.List as List
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
import GHC.Stack (HasCallStack, withFrozenCallStack)
import qualified GHC.Stack as GHC
import qualified System.Directory as IO
Expand All @@ -20,8 +26,6 @@ import qualified System.IO.Unsafe as IO

import Hedgehog
import qualified Hedgehog.Extras.Test as H
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.Golden as H
import qualified Hedgehog.Internal.Property as H
import Test.Tasty
import Test.Tasty.Hedgehog
Expand Down Expand Up @@ -108,7 +112,7 @@ testErrorMessage_ goldenFilesLocation moduleName typeName constructorName err =
let fqtn = moduleName <> "." <> typeName
testProperty constructorName . withTests 1 . property $ do
H.note_ "Incorrect error message in golden file"
H.diffVsGoldenFile
diffVsGoldenFile
(docToString (prettyError err))
(goldenFilesLocation </> fqtn </> constructorName <> ".txt")

Expand Down Expand Up @@ -146,7 +150,7 @@ writeGoldenFile
writeGoldenFile goldenFile actualContent = GHC.withFrozenCallStack $ do
H.note_ $ "Creating golden file " <> goldenFile
H.createDirectoryIfMissing_ (takeDirectory goldenFile)
writeFile goldenFile actualContent
writeFile' goldenFile actualContent

recreateGoldenFiles :: Bool
recreateGoldenFiles = IO.unsafePerformIO $ do
Expand All @@ -158,7 +162,57 @@ createGoldenFiles = IO.unsafePerformIO $ do
value <- IO.lookupEnv "CREATE_GOLDEN_FILES"
return $ value == Just "1"

writeFile :: (MonadTest m, MonadIO m, HasCallStack) => FilePath -> String -> m ()
writeFile 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 $ Text.writeFile filePath $ Text.pack contents

checkAgainstGoldenFile
:: ()
=> HasCallStack
=> MonadIO m
=> MonadTest m
=> FilePath
-> [String]
-> m ()
checkAgainstGoldenFile goldenFile actualLines = GHC.withFrozenCallStack $ do
referenceLines <- List.lines <$> H.readFile goldenFile
let difference = getGroupedDiff actualLines referenceLines
case difference of
[] -> pure ()
[Both{}] -> pure ()
_ -> do
H.note_ $
unlines
[ "Golden test failed against the golden file."
, "To recreate golden file, run with RECREATE_GOLDEN_FILES=1."
]
H.failMessage GHC.callStack $ ppDiff difference

sem :: IO.QSem
sem = IO.unsafePerformIO $ IO.newQSem 1
{-# NOINLINE sem #-}

semBracket :: IO a -> IO a
semBracket = bracket_ (IO.waitQSem sem) (IO.signalQSem sem)

mGoldenFileLogFile :: Maybe FilePath
mGoldenFileLogFile =
IO.unsafePerformIO $
IO.lookupEnv "GOLDEN_FILE_LOG_FILE"

reportGoldenFileMissing
:: ()
=> HasCallStack
=> MonadIO m
=> MonadTest m
=> FilePath
-> m ()
reportGoldenFileMissing goldenFile = GHC.withFrozenCallStack $ do
H.note_ $
unlines
[ "Golden file " <> goldenFile <> " does not exist."
, "To create it, run with CREATE_GOLDEN_FILES=1."
, "To recreate it, run with RECREATE_GOLDEN_FILES=1."
]
H.failure

0 comments on commit 1cbd09a

Please sign in to comment.