;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2016 David Craven ;;; Copyright © 2016 Eric Le Bihan ;;; Copyright © 2016 Nils Gillmann ;;; Copyright © 2017 Ben Woodcroft ;;; Copyright © 2017, 2018 Nikolai Merinov ;;; Copyright © 2017 Efraim Flashner ;;; Copyright © 2018 Tobias Geerinckx-Rice ;;; Copyright © 2018 Danny Milosavljevic ;;; ;;; This file is part of GNU Guix. ;;; ;;; GNU Guix is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; GNU Guix is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with GNU Guix. If not, see . (define-module (gnu packages rust) #:use-module (gnu packages base) #:use-module (gnu packages bison) #:use-module (gnu packages bootstrap) #:use-module (gnu packages cmake) #:use-module (gnu packages compression) #:use-module (gnu packages curl) #:use-module (gnu packages elf) #:use-module (gnu packages flex) #:use-module (gnu packages gcc) #:use-module (gnu packages gdb) #:use-module (gnu packages jemalloc) #:use-module (gnu packages linux) #:use-module (gnu packages llvm) #:use-module (gnu packages pkg-config) #:use-module (gnu packages python) #:use-module (gnu packages ssh) #:use-module (gnu packages tls) #:use-module (gnu packages version-control) #:use-module (gnu pa
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019-2020, 2022, 2024 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (test-git)
  #:use-module (git)
  #:use-module (guix git)
  #:use-module (guix tests git)
  #:use-module ((guix utils) #:select (call-with-temporary-directory))
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-64)
  #:use-module (srfi srfi-71)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 textual-ports))

;; Test the (guix git) tools.

(test-begin "git")

(test-assert "commit-difference, linear history"
  (with-temporary-git-repository directory
      '((add "a.txt" "A")
        (commit "first commit")
        (add "b.txt" "B")
        (commit "second commit")
        (add "c.txt" "C")
        (commit "third commit")
        (add "d.txt" "D")
        (commit "fourth commit"))
    (with-repository directory repository
      (let ((commit1 (find-commit repository "first"))
            (commit2 (find-commit repository "second"))
            (commit3 (find-commit repository "third"))
            (commit4 (find-commit repository "fourth")))
        (and (lset= eq? (commit-difference commit4 commit1)
                    (list commit2 commit3 commit4))
             (lset= eq? (commit-difference commit4 commit2)
                    (list commit3 commit4))
             (equal? (commit-difference commit3 commit2)
                     (list commit3))

             ;; COMMIT4 is not an ancestor of COMMIT1 so we should get the
             ;; empty list.
             (null? (commit-difference commit1 commit4)))))))

(test-assert "commit-difference, fork"
  (with-temporary-git-repository directory
      '((add "a.txt" "A")
        (commit "first commit")
        (branch "devel")
        (checkout "devel")
        (add "devel/1.txt" "1")
        (commit "first devel commit")
        (add "devel/2.txt" "2")
        (commit "second devel commit")
        (checkout "master")
        (add "b.txt" "B")
        (commit "second commit")
        (add "c.txt" "C")
        (commit "third commit")
        (merge "devel" "merge")
        (add "d.txt" "D")
        (commit "fourth commit"))
    (with-repository directory repository
      (let ((master1 (find-commit repository "first commit"))
            (master2 (find-commit repository "second commit"))
            (master3 (find-commit repository "third commit"))
            (master4 (find-commit repository "fourth commit"))
            (devel1  (find-commit repository "first devel"))
            (devel2  (find-commit repository "second devel"))
            (merge   (find-commit repository "merge")))
        (and (equal? (commit-difference master4 merge)
                     (list master4))
             (lset= eq? (commit-difference master3 master1)
                    (list master3 master2))
             (lset= eq? (commit-difference devel2 master1)
                    (list devel2 devel1))

             ;; The merge occurred between MASTER2 and MASTER4 so here we
             ;; expect to see all the commits from the "devel" branch in
             ;; addition to those on "master".
             (lset= eq? (commit-difference master4 master2)
                    (list master4 merge master3 devel1 devel2)))))))

(test-assert "commit-difference, excluded commits"
  (with-temporary-git-repository directory
      '((add "a.txt" "A")
        (commit "first commit")
        (add "b.txt" "B")
        (commit "second commit")
        (add "c.txt" "C")
        (commit "third commit")
        (add "d.txt" "D")
        (commit "fourth commit")
        (add "e.txt" "E")
        (commit "fifth commit"))
    (with-repository directory repository
      (let ((commit1 (find-commit repository "first"))
            (commit2 (find-commit repository "second"))
            (commit3 (find-commit repository "third"))
            (commit4 (find-commit repository "fourth"))
            (commit5 (find-commit repository "fifth")))
        (and (lset= eq? (commit-difference commit4 commit1 (list commit2))
                    (list commit3 commit4))
             (lset= eq? (commit-difference commit4 commit1 (list commit3))
                    (list commit4))
             (null? (commit-difference commit4 commit1 (list commit5))))))))

