Skip to content

Commit 2b8324d

Browse files
Migrate SRIHash tests to lib
1 parent 16c1165 commit 2b8324d

File tree

13 files changed

+149
-85
lines changed

13 files changed

+149
-85
lines changed

app/spago.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ package:
5353
- prelude
5454
- record
5555
- refs
56+
- registry-lib
5657
- safe-coerce
5758
- spec
5859
- strings

app/test/Foreign/Tar.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import Node.FS.Stats as FS.Stats
1212
import Node.Path as Path
1313
import Registry.SRIHash as SRIHash
1414
import Test.Spec as Spec
15+
import Test.Spec.Assertions (AnyShow(..))
1516
import Test.Spec.Assertions as Assert
16-
import Test.Utils (shouldEqual)
1717

1818
tar :: Spec.Spec Unit
1919
tar = do
@@ -26,7 +26,7 @@ tar = do
2626
tarball1 <- createTarball
2727
Aff.delay (Aff.Milliseconds 1010.0)
2828
tarball2 <- createTarball
29-
tarball1 `shouldEqual` tarball2
29+
AnyShow tarball1 `Assert.shouldEqual` AnyShow tarball2
3030
where
3131
createTarball = do
3232
packageTmp <- liftEffect Tmp.mkTmpDir

app/test/Main.purs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import Test.Fixture.Manifest as Fixture
3535
import Test.Foreign.JsonRepair as Foreign.JsonRepair
3636
import Test.Foreign.Licensee (licensee)
3737
import Test.Foreign.Tar as Foreign.Tar
38-
import Test.Registry.Hash as Registry.Hash
3938
import Test.Registry.Index as Registry.Index
4039
import Test.Registry.PackageSet as PackageSet
4140
import Test.Registry.SSH as SSH
@@ -89,8 +88,6 @@ main = launchAff_ do
8988
Spec.describe "Encoding examples" (manifestExamplesRoundtrip manifestExamplePaths)
9089
Spec.describe "Registry Index" do
9190
Registry.Index.spec registryEnv
92-
Spec.describe "Hash" do
93-
Registry.Hash.testHash
9491
Spec.describe "Tar" do
9592
Foreign.Tar.tar
9693
Spec.describe "Json Repair" do

app/test/Registry/Hash.purs

Lines changed: 0 additions & 68 deletions
This file was deleted.

app/test/Utils.purs

Lines changed: 0 additions & 11 deletions
This file was deleted.

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@
9696
registry-test = ''
9797
cd $(git rev-parse --show-toplevel)
9898
npm ci
99-
spago test
99+
spago test -p registry-app
100+
spago test -p registry-lib -m Test.Registry
100101
'';
101102

102103
registry-check-format = ''

lib/spago.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ package:
1111
- prelude
1212
- strings
1313
test_dependencies:
14+
- argonaut-core
1415
- spec
16+
- sunde

lib/test/Assert.purs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Test.Assert where
2+
3+
import Prelude
4+
5+
import Control.Monad.Error.Class (class MonadThrow)
6+
import Data.Argonaut.Core as Argonaut
7+
import Data.Either (Either(..))
8+
import Effect.Exception (Error)
9+
import Test.Spec.Assertions (AnyShow(..))
10+
import Test.Spec.Assertions as Assertions
11+
import Unsafe.Coerce (unsafeCoerce)
12+
13+
shouldEqual :: forall m a. MonadThrow Error m => Eq a => a -> a -> m Unit
14+
shouldEqual a b = Assertions.shouldEqual (AnyShow a) (AnyShow b)
15+
16+
shouldEqualRight :: forall m e a. MonadThrow Error m => Eq a => a -> Either e a -> m Unit
17+
shouldEqualRight a = case _ of
18+
Left e -> Assertions.fail (Argonaut.stringify (unsafeCoerce e :: Argonaut.Json))
19+
Right b -> Assertions.shouldEqual (AnyShow a) (AnyShow b)

lib/test/Registry.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Test.Registry where
2+
3+
import Prelude
4+
5+
import Effect (Effect)
6+
import Effect.Aff as Aff
7+
import Test.Registry.SRIHash as Test.SRIHash
8+
import Test.Spec as Spec
9+
import Test.Spec.Reporter as Spec.Reporter
10+
import Test.Spec.Runner as Spec.Runner
11+
12+
main :: Effect Unit
13+
main = Aff.launchAff_ $ Spec.Runner.runSpec [ Spec.Reporter.consoleReporter ] do
14+
Spec.describe "SRIHash" Test.SRIHash.spec

