cl-scopes/test/test-config.lisp

49 lines
1.7 KiB
Common Lisp

;;;; cl-scopes/test/test-config
;;;; Tests for the scopes/config functionality.
(defpackage :scopes/test-config
(:use :common-lisp)
(:local-nicknames (:config :scopes/config)
(:util :scopes/util)
(:t :scopes/testing))
(:export #:run #:user #:password)
(:import-from :scopes/testing #:deftest #:==))
(in-package :scopes/test-config)
(defvar *config* nil)
(defclass test-config (config:base)
((config:env-keys :initform '(:user :password))
(config:env-path :initform (t:test-path ".test.env"))
(user :accessor user :initarg :user)
(password :accessor password :initarg :password)))
(defmethod initialize-instance :after ((cfg test-config) &key &allow-other-keys)
(let* ((data (config:env-data cfg))
(u (if data (gethash :user data)))
(pw (if data (gethash :password data))))
(if u (setf (user cfg) u))
(if pw (setf (password cfg) pw))))
(defun run ()
(let ((*config* nil)
(t:*test-suite* (t:test-suite "config")))
(setf (uiop:getenv "SCOPES_PASSWORD") "very_secret")
(load (t:test-path "config-dummy" "etc"))
(test-make-path)
(test-env-override)
(t:show-result)))
(t:deftest test-make-path ()
;(format t "~&runtime-path (.env): ~s" (util:runtime-path ".env" "app"))
;(format t "~&home-path: ~s" (util:home-path ".env.txt" "lisp" "cl-scopes"))
(== (pathname-name (util:home-path ".env.txt" "lisp" "cl-scopes")) ".env"))
(t:deftest test-env-override ()
(let ((data (config:env-data *config*)))
(== (gethash :user data) "user-from-env-file")
(== (gethash :password data) "very_secret")
(== (user *config*) "user-from-env-file")
(== (password *config*) "very_secret")))