<commit_before>;;; runner.el --- run ERT tests with Bazel      -*- lexical-binding: t; -*-

;; Copyright 2020, 2021 Google LLC
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     https://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

;;; Commentary:

;; This library runs ERT tests under Bazel.  It provides support for the
;; --test_filter flag, as described in
;; https://docs.bazel.build/test-encyclopedia.html#initial-conditions.

;;; Code:

(require 'backtrace nil :noerror)  ; only in Emacs 27
(require 'bytecomp)
(require 'cl-lib)
(require 'cl-macs)
(require 'debug)
(require 'edebug)
(require 'ert)
(require 'format)
(require 'nadvice)
(require 'pp)
(require 'rx)
(require 'subr-x)
(require 'trampver)  ; load eagerly to work around Bug#11218
(require 'warnings)
(require 'xml)

(add-to-list 'command-switch-alist
             (cons "--test-source" #'elisp/ert/test-source))
(add-to-list 'command-switch-alist (cons "--skip-test" #'elisp/ert/skip-test))
(add-to-list 'command-switch-alist (cons "--skip-tag" #'elisp/ert/skip-tag))

(defvar elisp/ert/test--sources ()
  "Test source files to be loaded.
This list is populated by --test-source command-line options.")

;; Customizable Edebug behavior only appeared in Emacs 27.
(defvar edebug-behavior-alist)
(defvar edebug-after-instrumentation-function)
(defvar edebug-new-definition-function)

(defvar warning-fill-column)  ; only in Emacs 27 and later

(defun elisp/ert/run-batch-and-exit ()
  "Run ERT tests in batch mode.
This is similar to ‘ert-run-tests-batch-and-exit’, but uses the
TESTBRIDGE_TEST_ONLY environmental variable as test selector."
  (or noninteractive (error "This function works only in batch mode"))
  (let* ((attempt-stack-overflow-recovery nil)
         (attempt-orderly-shutdown-on-fatal-signal nil)
         (edebug-initial-mode 'Go-nonstop)  ; ‘step’ doesn’t work in batch mode
         ;; If possible, we perform our own coverage instrumentation, but that’s
         ;; only possible in Emacs 27.
         (edebug-behavior-alist (cons '(elisp/ert/coverage
                                        elisp/ert/edebug--enter
                                        elisp/ert/edebug--before
                                        elisp/ert/edebug--after)
                                      (bound-and-true-p edebug-behavior-alist)))
         (warning-fill-column 1000)  ; Bug#52281
         (source-dir (getenv "TEST_SRCDIR"))
         (temp-dir (getenv "TEST_TMPDIR"))
         (temporary-file-directory (concat "/:" temp-dir))
         (report-file (getenv "XML_OUTPUT_FILE"))
         (random-seed (or (getenv "TEST_RANDOM_SEED") ""))
         (shard-count (string-to-number (or (getenv "TEST_TOTAL_SHARDS") "1")))
         (shard-index (string-to-number (or (getenv "TEST_SHARD_INDEX") "0")))
         (shard-status-file (getenv "TEST_SHARD_STATUS_FILE"))
         (coverage-enabled (equal (getenv "COVERAGE") "1"))
         (coverage-manifest (getenv "COVERAGE_MANIFEST"))
         (coverage-dir (getenv "COVERAGE_DIR"))
         (selector (elisp/ert/make--selector<commit_msg>Add preliminary support for ERT resource macros<commit_after>;;; runner.el --- run ERT tests with Bazel      -*- lexical-binding: t; -*-

;; Copyright 2020, 2021 Google LLC
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     https://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

;;; Commentary:

;; This library runs ERT tests under Bazel.  It provides support for the
;; --test_filter flag, as described in
;; https://docs.bazel.build/test-encyclopedia.html#initial-conditions.

;;; Code:

(require 'backtrace nil :noerror)  ; only in Emacs 27
(require 'bytecomp)
(require 'cl-lib)
(require 'cl-macs)
(require 'debug)
(require 'edebug)
(require 'ert)
(require 'ert-x)
(require 'format)
(require 'nadvice)
(require 'pp)
(require 'rx)
(require 'subr-x)
(require 'trampver)  ; load eagerly to work around Bug#11218
(require 'warnings)
(require 'xml)

(add-to-list 'command-switch-alist
             (cons "--test-source" #'elisp/ert/test-source))
(add-to-list 'command-switch-alist (cons "--skip-test" #'elisp/ert/skip-test))
(add-to-list 'command-switch-alist (cons "--skip-tag" #'elisp/ert/skip-tag))

(defvar elisp/ert/test--sources ()
  "Test source files to be loaded.
This list is populated by --test-source command-line options.")

;; Customizable Edebug behavior only appeared in Emacs 27.
(defvar edebug-behavior-alist)
(defvar edebug-after-instrumentation-function)
(defvar edebug-new-definition-function)

(defvar warning-fill-column)  ; only in Emacs 27 and later

;; ERT resource variables only appeared in Emacs 28.
(defvar ert-resource-directory-trim-left-regexp)
(defvar ert-resource-directory-format)

(defun elisp/ert/run-batch-and-exit ()
  "Run ERT tests in batch mode.
This is similar to ‘ert-run-tests-batch-and-exit’, but uses the
TESTBRIDGE_TEST_ONLY environmental variable as test selector."
  (or noninteractive (error "This function works only in batch mode"))
  (let* ((attempt-stack-overflow-recovery nil)
         (attempt-orderly-shutdown-on-fatal-signal nil)
         (edebug-initial-mode 'Go-nonstop)  ; ‘step’ doesn’t work in batch mode
         ;; If possible, we perform our own coverage instrumentation, but that’s
         ;; only possible in Emacs 27.
         (edebug-behavior-alist (cons '(elisp/ert/coverage
                                        elisp/ert/edebug--enter
                                        elisp/ert/edebug--before
                                        elisp/ert/edebug--after)
                                      (bound-and-true-p edebug-behavior-alist)))
         (warning-fill-column 1000)  ; Bug#52281
         (source-dir (getenv "TEST_SRCDIR"))
         (temp-dir (getenv "TEST_TMPDIR"))
         (temporary-file-directory (concat "/:" temp-dir))
         ;; We could get the workspace name from the TEST_WORKSPACE environment
         ;; variable, but that one’s optional
         ;; (cf. https://docs.bazel.build/versions/4.2.2/test-encyclopedia.html#initial-conditions).
         (workspace-name (file-name-nondirectory
                          (directory-file-name default-directory)))
         ;; Best-effort support for ‘ert-resource-directory’ and
         ;; ‘ert-resource-file’.  The directory returned by
         ;; ‘ert-resource-directory’ will typically be in the execution root and
         ;; no longer be valid when the test runs.  Therefore, strip out
         ;; everything up to the workspace directory in the execution root
         ;; (cf. https://docs.bazel.build/versions/4.2.2/output_directories.html#layout-diagram),
         ;; and replace it with the default directory.  Robust tests should use
         ;; the ‘elisp/runfiles’ library to find their data files.
         (ert-resource-directory-trim-left-regexp
          (rx-to-string `(seq (* nonl) ?/ ,workspace-name ?/) :no-group))
         (ert-resource-directory-format
          (concat (replace-regexp-in-string (rx ?%) "%%" default-directory
                                            :fixedcase :literal)
                  "/%s-resources/"))
         (report-file (getenv "XML_OUTPUT_FILE"))
         (random-seed (or (getenv "TEST_RANDOM_SEED") ""))
         (shard-count (string-to-number (or (getenv "TEST_TOTAL_SHARDS") "1")))
         (shard-index (string-to-number (or (getenv "TEST_SHARD_INDEX") "0")))
         (shard-status-file (getenv "TEST_SHARD_STATUS_FILE"))
         (coverage-enabled (equal (getenv "COVERAGE") "1"))
         (coverage-manifest (getenv "COVERAGE_MANIFEST"))
         (coverage-dir (getenv "COVERAGE_DIR"))
         (selector (elisp/ert/make--selector
{'fraction_non_alphanumeric': 0.1257189811010682, 'fraction_numerical': 0.009625542904096725, 'mean_word_length': 2.870967741935484, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '308489', 'n_tokens_mistral': 2654, 'n_tokens_neox': 2579, 'n_words': 759}