+(test-assert "derivation fails but keep going"
+ ;; In keep-going mode, 'build-derivations' should fail because of D1, but it
+ ;; must return only after D2 has succeeded.
+ (with-store store
+ (let* ((d1 (derivation %store "fails"
+ %bash `("-c" "false")
+ #:inputs `((,%bash))))
+ (d2 (build-expression->derivation %store "sleep-then-succeed"
+ `(begin
+ ,(random-text)
+ ;; XXX: Hopefully that's long
+ ;; enough that D1 has already
+ ;; failed.
+ (sleep 2)
+ (mkdir %output)))))
+ (set-build-options %store
+ #:use-substitutes? #f
+ #:keep-going? #t)
+ (guard (c ((store-protocol-error? c)
+ (and (= 100 (store-protocol-error-status c))
+ (string-contains (store-protocol-error-message c)
+ (derivation-file-name d1))
+ (not (valid-path? %store (derivation->output-path d1)))
+ (valid-path? %store (derivation->output-path d2)))))
+ (build-derivations %store (list d1 d2))
+ #f))))
+
+(test-assert "identical files are deduplicated"
+ (let* ((build1 (add-text-to-store %store "one.sh"
+ "echo hello, world > \"$out\"\n"
+ '()))
+ (build2 (add-text-to-store %store "two.sh"
+ "# Hey!\necho hello, world > \"$out\"\n"
+ '()))
+ (drv1 (derivation %store "foo"
+ %bash `(,build1)
+ #:inputs `((,%bash) (,build1))))
+ (drv2 (derivation %store "bar"
+ %bash `(,build2)
+ #:inputs `((,%bash) (,build2)))))
+ (and (build-derivations %store (list drv1 drv2))
+ (let ((file1 (derivation->output-path drv1))
+ (file2 (derivation->output-path drv2)))
+ (and (valid-path? %store file1) (valid-path? %store file2)
+ (string=? (call-with-input-file file1 get-string-all)
+ "hello, world\n")
+ (= (stat:ino (lstat file1))
+ (stat:ino (lstat file2))))))))
+
+(test-equal "built-in-builders"
+ '("download")
+ (built-in-builders %store))
+
+(test-assert "unknown built-in builder"
+ (let ((drv (derivation %store "ohoh" "builtin:does-not-exist" '())))
+ (guard (c ((store-protocol-error? c)
+ (string-contains (store-protocol-error-message c) "failed")))
+ (build-derivations %store (list drv))
+ #f)))
+
+(unless (http-server-can-listen?)
+ (test-skip 1))
+(test-assert "'download' built-in builder"
+ (let ((text (random-text)))
+ (with-http-server 200 text
+ (let* ((drv (derivation %store "world"
+ "builtin:download" '()
+ #:env-vars `(("url"
+ . ,(object->string (%local-url))))
+ #:hash-algo 'sha256
+ #:hash (sha256 (string->utf8 text)))))
+ (and (build-derivations %store (list drv))
+ (string=? (call-with-input-file (derivation->output-path drv)
+ get-string-all)
+ text))))))
+
+(unless (http-server-can-listen?)
+ (test-skip 1))
+(test-assert "'download' built-in builder, invalid hash"
+ (with-http-server 200 "hello, world!"
+ (let* ((drv (derivation %store "world"
+ "builtin:download" '()
+ #:env-vars `(("url"
+ . ,(object->string (%local-url))))
+ #:hash-algo 'sha256
+ #:hash (sha256 (random-bytevector 100))))) ;wrong
+ (guard (c ((store-protocol-error? c)
+ (string-contains (store-protocol-error-message c) "failed")))
+ (build-derivations %store (list drv))
+ #f))))
+
+(unless (http-server-can-listen?)
+ (test-skip 1))
+(test-assert "'download' built-in builder, not found"
+ (with-http-server 404 "not found"
+ (let* ((drv (derivation %store "will-never-be-found"
+ "builtin:download" '()
+ #:env-vars `(("url"
+ . ,(object->string (%local-url))))
+ #:hash-algo 'sha256
+ #:hash (sha256 (random-bytevector 100)))))
+ (guard (c ((store-protocol-error? c)
+ (string-contains (store-protocol-error-message (pk c)) "failed")))
+ (build-derivations %store (list drv))
+ #f))))
+
+(test-assert "'download' built-in builder, not fixed-output"
+ (let* ((source (add-text-to-store %store "hello" "hi!"))
+ (url (string-append "file://" source))
+ (drv (derivation %store "world"
+ "builtin:download" '()
+ #:env-vars `(("url" . ,(object->string url))))))
+ (guard (c ((store-protocol-error? c)
+ (string-contains (store-protocol-error-message c) "failed")))
+ (build-derivations %store (list drv))
+ #f)))
+
+(unless (http-server-can-listen?)
+ (test-skip 1))
+(test-assert "'download' built-in builder, check mode"
+ ;; Make sure rebuilding the 'builtin:download' derivation in check mode
+ ;; works. See <http://bugs.gnu.org/25089>.
+ (let* ((text (random-text))
+ (drv (derivation %store "world"
+ "builtin:download" '()
+ #:env-vars `(("url"
+ . ,(object->string (%local-url))))
+ #:hash-algo 'sha256
+ #:hash (sha256 (string->utf8 text)))))
+ (and (with-http-server 200 text
+ (build-derivations %store (list drv)))
+ (with-http-server 200 text
+ (build-derivations %store (list drv)
+ (build-mode check)))
+ (string=? (call-with-input-file (derivation->output-path drv)
+ get-string-all)
+ text))))
+
+(test-equal "derivation-name"
+ "foo-0.0"
+ (let ((drv (derivation %store "foo-0.0" %bash '())))
+ (derivation-name drv)))
+
+(test-equal "derivation-output-names"
+ '(("out") ("bar" "chbouib"))
+ (let ((drv1 (derivation %store "foo-0.0" %bash '()))
+ (drv2 (derivation %store "foo-0.0" %bash '()
+ #:outputs '("bar" "chbouib"))))
+ (list (derivation-output-names drv1)
+ (derivation-output-names drv2))))
+
+(test-assert "offloadable-derivation?"
+ (and (offloadable-derivation? (derivation %store "foo" %bash '()))
+ (offloadable-derivation? ;see <http://bugs.gnu.org/18747>
+ (derivation %store "foo" %bash '()
+ #:substitutable? #f))
+ (not (offloadable-derivation?
+ (derivation %store "foo" %bash '()
+ #:local-build? #t)))))
+
+(test-assert "substitutable-derivation?"
+ (and (substitutable-derivation? (derivation %store "foo" %bash '()))
+ (substitutable-derivation? ;see <http://bugs.gnu.org/18747>
+ (derivation %store "foo" %bash '()
+ #:local-build? #t))
+ (not (substitutable-derivation?
+ (derivation %store "foo" %bash '()
+ #:substitutable? #f)))))
+