dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

test-helper.el (3515B)


      1 ;;; test-helper --- Test helper for pdf-tools  -*- lexical-binding: t; -*-
      2 
      3 ;;; Commentary:
      4 ;; test helper inspired from https://github.com/tonini/overseer.el/blob/master/test/test-helper.el
      5 
      6 ;;; Code:
      7 
      8 (require 'package)
      9 (require 'ert)
     10 (require 'cl-lib)
     11 
     12 (unless (getenv "PACKAGE_TAR")
     13   (error "Missing package tar. Must be passed by PACKAGE_TAR env variable"))
     14 
     15 (defvar pdf-tools-package (expand-file-name (getenv "PACKAGE_TAR")))
     16 
     17 (unless (and (file-exists-p pdf-tools-package)
     18              (string-match "\\.tar\\'" pdf-tools-package))
     19   (error "Invalid tar package: %s" pdf-tools-package))
     20 
     21 (unless load-file-name
     22   (error "load-file-name is unset"))
     23 
     24 (cd (file-name-directory load-file-name))
     25 (setq package-user-dir (expand-file-name "elpa" (make-temp-file "package" t)))
     26 
     27 (defvar cask-elpa
     28   (cl-labels ((directory-if-exists-p (directory)
     29                 (and (file-directory-p directory)
     30                      directory)))
     31     (or (directory-if-exists-p
     32          (format "../.cask/%s/elpa" emacs-version))
     33         (directory-if-exists-p
     34          (format "../.cask/%d.%d/elpa"
     35                  emacs-major-version emacs-minor-version))
     36         (error "Do `cask install' first"))))
     37 
     38 (add-to-list 'package-directory-list cask-elpa)
     39 (add-hook 'kill-emacs-hook (lambda nil
     40                              (when (file-exists-p package-user-dir)
     41                                (delete-directory package-user-dir t))))
     42 (package-initialize)
     43 (package-install-file pdf-tools-package)
     44 
     45 ;; FIXME: Move functions to new, loadable file.
     46 ;; Fake skipped as accepted failures if skip-unless is not available.
     47 (unless (fboundp 'ert--skip-unless)
     48   (defun skip-unless (arg)
     49     (unless arg
     50       (setf (ert-test-expected-result-type
     51              (car ert--running-tests))
     52             :failed)
     53       (ert-fail (list nil)))))
     54 
     55 (defun pdf-test-relative-edges-p (edges)
     56   (and (consp edges)
     57        (cl-every (lambda (x)
     58                    (and (numberp x)
     59                         (<= x 1)
     60                         (>= x 0)))
     61                  edges)))
     62 
     63 (defmacro pdf-test-with-pdf (pdf-filename &rest body)
     64   (declare (indent 0) (debug t))
     65   (let ((buffer (make-symbol "buffer")))
     66     `(let ((,buffer (find-file-noselect
     67                      (expand-file-name ,pdf-filename)))
     68            (pdf-info-epdfinfo-error-filename (make-temp-file "epdfinfo.log")))
     69        (unwind-protect
     70            (progn
     71              (pdf-info-quit)
     72              (pdf-info-process-assert-running t)
     73              (with-current-buffer ,buffer ,@body))
     74          (when (buffer-live-p ,buffer)
     75            (with-current-buffer ,buffer
     76              (set-buffer-modified-p nil)
     77              (let (kill-buffer-hook)
     78                (kill-buffer))))
     79          (when (file-exists-p pdf-info-epdfinfo-error-filename)
     80            (with-temp-buffer
     81              (insert-file-contents pdf-info-epdfinfo-error-filename)
     82              (unless (= 1 (point-max))
     83                (message ">>> %s <<<" (buffer-string))))
     84            (delete-file pdf-info-epdfinfo-error-filename))
     85          (pdf-info-quit)))))
     86 
     87 (defmacro pdf-test-with-test-pdf (&rest body)
     88   `(pdf-test-with-pdf "test.pdf" ,@body))
     89 
     90 (defmacro pdf-test-with-encrypted-pdf (&rest body)
     91   `(pdf-test-with-pdf "encrypted.pdf" ,@body))
     92 
     93 ;; ---
     94 (require 'undercover)
     95 (undercover "lisp/*.el")
     96 (require 'let-alist)
     97 (require 'pdf-info)
     98 (require 'ert)
     99 (require 'pdf-tools)
    100 
    101 ;; FIXME: Can't be compiled with the Package-Requires dependencies alone.
    102 ;; Local Variables:
    103 ;; no-byte-compile: t
    104 ;; End:
    105 
    106 ;;; test-helper.el ends here