Skip to content

Commit 11797c6

Browse files
authored
Merge pull request #258 from vaibhavsagar/cereal-test-suite
Use `cereal` instead of `binary` in -nar test suite
2 parents 1c401b6 + 414bae5 commit 11797c6

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

hnix-store-nar/hnix-store-nar.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ test-suite nar
9898
base
9999
, hnix-store-nar
100100
, base64-bytestring
101-
, binary
101+
, cereal
102102
, bytestring
103103
, containers
104104
, directory

hnix-store-nar/tests/NarFormat.hs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import Control.Applicative (many, optional, (<|>))
77
import qualified Control.Concurrent as Concurrent
88
import Control.Exception (SomeException, try)
99
import Control.Monad (replicateM, void, forM_, when)
10-
import Data.Binary (Binary(..), decodeFile)
11-
import Data.Binary.Get (Get, getByteString,
10+
import Data.Serialize (Serialize(..))
11+
import Data.Serialize (Get, getByteString,
1212
getInt64le,
1313
getLazyByteString, runGet)
14-
import Data.Binary.Put (Put, putInt64le,
15-
putLazyByteString, runPut)
14+
import Data.Serialize (Putter, putInt64le,
15+
putByteString, runPut)
1616
import Data.Bool (bool)
1717
import qualified Data.ByteString as BS
18-
import qualified Data.ByteString.Base64.Lazy as B64
18+
import qualified Data.ByteString.Base64 as B64
1919
import qualified Data.ByteString.Char8 as BSC
2020
import qualified Data.ByteString.Lazy as BSL
2121
import qualified Data.ByteString.Lazy.Char8 as BSLC
@@ -53,11 +53,11 @@ import GHC.Stats
5353
#endif
5454

5555

56-
withBytesAsHandle :: BSLC.ByteString -> (IO.Handle -> IO a) -> IO a
56+
withBytesAsHandle :: BSC.ByteString -> (IO.Handle -> IO a) -> IO a
5757
withBytesAsHandle bytes act = do
5858
Temp.withSystemTempFile "nar-test-file-XXXXX" $ \tmpFile h -> do
5959
IO.hClose h
60-
BSL.writeFile tmpFile bytes
60+
BSC.writeFile tmpFile bytes
6161
IO.withFile tmpFile IO.ReadMode act
6262

6363
spec_narEncoding :: Spec
@@ -85,14 +85,14 @@ spec_narEncoding = do
8585
res' <- Temp.withSystemTempFile "nar-test-file-hnix" $ \tmpFile h -> do
8686
buildNarIO narEffectsIO packageFilePath h
8787
IO.hClose h
88-
BSL.readFile tmpFile
88+
BSC.readFile tmpFile
8989

9090
res' `shouldBe` runPut (putNar n)
9191

9292
-- For a Haskell embedded Nar, check that encoding it gives
9393
-- the same bytestring as `nix-store --dump`
9494
let
95-
encEqualsNixStore :: Nar -> BSL.ByteString -> IO ()
95+
encEqualsNixStore :: Nar -> BSC.ByteString -> IO ()
9696
encEqualsNixStore n b = runPut (putNar n) `shouldBe` b
9797

9898

@@ -159,7 +159,7 @@ test_nixStoreBigDir = packThenExtract "bigdir" $ \baseDir -> do
159159

160160

161161
prop_narEncodingArbitrary :: Nar -> Property
162-
prop_narEncodingArbitrary n = runGet getNar (runPut $ putNar n) === n
162+
prop_narEncodingArbitrary n = runGet getNar (runPut $ putNar n) === Right n
163163

164164
unit_packSelfSrcDir :: HU.Assertion
165165
unit_packSelfSrcDir = Temp.withSystemTempDirectory "nar-test" $ \tmpDir -> do
@@ -472,42 +472,42 @@ sampleDirWithManyFiles nFiles =
472472
-- check our Haskell NAR generator against `nix-store`
473473

474474
-- "hi" file turned to a NAR with `nix-store --dump`, Base64 encoded
475-
sampleRegularBaseline :: BSL.ByteString
476-
sampleRegularBaseline = B64.decodeLenient $ BSL.concat
475+
sampleRegularBaseline :: BSC.ByteString
476+
sampleRegularBaseline = B64.decodeLenient $ BSC.concat
477477
["DQAAAAAAAABuaXgtYXJjaGl2ZS0xAAAAAQAAAAAAAAAoAAAAAAA"
478478
,"AAAQAAAAAAAAAdHlwZQAAAAAHAAAAAAAAAHJlZ3VsYXIACAAAAA"
479479
,"AAAABjb250ZW50cwMAAAAAAAAAaGkKAAAAAAABAAAAAAAAACkAA"
480480
,"AAAAAAA"
481481
]
482482

483-
sampleRegular'Baseline :: BSL.ByteString
484-
sampleRegular'Baseline = B64.decodeLenient $ BSL.concat
483+
sampleRegular'Baseline :: BSC.ByteString
484+
sampleRegular'Baseline = B64.decodeLenient $ BSC.concat
485485
["DQAAAAAAAABuaXgtYXJjaGl2ZS0xAAAAAQAAAAAAAAAoAAAAAAA"
486486
,"AAAQAAAAAAAAAdHlwZQAAAAAHAAAAAAAAAHJlZ3VsYXIACAAAAA"
487487
,"AAAABjb250ZW50c0AAAAAAAAAAI2luY2x1ZGUgPHN0ZGlvLmg+C"
488488
,"gppbnQgbWFpbihpbnQgYXJnYywgY2hhciAqYXJndltdKXsgZXhp"
489489
,"dCAwOyB9CgEAAAAAAAAAKQAAAAAAAAA="
490490
]
491491

492-
sampleExecutableBaseline :: BSL.ByteString
493-
sampleExecutableBaseline = B64.decodeLenient $ BSL.concat
492+
sampleExecutableBaseline :: BSC.ByteString
493+
sampleExecutableBaseline = B64.decodeLenient $ BSC.concat
494494
["DQAAAAAAAABuaXgtYXJjaGl2ZS0xAAAAAQAAAAAAAAAoAAAAAAA"
495495
,"AAAQAAAAAAAAAdHlwZQAAAAAHAAAAAAAAAHJlZ3VsYXIACgAAAA"
496496
,"AAAABleGVjdXRhYmxlAAAAAAAAAAAAAAAAAAAIAAAAAAAAAGNvb"
497497
,"nRlbnRzIgAAAAAAAAAjIS9iaW4vYmFzaAoKZ2NjIC1vIGhlbGxv"
498498
,"IGhlbGxvLmMKAAAAAAAAAQAAAAAAAAApAAAAAAAAAA=="
499499
]
500500

501-
sampleSymLinkBaseline :: BSL.ByteString
502-
sampleSymLinkBaseline = B64.decodeLenient $ BSL.concat
501+
sampleSymLinkBaseline :: BSC.ByteString
502+
sampleSymLinkBaseline = B64.decodeLenient $ BSC.concat
503503
["DQAAAAAAAABuaXgtYXJjaGl2ZS0xAAAAAQAAAAAAAAAoAAAAAAA"
504504
,"AAAQAAAAAAAAAdHlwZQAAAAAHAAAAAAAAAHN5bWxpbmsABgAAAA"
505505
,"AAAAB0YXJnZXQAAAcAAAAAAAAAaGVsbG8uYwABAAAAAAAAACkAA"
506506
,"AAAAAAA"
507507
]
508508

509-
sampleDirectoryBaseline :: BSL.ByteString
510-
sampleDirectoryBaseline = B64.decodeLenient $ BSL.concat
509+
sampleDirectoryBaseline :: BSC.ByteString
510+
sampleDirectoryBaseline = B64.decodeLenient $ BSC.concat
511511
["DQAAAAAAAABuaXgtYXJjaGl2ZS0xAAAAAQAAAAAAAAAoAAAAAAA"
512512
,"AAAQAAAAAAAAAdHlwZQAAAAAJAAAAAAAAAGRpcmVjdG9yeQAAAA"
513513
,"AAAAAFAAAAAAAAAGVudHJ5AAAAAQAAAAAAAAAoAAAAAAAAAAQAA"
@@ -531,8 +531,8 @@ sampleDirectoryBaseline = B64.decodeLenient $ BSL.concat
531531
,"AAAAKQAAAAAAAAABAAAAAAAAACkAAAAAAAAA"
532532
]
533533

534-
sampleLinkToDirectoryBaseline :: BSL.ByteString
535-
sampleLinkToDirectoryBaseline = B64.decodeLenient $ BSL.concat
534+
sampleLinkToDirectoryBaseline :: BSC.ByteString
535+
sampleLinkToDirectoryBaseline = B64.decodeLenient $ BSC.concat
536536
["DQAAAAAAAABuaXgtYXJjaGl2ZS0xAAAAAQAAAAAAAAAoAAAAAAAAAAQAAAAAAAAAdHlwZQAAAAAJ"
537537
,"AAAAAAAAAGRpcmVjdG9yeQAAAAAAAAAFAAAAAAAAAGVudHJ5AAAAAQAAAAAAAAAoAAAAAAAAAAQA"
538538
,"AAAAAAAAbmFtZQAAAAADAAAAAAAAAGZvbwAAAAAABAAAAAAAAABub2RlAAAAAAEAAAAAAAAAKAAA"
@@ -590,7 +590,7 @@ data FileSystemObject =
590590
newtype FilePathPart = FilePathPart { unFilePathPart :: BSC.ByteString }
591591
deriving (Eq, Ord, Show)
592592

593-
instance Binary Nar where
593+
instance Serialize Nar where
594594
get = getNar
595595
put = putNar
596596

@@ -630,8 +630,8 @@ instance Arbitrary FileSystemObject where
630630
pure (nm,f)
631631

632632
------------------------------------------------------------------------------
633-
-- | Serialize Nar to lazy ByteString
634-
putNar :: Nar -> Put
633+
-- | Serialize Nar to ByteString
634+
putNar :: Putter Nar
635635
putNar (Nar file) = header <> parens (putFile file)
636636
where
637637

@@ -642,10 +642,10 @@ putNar (Nar file) = header <> parens (putFile file)
642642
>> (if isExec == Executable
643643
then strs ["executable", ""]
644644
else pure ())
645-
>> putContents fSize contents
645+
>> putContents fSize (BSL.toStrict contents)
646646

647647
putFile (SymLink target) =
648-
strs ["type", "symlink", "target", BSL.fromStrict $ E.encodeUtf8 target]
648+
strs ["type", "symlink", "target", E.encodeUtf8 target]
649649

650650
-- toList sorts the entries by FilePathPart before serializing
651651
putFile (Directory entries) =
@@ -656,29 +656,29 @@ putNar (Nar file) = header <> parens (putFile file)
656656
str "entry"
657657
parens $ do
658658
str "name"
659-
str (BSL.fromStrict name)
659+
str name
660660
str "node"
661661
parens (putFile fso)
662662

663663
parens m = str "(" >> m >> str ")"
664664

665665
-- Do not use this for file contents
666-
str :: BSL.ByteString -> Put
667-
str t = let len = BSL.length t
668-
in int len <> pad len t
666+
str :: Putter BS.ByteString
667+
str t = let len = BS.length t
668+
in int len <> pad (fromIntegral len) t
669669

670-
putContents :: Int64 -> BSL.ByteString -> Put
670+
putContents :: Int64 -> Putter BS.ByteString
671671
putContents fSize bs = str "contents" <> int fSize <> pad fSize bs
672672

673-
int :: Integral a => a -> Put
673+
int :: Integral a => Putter a
674674
int n = putInt64le $ fromIntegral n
675675

676-
pad :: Int64 -> BSL.ByteString -> Put
676+
pad :: Int64 -> Putter BS.ByteString
677677
pad strSize bs = do
678-
putLazyByteString bs
679-
putLazyByteString (BSL.replicate (padLen strSize) 0)
678+
putByteString bs
679+
putByteString (BS.replicate (fromIntegral (padLen strSize)) 0)
680680

681-
strs :: [BSL.ByteString] -> Put
681+
strs :: Putter [BS.ByteString]
682682
strs = mapM_ str
683683

684684
-- | Distance to the next multiple of 8
@@ -687,7 +687,7 @@ padLen n = (8 - n) `mod` 8
687687

688688

689689
------------------------------------------------------------------------------
690-
-- | Deserialize a Nar from lazy ByteString
690+
-- | Deserialize a Nar from ByteString
691691
getNar :: Get Nar
692692
getNar = fmap Nar $ header >> parens getFile
693693
where
@@ -746,3 +746,13 @@ getNar = fmap Nar $ header >> parens getFile
746746
if s == s'
747747
then pure s
748748
else fail "No"
749+
750+
------------------------------------------------------------------------------
751+
-- | Deserialize from binary file
752+
decodeFile :: Serialize a => FilePath -> IO a
753+
decodeFile f = do
754+
bs <- BS.readFile f
755+
let result = runGet get bs
756+
case result of
757+
Left reason -> error reason
758+
Right output -> pure output

0 commit comments

Comments
 (0)