test-require.script 9.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
;; -*- Lisp -*-

;;; This script verifies the invariants of both
;;; *function* REQUIRE-SYSTEM and *class* REQUIRE-SYSTEM.
;;; Based on work by Daniel Kochmański

(import 'asdf::registered-system)


(DBG "---- Tests related to the behavior of preloaded systems")

(DBG "- Even dummy `preloaded-system' is listed in `already-loaded-systems'")
(register-preloaded-system "test-preloaded-dummy" :version "1.0")
(assert (registered-system "test-preloaded-dummy"))
(assert-equal (component-version (registered-system "test-preloaded-dummy")) "1.0")

(DBG "- Loaded `preloaded-system' is listed in `already-loaded-systems'")
(assert (component-loaded-p (registered-system "test-preloaded-dummy")))
(assert (member "test-preloaded-dummy" (already-loaded-systems) :test #'equal))

(DBG "- Dummy `preloaded-system' doesn't get overwritten by `require-system'")
(require-system "test-preloaded-dummy")
(assert-equal (component-version (registered-system "test-preloaded-dummy")) "1.0")

(DBG "- Dummy `preloaded-system' doesn't get overwritten by `load-system' because there is no alternative in the filesystem")
26 27
(load-test-system "test-preloaded-dummy")
(assert-equal (component-version (registered-system "test-preloaded-dummy")) "1.0")
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

(DBG "- Function `clear-system' doesn't remove `preloaded-system'")
(assert (not (clear-system "test-preloaded-dummy")))
(assert-equal (component-version (registered-system "test-preloaded-dummy")) "1.0")

(DBG "-`preloaded-system' overridden by defsystem")
(def-test-system "test-preloaded-dummy" :version "2.0")
(assert-equal (component-version (registered-system "test-preloaded-dummy")) "2.0")

(DBG "- Function `clear-system' restores `preloaded-system'")
(assert (not (clear-system "test-preloaded-dummy")))
(assert-equal (component-version (registered-system "test-preloaded-dummy")) "1.0")


(DBG "-`preloaded-system' overridden by find-system")
(assert (not (registered-system "require-system/preloaded")))
(register-preloaded-system "require-system/preloaded" :version "1.0")
(assert-equal (component-version (registered-system "require-system/preloaded")) "1.0")
(assert (component-loaded-p (registered-system "require-system/preloaded")))
(find-system "require-system/preloaded")
(assert-equal (component-version (registered-system "require-system/preloaded")) "2.0")

(DBG "-`find-system' doesn't make the new system loaded")
(assert (not (component-loaded-p (registered-system "require-system/preloaded"))))

(DBG "-`load-system' does")
(load-system "require-system/preloaded")
(assert (component-loaded-p (registered-system "require-system/preloaded")))

(DBG "- Function `clear-system' restores registered, preloaded `preloaded-system'")
(assert (not (clear-system "require-system/preloaded")))
(assert-equal (component-version (registered-system "require-system/preloaded")) "1.0")
(assert (component-loaded-p (registered-system "require-system/preloaded")))
(assert (member "require-system/preloaded" (already-loaded-systems) :test #'equal))

(DBG "- Function `require-system' doesn't overwrite `preloaded-system'")
(require-system "require-system/preloaded")
(assert-equal (component-version (registered-system "require-system/preloaded")) "1.0")

(DBG "- Function `require-system' doesn't trigger a side-effect reloading system")
;;; `require-system' called `load-system' regardless if the system was
;;; loaded or not. While the `:force' and `:force-not' flags were
;;; handled correctly, `load-system' had a nasty side-effect which
;;; were overwriting our system loaded in-memory, so the second
;;; `require-system' loaded the new `asd'-defined system.
(require-system "require-system/preloaded")
(assert-equal (component-version (registered-system "require-system/preloaded")) "1.0")

(DBG "- System `preloaded-system' won't be reloaded on `require-system' when cache is empty")
(clear-cache)
(assert-equal (component-version (registered-system "require-system/preloaded")) "1.0")
(require-system "require-system/preloaded")
(assert-equal (component-version (registered-system "require-system/preloaded")) "1.0")

(DBG "- Function `load-system' overwrites `preloaded-system'")
(load-system "require-system/preloaded")
(assert-equal (component-version (registered-system "require-system/preloaded")) "2.0")

(DBG "- Update the preloaded system version after loading from asd")
(register-preloaded-system "require-system/preloaded")
(assert-equal (component-version (registered-system "require-system/preloaded")) "2.0")
(assert (not (clear-system "require-system/preloaded")))
(assert-equal (component-version (registered-system "require-system/preloaded")) "2.0")

;; Not implemented:
#|
(deregister-preloaded-system "require-system/preloaded")
(assert-equal (component-version (registered-system "require-system/preloaded")) "2.0")
(assert (clear-system "require-system/preloaded"))
(assert (not (registered-system "require-system/preloaded")))
|#

(DBG "---- Tests related to the behavior of immutable systems")

(DBG "- Test function `register-immutable-system'")
;; Note: it's not immutable *yet*, but loaded from file.
(assert-equal (component-version (registered-system "require-system/immutable")) "2.0")
(clear-system "require-system/immutable")
(def-test-system "require-system/immutable" :version "1.0"
                 :depends-on ("require-system/not-loaded")
                 :components ((:file "file1")))
(assert (not (component-loaded-p "require-system/immutable")))
(assert-equal (component-version (registered-system "require-system/immutable")) "1.0")
(let ((system (registered-system "require-system/immutable")))
  (register-immutable-system "require-system/immutable")
  (assert (eq system (registered-system "require-system/immutable"))))
(assert (eq 'system (type-of (registered-system "require-system/immutable"))))
(assert-equal 1 (length (component-children (registered-system "require-system/immutable"))))
(assert (not (component-loaded-p "require-system/immutable"))) ;; *not* marked loaded
(assert-equal (component-version (registered-system "require-system/immutable")) "1.0")

(DBG "- Function `require-system' doesn't overwrite `immutable-system'")
(require-system "require-system/immutable")
(assert-equal (component-version (registered-system "require-system/immutable")) "1.0")
(assert (not (component-loaded-p "require-system/immutable"))) ;; *not* marked loaded

(DBG "- Function `load-system' doesn't overwrite `immutable-system'")
(load-system "require-system/immutable")
(assert-equal (component-version (registered-system "require-system/immutable")) "1.0")
(assert (not (component-loaded-p "require-system/immutable"))) ;; *not* marked loaded

(DBG "- Function `load-system' doesn't load an immutable system's dependencies")
(assert (not (component-loaded-p "require-system/not-loaded"))) ;; *not* marked loaded

(DBG "- Function `clear-system' leaves `immutable-system' loaded, but clears build information")
(assert-equal 1 (length (component-children (registered-system "require-system/immutable"))))
(assert (not (component-loaded-p "require-system/immutable"))) ;; still not marked loaded
(clear-system "require-system/immutable")
(assert-equal (component-version (registered-system "require-system/immutable")) "1.0")
(assert-equal 0 (length (component-children (registered-system "require-system/immutable"))))
(assert (component-loaded-p "require-system/immutable")) ;; *YES* marked loaded!

(DBG "- System `immutable-system' won't be reloaded when cache is empty")
(clear-cache)
(load-system "require-system/immutable")
(assert-equal (component-version (registered-system "require-system/immutable")) "1.0")



(DBG "---- Tests related to the behavior of ordinary systems")

(DBG "- Require-system doesn't replace already loaded system")
(def-test-system "require-system/ordinary" :version "1.0")
(asdf::mark-component-preloaded "require-system/ordinary")
(assert (component-loaded-p "require-system/ordinary"))
(require-system "require-system/ordinary")
(assert-equal (component-version (registered-system "require-system/ordinary")) "1.0")

(DBG "- Load-system loads system from the disk")
(clear-cache)
(load-system "require-system/ordinary")
(assert-equal (component-version (registered-system "require-system/ordinary")) "2.0")


Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
162 163 164 165 166 167 168 169 170 171 172 173
;;; ASDF assumes that if a same-named system can be found by find-system, then
;;; it overrides any same-named implementation-dependent module (after downcasing)
;;; specified via (:require ...), so as to avoid recursive calls to asdf:operate from
;;; within plan execution, and any double-loading or infinite loop that could result.
;;;
;;; That means that we can't "just" test the (:require ...) feature portably by
;;; relying on the ASDF hook into CL:REQUIRE that many implementations provide.

#+clisp
(defparameter *dynmod-list*
  (mapcar 'pathname-name
          (directory-files (subpathname (lisp-implementation-directory) "dynmod/") "*.lisp")))
174 175 176 177

(defparameter *mod*
  (or
   #+abcl :abcl-contrib
178
   #+allegro :sax
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
179
   #+clisp (first (remove "asdf" *dynmod-list* :test 'equal))
180
   #+(or clozure cmucl) :defsystem
Robert Goldman's avatar
Robert Goldman committed
181
   #+ecl :rt ;; loads faster than :ecl-quicklisp
182 183 184 185 186 187 188 189
   #+lispworks "comm"
   #+mkcl :walker
   #+sbcl :sb-md5
   #+scl :http-library))

(unless *mod*
  (leave-test "This implementation doesn't provide a known module" 0))

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
190 191 192 193 194 195 196
;; Prevent ASDF from finding the modules as ASDF systems on ECL, MKCL, SBCL (and maybe more).
(setf asdf::*system-definition-search-functions*
      (remove 'asdf::sysdef-source-registry-search asdf::*system-definition-search-functions*))
(asdf::clear-defined-systems)
(clear-cache)


197 198 199 200
(defun has-module-p (x)
  ;; note that the spec is case-sensitive, but we'll cast a wider net.
  (and (member x *modules* :test 'string-equal) t))

Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
201
(DBG :before *modules*)
202 203 204 205 206 207
(assert (not (find-system *mod* nil)))
(assert (not (has-module-p *mod*)))

(eval `(def-test-system :require-test :depends-on ((:require ,*mod*))))

(load-system :require-test)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
208
(DBG :after *modules*)
209
(assert (has-module-p *mod*))