From 041fff3d3dda2914f40b590a5e6a9f791a5a7fd0 Mon Sep 17 00:00:00 2001 From: Michael Albinus Date: Fri, 17 Dec 2021 20:02:21 +0100 Subject: [PATCH] Handle missing JUnit test reports * lisp/emacs-lisp/ert.el (ert-write-junit-test-report) (ert-write-junit-test-summary-report): Handle missing JUnit test reports. --- lisp/emacs-lisp/ert.el | 232 +++++++++++++++++++++-------------------- 1 file changed, 118 insertions(+), 114 deletions(-) diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el index 597044cf21c..019916e6172 100644 --- a/lisp/emacs-lisp/ert.el +++ b/lisp/emacs-lisp/ert.el @@ -1531,102 +1531,100 @@ the tests)." "Write a JUnit test report, generated from STATS." ;; https://www.ibm.com/docs/en/developer-for-zos/14.1.0?topic=formats-junit-xml-format ;; https://llg.cubic.org/docs/junit/ - (unless (zerop (length (ert--stats-tests stats))) - (when-let ((test-file - (symbol-file - (ert-test-name (aref (ert--stats-tests stats) 0)) 'ert--test)) - (test-report (file-name-with-extension test-file "xml"))) - (with-temp-file test-report - (insert "\n") - (insert (format "\n" - (file-name-nondirectory test-report) - (ert-stats-total stats) - (if (ert--stats-aborted-p stats) 1 0) - (ert-stats-completed-unexpected stats) - (ert-stats-skipped stats) - (float-time - (time-subtract - (ert--stats-end-time stats) - (ert--stats-start-time stats))))) - (insert (format " \n" - (file-name-nondirectory test-report) - (ert-stats-total stats) - (if (ert--stats-aborted-p stats) 1 0) - (ert-stats-completed-unexpected stats) - (ert-stats-skipped stats) - (float-time - (time-subtract - (ert--stats-end-time stats) - (ert--stats-start-time stats))) - (ert--format-time-iso8601 (ert--stats-end-time stats)))) - (insert " \n" - (format " \n" - (ert--stats-selector stats)) - " \n") - (cl-loop for test across (ert--stats-tests stats) - for result = (ert-test-most-recent-result test) do - (insert (format " \n") - (insert ">\n") - (cond - ((ert-test-skipped-p result) - (insert (format " \n" - (xml-escape-string - (string-trim - (ert-reason-for-test-result result))) - (ert-string-for-test-result - result - (ert-test-result-expected-p - test result))) - (xml-escape-string - (string-trim - (ert-reason-for-test-result result))) - "\n" - " \n")) - ((ert-test-aborted-with-non-local-exit-p result) - (insert (format " \n" - (file-name-nondirectory test-report) - (ert-string-for-test-result - result - (ert-test-result-expected-p - test result))) - (format "Test %s aborted with non-local exit\n" - (xml-escape-string - (symbol-name (ert-test-name test)))) - " \n")) - ((not (ert-test-result-type-p - result (ert-test-expected-result-type test))) - (insert (format " \n" - (xml-escape-string - (string-trim - (ert-reason-for-test-result result))) - (ert-string-for-test-result - result - (ert-test-result-expected-p - test result))) - (xml-escape-string - (string-trim - (ert-reason-for-test-result result))) - "\n" - " \n"))) - (unless (zerop (length (ert-test-result-messages result))) - (insert " \n" - (xml-escape-string - (ert-test-result-messages result)) - " \n")) - (insert " \n"))) - (insert " \n") - (insert "\n"))))) + (when-let ((symbol (car (apropos-internal "" #'ert-test-boundp))) + (test-file (symbol-file symbol 'ert--test)) + (test-report (file-name-with-extension test-file "xml"))) + (with-temp-file test-report + (insert "\n") + (insert (format "\n" + (file-name-nondirectory test-report) + (ert-stats-total stats) + (if (ert--stats-aborted-p stats) 1 0) + (ert-stats-completed-unexpected stats) + (ert-stats-skipped stats) + (float-time + (time-subtract + (ert--stats-end-time stats) + (ert--stats-start-time stats))))) + (insert (format " \n" + (file-name-nondirectory test-report) + (ert-stats-total stats) + (if (ert--stats-aborted-p stats) 1 0) + (ert-stats-completed-unexpected stats) + (ert-stats-skipped stats) + (float-time + (time-subtract + (ert--stats-end-time stats) + (ert--stats-start-time stats))) + (ert--format-time-iso8601 (ert--stats-end-time stats)))) + (insert " \n" + (format " \n" + (ert--stats-selector stats)) + " \n") + (cl-loop for test across (ert--stats-tests stats) + for result = (ert-test-most-recent-result test) do + (insert (format " \n") + (insert ">\n") + (cond + ((ert-test-skipped-p result) + (insert (format " \n" + (xml-escape-string + (string-trim + (ert-reason-for-test-result result))) + (ert-string-for-test-result + result + (ert-test-result-expected-p + test result))) + (xml-escape-string + (string-trim + (ert-reason-for-test-result result))) + "\n" + " \n")) + ((ert-test-aborted-with-non-local-exit-p result) + (insert (format " \n" + (file-name-nondirectory test-report) + (ert-string-for-test-result + result + (ert-test-result-expected-p + test result))) + (format "Test %s aborted with non-local exit\n" + (xml-escape-string + (symbol-name (ert-test-name test)))) + " \n")) + ((not (ert-test-result-type-p + result (ert-test-expected-result-type test))) + (insert (format " \n" + (xml-escape-string + (string-trim + (ert-reason-for-test-result result))) + (ert-string-for-test-result + result + (ert-test-result-expected-p + test result))) + (xml-escape-string + (string-trim + (ert-reason-for-test-result result))) + "\n" + " \n"))) + (unless (zerop (length (ert-test-result-messages result))) + (insert " \n" + (xml-escape-string + (ert-test-result-messages result)) + " \n")) + (insert " \n"))) + (insert " \n") + (insert "\n")))) (defun ert-write-junit-test-summary-report (&rest logfiles) "Write a JUnit summary test report, generated from LOGFILES." @@ -1637,24 +1635,30 @@ the tests)." (dolist (logfile logfiles) (let ((test-report (file-name-with-extension logfile "xml"))) (if (not (file-readable-p test-report)) - (let ((logfile (file-name-with-extension logfile "log"))) - (insert (format " \n" - id test-report - (ert--format-time-iso8601 (current-time)))) - (insert (format " \n" - (file-name-nondirectory test-report))) - (insert (format " \n" - (file-name-nondirectory test-report))) - (when (file-readable-p logfile) - (insert (xml-escape-string - (with-temp-buffer - (insert-file-contents-literally logfile) - (buffer-string))))) - (insert " \n" - " \n" - " \n") - (cl-incf errors 1) - (cl-incf id 1)) + (let* ((logfile (file-name-with-extension logfile "log")) + (logfile-contents + (when (file-readable-p logfile) + (with-temp-buffer + (insert-file-contents-literally logfile) + (buffer-string))))) + (unless + ;; No defined tests, perhaps a helper file. + (and logfile-contents + (string-match-p "^Running 0 tests" logfile-contents)) + (insert (format " \n" + id test-report + (ert--format-time-iso8601 (current-time)))) + (insert (format " \n" + (file-name-nondirectory test-report))) + (insert (format " \n" + (file-name-nondirectory test-report))) + (when logfile-contents + (insert (xml-escape-string logfile-contents))) + (insert " \n" + " \n" + " \n") + (cl-incf errors 1) + (cl-incf id 1))) (insert-file-contents-literally test-report) (when (looking-at-p -- 2.39.2