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

Ignore trailing newline when loading file #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/Test/Aeson/Internal/ADT/GoldenSpecs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions src/Test/Aeson/Internal/GoldenSpecs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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 .
Expand All @@ -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
Expand All @@ -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 =
Expand Down
18 changes: 18 additions & 0 deletions test/Test/Aeson/GenericSpecsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Test.Aeson.GenericSpecsSpec where

import Data.Proxy
import Data.Monoid ((<>))

import System.Directory

Expand Down Expand Up @@ -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"
Expand Down