aboutsummaryrefslogtreecommitdiff
;;; GNU
# GNU Guix --- Functional package management for GNU
# Copyright © 2012-2015, 2017, 2019, 2022 Ludovic Courtès <ludo@gnu.org>
# Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
# Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
#
# 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/>.

#
# Test the `guix package' command-line utility.  This test requires network
# access and is skipped when that is lacking.
#

guix package --version

readlink_base ()
{
    basename `readlink "$1"`
}

# Return true if a typical shebang in the store would exceed Linux's default
# static limit.
shebang_too_long ()
{
    test `echo $NIX_STORE_DIR/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bootstrap-binaries-0/bin/bash | wc -c` \
	 -ge 128
}

if ! guile -c '(getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)' 2> /dev/null \
	|| shebang_too_long
then
    # Skipping.
    exit 77
fi


profile="t-profile-$$"
profile_alt="t-profile-alt-$$"
rm -f "$profile"

module_dir="t-guix-package-net-$$"
mkdir "$module_dir"

trap 'rm -f "$profile" "$profile_alt" "$profile.lock" "$profile_alt.lock" "$profile-"[0-9]* "$profile_alt-"[0-9]* ; rm -r "$module_dir" t-home-'"$$" EXIT


guix package --bootstrap -p "$profile" -i guile-bootstrap
test -L "$profile" && test -L "$profile-1-link"
test ! -f "$profile-2-link"
test -f "$profile/bin/guile"

boot_make="(@ (guix tests) gnu-make-for-tests)"
boot_make_drv="`guix build -e "$boot_make" | grep -v -e -debug`"
guix package --bootstrap -p "$profile" -i "$boot_make_drv"
test -L "$profile-2-link"
test -f "$profile/bin/make" && test -f "$profile/bin/guile"

# Check whether `--list-installed' works.
# XXX: Change the tests when `--install' properly extracts the package
# name and version string.
installed="`guix package -p "$profile" --list-installed | cut -f1 | xargs echo | sort`"
case "x$installed" in
    "guile-bootstrap make-boot0")
        true;;
    "make-boot0 guile-bootstrap")
        true;;
    "*")
        false;;
esac

test "`guix package -p "$profile" -I 'g.*e' | cut -f1`" = "guile-bootstrap"

guix package --bootstrap -p "$profile_alt" -i gcc-bootstrap
installed="`guix package -p "$profile" -p "$profile_alt" -I | cut -f1 | xargs echo | sort`"
case "x$installed" in
    "gcc-bootstrap guile-bootstrap make-boot0")
        true;;
    "*")
        false;;
esac
test "`guix package -p "$profile_alt" -p "$profile" -I | wc -l`" = "3"
rm "$profile_alt"

# List generations.
test "`guix package -p "$profile" -l | cut -f1 | grep guile | head -n1`" \
     = "  guile-bootstrap"

# Exit with 1 when a generation does not exist.
guix package -p "$profile" --list-generations=42 && false
guix package -p "$profile" --switch-generation=99 && false

# Remove a package.
guix package --bootstrap -p "$profile" -r "guile-bootstrap"
test -L "$profile-3-link"
test -f "$profile/bin/make" && test ! -f "$profile/bin/guile"

# Roll back.
guix package --roll-back -p "$profile"
test "`readlink_base "$profile"`" = "$profile-2-link"
test -x "$profile/bin/guile" && test -x "$profile/bin/make"
guix package --roll-back -p "$profile"
test "`readlink_base "$profile"`" = "$profile-1-link"
test -x "$profile/bin/guile" && test ! -x "$profile/bin/make"

# Switch to the rolled generation and switch back.
guix package -p "$profile" --switch-generation=2
test "`readlink_base "$profile"`" = "$profile-2-link"
guix package -p "$profile" --switch-generation=-1
test "`readlink_base "$profile"`" = "$profile-1-link"

# Move to the empty profile.
for i in `seq 1 3`
do
    guix package --bootstrap --roll-back -p "$profile"
    test ! -f "$profile/bin"
    test ! -f "$profile/lib"
    test "`readlink_base "$profile"`" = "$profile-0-link"
done

# Test that '--list-generations' does not output the zeroth generation.
test -z "`guix package -p "$profile" -l 0`"

# Reinstall after roll-back to the empty profile.
guix package --bootstrap -p "$profile" -e "$boot_make"
test "`readlink_base "$profile"`" = "$profile-1-link"
test -x "$profile/bin/guile" && test ! -x "$profile/bin/make"

# Check that the first generation is the current one.
test "`guix package -p "$profile" -l 1 | cut -f3 | head -n1`" = "(current)"

