From f54c8097c46c0bb9d0e39f8aedfffbed0b45fe66 Mon Sep 17 00:00:00 2001 From: Jordan Millar Date: Tue, 19 Nov 2024 11:44:20 -0400 Subject: [PATCH] Fix encoding issue on Windows --- cardano-api/cardano-api.cabal | 1 + .../gen/Test/Hedgehog/Golden/ErrorMessage.hs | 68 +++++++++++++++++-- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/cardano-api/cardano-api.cabal b/cardano-api/cardano-api.cabal index cb01ebedd..2be6a6857 100644 --- a/cardano-api/cardano-api.cabal +++ b/cardano-api/cardano-api.cabal @@ -303,6 +303,7 @@ library gen tasty, tasty-hedgehog, text, + Diff, test-suite cardano-api-test import: project-config diff --git a/cardano-api/gen/Test/Hedgehog/Golden/ErrorMessage.hs b/cardano-api/gen/Test/Hedgehog/Golden/ErrorMessage.hs index 4994beb98..a948e1808 100644 --- a/cardano-api/gen/Test/Hedgehog/Golden/ErrorMessage.hs +++ b/cardano-api/gen/Test/Hedgehog/Golden/ErrorMessage.hs @@ -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 @@ -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 @@ -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") @@ -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 @@ -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