hnix-store-tests (empty) → 0.1.0.0
raw patch · 31 files changed
+955/−0 lines, 31 filesdep +QuickCheckdep +attoparsecdep +base
Dependencies added: QuickCheck, attoparsec, base, bytestring, containers, crypton, dependent-sum, generic-arbitrary, hashable, hnix-store-core, hnix-store-tests, hspec, text, time, unordered-containers, vector
Files
- CHANGELOG.md +9/−0
- LICENSE +201/−0
- README.md +7/−0
- hnix-store-tests.cabal +97/−0
- src/Data/ByteString/Arbitrary.hs +10/−0
- src/Data/HashSet/Arbitrary.hs +11/−0
- src/Data/Text/Arbitrary.hs +16/−0
- src/Data/Vector/Arbitrary.hs +20/−0
- src/System/Nix/Arbitrary.hs +19/−0
- src/System/Nix/Arbitrary/Base.hs +12/−0
- src/System/Nix/Arbitrary/Build.hs +37/−0
- src/System/Nix/Arbitrary/ContentAddress.hs +17/−0
- src/System/Nix/Arbitrary/Derivation.hs +19/−0
- src/System/Nix/Arbitrary/DerivedPath.hs +22/−0
- src/System/Nix/Arbitrary/Hash.hs +50/−0
- src/System/Nix/Arbitrary/OutputName.hs +22/−0
- src/System/Nix/Arbitrary/Realisation.hs +27/−0
- src/System/Nix/Arbitrary/Signature.hs +35/−0
- src/System/Nix/Arbitrary/Store/Types.hs +15/−0
- src/System/Nix/Arbitrary/StorePath.hs +56/−0
- src/System/Nix/Arbitrary/StorePath/Metadata.hs +35/−0
- src/System/Nix/Arbitrary/UTCTime.hs +26/−0
- src/Test/Hspec/Nix.hs +29/−0
- tests/BaseEncodingSpec.hs +30/−0
- tests/ContentAddressSpec.hs +16/−0
- tests/DerivationSpec.hs +27/−0
- tests/DerivedPathSpec.hs +17/−0
- tests/RealisationSpec.hs +26/−0
- tests/SignatureSpec.hs +15/−0
- tests/Spec.hs +1/−0
- tests/StorePathSpec.hs +31/−0
+ CHANGELOG.md view
@@ -0,0 +1,9 @@+# 0.1.0.0 2024-07-31++* Initial release++---++`hnix-store-tests` uses [PVP Versioning][1].++[1]: https://pvp.haskell.org
+ LICENSE view
@@ -0,0 +1,201 @@+ Apache License+ Version 2.0, January 2004+ http://www.apache.org/licenses/++ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION++ 1. Definitions.++ "License" shall mean the terms and conditions for use, reproduction,+ and distribution as defined by Sections 1 through 9 of this document.++ "Licensor" shall mean the copyright owner or entity authorized by+ the copyright owner that is granting the License.++ "Legal Entity" shall mean the union of the acting entity and all+ other entities that control, are controlled by, or are under common+ control with that entity. For the purposes of this definition,+ "control" means (i) the power, direct or indirect, to cause the+ direction or management of such entity, whether by contract or+ otherwise, or (ii) ownership of fifty percent (50%) or more of the+ outstanding shares, or (iii) beneficial ownership of such entity.++ "You" (or "Your") shall mean an individual or Legal Entity+ exercising permissions granted by this License.++ "Source" form shall mean the preferred form for making modifications,+ including but not limited to software source code, documentation+ source, and configuration files.++ "Object" form shall mean any form resulting from mechanical+ transformation or translation of a Source form, including but+ not limited to compiled object code, generated documentation,+ and conversions to other media types.++ "Work" shall mean the work of authorship, whether in Source or+ Object form, made available under the License, as indicated by a+ copyright notice that is included in or attached to the work+ (an example is provided in the Appendix below).++ "Derivative Works" shall mean any work, whether in Source or Object+ form, that is based on (or derived from) the Work and for which the+ editorial revisions, annotations, elaborations, or other modifications+ represent, as a whole, an original work of authorship. For the purposes+ of this License, Derivative Works shall not include works that remain+ separable from, or merely link (or bind by name) to the interfaces of,+ the Work and Derivative Works thereof.++ "Contribution" shall mean any work of authorship, including+ the original version of the Work and any modifications or additions+ to that Work or Derivative Works thereof, that is intentionally+ submitted to Licensor for inclusion in the Work by the copyright owner+ or by an individual or Legal Entity authorized to submit on behalf of+ the copyright owner. For the purposes of this definition, "submitted"+ means any form of electronic, verbal, or written communication sent+ to the Licensor or its representatives, including but not limited to+ communication on electronic mailing lists, source code control systems,+ and issue tracking systems that are managed by, or on behalf of, the+ Licensor for the purpose of discussing and improving the Work, but+ excluding communication that is conspicuously marked or otherwise+ designated in writing by the copyright owner as "Not a Contribution."++ "Contributor" shall mean Licensor and any individual or Legal Entity+ on behalf of whom a Contribution has been received by Licensor and+ subsequently incorporated within the Work.++ 2. Grant of Copyright License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ copyright license to reproduce, prepare Derivative Works of,+ publicly display, publicly perform, sublicense, and distribute the+ Work and such Derivative Works in Source or Object form.++ 3. Grant of Patent License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ (except as stated in this section) patent license to make, have made,+ use, offer to sell, sell, import, and otherwise transfer the Work,+ where such license applies only to those patent claims licensable+ by such Contributor that are necessarily infringed by their+ Contribution(s) alone or by combination of their Contribution(s)+ with the Work to which such Contribution(s) was submitted. If You+ institute patent litigation against any entity (including a+ cross-claim or counterclaim in a lawsuit) alleging that the Work+ or a Contribution incorporated within the Work constitutes direct+ or contributory patent infringement, then any patent licenses+ granted to You under this License for that Work shall terminate+ as of the date such litigation is filed.++ 4. Redistribution. You may reproduce and distribute copies of the+ Work or Derivative Works thereof in any medium, with or without+ modifications, and in Source or Object form, provided that You+ meet the following conditions:++ (a) You must give any other recipients of the Work or+ Derivative Works a copy of this License; and++ (b) You must cause any modified files to carry prominent notices+ stating that You changed the files; and++ (c) You must retain, in the Source form of any Derivative Works+ that You distribute, all copyright, patent, trademark, and+ attribution notices from the Source form of the Work,+ excluding those notices that do not pertain to any part of+ the Derivative Works; and++ (d) If the Work includes a "NOTICE" text file as part of its+ distribution, then any Derivative Works that You distribute must+ include a readable copy of the attribution notices contained+ within such NOTICE file, excluding those notices that do not+ pertain to any part of the Derivative Works, in at least one+ of the following places: within a NOTICE text file distributed+ as part of the Derivative Works; within the Source form or+ documentation, if provided along with the Derivative Works; or,+ within a display generated by the Derivative Works, if and+ wherever such third-party notices normally appear. The contents+ of the NOTICE file are for informational purposes only and+ do not modify the License. You may add Your own attribution+ notices within Derivative Works that You distribute, alongside+ or as an addendum to the NOTICE text from the Work, provided+ that such additional attribution notices cannot be construed+ as modifying the License.++ You may add Your own copyright statement to Your modifications and+ may provide additional or different license terms and conditions+ for use, reproduction, or distribution of Your modifications, or+ for any such Derivative Works as a whole, provided Your use,+ reproduction, and distribution of the Work otherwise complies with+ the conditions stated in this License.++ 5. Submission of Contributions. Unless You explicitly state otherwise,+ any Contribution intentionally submitted for inclusion in the Work+ by You to the Licensor shall be under the terms and conditions of+ this License, without any additional terms or conditions.+ Notwithstanding the above, nothing herein shall supersede or modify+ the terms of any separate license agreement you may have executed+ with Licensor regarding such Contributions.++ 6. Trademarks. This License does not grant permission to use the trade+ names, trademarks, service marks, or product names of the Licensor,+ except as required for reasonable and customary use in describing the+ origin of the Work and reproducing the content of the NOTICE file.++ 7. Disclaimer of Warranty. Unless required by applicable law or+ agreed to in writing, Licensor provides the Work (and each+ Contributor provides its Contributions) on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or+ implied, including, without limitation, any warranties or conditions+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A+ PARTICULAR PURPOSE. You are solely responsible for determining the+ appropriateness of using or redistributing the Work and assume any+ risks associated with Your exercise of permissions under this License.++ 8. Limitation of Liability. In no event and under no legal theory,+ whether in tort (including negligence), contract, or otherwise,+ unless required by applicable law (such as deliberate and grossly+ negligent acts) or agreed to in writing, shall any Contributor be+ liable to You for damages, including any direct, indirect, special,+ incidental, or consequential damages of any character arising as a+ result of this License or out of the use or inability to use the+ Work (including but not limited to damages for loss of goodwill,+ work stoppage, computer failure or malfunction, or any and all+ other commercial damages or losses), even if such Contributor+ has been advised of the possibility of such damages.++ 9. Accepting Warranty or Additional Liability. While redistributing+ the Work or Derivative Works thereof, You may choose to offer,+ and charge a fee for, acceptance of support, warranty, indemnity,+ or other liability obligations and/or rights consistent with this+ License. However, in accepting such obligations, You may act only+ on Your own behalf and on Your sole responsibility, not on behalf+ of any other Contributor, and only if You agree to indemnify,+ defend, and hold each Contributor harmless for any liability+ incurred by, or claims asserted against, such Contributor by reason+ of your accepting any such warranty or additional liability.++ END OF TERMS AND CONDITIONS++ APPENDIX: How to apply the Apache License to your work.++ To apply the Apache License to your work, attach the following+ boilerplate notice, with the fields enclosed by brackets "[]"+ replaced with your own identifying information. (Don't include+ the brackets!) The text should be enclosed in the appropriate+ comment syntax for the file format. We also recommend that a+ file or class name and description of purpose be included on the+ same "printed page" as the copyright notice for easier+ identification within third-party archives.++ Copyright 2018 Shea Levy.++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.
+ README.md view
@@ -0,0 +1,7 @@+# hnix-store-tests++Arbitrary instances for core types, roundtrip property tests, utility functions+and a harness for running tests that require nix-store+with nix-daemon.++[Test.Hspec.Nix]: ./src/Test/Hspec/Nix.hs
+ hnix-store-tests.cabal view
@@ -0,0 +1,97 @@+cabal-version: 2.2+name: hnix-store-tests+version: 0.1.0.0+synopsis: Test utilities and instances+description:+ This package contains Arbitrary instances for core+ types, roundtrip property tests, utility functions+ and a harness for running tests that require nix-store+ with nix-daemon.+homepage: https://github.com/haskell-nix/hnix-store+license: Apache-2.0+license-file: LICENSE+author: Sorki+maintainer: srk@48.io+copyright: 2023 Sorki+category: Nix+build-type: Simple+extra-doc-files:+ CHANGELOG.md+extra-source-files:+ README.md++common commons+ ghc-options: -Wall+ default-extensions:+ DerivingStrategies+ , DerivingVia+ , FlexibleInstances+ , ScopedTypeVariables+ , StandaloneDeriving+ , RecordWildCards+ , TypeApplications+ , LambdaCase+ default-language: Haskell2010++library+ import: commons+ exposed-modules:+ Data.ByteString.Arbitrary+ , Data.HashSet.Arbitrary+ , Data.Text.Arbitrary+ , Data.Vector.Arbitrary+ , System.Nix.Arbitrary+ , System.Nix.Arbitrary.Base+ , System.Nix.Arbitrary.Build+ , System.Nix.Arbitrary.ContentAddress+ , System.Nix.Arbitrary.Derivation+ , System.Nix.Arbitrary.DerivedPath+ , System.Nix.Arbitrary.Hash+ , System.Nix.Arbitrary.OutputName+ , System.Nix.Arbitrary.Realisation+ , System.Nix.Arbitrary.Signature+ , System.Nix.Arbitrary.Store.Types+ , System.Nix.Arbitrary.StorePath+ , System.Nix.Arbitrary.StorePath.Metadata+ , System.Nix.Arbitrary.UTCTime+ , Test.Hspec.Nix+ build-depends:+ base >=4.12 && <5+ , hnix-store-core >= 0.8+ , bytestring+ , containers+ , crypton+ , dependent-sum > 0.7+ , generic-arbitrary < 1.1+ , hashable+ , hspec+ , QuickCheck+ , text+ , time+ , unordered-containers+ , vector+ hs-source-dirs: src++test-suite props+ import: commons+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ BaseEncodingSpec+ ContentAddressSpec+ DerivationSpec+ DerivedPathSpec+ RealisationSpec+ StorePathSpec+ SignatureSpec+ hs-source-dirs:+ tests+ build-tool-depends:+ hspec-discover:hspec-discover+ build-depends:+ base+ , hnix-store-core+ , hnix-store-tests+ , attoparsec+ , text+ , hspec
+ src/Data/ByteString/Arbitrary.hs view
@@ -0,0 +1,10 @@+{-# OPTIONS_GHC -Wno-orphans #-}+module Data.ByteString.Arbitrary () where++import Data.ByteString (ByteString)+import Test.QuickCheck (Arbitrary(..))+import qualified Data.ByteString.Char8++instance Arbitrary ByteString where+ arbitrary = Data.ByteString.Char8.pack <$> arbitrary+ shrink xs = Data.ByteString.Char8.pack <$> shrink (Data.ByteString.Char8.unpack xs)
+ src/Data/HashSet/Arbitrary.hs view
@@ -0,0 +1,11 @@+{-# OPTIONS_GHC -fno-warn-orphans #-}+module Data.HashSet.Arbitrary where++import Data.Hashable (Hashable)+import Data.HashSet (HashSet)+import Test.QuickCheck (Arbitrary(..))+import qualified Data.HashSet++instance (Hashable a, Eq a, Arbitrary a) => Arbitrary (HashSet a) where+ arbitrary = Data.HashSet.fromList <$> arbitrary+ shrink hashset = Data.HashSet.fromList <$> shrink (Data.HashSet.toList hashset)
+ src/Data/Text/Arbitrary.hs view
@@ -0,0 +1,16 @@+{-# OPTIONS_GHC -Wno-orphans #-}+module Data.Text.Arbitrary () where++import Data.Text (Text)+import Test.QuickCheck (Arbitrary(..), frequency, suchThat)+import qualified Data.Text++instance Arbitrary Text where+ arbitrary = Data.Text.pack <$> arbitrary+ shrink xs = Data.Text.pack <$> shrink (Data.Text.unpack xs)++instance {-# OVERLAPPING #-} Arbitrary (Maybe Text) where+ arbitrary = frequency+ [ (1, pure Nothing)+ , (3, Just <$> arbitrary `suchThat` (/= mempty))+ ]
+ src/Data/Vector/Arbitrary.hs view
@@ -0,0 +1,20 @@+{-# OPTIONS_GHC -Wno-orphans #-}+-- Stolen from quickcheck-instances (BSD-3)+module Data.Vector.Arbitrary () where++import Data.Vector (Vector)+import Test.QuickCheck (Arbitrary(..), Arbitrary1(..), arbitrary1, shrink1)+import qualified Data.Vector++instance Arbitrary1 Vector where+ liftArbitrary =+ fmap Data.Vector.fromList+ . liftArbitrary+ liftShrink shr =+ fmap Data.Vector.fromList+ . liftShrink shr+ . Data.Vector.toList++instance Arbitrary a => Arbitrary (Vector a) where+ arbitrary = arbitrary1+ shrink = shrink1
+ src/System/Nix/Arbitrary.hs view
@@ -0,0 +1,19 @@+module System.Nix.Arbitrary where++import Data.ByteString.Arbitrary ()+import Data.HashSet.Arbitrary ()+import Data.Text.Arbitrary ()+import Data.Vector.Arbitrary ()++import System.Nix.Arbitrary.Base ()+import System.Nix.Arbitrary.Build ()+import System.Nix.Arbitrary.ContentAddress ()+import System.Nix.Arbitrary.Derivation ()+import System.Nix.Arbitrary.DerivedPath ()+import System.Nix.Arbitrary.Hash ()+import System.Nix.Arbitrary.OutputName ()+import System.Nix.Arbitrary.Realisation ()+import System.Nix.Arbitrary.Signature ()+import System.Nix.Arbitrary.Store.Types ()+import System.Nix.Arbitrary.StorePath ()+import System.Nix.Arbitrary.StorePath.Metadata ()
+ src/System/Nix/Arbitrary/Base.hs view
@@ -0,0 +1,12 @@+-- due to recent generic-arbitrary+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.Base where++import System.Nix.Base++import Test.QuickCheck (Arbitrary(..))+import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))++deriving via GenericArbitrary BaseEncoding+ instance Arbitrary BaseEncoding
+ src/System/Nix/Arbitrary/Build.hs view
@@ -0,0 +1,37 @@+-- due to recent generic-arbitrary+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.Build where++import Data.Time (UTCTime)+import Data.Text.Arbitrary ()+import Test.QuickCheck (Arbitrary(..), scale, suchThat)+import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))+import System.Nix.Arbitrary.OutputName ()+import System.Nix.Arbitrary.Realisation ()+import System.Nix.Arbitrary.UTCTime ()++import System.Nix.Build++import qualified Data.Time.Clock.POSIX++deriving via GenericArbitrary BuildMode+ instance Arbitrary BuildMode++deriving via GenericArbitrary BuildStatus+ instance Arbitrary BuildStatus++instance Arbitrary BuildResult where+ arbitrary = do+ buildResultStatus <- arbitrary+ buildResultErrorMessage <- arbitrary+ buildResultTimesBuilt <- arbitrary `suchThat` (/= Just 0)+ buildResultIsNonDeterministic <- arbitrary `suchThat` (/= Nothing)+ buildResultStartTime <- arbitrary `suchThat` (/= Just t0)+ buildResultStopTime <- arbitrary `suchThat` (/= Just t0)+ buildResultBuiltOutputs <- scale (`div` 10) (arbitrary `suchThat` (/= Nothing))++ pure BuildResult{..}+ where+ t0 :: UTCTime+ t0 = Data.Time.Clock.POSIX.posixSecondsToUTCTime 0
+ src/System/Nix/Arbitrary/ContentAddress.hs view
@@ -0,0 +1,17 @@+-- due to recent generic-arbitrary+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.ContentAddress where++import System.Nix.Arbitrary.Hash ()+import System.Nix.Arbitrary.Store.Types ()+import System.Nix.ContentAddress (ContentAddress, ContentAddressMethod)++import Test.QuickCheck (Arbitrary(..))+import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))++deriving via GenericArbitrary ContentAddressMethod+ instance Arbitrary ContentAddressMethod++deriving via GenericArbitrary ContentAddress+ instance Arbitrary ContentAddress
+ src/System/Nix/Arbitrary/Derivation.hs view
@@ -0,0 +1,19 @@+-- due to recent generic-arbitrary+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.Derivation where++import Data.Text (Text)+import Data.Text.Arbitrary ()+import Data.Vector.Arbitrary ()+import System.Nix.Derivation+import System.Nix.StorePath (StorePath)++import Test.QuickCheck (Arbitrary(..))+import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))+import System.Nix.Arbitrary.StorePath ()++deriving via GenericArbitrary (Derivation StorePath Text)+ instance Arbitrary (Derivation StorePath Text)+deriving via GenericArbitrary (DerivationOutput StorePath Text)+ instance Arbitrary (DerivationOutput StorePath Text)
+ src/System/Nix/Arbitrary/DerivedPath.hs view
@@ -0,0 +1,22 @@+-- due to recent generic-arbitrary+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.DerivedPath where++import qualified Data.Set+import Test.QuickCheck (Arbitrary(..), oneof)+import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))+import System.Nix.Arbitrary.OutputName ()+import System.Nix.Arbitrary.StorePath ()+import System.Nix.DerivedPath (DerivedPath, OutputsSpec(..))++instance Arbitrary OutputsSpec where+ arbitrary = oneof+ [ pure OutputsSpec_All+ , OutputsSpec_Names+ . Data.Set.fromList+ <$> ((:) <$> arbitrary <*> arbitrary)+ ]++deriving via GenericArbitrary DerivedPath+ instance Arbitrary DerivedPath
+ src/System/Nix/Arbitrary/Hash.hs view
@@ -0,0 +1,50 @@+-- due to recent generic-arbitrary+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.Hash where++import Data.ByteString (ByteString)+import Data.ByteString.Arbitrary ()+import Crypto.Hash (Digest, MD5(..), SHA1(..), SHA256(..), SHA512(..))+import Data.Dependent.Sum (DSum((:=>)))+import Data.Some (Some(Some))+import System.Nix.Hash (HashAlgo(..))++import Test.QuickCheck (Arbitrary(arbitrary), oneof)++import qualified Crypto.Hash++-- * Arbitrary @Digest@s++instance Arbitrary (Digest MD5) where+ arbitrary = Crypto.Hash.hash @ByteString <$> arbitrary++instance Arbitrary (Digest SHA1) where+ arbitrary = Crypto.Hash.hash @ByteString <$> arbitrary++instance Arbitrary (Digest SHA256) where+ arbitrary = Crypto.Hash.hash @ByteString <$> arbitrary++instance Arbitrary (Digest SHA512) where+ arbitrary = Crypto.Hash.hash @ByteString <$> arbitrary++-- * Arbitrary @DSum HashAlgo Digest@s++instance Arbitrary (DSum HashAlgo Digest) where+ arbitrary = oneof+ [ (HashAlgo_MD5 :=>) <$> arbitrary+ , (HashAlgo_SHA1 :=>) <$> arbitrary+ , (HashAlgo_SHA256 :=>) <$> arbitrary+ , (HashAlgo_SHA512 :=>) <$> arbitrary+ ]++instance Arbitrary (Some HashAlgo) where+ arbitrary =+ oneof+ $ pure+ <$> [+ Some HashAlgo_MD5+ , Some HashAlgo_SHA1+ , Some HashAlgo_SHA256+ , Some HashAlgo_SHA512+ ]
+ src/System/Nix/Arbitrary/OutputName.hs view
@@ -0,0 +1,22 @@+{-# LANGUAGE CPP #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.OutputName where++import System.Nix.OutputName (OutputName)+import qualified Data.Text+import qualified System.Nix.OutputName++import Test.QuickCheck (Arbitrary(arbitrary), choose, elements, vectorOf)++instance Arbitrary OutputName where+ arbitrary =+ either (error . show) id+ . System.Nix.OutputName.mkOutputName+ . Data.Text.pack <$> ((:) <$> s1 <*> limited sn)+ where+ alphanum = ['a' .. 'z'] <> ['A' .. 'Z'] <> ['0' .. '9']+ s1 = elements $ alphanum <> "+-_?="+ sn = elements $ alphanum <> "+-._?="+ limited n = do+ k <- choose (0, 210)+ vectorOf k n
+ src/System/Nix/Arbitrary/Realisation.hs view
@@ -0,0 +1,27 @@+-- due to Illegal equational constraint Test.QuickCheck.Arbitrary.Generic.TypesDiffer+{-# LANGUAGE TypeFamilies #-}+-- due to recent generic-arbitrary+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.Realisation where++import System.Nix.Arbitrary.Hash ()+import System.Nix.Arbitrary.OutputName ()+import System.Nix.Arbitrary.Signature ()+import System.Nix.Arbitrary.StorePath ()+import System.Nix.Realisation (DerivationOutput, Realisation)++import Test.QuickCheck (Arbitrary(..))+import Test.QuickCheck.Arbitrary.Generic (Arg, GenericArbitrary(..), genericArbitrary, genericShrink)++instance+ ( Arg (DerivationOutput outputName) outputName+ , Arbitrary outputName+ ) =>+ Arbitrary (DerivationOutput outputName)+ where+ arbitrary = genericArbitrary+ shrink = genericShrink++deriving via GenericArbitrary Realisation+ instance Arbitrary Realisation
+ src/System/Nix/Arbitrary/Signature.hs view
@@ -0,0 +1,35 @@+-- due to recent generic-arbitrary+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+{-# LANGUAGE OverloadedStrings #-}+module System.Nix.Arbitrary.Signature where++import qualified Crypto.PubKey.Ed25519+import Crypto.Random (drgNewTest, withDRG)+import qualified Data.ByteString as BS+import qualified Data.Text as Text+import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))+import Test.QuickCheck++import System.Nix.Signature++instance Arbitrary Crypto.PubKey.Ed25519.Signature where+ arbitrary = do+ seeds <- (,,,,) <$> arbitraryBoundedRandom <*> arbitraryBoundedRandom <*> arbitraryBoundedRandom <*> arbitraryBoundedRandom <*> arbitraryBoundedRandom+ let drg = drgNewTest seeds+ (secretKey, _) = withDRG drg Crypto.PubKey.Ed25519.generateSecretKey+ publicKey = Crypto.PubKey.Ed25519.toPublic secretKey+ msg :: BS.ByteString = "msg"+ pure $ Crypto.PubKey.Ed25519.sign secretKey publicKey msg++deriving via GenericArbitrary Signature+ instance Arbitrary Signature++instance Arbitrary NarSignature where+ arbitrary = do+ name <- Text.pack . getPrintableString <$> suchThat arbitrary (\(PrintableString str) -> validName str)+ NarSignature name <$> arbitrary++validName :: String -> Bool+validName txt = not (null txt) && not (elem ':' txt)+
+ src/System/Nix/Arbitrary/Store/Types.hs view
@@ -0,0 +1,15 @@+-- due to recent generic-arbitrary+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.Store.Types where++import System.Nix.Store.Types++import Test.QuickCheck (Arbitrary(..))+import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))++deriving via GenericArbitrary FileIngestionMethod+ instance Arbitrary FileIngestionMethod++deriving via GenericArbitrary RepairMode+ instance Arbitrary RepairMode
+ src/System/Nix/Arbitrary/StorePath.hs view
@@ -0,0 +1,56 @@+{-# LANGUAGE CPP #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.StorePath where++#if !MIN_VERSION_base(4,18,0)+import Control.Applicative (liftA2)+#endif+import Crypto.Hash (MD5, SHA1, SHA256, SHA512)+import qualified Data.ByteString.Char8+import qualified Data.Text+import System.Nix.StorePath (StoreDir(..)+ , StorePath+ , StorePathName+ , StorePathHashPart+ )+import qualified System.Nix.StorePath++import Test.QuickCheck (Arbitrary(arbitrary), choose, elements, oneof, vectorOf)++instance Arbitrary StoreDir where+ arbitrary =+ StoreDir+ . (Data.ByteString.Char8.singleton '/' <>) -- TODO(srk): nasty, see #237+ . Data.ByteString.Char8.pack <$> arbitrary++instance Arbitrary StorePath where+ arbitrary =+ liftA2 System.Nix.StorePath.unsafeMakeStorePath+ arbitrary+ arbitrary++instance Arbitrary StorePathName where+ arbitrary =+ either undefined id+ . System.Nix.StorePath.mkStorePathName+ . Data.Text.pack <$> ((:) <$> s1 <*> limited sn)+ where+ alphanum = ['a' .. 'z'] <> ['A' .. 'Z'] <> ['0' .. '9']+ s1 = elements $ alphanum <> "+-_?="+ sn = elements $ alphanum <> "+-._?="+ limited n = do+ k <- choose (0, 210)+ vectorOf k n++instance Arbitrary StorePathHashPart where+ arbitrary =+ oneof+ [ System.Nix.StorePath.mkStorePathHashPart @MD5+ . Data.ByteString.Char8.pack <$> arbitrary+ , System.Nix.StorePath.mkStorePathHashPart @SHA1+ . Data.ByteString.Char8.pack <$> arbitrary+ , System.Nix.StorePath.mkStorePathHashPart @SHA256+ . Data.ByteString.Char8.pack <$> arbitrary+ , System.Nix.StorePath.mkStorePathHashPart @SHA512+ . Data.ByteString.Char8.pack <$> arbitrary+ ]
+ src/System/Nix/Arbitrary/StorePath/Metadata.hs view
@@ -0,0 +1,35 @@+-- due to recent generic-arbitrary+{-# LANGUAGE CPP #-}+{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-}+{-# OPTIONS_GHC -Wno-orphans #-}+module System.Nix.Arbitrary.StorePath.Metadata where++import Data.Dependent.Sum (DSum((:=>)))+import Data.HashSet.Arbitrary ()+import System.Nix.Arbitrary.ContentAddress ()+import System.Nix.Arbitrary.Hash ()+import System.Nix.Arbitrary.Signature ()+import System.Nix.Arbitrary.StorePath ()+import System.Nix.Arbitrary.UTCTime ()+import System.Nix.StorePath (StorePath)+import System.Nix.StorePath.Metadata (Metadata(..), StorePathTrust)++import qualified System.Nix.Hash++import Test.QuickCheck (Arbitrary(..), suchThat)+import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..))++deriving via GenericArbitrary StorePathTrust+ instance Arbitrary StorePathTrust++instance Arbitrary (Metadata StorePath) where+ arbitrary = do+ metadataDeriverPath <- arbitrary+ metadataNarHash <- (System.Nix.Hash.HashAlgo_SHA256 :=>) <$> arbitrary+ metadataReferences <- arbitrary+ metadataRegistrationTime <- arbitrary+ metadataNarBytes <- arbitrary `suchThat` (/= Just 0)+ metadataTrust <- arbitrary+ metadataSigs <- arbitrary+ metadataContentAddress <- arbitrary+ pure Metadata{..}
+ src/System/Nix/Arbitrary/UTCTime.hs view
@@ -0,0 +1,26 @@+{-# OPTIONS_GHC -Wno-orphans #-}+-- Stolen from quickcheck-instances (BSD-3)+-- UTCTime/DiffTime slightly modified to produce+-- values rounded to whole seconds+module System.Nix.Arbitrary.UTCTime where++import Data.Time (Day(..), DiffTime, UTCTime(..))+import Test.QuickCheck (Arbitrary(..))++instance Arbitrary Day where+ arbitrary = ModifiedJulianDay <$> (2000 +) <$> arbitrary+ shrink = (ModifiedJulianDay <$>) . shrink . Data.Time.toModifiedJulianDay++instance Arbitrary DiffTime where+ -- without abs something weird happens, try it+ arbitrary = fromInteger . abs <$> arbitrary++instance Arbitrary UTCTime where+ arbitrary =+ UTCTime+ <$> arbitrary+ <*> arbitrary+ shrink ut@(UTCTime day dayTime) =+ [ ut { Data.Time.utctDay = d' } | d' <- shrink day ]+ ++ [ ut { Data.Time.utctDayTime = t' } | t' <- shrink dayTime ]+
+ src/Test/Hspec/Nix.hs view
@@ -0,0 +1,29 @@+module Test.Hspec.Nix+ ( forceRight+ , roundtrips+ ) where++import Test.Hspec (Expectation, shouldBe)++roundtrips+ :: forall a b f+ . ( Applicative f+ , Eq (f a)+ , Show a+ , Show b+ , Show (f a)+ )+ => (a -> b) -- ^ Encode+ -> (b -> f a) -- ^ Decode+ -> a+ -> Expectation+roundtrips encode decode x =+ decode (encode x) `shouldBe` pure x++forceRight+ :: Show a+ => Either a b+ -> b+forceRight = \case+ Right x -> x+ Left e -> error $ "forceRight failed: " ++ show e
+ tests/BaseEncodingSpec.hs view
@@ -0,0 +1,30 @@+module BaseEncodingSpec where++import Test.Hspec (Spec, describe)+import Test.Hspec.QuickCheck (prop)+import Test.Hspec.Nix (roundtrips)++import System.Nix.Base+import System.Nix.Arbitrary ()+import System.Nix.StorePath (StorePathHashPart(..))++spec :: Spec+spec = do+ describe "Hash" $ do+ prop "Base16 roundtrips" $+ roundtrips+ (encodeWith Base16)+ (decodeWith Base16)+ . unStorePathHashPart++ prop "Nix-like Base32 roundtrips" $+ roundtrips+ (encodeWith NixBase32)+ (decodeWith NixBase32)+ . unStorePathHashPart++ prop "Base64 roundtrips" $+ roundtrips+ (encodeWith Base64)+ (decodeWith Base64)+ . unStorePathHashPart
+ tests/ContentAddressSpec.hs view
@@ -0,0 +1,16 @@+module ContentAddressSpec where++import Test.Hspec (Spec, describe)+import Test.Hspec.QuickCheck (prop)+import Test.Hspec.Nix (roundtrips)+import System.Nix.Arbitrary ()++import qualified System.Nix.ContentAddress++spec :: Spec+spec = do+ describe "ContentAddress" $ do+ prop "roundtrips" $+ roundtrips+ System.Nix.ContentAddress.buildContentAddress+ System.Nix.ContentAddress.parseContentAddress
+ tests/DerivationSpec.hs view
@@ -0,0 +1,27 @@+module DerivationSpec where++import Test.Hspec (Spec, describe)+import Test.Hspec.QuickCheck (xprop)+import Test.Hspec.Nix (roundtrips)++import System.Nix.Arbitrary ()+import System.Nix.Derivation (parseDerivation, buildDerivation)++import qualified Data.Attoparsec.Text+import qualified Data.Text.Lazy+import qualified Data.Text.Lazy.Builder++-- TODO(srk): this won't roundtrip as Arbitrary Text+-- contains wild stuff like control characters and UTF8 sequences.+-- Either fix in nix-derivation or use wrapper type+-- (but we use Nix.Derivation.textParser so we need Text for now)+spec :: Spec+spec = do+ describe "Derivation" $ do+ xprop "roundtrips via Text" $ \sd ->+ roundtrips+ ( Data.Text.Lazy.toStrict+ . Data.Text.Lazy.Builder.toLazyText+ . buildDerivation sd+ )+ (Data.Attoparsec.Text.parseOnly (parseDerivation sd))
+ tests/DerivedPathSpec.hs view
@@ -0,0 +1,17 @@+module DerivedPathSpec where++import Test.Hspec (Spec, describe)+import Test.Hspec.QuickCheck (prop)+import Test.Hspec.Nix (roundtrips)++import System.Nix.Arbitrary ()++import qualified System.Nix.DerivedPath++spec :: Spec+spec = do+ describe "DerivedPath" $ do+ prop "roundtrips" $ \sd ->+ roundtrips+ (System.Nix.DerivedPath.derivedPathToText sd)+ (System.Nix.DerivedPath.parseDerivedPath sd)
+ tests/RealisationSpec.hs view
@@ -0,0 +1,26 @@+module RealisationSpec where++import Test.Hspec (Spec, describe)+import Test.Hspec.QuickCheck (prop)+import Test.Hspec.Nix (roundtrips)++import System.Nix.Arbitrary ()++import qualified Data.Text.Lazy+import qualified Data.Text.Lazy.Builder+import qualified System.Nix.OutputName+import qualified System.Nix.Realisation++spec :: Spec+spec = do+ describe "DerivationOutput" $ do+ prop "roundtrips" $+ roundtrips+ ( Data.Text.Lazy.toStrict+ . Data.Text.Lazy.Builder.toLazyText+ . System.Nix.Realisation.derivationOutputBuilder+ System.Nix.OutputName.unOutputName+ )+ ( System.Nix.Realisation.derivationOutputParser+ System.Nix.OutputName.mkOutputName+ )
+ tests/SignatureSpec.hs view
@@ -0,0 +1,15 @@+module SignatureSpec where++import Test.Hspec (Spec, describe)+import Test.Hspec.Nix (roundtrips)+import Test.Hspec.QuickCheck (prop)++import System.Nix.Signature (signatureToText, parseSignature, narSignatureToText, parseNarSignature)+import System.Nix.Arbitrary ()++spec :: Spec+spec = do+ describe "Signature" $ do+ prop "roundtrips" $ roundtrips signatureToText parseSignature+ describe "NarSignature" $ do+ prop "roundtrips" $ roundtrips narSignatureToText parseNarSignature
+ tests/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
+ tests/StorePathSpec.hs view
@@ -0,0 +1,31 @@+module StorePathSpec where++import Test.Hspec (Spec, describe)+import Test.Hspec.QuickCheck (prop)+import Test.Hspec.Nix (roundtrips)++import System.Nix.Arbitrary ()+import System.Nix.StorePath++import qualified Data.Attoparsec.Text++spec :: Spec+spec = do+ describe "StorePath" $ do+ prop "roundtrips using parsePath . storePathToRawFilePath" $+ \storeDir ->+ roundtrips+ (storePathToRawFilePath storeDir)+ (parsePath storeDir)++ prop "roundtrips using parsePathFromText . storePathToText" $+ \storeDir ->+ roundtrips+ (storePathToText storeDir)+ (parsePathFromText storeDir)++ prop "roundtrips using pathParser . storePathToText" $+ \storeDir ->+ roundtrips+ (storePathToText storeDir)+ (Data.Attoparsec.Text.parseOnly $ pathParser storeDir)