diff options
| author | Giacomo Leidi <therewasa@fishinthecalculator.me> | 2026-03-08 17:27:13 +0100 |
|---|---|---|
| committer | Liliana Marie Prikler <liliana.prikler@gmail.com> | 2026-04-12 17:23:42 +0200 |
| commit | 0b8e838208a26fb8d7785b24fe26a4f9c9f744ac (patch) | |
| tree | a00916cc8df1e360cf1d082bde415d2a82bedad9 /tests | |
| parent | 2abfd1370fbdd2e6e859c7bd0f491c9f7bda4f0b (diff) | |
services: configuration: Add environment variable serializer.
This patch implements a general API to serialize configuration records
to list of pairs representing environment variables. The car of each
pair represents the variable name and the cdr the variable value.
* gnu/services/configuration/environment-variables.scm: New file.
(serialize-string-environment-variable)
(serialize-maybe-string-environment-variable)
(serialize-boolean-environment-variable)
(serialize-maybe-boolean-environment-variable)
(serialize-number-environment-variable)
(serialize-maybe-number-environment-variable): New variables.
(serialize-environment-variables): New variable.
* gnu/services/configuration/utils.scm: New file.
(uglify-snake-case): New variable.
* tests/services/configuration.scm: Add tests for environment serializer.
(wrong type for a field): Adjust error location.
* doc/guix.texi: Document it.
Change-Id: I81a166576f94d3c8f5bf78c82a02183689a3091c
Signed-off-by: Liliana Marie Prikler <liliana.prikler@gmail.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/services/configuration.scm | 129 |
1 files changed, 127 insertions, 2 deletions
diff --git a/tests/services/configuration.scm b/tests/services/configuration.scm index f9012b159d5..0c2a0810e12 100644 --- a/tests/services/configuration.scm +++ b/tests/services/configuration.scm | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz> | 3 | ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz> |
| 4 | ;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org> | 4 | ;;; Copyright © 2022 Ludovic Courtès <ludo@gnu.org> |
| 5 | ;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu> | 5 | ;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu> |
| 6 | ;;; Copyright © 2026 Giacomo Leidi <therewasa@fishinthecalculator.me> | ||
| 6 | ;;; | 7 | ;;; |
| 7 | ;;; This file is part of GNU Guix. | 8 | ;;; This file is part of GNU Guix. |
| 8 | ;;; | 9 | ;;; |
| @@ -21,6 +22,7 @@ | |||
| 21 | 22 | ||
| 22 | (define-module (tests services configuration) | 23 | (define-module (tests services configuration) |
| 23 | #:use-module (gnu services configuration) | 24 | #:use-module (gnu services configuration) |
| 25 | #:use-module (gnu services configuration environment-variables) | ||
| 24 | #:use-module (guix diagnostics) | 26 | #:use-module (guix diagnostics) |
| 25 | #:use-module (guix gexp) | 27 | #:use-module (guix gexp) |
| 26 | #:autoload (guix i18n) (G_) | 28 | #:autoload (guix i18n) (G_) |
| @@ -48,14 +50,14 @@ | |||
| 48 | (port-configuration-port (port-configuration))) | 50 | (port-configuration-port (port-configuration))) |
| 49 | 51 | ||
| 50 | (test-equal "wrong type for a field" | 52 | (test-equal "wrong type for a field" |
| 51 | '("configuration.scm" 59 11) ;error location | 53 | '("configuration.scm" 61 11) ;error location |
| 52 | (guard (c ((configuration-error? c) | 54 | (guard (c ((configuration-error? c) |
| 53 | (let ((loc (error-location c))) | 55 | (let ((loc (error-location c))) |
| 54 | (list (basename (location-file loc)) | 56 | (list (basename (location-file loc)) |
| 55 | (location-line loc) | 57 | (location-line loc) |
| 56 | (location-column loc))))) | 58 | (location-column loc))))) |
| 57 | (port-configuration | 59 | (port-configuration |
| 58 | ;; This is line 58; the test relies on line/column numbers! | 60 | ;; This is line 60; the test relies on line/column numbers! |
| 59 | (port "This is not a number!")))) | 61 | (port "This is not a number!")))) |
| 60 | 62 | ||
| 61 | (define-configuration port-configuration-cs | 63 | (define-configuration port-configuration-cs |
| @@ -363,3 +365,126 @@ | |||
| 363 | (config-with-maybe-string/no-serialization-name | 365 | (config-with-maybe-string/no-serialization-name |
| 364 | (config-with-maybe-string/no-serialization | 366 | (config-with-maybe-string/no-serialization |
| 365 | (name "foo"))))) | 367 | (name "foo"))))) |
| 368 | |||
| 369 | |||
| 370 | ;;; | ||
| 371 | ;;; environment-variables serializer | ||
| 372 | ;;; | ||
| 373 | |||
| 374 | (define-configuration/no-serialization env-config | ||
| 375 | (port (number 80) "") | ||
| 376 | (count maybe-number "") | ||
| 377 | (name string "") | ||
| 378 | (url maybe-string "") | ||
| 379 | (active? (boolean #f) "")) | ||
| 380 | |||
| 381 | (test-group "environment variables serializer" | ||
| 382 | |||
| 383 | (test-equal "basic serialization" | ||
| 384 | '(("PORT" . "70") | ||
| 385 | ("COUNT" . "80") | ||
| 386 | ("NAME" . "Hello World") | ||
| 387 | ("URL" . "https://example.org") | ||
| 388 | ("ACTIVE" . "true")) | ||
| 389 | (serialize-environment-variables | ||
| 390 | (env-config | ||
| 391 | (port 70) | ||
| 392 | (name "Hello World") | ||
| 393 | (url "https://example.org") | ||
| 394 | (active? #t) | ||
| 395 | (count 80)) | ||
| 396 | env-config-fields)) | ||
| 397 | |||
| 398 | (test-equal "field selection" | ||
| 399 | '(("PORT" . "80")) | ||
| 400 | (serialize-environment-variables | ||
| 401 | (env-config | ||
| 402 | (name "Hello World")) | ||
| 403 | env-config-fields | ||
| 404 | '(port))) | ||
| 405 | |||
| 406 | (test-equal "field negative selection" | ||
| 407 | '(("NAME" . "Hello World") | ||
| 408 | ("ACTIVE" . "false")) | ||
| 409 | (serialize-environment-variables | ||
| 410 | (env-config | ||
| 411 | (name "Hello World")) | ||
| 412 | env-config-fields | ||
| 413 | '(port) | ||
| 414 | #t)) | ||
| 415 | |||
| 416 | (test-equal "variable prefixes" | ||
| 417 | '(("TEST_PORT" . "80") | ||
| 418 | ("TEST_NAME" . "Hello World") | ||
| 419 | ("TEST_ACTIVE" . "false")) | ||
| 420 | (serialize-environment-variables | ||
| 421 | (env-config | ||
| 422 | (name "Hello World")) | ||
| 423 | env-config-fields | ||
| 424 | #:prefix "TEST_")) | ||
| 425 | |||
| 426 | (test-equal "boolean serialization" | ||
| 427 | '(("PORT" . "80") | ||
| 428 | ("NAME" . "Hello World") | ||
| 429 | ("ACTIVE" . "1")) | ||
| 430 | (serialize-environment-variables | ||
| 431 | (env-config | ||
| 432 | (name "Hello World") | ||
| 433 | (active? #t)) | ||
| 434 | env-config-fields | ||
| 435 | #:true-value "1" | ||
| 436 | #:false-value "0")) | ||
| 437 | |||
| 438 | (test-equal "full record serialization" | ||
| 439 | '(("TEST_COUNT" . "800") | ||
| 440 | ("TEST_NAME" . "Hello World") | ||
| 441 | ("TEST_URL" . "https://example.org") | ||
| 442 | ("TEST_ACTIVE" . "1")) | ||
| 443 | (serialize-environment-variables | ||
| 444 | (env-config | ||
| 445 | (port 90) | ||
| 446 | (name "Hello World") | ||
| 447 | (count 800) | ||
| 448 | (url "https://example.org") | ||
| 449 | (active? #t)) | ||
| 450 | env-config-fields | ||
| 451 | '(port) | ||
| 452 | #t | ||
| 453 | #:prefix "TEST_" | ||
| 454 | #:true-value "1" | ||
| 455 | #:false-value "0"))) | ||
| 456 | |||
| 457 | (define-configuration another-env-config | ||
| 458 | (port | ||
| 459 | (number 80) | ||
| 460 | "" | ||
| 461 | (serializer serialize-number-environment-variable)) | ||
| 462 | (count | ||
| 463 | (maybe-number) | ||
| 464 | "" | ||
| 465 | (serializer serialize-maybe-number-environment-variable)) | ||
| 466 | (name | ||
| 467 | (string) | ||
| 468 | "" | ||
| 469 | (serializer serialize-string-environment-variable)) | ||
| 470 | (url | ||
| 471 | (maybe-string) | ||
| 472 | "" | ||
| 473 | (serializer serialize-maybe-string-environment-variable)) | ||
| 474 | (active? | ||
| 475 | (boolean #f) | ||
| 476 | "" | ||
| 477 | (serializer serialize-boolean-environment-variable))) | ||
| 478 | |||
| 479 | (test-group "environment variables serializer, with field serializers" | ||
| 480 | |||
| 481 | (test-assert "full record serialization" | ||
| 482 | (gexp? | ||
| 483 | (serialize-configuration | ||
| 484 | (another-env-config | ||
| 485 | (port 90) | ||
| 486 | (name "Hello World") | ||
| 487 | (count 800) | ||
| 488 | (url "https://example.org") | ||
| 489 | (active? #t)) | ||
| 490 | another-env-config-fields)))) | ||