(test-equal "commit-relation"
  '(self                                          ;master3 master3
    ancestor                                      ;master1 master3
    descendant                                    ;master3 master1
    unrelated                                     ;master2 branch1
    unrelated                                     ;branch1 master2
    ancestor                                      ;branch1 merge
    descendant                                    ;merge branch1
    ancestor                                      ;master1 merge
    descendant)                                   ;merge master1
  (with-temporary-git-repository directory
      '((add "a.txt" "A")
        (commit "first commit")
        (branch "hack")
        (checkout "hack")
        (add "1.txt" "1")
        (commit "branch commit")
        (checkout "master")
        (add "b.txt" "B")
        (commit "second commit")
        (add "c.txt" "C")
        (commit "third commit")
        (merge "hack" "merge"))
    (with-repository directory repository
      (let ((master1 (find-commit repository "first"))
            (master2 (find-commit repository "second"))
            (master3 (find-commit repository "third"))
            (branch1 (find-commit repository "branch"))
            (merge   (find-commit repository "merge")))
        (list (commit-relation master3 master3)
              (commit-relation master1 master3)
              (commit-relation master3 master1)
              (commit-relation master2 branch1)
              (commit-relation branch1 master2)
              (commit-relation branch1 merge)
              (commit-relation merge branch1)
              (commit-relation master1 merge)
              (commit-relation merge master1))))))