# Roll-back to generation 0, and install---all at once.
guix package --bootstrap -p "$profile" --roll-back -i guile-bootstrap
test "`readlink_base "$profile"`" = "$profile-1-link"
test -x "$profile/bin/guile" && test ! -x "$profile/bin/make"

# Install Make.
guix package --bootstrap -p "$profile" -e "$boot_make"
test "`readlink_base "$profile"`" = "$profile-2-link"
test -x "$profile/bin/guile" && test -x "$profile/bin/make"
grep "`guix build -e "$boot_make"`" "$profile/manifest"

# Make a "hole" in the list of generations, and make sure we can
# roll back and switch "over" it.
rm "$profile-1-link"
guix package --bootstrap -p "$profile" --roll-back
test "`readlink_base "$profile"`" = "$profile-0-link"
guix package -p "$profile" --switch-generation=+1
test "`readlink_base "$profile"`" = "$profile-2-link"

# Make sure LIBRARY_PATH gets listed by `--search-paths'.
guix package --bootstrap -p "$profile" -i guile-bootstrap -i gcc-bootstrap
guix package -p "$profile" --search-paths | grep LIBRARY_PATH

# Roll back so we can delete #3 below.
guix package -p "$profile" --switch-generation=2

# Delete the third generation and check that it was actually deleted.
guix package -p "$profile" --delete-generations=3
test -z "`guix package -p "$profile" -l 3`"

# Search path of combined profiles.  'LIBRARY_PATH' should show up only in the
# combination, not in the individual profiles.
rm "$profile"
guix package --bootstrap -p "$profile" -i guile-bootstrap
guix package --bootstrap -p "$profile_alt" -i gcc-bootstrap
guix package -p "$profile" --search-paths | grep LIBRARY_PATH && false
guix package -p "$profile" -p "$profile_alt" --search-paths \
     | grep "LIBRARY_PATH.*$profile/lib.$profile_alt/lib"

