;;-*- Lisp -*-

(in-package :asdf-test)

(def-test-system test-defsystem-depends-on-1
  :defsystem-depends-on (test-asdf/l-file)
  :default-component-class "l-file:cl-source-file.l"
  :components
  ((:cl-source-file.l "file1" :type "lisp")
   (:cl-source-file "file2" :depends-on ("file1"))
   (:file "test-extension")))

(load-system :test-defsystem-depends-on-1)

(def-test-system test-defsystem-depends-on-2
  :defsystem-depends-on (test-asdf/l-operation)
  :build-operation "l-operation:op"
  :components
  ((:file "file3")))

(assert-equal 0 l-operation:*x*)

(reset-session)
(make :test-defsystem-depends-on-2)

(assert-equal 2 l-operation:*x*) ;; perform called twice, on file and on system.

(def-test-system test-defsystem-depends-on-3
  :defsystem-depends-on (test-asdf/l-operation)
  :build-operation "does-not-exist"
  :components ((:file "file3")))

(reset-session)
(signals asdf::formatted-system-definition-error
         (handler-bind ((system-out-of-date 'continue)) (make :test-defsystem-depends-on-3)))

(defparameter *newsym* (gentemp (symbol-name 'feature) :keyword))

(reset-session)
;;; FIXME: the following form triggers bug https://bugs.launchpad.net/asdf/+bug/1445638
(eval `(def-test-system test-defsystem-depends-on-4
         :defsystem-depends-on ((:feature ,*newsym* "test-defsystem-depends-on-3"))))

(assert (find-system "test-defsystem-depends-on-4"))

(progn
  (reset-session)
  (signals missing-dependency (def-test-system unloadable-system
                                  :defsystem-depends-on (nonexistent-system)))
  (assert (not (registered-system "unloadable-system"))))

(progn
  (signals missing-dependency
           (handler-bind ((system-out-of-date 'continue)) (find-system "test-defsystem-depends-on-missing-system")))
  (assert (not (asdf::registered-system "test-defsystem-depends-on-missing-system"))))

(progn
  (signals asdf::system-definition-error
           (handler-bind ((system-out-of-date 'continue)) (find-system "test-defsystem-depends-on-circular")))
  (assert (not (asdf::registered-system "test-defsystem-depends-on-circular"))))