(test-equal "commit-descendant?"
  '((master3 master3 => #t)
    (master1 master3 => #f)
    (master3 master1 => #t)
    (master2 branch1 => #f)
    (master2 branch1 master1 => #t)
    (branch1 master2 => #f)
    (branch1 merge   => #f)
    (merge branch1   => #t)
    (master1 merge   => #f)
    (merge master1   => #t))
  (with-temporary-git-repository directory
      '((add "a.txt" "A")
        (commit "first commit")
        (branch "hack")
        (checkout "hack")
        (add "1.txt" "1")
        (commit "branch commit")
        (checkout "master")
        (add "b.txt" "B")
        (commit "second commit")
        (add "c.txt" "C")
        (commit "third commit")
        (merge "hack" "merge"))
    (with-repository directory repository
      (let ((master1 (find-commit repository "first"))
            (master2 (find-commit repository "second"))
            (master3 (find-commit repository "third"))
            (branch1 (find-commit repository "branch"))
            (merge   (find-commit repository "merge")))
        (letrec-syntax ((verify
                         (syntax-rules ()
                           ((_) '())
                           ((_ (new old ...) rest ...)
                            (cons `(new old ... =>
                                        ,(commit-descendant? new
                                                             (list old ...)))
                                  (verify rest ...))))))
          (verify (master3 master3)
                  (master1 master3)
                  (master3 master1)
                  (master2 branch1)
                  (master2 branch1 master1)
                  (branch1 master2)
                  (branch1 merge)
                  (merge branch1)
                  (master1 merge)
                  (merge master1)))))))

(test-equal "remote-refs"
  '("refs/heads/develop" "refs/heads/master"
    "refs/tags/v1.0" "refs/tags/v1.1")
  (with-temporary-git-repository directory
      '((add "a.txt" "A")
        (commit "First commit")
        (tag "v1.0" "release-1.0")
        (branch "develop")
        (checkout "develop")
        (add "b.txt" "B")
        (commit "Second commit")
        (tag "v1.1" "release-1.1"))
    (remote-refs directory)))

(test-equal "remote-refs: only tags"
 '("refs/tags/v1.0" "refs/tags/v1.1")
  (with-temporary-git-repository directory
      '((add "a.txt" "A")
        (commit "First commit")
        (tag "v1.0" "Release 1.0")
        (add "b.txt" "B")
        (commit "Second commit")
        (tag "v1.1" "Release 1.1"))
    (remote-refs directory #:tags? #t)))

(test-assert "update-cached-checkout, tag"
  (call-with-temporary-directory
   (lambda (cache)
     (with-temporary-git-repository directory
         '((add "a.txt" "A")
           (commit "First commit")
           (tag "v1.0" "release-1.0")
           (branch "develop")
           (checkout "develop")
           (add "b.txt" "B")
           (commit "Second commit")
           (tag "v1.1" "release-1.1"))
       (let ((directory commit relation
                        (update-cached-checkout directory
                                                #:ref '(tag . "v1.1")
                                                #:cache-directory cache))
             (head   (let* ((pipe (open-pipe* OPEN_READ (git-command)
                                              "-C" directory
                                              "rev-parse" "HEAD"))
                            (str  (get-string-all pipe)))
                       (close-pipe pipe)
                       (string-trim-right str))))
         ;; COMMIT should be the ID of the commit object, not that of the tag.
         (string=? commit head))))))

(test-assert "update-cached-checkout, untracked files removed"
  (call-with-temporary-directory
   (lambda (cache)
     (with-temporary-git-repository directory
         '((add "a.txt" "A")
           (add ".gitignore" ".~\n")
           (commit "First commit"))
       (let ((directory commit relation
                        (update-cached-checkout directory
                                                #:ref '()
                                                #:cache-directory cache)))
         (close-port
          (open-output-file (in-vicinity cache "stale-untracked-file")))
         (let ((directory2 commit2 relation2
                           (update-cached-checkout directory
                                                   #:ref '()
                                                   #:cache-directory cache)))
           (not (file-exists?
                 (in-vicinity cache "stale-untracked-file")))))))))

(test-end "git")
fter 'build 'build-minicargo (lambda _ (for-each (lambda (target) (invoke "make" "-f" "minicargo.mk" target)) '("output/libstd.hir" "output/libpanic_unwind.hir" "output/libproc_macro.hir" "output/libtest.hir")) ;; Technically the above already does it - but we want to be clear. (invoke "make" "-C" "tools/minicargo"))) (replace 'install (lambda* (#:key inputs outputs #:allow-other-keys) (let* ((out (assoc-ref outputs "out")) (bin (string-append out "/bin")) (tools-bin (string-append out "/tools/bin")) (cargo-out (assoc-ref outputs "cargo")) (cargo-bin (string-append cargo-out "/bin")) (lib (string-append out "/lib")) (lib/rust (string-append lib "/mrust")) (gcc (assoc-ref inputs "gcc"))) ;; These files are not reproducible. (for-each delete-file (find-files "output" "\\.txt$")) (mkdir-p lib) (copy-recursively "output" lib/rust) (mkdir-p bin) (mkdir-p tools-bin) (install-file "bin/mrustc" bin) ;; minicargo uses relative paths to resolve mrustc. (install-file "tools/bin/minicargo" tools-bin) (install-file "tools/bin/minicargo" cargo-bin) #t)))))) (synopsis "Compiler for the Rust progamming language") (description "Rust is a systems programming language that provides memory safety and thread safety guarantees.") (home-page "https://github.com/thepowersgang/mrustc") ;; Dual licensed. (license (list license:asl2.0 license:expat))))) (define-public rust-1.23 (package (inherit rust-1.19) (name "rust") (version "1.23.0") (source (rust-source version "14fb8vhjzsxlbi6yrn1r6fl5dlbdd1m92dn5zj5gmzfwf4w9ar3l")) (outputs '("out" "doc" "cargo")) (arguments (substitute-keyword-arguments (package-arguments rust-1.19) ((#:phases phases) `(modify-phases ,phases (add-after 'unpack 'dont-build-native (lambda _ ;; XXX: Revisit this when we use gcc 6. (substitute* "src/binaryen/CMakeLists.txt" (("ADD_COMPILE_FLAG\\(\\\"-march=native\\\"\\)") "")) #t)) (add-after 'patch-tests 'patch-cargo-tests (lambda _ (substitute* "src/tools/cargo/tests/build.rs" (("/usr/bin/env") (which "env")) ;; Guix llvm is compiled without asmjs-unknown-emscripten. (("fn wasm32_final_outputs") "#[ignore]\nfn wasm32_final_outputs")) (substitute* "src/tools/cargo/tests/death.rs" ;; This is stuck when built in container. (("fn ctrl_c_kills_everyone") "#[ignore]\nfn ctrl_c_kills_everyone")) ;; Prints test output in the wrong order when built on ;; i686-linux. (substitute* "src/tools/cargo/tests/test.rs" (("fn cargo_test_env") "#[ignore]\nfn cargo_test_env")) #t)) (add-after 'patch-cargo-tests 'ignore-glibc-2.27-incompatible-test ;; https://github.com/rust-lang/rust/issues/47863 (lambda _ (substitute* "src/test/run-pass/out-of-stack.rs" (("// ignore-android") "// ignore-test\n// ignore-android")))) (add-after 'ignore-glibc-2.27-incompatible-test 'fix-mtime-bug (lambda* _ (substitute* "src/build_helper/lib.rs" ;; Bug in Rust code. ;; Current implementation assume that if dst not exist then it's mtime ;; is 0, but in same time "src" have 0 mtime in guix build! (("let threshold = mtime\\(dst\\);") "if !dst.exists() {\nreturn false\n}\n let threshold = mtime(dst);")) #t)) (replace 'configure (lambda* (#:key inputs outputs #:allow-other-keys) (let* ((out (assoc-ref outputs "out")) (doc (assoc-ref outputs "doc")) (gcc (assoc-ref inputs "gcc")) (gdb (assoc-ref inputs "gdb")) (binutils (assoc-ref inputs "binutils")) (python (assoc-ref inputs "python-2")) (rustc (assoc-ref inputs "rustc-bootstrap")) (cargo (assoc-ref inputs "cargo-bootstrap")) (llvm (assoc-ref inputs "llvm")) (jemalloc (assoc-ref inputs "jemalloc"))) (call-with-output-file "config.toml" (lambda (port) (display (string-append " [llvm] [build] cargo = \"" cargo "/bin/cargo" "\" rustc = \"" rustc "/bin/rustc" "\" docs = true python = \"" python "/bin/python2" "\" gdb = \"" gdb "/bin/gdb" "\" vendor = true submodules = false [install] prefix = \"" out "\" docdir = \"" doc "/share/doc/rust" "\" sysconfdir = \"etc\" localstatedir = \"var/lib\" [rust] default-linker = \"" gcc "/bin/gcc" "\" channel = \"stable\" rpath = true " ;; There are 2 failed codegen tests: ;; codegen/mainsubprogram.rs and codegen/mainsubprogramstart.rs ;; These tests require a patched LLVM "codegen-tests = false [target." ,(nix-system->gnu-triplet-for-rust) "] llvm-config = \"" llvm "/bin/llvm-config" "\" cc = \"" gcc "/bin/gcc" "\" cxx = \"" gcc "/bin/g++" "\" ar = \"" binutils "/bin/ar" "\" jemalloc = \"" jemalloc "/lib/libjemalloc_pic.a" "\" [dist] ") port))) #t))) (add-before 'build 'reset-timestamps-after-changes (lambda* _ (define ref (stat "README.md")) (for-each (lambda (filename) (set-file-time filename ref)) (find-files "." #:directories? #t)) #t)) (replace 'build (lambda* _ (invoke "./x.py" "build") (invoke "./x.py" "build" "src/tools/cargo"))) (replace 'check (lambda* _ ;; Disable parallel execution to prevent EAGAIN errors when ;; running tests. (invoke "./x.py" "-j1" "test") (invoke "./x.py" "-j1" "test" "src/tools/cargo"))) (replace 'install (lambda* (#:key outputs #:allow-other-keys) (invoke "./x.py" "install") (substitute* "config.toml" ;; replace prefix to specific output (("prefix = \"[^\"]*\"") (string-append "prefix = \"" (assoc-ref outputs "cargo") "\""))) (invoke "./x.py" "install" "cargo") #t)) (add-after 'install 'wrap-rustc (lambda* (#:key inputs outputs #:allow-other-keys) (let ((out (assoc-ref outputs "out")) (libc (assoc-ref inputs "libc")) (ld-wrapper (assoc-ref inputs "ld-wrapper"))) ;; Let gcc find ld and libc startup files. (wrap-program (string-append out "/bin/rustc") `("PATH" ":" prefix (,(string-append ld-wrapper "/bin"))) `("LIBRARY_PATH" ":" suffix (,(string-append libc "/lib")))) #t))))))))) (define-public rust-1.24 (let ((base-rust (rust-bootstrapped-package rust-1.23 "1.24.1" "1vv10x2h9kq7fxh2v01damdq8pvlp5acyh1kzcda9sfjx12kv99y"))) (package (inherit base-rust) (arguments (substitute-keyword-arguments (package-arguments base-rust) ((#:phases phases) `(modify-phases ,phases (delete 'use-readelf-for-tests) (replace 'patch-aarch64-test (lambda* _ (substitute* "src/librustc_metadata/dynamic_lib.rs" ;; This test is known to fail on aarch64 and powerpc64le: ;; https://github.com/rust-lang/rust/issues/45410 (("fn test_loading_cosine") "#[ignore]\nfn test_loading_cosine")) #t)) (delete 'fix-mtime-bug)))))))) (define-public rust-1.25 (let ((base-rust (rust-bootstrapped-package rust-1.24 "1.25.0" "0baxjr99311lvwdq0s38bipbnj72pn6fgbk6lcq7j555xq53mxpf"))) (package (inherit base-rust) (inputs ;; Use LLVM 6.0 (alist-replace "llvm" (list llvm) (package-inputs base-rust))) (arguments (substitute-keyword-arguments (package-arguments base-rust) ((#:phases phases) `(modify-phases ,phases (add-after 'patch-cargo-tests 'patch-cargo-index-update (lambda _ (substitute* "src/tools/cargo/tests/generate-lockfile.rs" ;; This test wants to update the crate index. (("fn no_index_update") "#[ignore]\nfn no_index_update")) #t)) (add-after 'configure 'enable-codegen-tests (lambda _ (substitute* "config.toml" (("codegen-tests = false") "")) #t)) (replace 'patch-aarch64-test (lambda _ (substitute* "src/librustc_metadata/dynamic_lib.rs" ;; This test is known to fail on aarch64 and powerpc64le: ;; https://github.com/rust-lang/rust/issues/45410 (("fn test_loading_cosine") "#[ignore]\nfn test_loading_cosine")) ;; This test fails on aarch64 with llvm@6.0: ;; https://github.com/rust-lang/rust/issues/49807 ;; other possible solution: ;; https://github.com/rust-lang/rust/pull/47688 (delete-file "src/test/debuginfo/by-value-self-argument-in-trait-impl.rs") #t)) (delete 'ignore-glibc-2.27-incompatible-test)))))))) (define-public rust-1.26 (let ((base-rust (rust-bootstrapped-package rust-1.25 "1.26.2" "0047ais0fvmqvngqkdsxgrzhb0kljg8wy85b01kbbjc88hqcz7pv" #:patches '("rust-coresimd-doctest.patch")))) (package (inherit base-rust) (arguments (substitute-keyword-arguments (package-arguments base-rust) ((#:phases phases) `(modify-phases ,phases ;; binaryen was replaced with LLD project from LLVM (delete 'dont-build-native) (replace 'remove-unsupported-tests (lambda* _ ;; Our ld-wrapper cannot process non-UTF8 bytes in LIBRARY_PATH. ;; (delete-file-recursively "src/test/run-make-fulldeps/linker-output-non-utf8") #t)) (replace 'patch-cargo-tests (lambda* _ (substitute* "src/tools/cargo/tests/testsuite/build.rs" (("/usr/bin/env") (which "env")) ;; Guix llvm is compiled without asmjs-unknown-emscripten. (("fn wasm32_final_outputs") "#[ignore]\nfn wasm32_final_outputs")) (substitute* "src/tools/cargo/tests/testsuite/death.rs" ;; This is stuck when built in container. (("fn ctrl_c_kills_everyone") "#[ignore]\nfn ctrl_c_kills_everyone")) ;; Prints test output in the wrong order when built on ;; i686-linux. (substitute* "src/tools/cargo/tests/testsuite/test.rs" (("fn cargo_test_env") "#[ignore]\nfn cargo_test_env")) #t)) (add-after 'patch-cargo-tests 'disable-cargo-test-for-nightly-channel (lambda* _ ;; This test failed to work on "nightly" channel builds ;; https://github.com/rust-lang/cargo/issues/5648 (substitute* "src/tools/cargo/tests/testsuite/resolve.rs" (("fn test_resolving_minimum_version_with_transitive_deps") "#[ignore]\nfn test_resolving_minimum_version_with_transitive_deps")) #t)) (replace 'patch-cargo-index-update (lambda* _ (substitute* "src/tools/cargo/tests/testsuite/generate_lockfile.rs" ;; This test wants to update the crate index. (("fn no_index_update") "#[ignore]\nfn no_index_update")) #t))))))))) (define-public rust (let ((base-rust (rust-bootstrapped-package rust-1.26 "1.27.0" "089d7rhw55zpvnw71dj8vil6qrylvl4xjr4m8bywjj83d4zq1f9c" #:patches '("rust-coresimd-doctest.patch" "rust-bootstrap-stage0-test.patch")))) (package (inherit base-rust) (arguments (substitute-keyword-arguments (package-arguments base-rust) ((#:phases phases) `(modify-phases ,phases (add-before 'install 'mkdir-prefix-paths (lambda* (#:key outputs #:allow-other-keys) ;; As result of https://github.com/rust-lang/rust/issues/36989 ;; `prefix' directory should exist before `install' call (mkdir-p (assoc-ref outputs "out")) (mkdir-p (assoc-ref outputs "cargo")) #t)))))))))