aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;; Copyright © 2022 Arjan Adriaanse <arjan@adriaan.se>
;;;
;;; 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-home-import)
  #:use-module (guix scripts home import)
  #:use-module (guix utils)
  #:use-module (guix build utils)
  #:use-module (guix packages)
  #:use-module (ice-9 match)
  #:use-module ((guix read-print) #:select (blank?))
  #:use-module ((guix profiles) #:hide (manifest->code))
  #:use-module ((guix build syscalls) #:select (mkdtemp!))
  #:use-module ((guix scripts package)
                #:select (manifest-entry-version-prefix))
  #:use-module (gnu packages)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-64))

;; Test the (guix scripts home import) tools.

(test-begin "home-import")

;; Example manifest entries.

(define guile-2.0.9
  (manifest-entry
    (name "guile")
    (version "2.0.9")
    (item "/gnu/store/...")))

(define glibc
  (manifest-entry
    (name "glibc")
    (version "2.19")
    (item "/gnu/store/...")))

(define gcc
  (manifest-entry
    (name "gcc")
    (version "")
    (output "lib")
    (item "/gnu/store/...")))

;; Helpers for checking and generating home environments.

(define %destination-directory "/tmp/guix-config")
(mkdir-p %destination-directory)

(define %temporary-home-directory (mkdtemp! "/tmp/guix-home-import.XXXXXX"))

