From bcfb8946b3e5f7d297e0a624544e0ea9e7da0d7e Mon Sep 17 00:00:00 2001 From: parsonsmatt Date: Fri, 8 Jan 2021 10:10:18 -0700 Subject: [PATCH] Ignore trailing whitespace when loading file --- src/Test/Aeson/Internal/ADT/GoldenSpecs.hs | 6 +++++- src/Test/Aeson/Internal/GoldenSpecs.hs | 10 +++++++--- test/Test/Aeson/GenericSpecsSpec.hs | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Test/Aeson/Internal/ADT/GoldenSpecs.hs b/src/Test/Aeson/Internal/ADT/GoldenSpecs.hs index 7989e5c..8af5ed9 100644 --- a/src/Test/Aeson/Internal/ADT/GoldenSpecs.hs +++ b/src/Test/Aeson/Internal/ADT/GoldenSpecs.hs @@ -27,6 +27,8 @@ import Data.Aeson (ToJSON, FromJSON) import qualified Data.Aeson as A import Data.Aeson.Encode.Pretty import Data.ByteString.Lazy (writeFile, readFile) +import qualified Data.ByteString.Lazy.Char8 as BS8 +import Data.Maybe (fromMaybe) import Data.Proxy import Prelude hiding (writeFile,readFile) @@ -96,7 +98,7 @@ compareWithGolden randomOption topDir mModuleName typeName cap goldenFile = do sampleSize <- readSampleSize =<< readFile goldenFile newSamples <- mkRandomADTSamplesForConstructor sampleSize (Proxy :: Proxy a) (capConstructor cap) goldenSeed whenFails (writeComparisonFile newSamples) $ do - goldenBytes <- readFile goldenFile + goldenBytes <- stripTrailingNewline <$> readFile goldenFile goldenSamples :: RandomSamples a <- either (throwIO . ErrorCall) return $ A.eitherDecode' goldenBytes @@ -139,6 +141,8 @@ compareWithGolden randomOption topDir mModuleName typeName cap goldenFile = do expectationFailure failureMessage finalResult where + stripTrailingNewline bs = + fromMaybe bs $ BS8.stripSuffix "\n" bs whenFails :: forall b c. IO c -> IO b -> IO b whenFails = flip onException diff --git a/src/Test/Aeson/Internal/GoldenSpecs.hs b/src/Test/Aeson/Internal/GoldenSpecs.hs index 3a0b52c..efb2d21 100644 --- a/src/Test/Aeson/Internal/GoldenSpecs.hs +++ b/src/Test/Aeson/Internal/GoldenSpecs.hs @@ -23,6 +23,8 @@ import Control.Monad import Data.Aeson import Data.Aeson.Encode.Pretty import Data.ByteString.Lazy hiding (putStrLn) +import qualified Data.ByteString.Lazy.Char8 as BS8 +import Data.Maybe (fromMaybe) import Data.Proxy import Data.Typeable @@ -66,7 +68,7 @@ goldenSpecsWithNote settings@Settings{..} proxy mNote = do goldenSpecsWithNotePlain :: forall a. (Arbitrary a, ToJSON a, FromJSON a) => Settings -> TypeNameInfo a -> Maybe String -> Spec goldenSpecsWithNotePlain settings@Settings{..} typeNameInfo@(TypeNameInfo{typeNameTypeName}) mNote = do - let proxy = Proxy :: Proxy a + let proxy = Proxy :: Proxy a let goldenFile = mkGoldenFile typeNameInfo note = maybe "" (" " ++) mNote @@ -77,7 +79,7 @@ goldenSpecsWithNotePlain settings@Settings{..} typeNameInfo@(TypeNameInfo{typeNa then compareWithGolden typeNameInfo proxy goldenFile comparisonFile else createGoldenfile settings proxy goldenFile - + -- | The golden files already exist. Serialize values with the same seed from -- the golden file and compare the with the JSON in the golden file. compareWithGolden :: forall a . @@ -88,7 +90,7 @@ compareWithGolden typeNameInfo proxy goldenFile comparisonFile = do sampleSize <- readSampleSize =<< readFile goldenFile newSamples <- mkRandomSamples sampleSize proxy goldenSeed whenFails (writeComparisonFile newSamples) $ do - goldenBytes <- readFile goldenFile + goldenBytes <- stripTrailingNewline <$> readFile goldenFile goldenSamples :: RandomSamples a <- either (throwIO . ErrorCall) return $ eitherDecode' goldenBytes @@ -106,6 +108,8 @@ compareWithGolden typeNameInfo proxy goldenFile comparisonFile = do writeReencodedComparisonFile goldenSamples expectationFailure $ "Serialization has changed. Compare golden file with " ++ faultyReencodedFilePath ++ "." where + stripTrailingNewline bs = + fromMaybe bs $ BS8.stripSuffix "\n" bs whenFails :: forall b c . IO c -> IO b -> IO b whenFails = flip onException filePath = diff --git a/test/Test/Aeson/GenericSpecsSpec.hs b/test/Test/Aeson/GenericSpecsSpec.hs index 0b42a0d..08ccc07 100644 --- a/test/Test/Aeson/GenericSpecsSpec.hs +++ b/test/Test/Aeson/GenericSpecsSpec.hs @@ -3,6 +3,7 @@ module Test.Aeson.GenericSpecsSpec where import Data.Proxy +import Data.Monoid ((<>)) import System.Directory @@ -246,6 +247,23 @@ spec = do (s1,_) <- hspecSilently $ goldenADTSpecs defaultSettings (Proxy :: Proxy T.Person) summaryFailures s1 `shouldBe` 0 + it "ignores a final newline (GH #12)" $ do + let + goldenBytePlusNewline = + goldenByteIdentical <> "\n" + -- clean up previously existing golden folder + bg <- doesDirectoryExist "golden" + if bg + then removeDirectoryRecursive "golden" + else return () + + -- directly create golden file for tests + createDirectoryIfMissing True "golden/Person" + writeFile "golden/Person/Person.json" goldenBytePlusNewline + + (s1,_) <- hspecSilently $ goldenADTSpecs defaultSettings (Proxy :: Proxy T.Person) + summaryFailures s1 `shouldBe` 0 + it "different random seed but byte-for-byte identical should fail (with custom setting)" $ do -- clean up previously existing golden folder bg <- doesDirectoryExist "golden"