1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
;;; wgrep-test-helper.el --- A wgrep test helper -*- lexical-binding: t-*-
;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep-test-helper.el
(require 'wgrep)
(defun wgrep-test-helper--wait (buf)
(let ((proc (get-buffer-process buf)))
(while (eq (process-status proc) 'run)
(sit-for 0.1))
(sleep-for 0.2)
(switch-to-buffer buf)))
(defun wgrep-test-helper--grep (command)
(let ((buf (grep command)))
(wgrep-test-helper--wait buf)))
(defun wgrep-test-helper--get-contents (file &optional cs)
(let ((coding-system-for-read cs))
(with-temp-buffer
(insert-file-contents file)
(buffer-string))))
(defun wgrep-test-helper--prepare-file (file contents &optional cs)
;; cleanup for convenience
(let ((buf (get-file-buffer file)))
(when (buffer-live-p buf)
(kill-buffer buf)))
(let ((coding-system-for-write cs))
(write-region contents nil file)))
(defun wgrep-test-helper--cleanup-file (file)
(when (file-exists-p file)
(delete-file file))
(when (file-exists-p (concat file "~"))
(delete-file (concat file "~"))))
(defmacro wgrep-test-helper--default (&rest body)
`(let ((wgrep-change-readonly-file nil)
(wgrep-auto-save-buffer nil))
(progn ,@body)))
(defun wgrep-test-helper--ag (string file)
(let ((buf (ag/search string default-directory :file-regex (regexp-quote file) :regexp t)))
(wgrep-test-helper--wait buf)))
(defun wgrep-test-helper--deadgrep (string)
(let ((deadgrep-project-root-function (lambda () default-directory))
(deadgrep--search-type 'regexp))
(deadgrep string))
(wgrep-test-helper--wait (current-buffer)))
(defun wgrep-test-helper-fixture (data body-fn)
(let ((test-directory (expand-file-name "test-work" default-directory)))
(unless (file-directory-p test-directory)
(make-directory test-directory t))
(let ((default-directory (file-name-as-directory test-directory)))
(let ((file (concat (make-temp-name "test-data") ".txt")))
(pcase data
((pred stringp)
(wgrep-test-helper--prepare-file file data))
(`(,(and (pred stringp) data) ,(and (pred coding-system-p) cs))
(wgrep-test-helper--prepare-file file data cs))
(_
(error "DATA should be STRING or (STRING CODING-SYSTEM)")))
(unwind-protect
(funcall body-fn file)
(wgrep-test-helper--cleanup-file file))))))
(put 'wgrep-test-helper-fixture 'lisp-indent-function 1)
(provide 'wgrep-test-helper)
|