core: add method cfgl-package-get-safe-owner

Safe method to get the owner of a package.
This commit is contained in:
syl20bnr 2016-06-10 22:35:59 -04:00
parent b2d4adb14d
commit 3a9cffd2d9
2 changed files with 32 additions and 0 deletions

View file

@ -145,6 +145,24 @@ LAYER has to be installed for this method to work properly."
"Evaluate the `toggle' slot of passed PKG."
(let ((toggle (oref pkg :toggle))) (eval toggle)))
(defmethod cfgl-package-get-safe-owner ((pkg cfgl-package))
"Safe method to return the name of the layer which owns PKG."
;; The owner of a package is the first *used* layer in `:owners' slot.
;; Note: for packages in `configuration-layer--packages' the owner is always
;; the car of the `:owners' slot.
;; An example where it is not necessarily the case is in
;; `helm-spacemacs-help-all-packages' which we can find in the helm
;; layer.
;; For performance reason `cfgl-package-get-safe-owner' is not used in the
;; layer system itself. This functions should be only used outside of the
;; system in order to safely get the true owner of a layer.
(let ((layers (oref pkg :owners)))
(while (and (consp layers)
(not (configuration-layer/layer-usedp (car layers))))
(pop layers))
(when (configuration-layer/layer-usedp (car layers))
(car layers))))
(defvar configuration-layer--elpa-archives
'(("melpa" . "melpa.org/packages/")
("org" . "orgmode.org/elpa/")

View file

@ -66,6 +66,20 @@
(package-toggle 'other))
(should (null (cfgl-package-enabledp pkg)))))
;; method: cfgl-package-get-safe-owner
(ert-deftest test-cfgl-package-get-safe-owner--return-car ()
(let ((pkg (cfgl-package "testpkg" :name 'testpkg :owners '(layer1 layer2)))
(configuration-layer--layers `(,(cfgl-layer "layer1" :name 'layer1)
,(cfgl-layer "layer2" :name 'layer2))))
(should (eq 'layer1 (cfgl-package-get-safe-owner pkg)))))
(ert-deftest test-cfgl-package-get-safe-owner--return-cadr ()
(let ((pkg (cfgl-package "testpkg" :name 'testpkg :owners '(layer1 layer2)))
;; layer1 is not used so it cannot be the owner
(configuration-layer--layers `(,(cfgl-layer "layer2" :name 'layer2))))
(should (eq 'layer2 (cfgl-package-get-safe-owner pkg)))))
;; ---------------------------------------------------------------------------
;; configuration-layer//resolve-package-archives
;; ---------------------------------------------------------------------------