|
| 1 | +(require 'el-get) |
| 2 | +(require 'ert nil t) |
| 3 | + |
| 4 | +(eval-when-compile |
| 5 | + (require 'cl) |
| 6 | + (unless (featurep 'ert) |
| 7 | + (defmacro* ert-deftest (name () &body docstring-keys-and-body) |
| 8 | + (message "Skipping tests, ERT is not available")))) |
| 9 | + |
| 10 | +(defvar el-get-test-output-buffer nil) |
| 11 | +(when noninteractive |
| 12 | + (defadvice message (around el-get-test-catch-output activate) |
| 13 | + "redirect all `message' output to `el-get-test-output-buffer'." |
| 14 | + (if el-get-test-output-buffer |
| 15 | + (with-current-buffer el-get-test-output-buffer |
| 16 | + (insert (apply #'format (ad-get-args 0)) "\n")) |
| 17 | + ad-do-it))) |
| 18 | + |
| 19 | +(defconst el-get-test-files-dir |
| 20 | + (file-name-directory |
| 21 | + (or (eval-when-compile (bound-and-true-p byte-compile-file)) |
| 22 | + load-file-name |
| 23 | + buffer-file-name))) |
| 24 | + |
| 25 | +(defmacro el-get-with-temp-home (&rest body) |
| 26 | + "Evaluate BODY with a temporary `user-emacs-directory'. |
| 27 | +
|
| 28 | +In batch mode, `message' output is suppressed unless there is an |
| 29 | +error. |
| 30 | +
|
| 31 | +Following variables are bound to temporal values: |
| 32 | +* `user-emacs-directory' |
| 33 | +* `el-get-dir' |
| 34 | +* `el-get-status-file' |
| 35 | +* `el-get-status-cache' |
| 36 | +* `el-get-autoload-file'" |
| 37 | + (declare (debug t)) |
| 38 | + `(let* ((user-emacs-directory |
| 39 | + (make-temp-file "emacs.d.el-get-testing" 'dir "/")) |
| 40 | + (el-get-dir (mapconcat #'file-name-as-directory |
| 41 | + `(,user-emacs-directory "el-get") "")) |
| 42 | + (el-get-status-file (concat el-get-dir ".status.el")) |
| 43 | + (el-get-status-cache nil) |
| 44 | + (el-get-autoload-file (concat el-get-dir ".loaddefs.el")) |
| 45 | + (el-get-test-output-buffer |
| 46 | + (when noninteractive (get-buffer-create "*el-get-test-output*")))) |
| 47 | + (unwind-protect |
| 48 | + (condition-case err |
| 49 | + (progn |
| 50 | + (make-directory el-get-dir t) |
| 51 | + ,@body) |
| 52 | + (error (when el-get-test-output-buffer |
| 53 | + (with-current-buffer el-get-test-output-buffer |
| 54 | + (princ (buffer-string)))) |
| 55 | + (signal (car err) (cdr err)))) |
| 56 | + (let ((kill-buffer-query-functions nil)) |
| 57 | + (dolist (buf (buffer-list)) |
| 58 | + (with-current-buffer buf |
| 59 | + (when (string-prefix-p user-emacs-directory default-directory) |
| 60 | + (set-buffer-modified-p nil) |
| 61 | + (kill-buffer))))) |
| 62 | + (delete-directory user-emacs-directory t) |
| 63 | + (when el-get-test-output-buffer |
| 64 | + (kill-buffer el-get-test-output-buffer))))) |
| 65 | + |
| 66 | +(ert-deftest el-get-recipe-dirs-test () |
| 67 | + (let ((el-get-recipe-path |
| 68 | + `("/" |
| 69 | + ,(let ((f "/foo")) |
| 70 | + (while (file-exists-p f) |
| 71 | + (setq f (concat f f))) f)))) |
| 72 | + (should (equal (el-get-recipe-dirs) |
| 73 | + (loop for f in el-get-recipe-path |
| 74 | + when (file-exists-p f) |
| 75 | + collect f))))) |
| 76 | + |
| 77 | +(ert-deftest el-get-trivial-install-test () |
| 78 | + (el-get-with-temp-home |
| 79 | + (let* ((pkg 'el-get-test-package) |
| 80 | + (pkg-name (symbol-name pkg)) |
| 81 | + (pkg-file (concat pkg-name ".el")) |
| 82 | + (pkg-source (expand-file-name |
| 83 | + pkg-file (concat el-get-test-files-dir "pkgs"))) |
| 84 | + (pkg-destination (expand-file-name |
| 85 | + pkg-file (concat el-get-dir pkg-name))) |
| 86 | + (el-get-sources `((:name ,pkg |
| 87 | + :features (,pkg) |
| 88 | + :type http |
| 89 | + :url ,(concat "file://" pkg-source))))) |
| 90 | + (unwind-protect |
| 91 | + (progn |
| 92 | + (message "Checking %s is not loaded" pkg) |
| 93 | + (should-not (featurep pkg)) |
| 94 | + (message "Installing %s" pkg) |
| 95 | + (should (progn (el-get 'sync pkg) t)) |
| 96 | + (message "Verifying installed package file %s" pkg-destination) |
| 97 | + (should (file-exists-p pkg-destination)) |
| 98 | + (should-not (zerop (nth 7 (file-attributes pkg-destination)))) |
| 99 | + (message "Verifying package %s was loaded" pkg) |
| 100 | + (should (featurep pkg)) |
| 101 | + (message "Unloading and removing package %s" pkg) |
| 102 | + (el-get-remove (symbol-name pkg)) |
| 103 | + (message "Verifying package %s was unloaded and removed" pkg) |
| 104 | + (should-not (file-exists-p pkg-destination))) |
| 105 | + (when (featurep pkg) |
| 106 | + (unload-feature pkg)))))) |
| 107 | + |
| 108 | +(ert-deftest el-get-elpa-feature () |
| 109 | + "`:features' option should work for ELPA type recipe." |
| 110 | + :expected-result :failed |
| 111 | + (el-get-with-temp-home |
| 112 | + (require 'package-x) ; create local package archive |
| 113 | + (let* ((pkg 'el-get-test-package) |
| 114 | + (package-archive-upload-base (expand-file-name "~/pkg-repo")) |
| 115 | + (package-archives nil) |
| 116 | + (el-get-sources |
| 117 | + `((:name package :post-init nil) ; avoid adding other repos |
| 118 | + (:name el-get-test-package |
| 119 | + :type elpa |
| 120 | + :repo ("test-repo" . ,package-archive-upload-base) |
| 121 | + :features el-get-test-package)))) |
| 122 | + (make-directory package-archive-upload-base t) |
| 123 | + (package-upload-file (expand-file-name "pkgs/el-get-test-package.el" |
| 124 | + el-get-test-files-dir)) |
| 125 | + (should-not (featurep pkg)) |
| 126 | + (el-get 'sync (mapcar 'el-get-source-name el-get-sources)) |
| 127 | + (should (featurep pkg))))) |
0 commit comments