A JavaScript development environment for Emacs https://indium.readthedocs.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.6 KiB

;;; test-helper.el --- Helpers for running Indium tests -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2017 Nicolas Petton
;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords: convenience
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides helpers for running Indium tests.
;;; Code:
(require 'seq)
(require 'map)
(when (require 'undercover nil t)
(setq undercover-force-coverage t)
(undercover "*.el" (:exclude "run-lint.el" "test/*.el")))
(advice-add 'undercover-report :after #'print-coverage-report-safe)
(defun print-coverage-report-safe (&rest _)
(defun print-coverage-report ()
(let* ((coverage (apply #'seq-concatenate 'list
(seq-map (lambda (src)
(let ((coverage (map-elt src 'coverage)))
(seq-filter #'identity coverage)))
(map-elt (json-read-file "/tmp/undercover_coveralls_report") 'source_files))))
(covered-lines (seq-filter (lambda (line)
(not (zerop line)))
(percentage (round (* (/ (seq-length covered-lines) (seq-length coverage) 1.0) 100))))
(message "%d%% covered" percentage)))
(defmacro with-js2-buffer (contents &rest body)
"Evaluate BODY.
BODY is evaluated with the current buffer set to a JavaScript
buffer in `js2-mode' with CONTENTS."
(declare (indent 1))
(insert ,contents)
(goto-char (point-max))
(defmacro with-temp-workspace-file (&rest body)
"Evaluate BODY.
During the evaluation of BODY, `indium-workspace-file' is set to
a temporary file, which is removed afterwards."
(declare (indent 0))
`(let ((indium-workspace-file (make-temp-file "indium-workspace.el")))
(progn ,@body)
(ignore-errors (delete-file indium-workspace-file nil)))))
(defmacro with-indium-connection (connection &rest body)
"Evaluate BODY with CONNECTION as the indium-current-connection."
(declare (indent 1))
`(let ((indium-current-connection ,connection))
(defmacro with-fake-indium-connection (&rest body)
"Evaluate BODY with an indium connection with a fake backend."
(declare (indent 0))
`(with-indium-connection (make-indium-connection :backend 'fake)
(defmacro with-nodejs-connection (&rest body)
"Run BODY within a NodeJS connection on a process on fixtures/test.js."
(declare (indent 0))
(ignore-errors (exec-path-from-shell-initialize))
(indium-run-node "node fixtures/test.js")
(defun wait-for-repl-buffer (&optional retry)
(unless retry (setq retry 10))
(sleep-for 0.2)
(unless (or (get-buffer (indium-repl-buffer-name))
(= retry 0))
(wait-for-repl-buffer (1- retry)))
(sleep-for 0.2))
(defun kill-nodejs-process ()
"Kill the nodejs process if any."
(when-let ((buf (get-buffer "*node process*")))
(when-let ((process (get-buffer-process buf)))
(set-process-query-on-exit-flag process nil))
(kill-buffer buf))
(defmacro with-repl-buffer (&rest body)
"Execute BODY within a REPL buffer with a NodeJS connection."
(declare (indent 0))
(with-current-buffer (indium-repl-buffer-name)
(defun repl-eval (expression)
"Send EXPRESSION to be evaluated.
Onece EXPRESSION has been sent for evaluation, sleep for 500ms to
give time for the runtime to send a response."
(insert expression)
(press-and-sleep-for "RET" 1))
(defun press (keybinding)
"Call interactively the command bound to KEYBINDING."
(call-interactively (key-binding (kbd keybinding))))
(defun press-and-sleep-for (keybinding seconds)
"Call `(press KEYBINDING)` and sleep for SECONDS."
(press keybinding)
(sleep-for seconds))
(provide 'test-helper)
;;; test-helper.el ends here