lib/test/Registry/SRIHash.purs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module Test.Registry.SRIHash (spec) where
2+
3+
import Prelude
4+
5+
import Control.Monad.Except (ExceptT(..))
6+
import Control.Monad.Except as Except
7+
import Data.Either (Either(..))
8+
import Data.Maybe (Maybe(..))
9+
import Data.String as String
10+
import Effect.Aff (Aff)
11+
import Node.ChildProcess as Process
12+
import Node.Path (FilePath)
13+
import Node.Path as Path
14+
import Registry.SRIHash as SRIHash
15+
import Sunde as Sunde
16+
import Test.Assert as Assert
17+
import Test.Spec as Spec
18+
import Test.Utils as Utils
19+
20+
spec :: Spec.Spec Unit
21+
spec = do
22+
Spec.describe "Creates a valid sha256 SRI hash" do
23+
Spec.it "Hashes a spago.dhall file the same as openssl" do
24+
hash <- SRIHash.hashFile hooksSpago
25+
hash `Assert.shouldEqual` hooksSpagoHash
26+
27+
Spec.it "Hashes a LICENSE file the same as openssl" do
28+
hash <- SRIHash.hashFile hooksLicense
29+
hash `Assert.shouldEqual` hooksLicenseHash
30+
31+
Spec.it "spago.dhall hash matches the nix hash" do
32+
hash <- SRIHash.hashFile hooksSpago
33+
nix <- Except.runExceptT $ sha256Nix hooksSpago
34+
hash `Assert.shouldEqualRight` nix
35+
36+
Spec.it "LICENSE hash matches the nix hash" do
37+
hash <- SRIHash.hashFile hooksLicense
38+
nix <- Except.runExceptT $ sha256Nix hooksLicense
39+
hash `Assert.shouldEqualRight` nix
40+
41+
Spec.describe "Encodes and decodes from string" do
42+
Spec.it "Round-trips spago.dhall file" do
43+
hooksSpagoHash `Assert.shouldEqualRight` SRIHash.parse (SRIHash.print hooksSpagoHash)
44+
45+
Spec.it "Round-trips LICENSE file" do
46+
hooksLicenseHash `Assert.shouldEqualRight` SRIHash.parse (SRIHash.print hooksLicenseHash)
47+
48+
-- Test hash produced by `openssl`:
49+
-- openssl dgst -sha256 -binary < test/_fixtures/halogen-hooks/spago.dhall | openssl base64 -A
50+
hooksSpagoHash :: SRIHash.SRIHash
51+
hooksSpagoHash = Utils.fromRight "Failed to parse SRIHash" $ SRIHash.parse "sha256-fN9RUAzN21ZY4Y0UwqUSxwUPVz1g7/pcqoDvbJZoT04="
52+
53+
hooksSpago :: FilePath
54+
hooksSpago = Path.concat [ "test", "_fixtures", "halogen-hooks", "spago.dhall" ]
55+
56+
-- Test hash produced by `openssl`:
57+
-- openssl dgst -sha256 -binary < test/_fixtures/LICENSE | openssl base64 -A
58+
hooksLicenseHash :: SRIHash.SRIHash
59+
hooksLicenseHash = Utils.fromRight "Failed to parse SRIHash" $ SRIHash.parse "sha256-wOzNcCq20TAL/LMT1lYIiaoEIFGDBw+yp14bj7qK9v4="
60+
61+
hooksLicense :: FilePath
62+
hooksLicense = Path.concat [ "test", "_fixtures", "halogen-hooks", "LICENSE" ]
63+
64+
sha256Nix :: FilePath -> ExceptT String Aff SRIHash.SRIHash
65+
sha256Nix path = ExceptT do
66+
-- In Nix 2.4 this will become `nix hash file`
67+
let args = [ "hash-file", "--sri", path ]
68+
result <- Sunde.spawn { cmd: "nix", args, stdin: Nothing } Process.defaultSpawnOptions
69+
pure $ case result.exit of
70+
Process.Normally 0 -> do
71+
SRIHash.parse $ String.trim result.stdout
72+
_ ->
73+
Left result.stderr

0 commit comments

Comments
 (0)