2018-02-27 14:38:13 -06:00
|
|
|
;;; ein-testing.el --- Tools for testing
|
|
|
|
|
|
|
|
;; Copyright (C) 2012 Takafumi Arakaki
|
|
|
|
|
|
|
|
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;; ein-testing.el 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.
|
|
|
|
|
|
|
|
;; ein-testing.el is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with ein-testing.el. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'ein-log)
|
2020-01-02 20:09:42 -05:00
|
|
|
(require 'ein-jupyter)
|
2018-10-01 18:40:31 -04:00
|
|
|
(require 'request)
|
2019-11-24 00:17:52 -05:00
|
|
|
(require 'anaphora)
|
2018-02-27 14:38:13 -06:00
|
|
|
|
|
|
|
(defmacro ein:setq-if-not (sym val)
|
|
|
|
`(unless ,sym (setq ,sym ,val)))
|
|
|
|
|
|
|
|
(defvar ein:testing-dump-file-log nil
|
|
|
|
"File to save buffer specified by `ein:log-all-buffer-name'.")
|
|
|
|
|
|
|
|
(defvar ein:testing-dump-file-messages nil
|
|
|
|
"File to save the ``*Messages*`` buffer.")
|
|
|
|
|
2018-10-01 18:40:31 -04:00
|
|
|
(defvar ein:testing-dump-file-server nil
|
2019-11-24 00:17:52 -05:00
|
|
|
"File to save `*ein:jupyter-server-buffer-name*`.")
|
2018-02-27 14:38:13 -06:00
|
|
|
|
2018-10-01 18:40:31 -04:00
|
|
|
(defvar ein:testing-dump-file-request nil
|
|
|
|
"File to save `request-log-buffer-name`.")
|
2018-02-27 14:38:13 -06:00
|
|
|
|
|
|
|
(defun ein:testing-save-buffer (buffer-or-name file-name)
|
|
|
|
(when (and buffer-or-name (get-buffer buffer-or-name) file-name)
|
2019-11-14 19:35:40 -05:00
|
|
|
(let ((dir (file-name-directory file-name)))
|
|
|
|
(make-directory dir t)
|
|
|
|
(with-current-buffer buffer-or-name
|
|
|
|
(let ((coding-system-for-write 'raw-text))
|
|
|
|
(write-region (point-min) (point-max) file-name))))))
|
2018-02-27 14:38:13 -06:00
|
|
|
|
|
|
|
(defun ein:testing-dump-logs ()
|
|
|
|
(ein:testing-save-buffer "*Messages*" ein:testing-dump-file-messages)
|
2020-01-02 20:09:42 -05:00
|
|
|
(ein:testing-save-buffer *ein:jupyter-server-buffer-name* ein:testing-dump-file-server)
|
2019-05-07 15:13:29 -04:00
|
|
|
(mapc (lambda (b)
|
|
|
|
(ein:and-let* ((bname (buffer-name b))
|
|
|
|
(prefix "kernels/")
|
|
|
|
(is-websocket (search "*websocket" bname))
|
|
|
|
(kernel-start (search prefix bname))
|
|
|
|
(sofar (subseq bname (+ kernel-start (length prefix))))
|
|
|
|
(kernel-end (search "/" sofar)))
|
|
|
|
(ein:testing-save-buffer
|
|
|
|
bname
|
|
|
|
(concat ein:testing-dump-file-websocket "."
|
|
|
|
(seq-take sofar kernel-end)))))
|
|
|
|
(buffer-list))
|
2018-10-01 18:40:31 -04:00
|
|
|
(ein:testing-save-buffer ein:log-all-buffer-name ein:testing-dump-file-log)
|
|
|
|
(ein:testing-save-buffer request-log-buffer-name ein:testing-dump-file-request))
|
|
|
|
|
2018-10-15 16:57:22 -04:00
|
|
|
(defun ein:testing-flush-queries (&optional ms interval)
|
2018-10-24 13:12:16 -04:00
|
|
|
"I need the smoke to clear, but just waiting for zero running processes doesn't work
|
|
|
|
if I call this between links in a deferred chain. Adding a flush-queue."
|
2018-10-23 02:12:45 -04:00
|
|
|
(deferred:flush-queue!)
|
2018-10-11 16:53:02 -04:00
|
|
|
(ein:testing-wait-until (lambda ()
|
2020-01-10 23:58:51 -05:00
|
|
|
(not (seq-some (lambda (proc)
|
|
|
|
(cl-search "request curl"
|
|
|
|
(process-name proc)))
|
|
|
|
(process-list))))
|
2018-10-15 16:57:22 -04:00
|
|
|
nil ms interval t))
|
2018-10-11 16:53:02 -04:00
|
|
|
|
2018-10-24 13:12:16 -04:00
|
|
|
(defun ein:testing-make-directory-level (parent current-depth width depth)
|
2018-12-01 18:54:58 -05:00
|
|
|
(let ((write-region-inhibit-sync nil))
|
|
|
|
(f-touch (concat (file-name-as-directory parent) "foo.txt"))
|
|
|
|
(f-touch (concat (file-name-as-directory parent) "bar.ipynb"))
|
|
|
|
(f-write-text "{
|
2018-10-24 13:12:16 -04:00
|
|
|
\"cells\": [],
|
|
|
|
\"metadata\": {},
|
|
|
|
\"nbformat\": 4,
|
|
|
|
\"nbformat_minor\": 2
|
|
|
|
}
|
2018-12-01 18:54:58 -05:00
|
|
|
" 'utf-8 (concat (file-name-as-directory parent) "bar.ipynb")))
|
2018-10-24 13:12:16 -04:00
|
|
|
(if (< current-depth depth)
|
2020-01-02 20:09:42 -05:00
|
|
|
(cl-loop for w from 1 to width
|
2018-10-24 13:12:16 -04:00
|
|
|
for dir = (concat (file-name-as-directory parent) (number-to-string w))
|
|
|
|
do (f-mkdir dir)
|
|
|
|
(ein:testing-make-directory-level dir (1+ current-depth) width depth))))
|
|
|
|
|
2018-10-11 16:53:02 -04:00
|
|
|
(defun ein:testing-wait-until (predicate &optional predargs ms interval continue)
|
2018-10-01 18:40:31 -04:00
|
|
|
"Wait until PREDICATE function returns non-`nil'.
|
|
|
|
PREDARGS is argument list for the PREDICATE function.
|
|
|
|
MS is milliseconds to wait. INTERVAL is polling interval in milliseconds."
|
2019-11-24 00:17:52 -05:00
|
|
|
(let* ((int (aif interval it (aif ms (max 300 (/ ms 10)) 300)))
|
2018-10-11 16:53:02 -04:00
|
|
|
(count (max 1 (if ms (truncate (/ ms int)) 25))))
|
2020-01-02 20:09:42 -05:00
|
|
|
(unless (or (cl-loop repeat count
|
2018-10-11 16:53:02 -04:00
|
|
|
when (apply predicate predargs)
|
|
|
|
return t
|
2018-10-24 13:12:16 -04:00
|
|
|
do (sleep-for 0 int))
|
2018-10-11 16:53:02 -04:00
|
|
|
continue)
|
2018-10-01 18:40:31 -04:00
|
|
|
(error "Timeout: %s" predicate))))
|
2018-02-27 14:38:13 -06:00
|
|
|
|
2019-02-19 15:29:18 -05:00
|
|
|
(defun ein:testing-new-notebook (url-or-port ks &optional retry)
|
2018-10-24 13:12:16 -04:00
|
|
|
(lexical-let (notebook)
|
|
|
|
(condition-case err
|
|
|
|
(progn
|
2019-02-14 15:28:18 -05:00
|
|
|
(ein:testing-wait-until (lambda ()
|
|
|
|
(ein:notebooklist-list-get url-or-port))
|
|
|
|
nil 10000 1000)
|
|
|
|
(ein:notebooklist-new-notebook url-or-port ks
|
|
|
|
(lambda (nb created)
|
2018-10-24 13:12:16 -04:00
|
|
|
(setq notebook nb)))
|
2018-12-25 19:12:26 -05:00
|
|
|
(ein:testing-wait-until (lambda ()
|
2018-10-24 13:12:16 -04:00
|
|
|
(and notebook
|
|
|
|
(ein:aand (ein:$notebook-kernel notebook)
|
|
|
|
(ein:kernel-live-p it))))
|
2019-05-16 15:01:45 -04:00
|
|
|
nil 20000 1000)
|
2018-10-24 13:12:16 -04:00
|
|
|
notebook)
|
2019-02-19 15:29:18 -05:00
|
|
|
(error (let ((notice (format "ein:testing-new-notebook: [%s] %s"
|
|
|
|
url-or-port (error-message-string err))))
|
|
|
|
(if retry
|
|
|
|
(progn (ein:log 'error notice) nil)
|
|
|
|
(ein:log 'info notice)
|
|
|
|
(sleep-for 0 1500)
|
|
|
|
(ein:testing-new-notebook url-or-port ks t)))))))
|
2018-10-24 13:12:16 -04:00
|
|
|
|
2018-02-27 14:38:13 -06:00
|
|
|
(defadvice ert-run-tests-batch (after ein:testing-dump-logs-hook activate)
|
2018-10-01 18:40:31 -04:00
|
|
|
"Hook `ein:testing-dump-logs-hook' because `kill-emacs-hook'
|
2018-02-27 14:38:13 -06:00
|
|
|
is not run in batch mode before Emacs 24.1."
|
2018-10-01 18:40:31 -04:00
|
|
|
(ein:testing-dump-logs))
|
2018-02-27 14:38:13 -06:00
|
|
|
|
2018-10-01 18:40:31 -04:00
|
|
|
(add-hook 'kill-emacs-hook #'ein:testing-dump-logs)
|
2018-02-27 14:38:13 -06:00
|
|
|
|
2019-05-03 09:23:29 -04:00
|
|
|
(with-eval-after-load "ein-notebook"
|
|
|
|
;; if y-or-n-p isn't specially overridden, make it always "no"
|
|
|
|
(lexical-let ((original-y-or-n-p (symbol-function 'y-or-n-p)))
|
|
|
|
(add-function :around (symbol-function 'ein:notebook-ask-save)
|
|
|
|
(lambda (f &rest args)
|
|
|
|
(if (not (eq (symbol-function 'y-or-n-p) original-y-or-n-p))
|
|
|
|
(apply f args)
|
|
|
|
(cl-letf (((symbol-function 'y-or-n-p) (lambda (&rest args) nil)))
|
|
|
|
(apply f args)))))))
|
|
|
|
|
2018-02-27 14:38:13 -06:00
|
|
|
(provide 'ein-testing)
|
|
|
|
|
|
|
|
;;; ein-testing.el ends here
|