From a126c3684f8854f8c0d7ab5dcf55f31bac77dcf9 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Mon, 30 Nov 2020 22:42:08 +0100 Subject: [PATCH] Test byte-compiler free variable warning * test/lisp/emacs-lisp/bytecomp-tests.el (ert-x): Require. (bytecomp--define-warning-file-test): New macro. (bytecomp-warn/warn-free-setq\.el) (bytecomp-warn/warn-free-variable-reference\.el): New tests. * test/lisp/emacs-lisp/bytecomp-resources/warn-free-setq.el: * test/lisp/emacs-lisp/bytecomp-resources/warn-free-variable-reference.el: New files. --- .../bytecomp-resources/warn-free-setq.el | 2 ++ .../warn-free-variable-reference.el | 4 ++++ test/lisp/emacs-lisp/bytecomp-tests.el | 14 ++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 test/lisp/emacs-lisp/bytecomp-resources/warn-free-setq.el create mode 100644 test/lisp/emacs-lisp/bytecomp-resources/warn-free-variable-reference.el diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-free-setq.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-free-setq.el new file mode 100644 index 00000000000..6e187129c9b --- /dev/null +++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-free-setq.el @@ -0,0 +1,2 @@ +;;; -*- lexical-binding: t -*- +(setq foo 'bar) diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-free-variable-reference.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-free-variable-reference.el new file mode 100644 index 00000000000..50a95272874 --- /dev/null +++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-free-variable-reference.el @@ -0,0 +1,4 @@ +;;; -*- lexical-binding: t -*- +(defvar xxx-test) +(defun foo () + (setq xxx-test bar)) diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el index 680aa514a27..c9070c03b3f 100644 --- a/test/lisp/emacs-lisp/bytecomp-tests.el +++ b/test/lisp/emacs-lisp/bytecomp-tests.el @@ -26,6 +26,7 @@ ;;; Commentary: (require 'ert) +(require 'ert-x) (require 'cl-lib) (require 'subr-x) (require 'bytecomp) @@ -546,6 +547,19 @@ Subtests signal errors if something goes wrong." '(eval-and-compile (defmacro abc (arg) -1) (defun def () (abc 2)))) (should (equal (funcall 'def) -1))) +(defmacro bytecomp--define-warning-file-test (file re-warning &optional reverse) + `(ert-deftest ,(intern (format "bytecomp-warn/%s" file)) () + :expected-result ,(if reverse :failed :passed) + (with-current-buffer (get-buffer-create "*Compile-Log*") + (let ((inhibit-read-only t)) (erase-buffer)) + (byte-compile-file ,(ert-resource-file file)) + (ert-info ((buffer-string) :prefix "buffer: ") + (should (re-search-forward ,re-warning)))))) + +(bytecomp--define-warning-file-test "warn-free-setq.el" "free.*foo") + +(bytecomp--define-warning-file-test "warn-free-variable-reference.el" "free.*bar") + (ert-deftest test-eager-load-macro-expansion-eval-when-compile () ;; Make sure we interpret eval-when-compile forms properly. CLISP ;; and SBCL interpreter eval-when-compile (well, the CL equivalent) -- 2.39.5