tests: Mock find-packages-by-name in crate importer test.

* tests/crate.scm: Import only sha256 from (gcrypt hash) as gcrypt-sha256 to
prevent a name collision. Rename test 'cargo-recursive-import' to
'crate-recursive-import' and 'cargo-recursive-import-hoors-existing-packages'
to 'crate-recursive-import-honors-existing-packages'. Mock
find-packages-by-name from (gnu packages). Adjust test to import fake 'bar'
crate instead of doctool.
(test-bar-crate): New variable.
(test-bar-dependencies): New variable.
(test-root-crate): Adjust sha256 -> gcrypt-sha256.
(test-doctool-crate,test-doctool-dependencies): Remove variables.
(rust-leaf-bob-3): New variable.

Signed-off-by: Efraim Flashner <efraim@flashner.co.il>
This commit is contained in:
David Elsing 2023-12-21 22:01:51 +00:00 committed by Efraim Flashner
parent 9f44ff2bb4
commit 53add91be6
No known key found for this signature in database
GPG key ID: 41AAE7DCCA3D8351

View file

@ -25,7 +25,9 @@ (define-module (test-crate)
#:use-module (guix import crate) #:use-module (guix import crate)
#:use-module (guix base32) #:use-module (guix base32)
#:use-module (guix build-system cargo) #:use-module (guix build-system cargo)
#:use-module (gcrypt hash) #:use-module ((gcrypt hash)
#:select ((sha256 . gcrypt-sha256)))
#:use-module (guix packages)
#:use-module (guix tests) #:use-module (guix tests)
#:use-module (gnu packages) #:use-module (gnu packages)
#:use-module (ice-9 iconv) #:use-module (ice-9 iconv)
@ -38,6 +40,8 @@ (define-module (test-crate)
;; foo-1.0.0 ;; foo-1.0.0
;; foo-1.0.3 ;; foo-1.0.3
;; leaf-alice 0.7.5 ;; leaf-alice 0.7.5
;; bar-1.0.0
;; leaf-bob 3.0.1
;; ;;
;; root-1.0.0 ;; root-1.0.0
;; root-1.0.4 ;; root-1.0.4
@ -116,6 +120,40 @@ (define test-foo-dependencies
] ]
}") }")
(define test-bar-crate
"{
\"crate\": {
\"max_version\": \"1.0.0\",
\"name\": \"bar\",
\"description\": \"summary\",
\"homepage\": \"http://example.com\",
\"repository\": \"http://example.com\",
\"keywords\": [\"dummy\", \"test\"],
\"categories\": [\"test\"],
\"actual_versions\": [
{ \"id\": 234100,
\"num\": \"1.0.0\",
\"license\": \"MIT OR Apache-2.0\",
\"links\": {
\"dependencies\": \"/api/v1/crates/bar/1.0.0/dependencies\"
},
\"yanked\": false
}
]
}
}")
(define test-bar-dependencies
"{
\"dependencies\": [
{
\"crate_id\": \"leaf-bob\",
\"kind\": \"normal\",
\"req\": \"3.0.1\"
}
]
}")
(define test-root-crate (define test-root-crate
"{ "{
\"crate\": { \"crate\": {
@ -399,7 +437,7 @@ (define have-guile-semver?
("https://crates.io/api/v1/crates/foo/1.0.3/download" ("https://crates.io/api/v1/crates/foo/1.0.3/download"
(set! test-source-hash (set! test-source-hash
(bytevector->nix-base32-string (bytevector->nix-base32-string
(sha256 (string->bytevector "empty file\n" "utf-8")))) (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(open-input-string "empty file\n")) (open-input-string "empty file\n"))
("https://crates.io/api/v1/crates/foo/1.0.3/dependencies" ("https://crates.io/api/v1/crates/foo/1.0.3/dependencies"
(open-input-string test-foo-dependencies)) (open-input-string test-foo-dependencies))
@ -408,7 +446,7 @@ (define have-guile-semver?
("https://crates.io/api/v1/crates/leaf-alice/0.7.5/download" ("https://crates.io/api/v1/crates/leaf-alice/0.7.5/download"
(set! test-source-hash (set! test-source-hash
(bytevector->nix-base32-string (bytevector->nix-base32-string
(sha256 (string->bytevector "empty file\n" "utf-8")))) (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(open-input-string "empty file\n")) (open-input-string "empty file\n"))
("https://crates.io/api/v1/crates/leaf-alice/0.7.5/dependencies" ("https://crates.io/api/v1/crates/leaf-alice/0.7.5/dependencies"
(open-input-string test-leaf-alice-dependencies)) (open-input-string test-leaf-alice-dependencies))
@ -442,7 +480,7 @@ (define have-guile-semver?
(pk 'fail x #f))))) (pk 'fail x #f)))))
(unless have-guile-semver? (test-skip 1)) (unless have-guile-semver? (test-skip 1))
(test-assert "cargo-recursive-import" (test-assert "crate-recursive-import"
;; Replace network resources with sample data. ;; Replace network resources with sample data.
(mock ((guix http-client) http-fetch (mock ((guix http-client) http-fetch
(lambda (url . rest) (lambda (url . rest)
@ -452,7 +490,7 @@ (define have-guile-semver?
("https://crates.io/api/v1/crates/root/1.0.4/download" ("https://crates.io/api/v1/crates/root/1.0.4/download"
(set! test-source-hash (set! test-source-hash
(bytevector->nix-base32-string (bytevector->nix-base32-string
(sha256 (string->bytevector "empty file\n" "utf-8")))) (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(open-input-string "empty file\n")) (open-input-string "empty file\n"))
("https://crates.io/api/v1/crates/root/1.0.4/dependencies" ("https://crates.io/api/v1/crates/root/1.0.4/dependencies"
(open-input-string test-root-dependencies)) (open-input-string test-root-dependencies))
@ -461,7 +499,7 @@ (define have-guile-semver?
("https://crates.io/api/v1/crates/intermediate-a/1.0.42/download" ("https://crates.io/api/v1/crates/intermediate-a/1.0.42/download"
(set! test-source-hash (set! test-source-hash
(bytevector->nix-base32-string (bytevector->nix-base32-string
(sha256 (string->bytevector "empty file\n" "utf-8")))) (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(open-input-string "empty file\n")) (open-input-string "empty file\n"))
("https://crates.io/api/v1/crates/intermediate-a/1.0.42/dependencies" ("https://crates.io/api/v1/crates/intermediate-a/1.0.42/dependencies"
(open-input-string test-intermediate-a-dependencies)) (open-input-string test-intermediate-a-dependencies))
@ -470,7 +508,7 @@ (define have-guile-semver?
("https://crates.io/api/v1/crates/intermediate-b/1.2.3/download" ("https://crates.io/api/v1/crates/intermediate-b/1.2.3/download"
(set! test-source-hash (set! test-source-hash
(bytevector->nix-base32-string (bytevector->nix-base32-string
(sha256 (string->bytevector "empty file\n" "utf-8")))) (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(open-input-string "empty file\n")) (open-input-string "empty file\n"))
("https://crates.io/api/v1/crates/intermediate-b/1.2.3/dependencies" ("https://crates.io/api/v1/crates/intermediate-b/1.2.3/dependencies"
(open-input-string test-intermediate-b-dependencies)) (open-input-string test-intermediate-b-dependencies))
@ -479,7 +517,7 @@ (define have-guile-semver?
("https://crates.io/api/v1/crates/intermediate-c/1.0.1/download" ("https://crates.io/api/v1/crates/intermediate-c/1.0.1/download"
(set! test-source-hash (set! test-source-hash
(bytevector->nix-base32-string (bytevector->nix-base32-string
(sha256 (string->bytevector "empty file\n" "utf-8")))) (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(open-input-string "empty file\n")) (open-input-string "empty file\n"))
("https://crates.io/api/v1/crates/intermediate-c/1.0.1/dependencies" ("https://crates.io/api/v1/crates/intermediate-c/1.0.1/dependencies"
(open-input-string test-intermediate-c-dependencies)) (open-input-string test-intermediate-c-dependencies))
@ -488,7 +526,7 @@ (define have-guile-semver?
("https://crates.io/api/v1/crates/leaf-alice/0.7.5/download" ("https://crates.io/api/v1/crates/leaf-alice/0.7.5/download"
(set! test-source-hash (set! test-source-hash
(bytevector->nix-base32-string (bytevector->nix-base32-string
(sha256 (string->bytevector "empty file\n" "utf-8")))) (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(open-input-string "empty file\n")) (open-input-string "empty file\n"))
("https://crates.io/api/v1/crates/leaf-alice/0.7.5/dependencies" ("https://crates.io/api/v1/crates/leaf-alice/0.7.5/dependencies"
(open-input-string test-leaf-alice-dependencies)) (open-input-string test-leaf-alice-dependencies))
@ -497,7 +535,7 @@ (define have-guile-semver?
("https://crates.io/api/v1/crates/leaf-bob/3.0.1/download" ("https://crates.io/api/v1/crates/leaf-bob/3.0.1/download"
(set! test-source-hash (set! test-source-hash
(bytevector->nix-base32-string (bytevector->nix-base32-string
(sha256 (string->bytevector "empty file\n" "utf-8")))) (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(open-input-string "empty file\n")) (open-input-string "empty file\n"))
("https://crates.io/api/v1/crates/leaf-bob/3.0.1/dependencies" ("https://crates.io/api/v1/crates/leaf-bob/3.0.1/dependencies"
(open-input-string test-leaf-bob-dependencies)) (open-input-string test-leaf-bob-dependencies))
@ -814,85 +852,74 @@ (define-public 'rust-root-1
(define test-doctool-crate (define rust-leaf-bob-3
"{ (package
\"crate\": { (name "rust-leaf-bob")
\"max_version\": \"2.2.2\", (version "3.0.1")
\"name\": \"leaf-bob\", (source #f)
\"description\": \"summary\", (build-system #f)
\"homepage\": \"http://example.com\", (home-page #f)
\"repository\": \"http://example.com\", (synopsis #f)
\"keywords\": [\"dummy\", \"test\"], (description #f)
\"categories\": [\"test\"] (license #f)))
\"actual_versions\": [
{ \"id\": 234280,
\"num\": \"2.2.2\",
\"license\": \"MIT OR Apache-2.0\",
\"links\": {
\"dependencies\": \"/api/v1/crates/doctool/2.2.2/dependencies\"
},
\"yanked\": false
}
]
}
}")
;; FIXME: This test depends on some existing packages
(define test-doctool-dependencies
"{
\"dependencies\": [
{
\"crate_id\": \"docopt\",
\"kind\": \"normal\",
\"req\": \"^0.8.1\"
}
]
}")
(test-assert "self-test: rust-docopt 0.8.x is gone, please adjust the test case"
(not (null? (find-packages-by-name "rust-docopt" "0.8"))))
(unless have-guile-semver? (test-skip 1)) (unless have-guile-semver? (test-skip 1))
(test-assert "cargo-recursive-import-hoors-existing-packages" (test-assert "crate-recursive-import-honors-existing-packages"
(mock ((guix http-client) http-fetch (mock
(lambda (url . rest) ((gnu packages) find-packages-by-name
(match url (lambda* (name #:optional version)
("https://crates.io/api/v1/crates/doctool" (match name
(open-input-string test-doctool-crate)) ("rust-leaf-bob"
("https://crates.io/api/v1/crates/doctool/2.2.2/download" (list rust-leaf-bob-3))
(set! test-source-hash (_ '()))))
(bytevector->nix-base32-string (mock
(sha256 (string->bytevector "empty file\n" "utf-8")))) ((guix http-client) http-fetch
(open-input-string "empty file\n")) (lambda (url . rest)
("https://crates.io/api/v1/crates/doctool/2.2.2/dependencies" (match url
(open-input-string test-doctool-dependencies)) ("https://crates.io/api/v1/crates/bar"
(_ (error "Unexpected URL: " url))))) (open-input-string test-bar-crate))
(match (crate-recursive-import "doctool") ("https://crates.io/api/v1/crates/bar/1.0.0/download"
(((define-public 'rust-doctool-2 (set! test-source-hash
(package (bytevector->nix-base32-string
(name "rust-doctool") (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(version "2.2.2") (open-input-string "empty file\n"))
(source ("https://crates.io/api/v1/crates/bar/1.0.0/dependencies"
(origin (open-input-string test-bar-dependencies))
(method url-fetch) ("https://crates.io/api/v1/crates/leaf-bob"
(uri (crate-uri "doctool" version)) (open-input-string test-leaf-bob-crate))
(file-name ("https://crates.io/api/v1/crates/leaf-bob/3.0.2/download"
(string-append name "-" version ".tar.gz")) (set! test-source-hash
(sha256 (bytevector->nix-base32-string
(base32 (gcrypt-sha256 (string->bytevector "empty file\n" "utf-8"))))
(? string? hash))))) (open-input-string "empty file\n"))
(build-system cargo-build-system) ("https://crates.io/api/v1/crates/leaf-bob/3.0.2/dependencies"
(arguments (open-input-string test-leaf-bob-dependencies))
('quasiquote (#:cargo-inputs (_ (error "Unexpected URL: " url)))))
(("rust-docopt" (match (crate-recursive-import "bar")
('unquote 'rust-docopt-0.8)))))) (((define-public 'rust-bar-1
(home-page "http://example.com") (package
(synopsis "summary") (name "rust-bar")
(description "summary") (version "1.0.0")
(license (list license:expat license:asl2.0))))) (source
#t) (origin
(x (method url-fetch)
(pk 'fail x #f))))) (uri (crate-uri "bar" version))
(file-name
(string-append name "-" version ".tar.gz"))
(sha256
(base32
(? string? hash)))))
(build-system cargo-build-system)
(arguments
('quasiquote (#:cargo-inputs
(("rust-leaf-bob"
('unquote 'rust-leaf-bob-3))))))
(home-page "http://example.com")
(synopsis "summary")
(description "summary")
(license (list license:expat license:asl2.0)))))
#t)
(x
(pk 'fail x #f))))))
(test-end "crate") (test-end "crate")