(define-syntax-rule (define-home-environment-matcher name pattern)
  (define (name obj)
    (match obj
      (pattern #t)
      (x (pk 'fail x #f)))))

(define (create-temporary-home files-alist)
  "Create a temporary home directory in '%temporary-home-directory'.
FILES-ALIST is an association list of files and the content of the
corresponding file."
  (define (create-file file content)
    (let ((absolute-path (string-append %temporary-home-directory "/" file)))
      (unless (file-exists? absolute-path)
        (mkdir-p (dirname absolute-path)))
      (call-with-output-file absolute-path
        (cut display content <>))))

  (for-each (match-lambda
              ((file . content) (create-file file content)))
            files-alist))

(define (remove-recursively pred sexp)
  "Like SRFI-1 'remove', but recurse within SEXP."
  (let loop ((sexp sexp))
    (match sexp
      ((lst ...)
       (map loop (remove pred lst)))
      (x x))))

(define (eval-test-with-home-environment files-alist manifest matcher)
  (create-temporary-home files-alist)
  (setenv "HOME" %temporary-home-directory)
  (mkdir-p %temporary-home-directory)
  (let* ((home-environment (manifest+configuration-files->code
                            manifest %destination-directory))
         (result (matcher (remove-recursively blank? home-environment))))
    (delete-file-recursively %temporary-home-directory)
    result))

(define-home-environment-matcher match-home-environment-no-services
  ('begin
    ('use-modules
     ('gnu 'home)
     ('gnu 'packages)
     ('gnu 'services))
    ('home-environment
     ('packages
      ('specifications->packages
       ('list "guile@2.0.9" "gcc:lib" "glibc@2.19")))
     ('services
      ('list)))))

(define-home-environment-matcher match-home-environment-transformations
  ('begin
    ('use-modules
     ('gnu 'home)
     ('gnu 'packages)
     ('gnu 'services)
     ('guix 'transformations))

    ('define transform ('options->transformation _))
    ('home-environment
     ('packages
      ('list (transform ('specification->package "guile@2.0.9"))
             ('list ('specification->package "gcc") "lib")
             ('specification->package "glibc@2.19")))
     ('services ('list)))))

(define-home-environment-matcher match-home-environment-no-services-nor-packages
  ('begin
    ('use-modules
     ('gnu 'home)
     ('gnu 'packages)
     ('gnu 'services))
    ('home-environment
     ('packages
      ('specifications->packages ('list)))
     ('services
      ('list)))))

(define-home-environment-matcher match-home-environment-bash-service
  ('begin
    ('use-modules
     ('gnu 'home)
     ('gnu 'packages)
     ('gnu 'services)
     ('guix 'gexp)
     ('gnu 'home 'services 'shells))
    ('home-environment
     ('packages
      ('specifications->packages ('list)))
     ('services
      ('list ('service
              'home-bash-service-type
              ('home-bash-configuration
               ('aliases ('quote ()))
               ('bashrc
                ('list ('local-file "/tmp/guix-config/.bashrc"
                                    "bashrc"))))))))))

(define-home-environment-matcher match-home-environment-bash-service-with-alias
  ('begin
    ('use-modules
     ('gnu 'home)
     ('gnu 'packages)
     ('gnu 'services)
     ('guix 'gexp)
     ('gnu 'home 'services 'shells))
    ('home-environment
     ('packages
      ('specifications->packages ('list)))
     ('services
      ('list ('service
              'home-bash-service-type
              ('home-bash-configuration
               ('aliases
                ('quote (("grep" . "grep --exclude-from=\"$HOME/.grep-exclude\"")
                         ("ls" . "ls -p"))))
               ('bashrc
                ('list ('local-file "/tmp/guix-config/.bashrc"
                                    "bashrc"))))))))))


(test-assert "manifest->code: No services"
  (eval-test-with-home-environment
   '()
   (make-manifest (list guile-2.0.9 gcc glibc))
   match-home-environment-no-services))

(test-assert "manifest->code: No services, package transformations"
  (eval-test-with-home-environment
   '()
   (make-manifest (list (manifest-entry
                          (inherit guile-2.0.9)
                          (properties `((transformations
                                         . ((foo . "bar"))))))
                        gcc glibc))
   match-home-environment-transformations))

(test-assert "manifest->code: No packages nor services"
  (eval-test-with-home-environment
   '()
   (make-manifest '())
   match-home-environment-no-services-nor-packages))

(test-assert "manifest->code: Bash service"
  (eval-test-with-home-environment
   '((".bashrc" . "echo 'hello guix'"))
   (make-manifest '())
   match-home-environment-bash-service))

(test-assert "manifest->code: Bash service with aliases"
  (eval-test-with-home-environment
   '((".bashrc"
      . "# Aliases
alias ls=\"ls -p\"; alias grep='grep --exclude-from=\"$HOME/.grep-exclude\"'\n"))
   (make-manifest '())
   match-home-environment-bash-service-with-alias))

(test-end "home-import")
: Support more file types, including uncompressed plain files. Maxim Cournoyer 2019-06-14tests: Remove expensive and pointless test....This test had become too expensive since the introduction of the reduced bootstrap. * tests/builders.scm ("gnu-build"): Remove. (%bootstrap-inputs, %bootstrap-search-paths): Remove. Ludovic Courtès 2018-09-22bootstrap: %bootstrap-inputs: Wrap input lists into thunks....* gnu/packages/bootstrap.scm (%bootstrap-inputs): Change to procedure. Update users; prepares for Mes bootstrap. * gnu/packages/commencement.scm (%boot0-inputs, %boot1-inputs, %boot2-inputs, %boot3-inputs, %boot4-inputs, %boot5-inputs, %boot-6-inputs): Change to procedure. Update users. * tests/builders.scm (%bootstrap-inputs, %bootstrap-search-paths): Make a procedure, filter on package?. Update users. Jan Nieuwenhuizen 2018-09-04Switch to Guile-Gcrypt....This removes (guix hash) and (guix pk-crypto), which now live as part of Guile-Gcrypt (version 0.1.0.) * guix/gcrypt.scm, guix/hash.scm, guix/pk-crypto.scm, tests/hash.scm, tests/pk-crypto.scm: Remove. * configure.ac: Test for Guile-Gcrypt. Remove LIBGCRYPT and LIBGCRYPT_LIBDIR assignments. * m4/guix.m4 (GUIX_ASSERT_LIBGCRYPT_USABLE): Remove. * README: Add Guile-Gcrypt to the dependencies; move libgcrypt as "required unless --disable-daemon". * doc/guix.texi (Requirements): Likewise. * gnu/packages/bash.scm, guix/derivations.scm, guix/docker.scm, guix/git.scm, guix/http-client.scm, guix/import/cpan.scm, guix/import/cran.scm, guix/import/crate.scm, guix/import/elpa.scm, guix/import/gnu.scm, guix/import/hackage.scm, guix/import/texlive.scm, guix/import/utils.scm, guix/nar.scm, guix/pki.scm, guix/scripts/archive.scm, guix/scripts/authenticate.scm, guix/scripts/download.scm, guix/scripts/hash.scm, guix/scripts/pack.scm, guix/scripts/publish.scm, guix/scripts/refresh.scm, guix/scripts/substitute.scm, guix/store.scm, guix/store/deduplication.scm, guix/tests.scm, tests/base32.scm, tests/builders.scm, tests/challenge.scm, tests/cpan.scm, tests/crate.scm, tests/derivations.scm, tests/gem.scm, tests/nar.scm, tests/opam.scm, tests/pki.scm, tests/publish.scm, tests/pypi.scm, tests/store-deduplication.scm, tests/store.scm, tests/substitute.scm: Adjust imports. * gnu/system/vm.scm: Likewise. (guile-sqlite3&co): Rename to... (gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT. (expression->derivation-in-linux-vm)[config]: Remove. (iso9660-image)[config]: Remove. (qemu-image)[config]: Remove. (system-docker-image)[config]: Remove. * guix/scripts/pack.scm: Adjust imports. (guile-sqlite3&co): Rename to... (gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT. (self-contained-tarball)[build]: Call 'make-config.scm' without #:libgcrypt argument. (squashfs-image)[libgcrypt]: Remove. [build]: Call 'make-config.scm' without #:libgcrypt. (docker-image)[config, json]: Remove. [build]: Add GUILE-GCRYPT to the extensions Remove (guix config) from the imported modules. * guix/self.scm (specification->package): Remove "libgcrypt", add "guile-gcrypt". (compiled-guix): Remove #:libgcrypt. [guile-gcrypt]: New variable. [dependencies]: Add it. [*core-modules*]: Remove #:libgcrypt from 'make-config.scm' call. Add #:extensions. [*config*]: Remove #:libgcrypt from 'make-config.scm' call. (%dependency-variables): Remove %libgcrypt. (make-config.scm): Remove #:libgcrypt. * build-aux/build-self.scm (guile-gcrypt): New variable. (make-config.scm): Remove #:libgcrypt. (build-program)[fake-gcrypt-hash]: New variable. Add (gcrypt hash) to the imported modules. Adjust load path assignments. * gnu/packages/package-management.scm (guix)[propagated-inputs]: Add GUILE-GCRYPT. [arguments]: In 'wrap-program' phase, add GUILE-GCRYPT to the search path. Ludovic Courtès