#include "store-api.hh" #include "globals.hh" #include "util.hh" #include namespace nix { GCOptions::GCOptions() { action = gcDeleteDead; ignoreLiveness = false; maxFreed = ULLONG_MAX; } bool isInStore(const Path & path) { return isInDir(path, settings.nixStore); } bool isStorePath(const Path & path) { return isInStore(path) && path.find('/', settings.nixStore.size() + 1) == Path::npos; } void assertStorePath(const Path & path) { if (!isStorePath(path)) throw Error(format("path `%1%' is not in the store") % path); } Path toStorePath(const Path & path) { if (!isInStore(path)) throw Error(format("path `%1%' is not in the store") % path); Path::size_type slash = path.find('/', settings.nixStore.size() + 1); if (slash == Path::npos) return path; else return Path(path, 0, slash); } string storePathToName(const Path & path) { assertStorePath(path); return string(path, settings.nixStore.size() + 34); } void checkStoreName(const string & name) { string validChars = "+-._?="; /* Disallow names starting with a dot for possible security reasons (e.g., "." and ".."). */ if (string(name, 0, 1) == ".") throw Error(format("illegal name: `%1%'") % name); foreach (string::const_iterator, i, name) if (!((*i >= 'A' && *i <= 'Z') || (*i >= 'a' && *i <= 'z') || (*i >= '0' && *i <= '9') || validChars.find(*i) != string::npos)) { throw Error(format("invalid character `%1%' in name `%2%'") % *i % name); } } /* Store paths have the following form: /- where = the location of the store, usually /gnu/store = a human readable name for the path, typically obtained from the name attribute of the derivation, or the name of the source file from which the store path is crea
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2021, 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
;;;
;;; 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 (tests services configuration)
  #:use-module (gnu services configuration)
  #:use-module (guix diagnostics)
  #:use-module (guix gexp)
  #:autoload (guix i18n) (G_)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-64))

;;; Tests for the (gnu services configuration) module.

(test-begin "services-configuration")

(define (serialize-number field value)
  (format #f "~a=~a" field value))


;;;
;;; define-configuration macro.
;;;

(define-configuration port-configuration
  (port (number 80) "The port number.")
  (no-serialization))

(test-equal "default value, no serialization"
  80
  (port-configuration-port (port-configuration)))

(test-equal "wrong type for a field"
  '("configuration.scm" 59 11)                    ;error location
  (guard (c ((configuration-error? c)
             (let ((loc (error-location c)))
               (list (basename (location-file loc))
                     (location-line loc)
                     (location-column loc)))))
    (port-configuration
     ;; This is line 58; the test relies on line/column numbers!
     (port "This is not a number!"))))

(define-configuration port-configuration-cs
  (port (number 80) "The port number." empty-serializer))

(test-equal "default value, custom serializer"
  80
  (port-configuration-cs-port (port-configuration-cs)))

(define-configuration port-configuration-ndv
  (port (number) "The port number."))

(test-equal "no default value, provided"
  55
  (port-configuration-ndv-port (port-configuration-ndv
                                (port 55))))

(test-assert "no default value, not provided"
  (guard (c ((configuration-error? c)
             #t))
    (port-configuration-ndv-port (port-configuration-ndv))))

(define (custom-number-serializer name value)
  (format #f "~a = ~a;" name value))

(define-configuration serializable-configuration
  (port (number 80) "The port number." (serializer custom-number-serializer)))

(define-configuration serializable-configuration-deprecated
  (port (number 80) "The port number." custom-number-serializer))

(test-assert "serialize-configuration"
  (gexp?
   (let ((config (serializable-configuration)))
     (serialize-configuration config serializable-configuration-fields))))

(test-assert "serialize-configuration [deprecated]"
  (gexp?
   (let ((config (serializable-configuration-deprecated)))
     (serialize-configuration
      config serializable-configuration-deprecated-fields))))

(define-configuration serializable-configuration
  (port (number 80) "The port number." (serializer custom-number-serializer))
  (no-serialization))

(test-assert "serialize-configuration with no-serialization"
  ;; When serialization is disabled, the serializer is set to #f, so
  ;; attempting to use it fails with a 'wrong-type-arg' error.
  (not (false-if-exception
        (let ((config (serializable-configuration)))
          (serialize-configuration config serializable-configuration-fields)))))

(define (custom-prefix-serialize-integer field-name name) name)

(define-configuration configuration-with-prefix
  (port (integer 10) "The port number.")
  (prefix custom-prefix-))

(test-assert "serialize-configuration with prefix"
  (gexp?
   (let ((config (configuration-with-prefix)))
     (serialize-configuration config configuration-with-prefix-fields))))


;;;
;;; define-configuration macro, extra-args literals
;;;

(define (eval-gexp x)
  "Get serialized config as string."
  (eval (gexp->approximate-sexp x)
        (current-module)))

(define (port? value)
  (or (string? value) (number? value)))

(define (sanitize-port value)
  (cond ((number? value) value)
        ((string? value) (string->number value))
        (else (raise (formatted-message (G_ "Bad value: ~a") value)))))

(test-group "Basic sanitizer literal tests"
  (define serialize-port serialize-number)

  (define-configuration config-with-sanitizer
    (port
     (port 80)
     "Lorem Ipsum."
     (sanitizer sanitize-port)))

  (test-equal "default value, sanitizer"
    80
    (config-with-sanitizer-port (config-with-sanitizer)))

  (test-equal "string value, sanitized to number"
    56
    (config-with-sanitizer-port (config-with-sanitizer
                                 (port "56"))))

  (define (custom-serialize-port field-name value)
    (number->string value))

  (define-configuration config-serializer
    (port
     (port 80)
     "Lorem Ipsum."
     (serializer custom-serialize-port)))

  (test-equal "default value, serializer literal"
    "80"
    (eval-gexp
     (serialize-configuration (config-serializer