# Simulate an upgrade and make sure the package order is preserved.
cat > "$module_dir/new.scm" <<EOF
(define-module (new)
  #:use-module (guix)
  #:use-module (gnu packages bootstrap))

(define-public new-guile
  (package (inherit %bootstrap-guile)
           (version (string-append "42." (getenv "V_MINOR")))))
(define-public new-gcc
  (package (inherit %bootstrap-gcc)
           (version (string-append "77." (getenv "V_MINOR")))))
EOF

guix package --bootstrap -p "$profile" -i gcc-bootstrap
installed="`guix package -p "$profile" -I | cut -f1`"

# Dry-run upgrade.  Make sure no new generation is created when things are
# already in store and '-n' is used: <https://issues.guix.gnu.org/53267>.
V_MINOR=0
export V_MINOR
profile_before="$(readlink "$profile")"
guix package -p "$profile" --bootstrap -L "$module_dir" -u # build the profile
guix package -p "$profile" --roll-back
guix package -p "$profile" --bootstrap -L "$module_dir" -u . -n # check '-n'
test "$(readlink "$profile")" = "$profile_before"

for i in 1 2
do
    V_MINOR="$i"
    export V_MINOR

    guix package -p "$profile" --bootstrap -L "$module_dir" -u .
    post_upgrade="`guix package -p "$profile" -I | cut -f1`"
    test "$post_upgrade" = "$installed"
done

#
# Try with the default profile.
#

XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_CACHE_HOME
HOME="$PWD/t-home-$$"
export HOME

mkdir -p "$HOME"

# Get the canonical directory name so that 'guix package' recognizes it.
HOME="`cd $HOME; pwd -P`"

guix package --bootstrap -e "$boot_make"
test -f "$HOME/.guix-profile/bin/make"

guix package --bootstrap --roll-back
test ! -f "$HOME/.guix-profile/bin/make"
ontents\">Gocheck - A rich testing framework for Go\n \nCopyright line\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met: \n\n1. Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer. \n2. Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution. \n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &#34;AS IS&#34; AND\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\nON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n</pre>\n </section>\n <div class=\"License-source\">Source: github.com/go-check/check@v0.0.0-20201128035030-22ab2dfb190c/LICENSE</div>\n </div>\n </div>\n")) `(("https://proxy.golang.org/github.com/go-check/check/@v/v0.0.0-20201130134442-10cb98267c6c.mod" . ,go.mod) ("https://proxy.golang.org/github.com/go-check/check/@latest" . ,version) ("https://proxy.golang.org/github.com/go-check/check/@v/v0.0.0-20201130134442-10cb98267c6c.info" . ,go.info) ("https://github.com/go-check/check?go-get=1" . ,go-get) ("https://pkg.go.dev/github.com/go-check/check" . ,pkg.go.dev) ("https://pkg.go.dev/github.com/go-check/check?tab=licenses" . ,pkg.go.dev-licence) ("https://proxy.golang.org/github.com/go-check/check/@v/list" . "")))) (test-begin "go") ;;; Unit tests for go build-system (test-equal "go-version basic" "v1.0.2" (go-version->git-ref "v1.0.2")) (test-equal "go-version with embedded git-ref" "65e3620a7ae7" (go-version->git-ref "v0.0.0-20190821162956-65e3620a7ae7")) (test-equal "go-version with complex embedded git-ref" "daa7c04131f5" (go-version->git-ref "v1.2.4-0.20191109021931-daa7c04131f5")) (test-assert "go-pseudo-version? multi-digit version number" (go-pseudo-version? "v1.23.1-0.20200526195155-81db48ad09cc")) (test-assert "go-pseudo-version? semantic version with rc" (go-pseudo-version? "v1.4.0-rc.4.0.20200313231945-b860323f09d0")) ;;; Unit tests for (guix import go) (test-equal "go-path-escape" "github.com/!azure/!avere" ((@@ (guix import go) go-path-escape) "github.com/Azure/Avere")) ;; We define a function for all similar tests with different go.mod files (define (testing-parse-mod name expected input) (define (inf? p1 p2) (string<? (car p1) (car p2))) (test-equal name (sort expected inf?) (sort (go.mod-requirements (parse-go.mod input)) inf?))) (testing-parse-mod "parse-go.mod-simple" '(("good/thing" "v1.4.5") ("new/thing/v2" "v2.3.4") ("other/thing" "v1.0.2")) fixture-go-mod-simple) (testing-parse-mod "parse-go.mod-with-block" '(("A" "v1") ("B" "v1.0.0") ("C" "v1.0.0") ("D" "v1.2.3") ("E" "dev")) fixture-go-mod-with-block) (testing-parse-mod "parse-go.mod-complete" '(("github.com/corp/arbitrary-repo" "v0.0.2") ("quoted.example.com/abitrary/repo" "v0.0.2") ("one.example.com/abitrary/repo" "v1.1.111") ("hub.jazz.net/git/user/project/sub/directory" "v1.1.19") ("hub.jazz.net/git/user/project" "v1.1.18") ("launchpad.net/~user/project/branch/sub/directory" "v1.1.17") ("launchpad.net/~user/project/branch" "v1.1.16") ("launchpad.net/project/series/sub/directory" "v1.1.15") ("launchpad.net/project/series" "v1.1.14") ("launchpad.net/project" "v1.1.13") ("bitbucket.org/user/project/sub/directory" "v1.11.21") ("bitbucket.org/user/project" "v1.11.20") ("k8s.io/kubernetes/subproject" "v1.1.101") ("github.com/user/project/sub/directory" "v1.1.12") ("github.com/user/project" "v1.1.11") ("github.com/go-check/check" "v0.0.0-20140225173054-eb6ee6f84d0a")) fixture-go-mod-complete) (test-equal "parse-go.mod: simple" `((module (module-path "my/thing")) (go (version "1.12")) (require (module-path "other/thing") (version "v1.0.2")) (require (module-path "new/thing/v2") (version "v2.3.4")) (exclude (module-path "old/thing") (version "v1.2.3")) (replace (original (module-path "bad/thing") (version "v1.4.5")) (with (module-path "good/thing") (version "v1.4.5")))) (parse-go.mod fixture-go-mod-simple)) (test-equal "parse-go.mod: comments and unparsable lines" `((module (module-path "my/thing")) (go (version "1.12") (comment "avoid feature X")) (require (module-path "other/thing") (version "v1.0.2")) (comment "Security issue: CVE-XXXXX") (exclude (module-path "old/thing") (version "v1.2.3")) (unknown "new-directive another/thing yet-another/thing") (replace (original (module-path "bad/thing") (version "v1.4.5")) (with (module-path "good/thing") (version "v1.4.5"))) (comment "Unparseable") (unknown "bad/thing [v1.4.5, v1.9.7] => good/thing v2.0.0")) (parse-go.mod fixture-go-mod-unparsable)) (test-equal "parse-go.mod: retract" `((retract (version "v0.9.1")) (retract (version "v1.9.2")) (retract (range (version "v1.0.0") (version "v1.7.9")))) (parse-go.mod fixture-go-mod-retract)) (test-equal "parse-go.mod: raw strings and quoted strings" `((require (module-path "example.com/\"some-repo\"") (version "v1.9.3")) (require (module-path "example.com/\"another.repo\"") (version "v1.0.0")) (require (module-path "example.com/special!repo") (version "v9.3.1")) (replace (original (module-path "example.com/\"some-repo\"")) (with (module-path "launchpad.net/some-repo") (version "v1.9.3"))) (replace (original (module-path "example.com/\"another.repo\"")) (with (module-path "launchpad.net/another-repo") (version "v1.0.0")))) (parse-go.mod fixture-go-mod-strings)) (test-equal "parse-go.mod: complete" `((module (module-path "M")) (go (version "1.13")) (replace (original (module-path "github.com/myname/myproject/myapi")) (with (file-path "./api"))) (replace (original (module-path "github.com/mymname/myproject/thissdk")) (with (file-path "../sdk"))) (replace (original (module-path "launchpad.net/gocheck")) (with (module-path "github.com/go-check/check") (version "v0.0.0-20140225173054-eb6ee6f84d0a"))) (require (module-path "github.com/user/project") (version "v1.1.11")) (require (module-path "github.com/user/project/sub/directory") (version "v1.1.12")) (require (module-path "bitbucket.org/user/project") (version "v1.11.20")) (require (module-path "bitbucket.org/user/project/sub/directory") (version "v1.11.21")) (require (module-path "launchpad.net/project") (version "v1.1.13")) (require (module-path "launchpad.net/project/series") (version "v1.1.14")) (require (module-path "launchpad.net/project/series/sub/directory") (version "v1.1.15")) (require (module-path "launchpad.net/~user/project/branch") (version "v1.1.16")) (require (module-path "launchpad.net/~user/project/branch/sub/directory") (version "v1.1.17")) (require (module-path "hub.jazz.net/git/user/project") (version "v1.1.18")) (require (module-path "hub.jazz.net/git/user/project/sub/directory") (version "v1.1.19")) (require (module-path "k8s.io/kubernetes/subproject") (version "v1.1.101")) (require (module-path "one.example.com/abitrary/repo") (version "v1.1.111")) (require (module-path "two.example.com/abitrary/repo") (version "v0.0.2")) (require (module-path "quoted.example.com/abitrary/repo") (version "v0.0.2")) (replace (original (module-path "two.example.com/abitrary/repo")) (with (module-path "github.com/corp/arbitrary-repo") (version "v0.0.2"))) (replace (original (module-path "golang.org/x/sys")) (with (module-path "golang.org/x/sys") (version "v0.0.0-20190813064441-fde4db37ae7a")) (comment "pinned to release-branch.go1.13")) (replace (original (module-path "golang.org/x/tools")) (with (module-path "golang.org/x/tools") (version "v0.0.0-20190821162956-65e3620a7ae7")) (comment "pinned to release-branch.go1.13"))) (parse-go.mod fixture-go-mod-complete)) ;;; End-to-end tests for (guix import go) (define (mock-http-fetch testcase) (lambda (url . rest) (let ((body (assoc-ref testcase url))) (if body (open-input-string body) (error "mocked http-fetch Unexpected URL: " url))))) (define (mock-http-get testcase) (lambda (url . rest) (let ((body (assoc-ref testcase url)) (response-header (build-response #:version '(1 . 1) #:code 200 #:reason-phrase "Ok" #:headers `( (content-type text/html (charset . "utf-8")) (date . ,(make-date 0 10 58 12 6 3 2021 0)) (transfer-encoding (chunked))) #:port #f #:validate-headers? #t))) (if body (values response-header body) (error "mocked http-get Unexpected URL: " url))))) (test-equal "go-module->guix-package" '(package (name "go-github-com-go-check-check") (version "0.0.0-20201130134442-10cb98267c6c") (source (origin (method git-fetch) (uri (git-reference (url "https://github.com/go-check/check") (commit (go-version->git-ref version)))) (file-name (git-file-name name version)) (sha256 (base32 "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5")))) (build-system go-build-system) (arguments (list #:import-path "github.com/go-check/check")) (propagated-inputs (list go-github-com-kr-pretty)) (home-page "https://github.com/go-check/check") (synopsis "Instructions") (description "Package check is a rich testing extension for Go's testing \ package.") (license license:bsd-2)) ;; Replace network resources with sample data. (call-with-temporary-directory (lambda (checkout) (mock ((web client) http-get (mock-http-get fixtures-go-check-test)) (mock ((guix http-client) http-fetch (mock-http-fetch fixtures-go-check-test)) (mock ((guix git) update-cached-checkout (lambda* (url #:key ref) ;; Return an empty directory and its hash. (values checkout (nix-base32-string->bytevector "0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5") #f))) (go-module->guix-package* "github.com/go-check/check"))))))) (test-end "go")