all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alex <agrambot@gmail.com>
To: Gemini Lasswell <gazally@runbox.com>
Cc: 24402@debbugs.gnu.org
Subject: bug#24402: should-error doesn't catch all errors
Date: Tue, 04 Jul 2017 18:00:04 -0600	[thread overview]
Message-ID: <877eznda7v.fsf@lylat> (raw)
In-Reply-To: <87zickhoco.fsf_-_@lylat> (Alex's message of "Mon, 03 Jul 2017 21:28:55 -0600")

[-- Attachment #1: Type: text/plain, Size: 1495 bytes --]

tags 24402 patch
quit

Alex <agrambot@gmail.com> writes:

> Gemini Lasswell <gazally@runbox.com> writes:
>
>> I’ve done some investigating of why this is happening. testcover-start
>> transforms:
>>     (should-error (my-error))
>> into:
>>     (should-error (testcover-after 2 (my-error)))
>>
>> Then the macro expansion of should-error separates the form which it
>> is passed into a function symbol and list of arguments, and applies
>> the function to the arguments inside of a condition-case that traps
>> errors. The problem is that the arguments are evaluated first, outside
>> of the condition-case, so errors in their evaluation do not get
>> caught. This problem is not specific to testcover. In this example,
>> the first test passes and the second fails:
>>
>> (defun div-by (n)
>>   (/ 100 n))
>>
>> (defmacro log-div-by (n)
>>   `(message "div-by: %d" (div-by ,n)))
>>
>> (ert-deftest test-div-by ()
>>   (should-error (div-by 0)))
>>
>> (ert-deftest test-log-div-by ()
>>   (should-error (log-div-by 0)))
>>   
>
> I just ran into this as well. Consider these two forms:
>
> (should-error (cl-fourth "1234") :type 'wrong-type-argument)
>
> (should-error (car (cdr (cdr (cdr "1234")))) :type 'wrong-type-argument)
>
> Only the second raises an error, even though cl-fourth is equivalent to
> the car/cdr chain.

Here's a diff that appears to fix this. It's a bit crude, but I'd rather
not mess around too much with ert's internals.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: first --]
[-- Type: text/x-diff, Size: 918 bytes --]

diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el
index 2c49a634e3..f5f3e30c0d 100644
--- a/lisp/emacs-lisp/ert.el
+++ b/lisp/emacs-lisp/ert.el
@@ -303,8 +303,12 @@ ert--expand-should-1
               (args (cl-gensym "args-"))
               (value (cl-gensym "value-"))
               (default-value (cl-gensym "ert-form-evaluation-aborted-")))
-          `(let ((,fn (function ,fn-name))
-                 (,args (list ,@arg-forms)))
+          `(let* ((,fn (function ,fn-name))
+                  (,args (condition-case err
+                             (list ,@arg-forms)
+                           (error (progn (setq ,fn #'signal)
+                                         (list (car err)
+                                               (cdr err)))))))
              (let ((,value ',default-value))
                ,(funcall inner-expander
                          `(setq ,value (apply ,fn ,args))

[-- Attachment #3: Type: text/plain, Size: 254 bytes --]


There's a similar issue with macro-expansions inside of
should/should-error/should-not that could/should be fixed by wrapping
the macroexpand call at the top of ert--expand-should-1 in a similar
condition-case. Here's another diff that does just that:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: second --]
[-- Type: text/x-diff, Size: 2316 bytes --]

diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el
index eb2b2e3e11..66ae31fd51 100644
--- a/lisp/emacs-lisp/ert.el
+++ b/lisp/emacs-lisp/ert.el
@@ -276,13 +276,15 @@ ert--special-operator-p
 (defun ert--expand-should-1 (whole form inner-expander)
   "Helper function for the `should' macro and its variants."
   (let ((form
-         (macroexpand form (append (bound-and-true-p
-                                    byte-compile-macro-environment)
-                                   (cond
-                                    ((boundp 'macroexpand-all-environment)
-                                     macroexpand-all-environment)
-                                    ((boundp 'cl-macro-environment)
-                                     cl-macro-environment))))))
+         (condition-case err
+             (macroexpand-all form (append (bound-and-true-p
+                                            byte-compile-macro-environment)
+                                           (cond
+                                            ((boundp 'macroexpand-all-environment)
+                                             macroexpand-all-environment)
+                                            ((boundp 'cl-macro-environment)
+                                             cl-macro-environment))))
+           (error `(signal ',(car err) ',(cdr err))))))
     (cond
      ((or (atom form) (ert--special-operator-p (car form)))
       (let ((value (cl-gensym "value-")))
@@ -303,8 +305,13 @@ ert--expand-should-1
               (args (cl-gensym "args-"))
               (value (cl-gensym "value-"))
               (default-value (cl-gensym "ert-form-evaluation-aborted-")))
-          `(let ((,fn (function ,fn-name))
-                 (,args (list ,@arg-forms)))
+          `(let* ((,fn (function ,fn-name))
+                  (,args (condition-case err
+                             (list ,@arg-forms)
+                           ;; (ert-test-failed (signal (car err) (cdr err)))
+                           (error (progn (setq ,fn #'signal)
+                                         (list (car err)
+                                               (cdr err)))))))
              (let ((,value ',default-value))
                ,(funcall inner-expander
                          `(setq ,value (apply ,fn ,args))

[-- Attachment #5: Type: text/plain, Size: 622 bytes --]


I ran "make check" and found only one test that the above diff breaks:
ert-test-test-result-expected-p.

I can't seem to figure out why it doesn't work. The test fails because
of these two:

(let ((test (make-ert-test :body (lambda () (ert-fail "failed")))))
  (should-not (ert-test-result-expected-p test (ert-run-test test))))
(let ((test (make-ert-test :body (lambda () (ert-fail "failed"))
                           :expected-result-type ':failed)))
  (should (ert-test-result-expected-p test (ert-run-test test))))

I tried to re-throw the ert-test-failed signal and still the above two
forms raise error an error.

  reply	other threads:[~2017-07-05  0:00 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-10  1:26 bug#24402: 25.1.50; testcover-start breaks should-error Gemini Lasswell
     [not found] ` <handler.24402.B.14734739098199.ack@debbugs.gnu.org>
2016-09-20  4:27   ` bug#24402: More Information Gemini Lasswell
2017-07-04  3:28     ` bug#24402: should-error doesn't catch all errors (Was:bug#24402: More Information) Alex
2017-07-05  0:00       ` Alex [this message]
2017-07-05 13:43         ` bug#24402: should-error doesn't catch all errors Tino Calancha
2017-07-05 19:56           ` Alex
2017-07-07 10:15             ` Tino Calancha
2017-07-09  7:04               ` Alex
2017-07-11 12:18             ` npostavs
2017-07-12  3:47               ` Alex
2017-07-12 12:30                 ` npostavs
2017-07-12 16:45                   ` Alex
2017-07-13  1:31                     ` npostavs
2017-07-13  3:04                       ` Alex
2017-07-13  3:44                         ` npostavs
2017-07-13 22:45                           ` Alex
2017-07-13 23:49                             ` npostavs
2017-07-14  4:42                               ` Alex
2017-07-14  4:45                                 ` Alex
2017-07-15 21:57                                 ` npostavs
2017-07-16  3:49                                   ` Alex
2017-07-17  0:46                                     ` npostavs
2017-07-19 21:23                                     ` Gemini Lasswell
2017-07-19 22:40                                       ` Alex
2017-07-19 23:04                                       ` npostavs
2017-07-20  4:04                                         ` Alex
2017-07-20 19:23                                         ` Gemini Lasswell
2017-08-08  1:15                                           ` npostavs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877eznda7v.fsf@lylat \
    --to=agrambot@gmail.com \
    --cc=24402@debbugs.gnu.org \
    --cc=gazally@runbox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.