contrib/hg-test-mode.el
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 46819 d4ba4d51f85f
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.

;; hg-test-mode.el - Major mode for editing Mercurial tests
;;
;; Copyright 2014 Olivia Mackall <olivia@selenic.com>
;; "I have no idea what I'm doing"
;;
;; This software may be used and distributed according to the terms of the
;; GNU General Public License version 2 or any later version.
;;
;; To enable, add something like the following to your .emacs:
;;
;; (if (file-exists-p "~/hg/contrib/hg-test-mode.el")
;;    (load "~/hg/contrib/hg-test-mode.el"))

(defvar hg-test-mode-hook nil)

(defvar hg-test-mode-map
  (let ((map (make-keymap)))
    (define-key map "\C-j" 'newline-and-indent)
    map)
  "Keymap for hg test major mode")

(add-to-list 'auto-mode-alist '("\\.t\\'" . hg-test-mode))

(defconst hg-test-font-lock-keywords-1
  (list
   '("^  \\(\\$\\|>>>\\) " 1 font-lock-builtin-face)
   '("^  \\(>\\|\\.\\.\\.\\) " 1 font-lock-constant-face)
   '("^  \\([[][0-9]+[]]\\)$" 1 font-lock-warning-face)
   '("^  \\(.*?\\)\\(\\( [(][-a-z]+[)]\\)*\\)$" 1 font-lock-string-face)
   '("\\$?\\(HG\\|TEST\\)\\w+=?" . font-lock-variable-name-face)
   '("^  \\(.*?\\)\\(\\( [(][-a-z]+[)]\\)+\\)$" 2 font-lock-type-face)
   '("^#.*" . font-lock-preprocessor-face)
   '("^\\([^ ].*\\)$" 1 font-lock-comment-face)
   )
  "Minimal highlighting expressions for hg-test mode")

(defvar hg-test-font-lock-keywords hg-test-font-lock-keywords-1
  "Default highlighting expressions for hg-test mode")

(defvar hg-test-mode-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?\" "w" st) ;; disable standard quoting
    st)
"Syntax table for hg-test mode")

(defun hg-test-mode ()
  (interactive)
  (kill-all-local-variables)
  (use-local-map hg-test-mode-map)
  (set-syntax-table hg-test-mode-syntax-table)
  (set (make-local-variable 'font-lock-defaults) '(hg-test-font-lock-keywords))
  (setq major-mode 'hg-test-mode)
  (setq mode-name "hg-test")
  (run-hooks 'hg-test-mode-hook))

(with-eval-after-load "compile"
  ;; Link to Python sources in tracebacks in .t failures.
  (add-to-list 'compilation-error-regexp-alist-alist
               '(hg-test-output-python-tb
                 "^\\+ +File ['\"]\\([^'\"]+\\)['\"], line \\([0-9]+\\)," 1 2))
  (add-to-list 'compilation-error-regexp-alist 'hg-test-output-python-tb)
  ;; Link to source files in test-check-code.t violations.
  (add-to-list 'compilation-error-regexp-alist-alist
               '(hg-test-check-code-output
                 "\\+  \\([^:\n]+\\):\\([0-9]+\\):$" 1 2))
  (add-to-list 'compilation-error-regexp-alist 'hg-test-check-code-output))

(defun hg-test-mode--test-one-error-line-regexp (test)
  (erase-buffer)
  (setq compilation-locs (make-hash-table))
  (insert (car test))
  (compilation-parse-errors (point-min) (point-max))
  (let ((msg (get-text-property 1 'compilation-message)))
    (should msg)
    (let ((loc (compilation--message->loc msg))
          (line (nth 1 test))
          (file (nth 2 test)))
      (should (equal (compilation--loc->line loc) line))
      (should (equal (caar (compilation--loc->file-struct loc)) file)))
      msg))

(require 'ert)
(ert-deftest hg-test-mode--compilation-mode-support ()
  "Test hg-specific compilation-mode regular expressions"
  (require 'compile)
  (with-temp-buffer
    (font-lock-mode -1)
    (mapc 'hg-test-mode--test-one-error-line-regexp
          '(
            ("+  contrib/debugshell.py:37:" 37 "contrib/debugshell.py")
            ("+    File \"/tmp/hg/mercurial/commands.py\", line 3115, in help_"
             3115 "/tmp/hg/mercurial/commands.py")
            ("+    File \"mercurial/dispatch.py\", line 225, in dispatch"
             225 "mercurial/dispatch.py")))))


(provide 'hg-test-mode)