aboutsummaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorJonas Bernoulli <jonas@bernoul.li>2015-08-23 22:21:34 +0200
committerJonas Bernoulli <jonas@bernoul.li>2015-08-23 22:21:34 +0200
commita01717ec56231a7df25fb0d94f0d58f22e2f18b8 (patch)
treedc4f672e927a04bf18f5cefc50dd221c2245081c /t
parentafba3f95da679e871a91e46d61586a19d5191cde (diff)
sanitize existing tests
Diffstat (limited to 't')
-rw-r--r--t/magit-tests.el261
1 files changed, 102 insertions, 159 deletions
diff --git a/t/magit-tests.el b/t/magit-tests.el
index ec56acc..aa50a29 100644
--- a/t/magit-tests.el
+++ b/t/magit-tests.el
@@ -7,177 +7,120 @@
;;; Code:
(require 'cl-lib)
+(require 'dash)
(require 'ert)
-(require 'magit)
-
-;;; Utilities
-(defmacro magit-tests--silentio (&rest body)
- ;; Once upon a time there was a dynamic `flet'...
- (declare (indent defun))
- (let ((orig (cl-gensym)))
- `(let ((,orig (symbol-function 'message)))
- (fset 'message (lambda (&rest silentio)))
- (prog1 (progn ,@body)
- (fset 'message ,orig)))))
+(require 'magit)
-(defmacro magit-tests--with-temp-dir (&rest body)
+(defmacro magit-with-test-directory (&rest body)
(declare (indent 0) (debug t))
- (let ((dir (cl-gensym)))
- `(let ((,dir (file-name-as-directory (make-temp-file "dir" t))))
- (unwind-protect
- (let ((default-directory ,dir)) ,@body)
- (delete-directory ,dir t)))))
-
-(defmacro magit-tests--with-temp-repo (&rest body)
+ (let ((dir (make-symbol "dir")))
+ `(let ((,dir (file-name-as-directory (make-temp-file "magit-" t))))
+ (condition-case err
+ (cl-letf (((symbol-function #'message) (lambda (&rest _))))
+ (let ((default-directory ,dir))
+ ,@body))
+ (error (message "Keeping test directory:\n %s" ,dir)
+ (signal (car err) (cdr err))))
+ (delete-directory ,dir t))))
+
+(defmacro magit-with-test-repository (&rest body)
(declare (indent 0) (debug t))
- `(magit-tests--with-temp-dir
- (magit-call-git "init" ".")
- (magit-tests--silentio ,@body)))
-
-(defmacro magit-tests--with-temp-clone (url &rest body)
- (declare (indent 1) (debug t))
- (let ((repo (cl-gensym)))
- `(let ((,repo ,(or url 'default-directory)))
- (magit-tests--with-temp-dir
- (magit-call-git "clone" ,repo ".")
- ,@body))))
-
-(defmacro magit-tests--with-open-file (filename &rest body)
- (declare (indent 1) (debug t))
- (let ((buffer (make-symbol "*buffer*")))
- `(let (,buffer)
- (unwind-protect
- (progn
- (setq ,buffer (find-file-literally ,filename))
- ,@body)
- (when ,buffer (kill-buffer ,buffer))))))
-
-(defun magit-tests--modify-file (filename)
- (with-temp-file (expand-file-name filename)
- (insert (make-temp-name "content"))))
-
-(defun magit-tests--modify-and-commit (filename)
- (magit-tests--modify-file filename)
- (magit-call-git "add" filename)
- (magit-call-git "-c" "user.name=foo bar"
- "-c" "user.email=foo@bar.baz"
- "commit"
- "-m" (symbol-name (cl-gensym "message"))
- "--" filename))
-
-(defun magit-tests--should-have-section (type info)
- (magit-status-internal default-directory)
- (message (buffer-string))
- (should (--first (equal (magit-section-value it) info)
- (magit-section-children
- (magit-get-section `((,type) (status)))))))
-
-;;; Tests
-;;;; status
-
-(ert-deftest magit-status-untracked ()
- (magit-tests--with-temp-repo
- (magit-tests--modify-file "file")
- (magit-tests--modify-file "file with space")
- (magit-tests--modify-file "φιλε")
- (magit-tests--should-have-section 'untracked "file")
- (magit-tests--should-have-section 'untracked "file with space")
- (magit-tests--should-have-section 'untracked "φιλε")))
-
-(ert-deftest magit-status-staged-modified ()
- (magit-tests--with-temp-repo
- (magit-tests--modify-file "file")
- (magit-status-internal default-directory)
- (magit-stage-modified t)
- (magit-tests--should-have-section 'staged "file")))
-
-(ert-deftest magit-status-staged-modified-with-space ()
- (magit-tests--with-temp-repo
- (magit-tests--modify-file "file with space")
- (magit-status-internal default-directory)
- (magit-stage-modified t)
- (magit-tests--should-have-section 'staged "file with space")))
-
-(ert-deftest magit-status-modified ()
- (magit-tests--with-temp-repo
- (magit-tests--modify-and-commit "file")
- (magit-tests--modify-file "file")
- (magit-status-internal default-directory)
- (magit-tests--should-have-section 'unstaged "file")))
+ `(magit-with-test-directory (magit-git "init" ".") ,@body))
-(ert-deftest magit-status-modified-with-space ()
- (magit-tests--with-temp-repo
- (magit-tests--modify-and-commit "file with space")
- (magit-tests--modify-file "file with space")
- (magit-status-internal default-directory)
- (magit-tests--should-have-section 'unstaged "file with space")))
+;;; Git
-(ert-deftest magit-status-unpushed ()
- (magit-tests--with-temp-repo
- (magit-tests--modify-and-commit "file")
-
- (magit-tests--with-temp-clone default-directory
- (magit-tests--modify-and-commit "file")
- (magit-tests--should-have-section
- 'unpushed (magit-rev-parse "--short" "HEAD"))
-
- (magit-tests--modify-and-commit "file")
- (magit-tests--should-have-section
- 'unpushed (magit-rev-parse "--short" "HEAD")))))
-
-(ert-deftest magit-get-next-tag ()
- (magit-tests--with-temp-repo
- (magit-tests--modify-and-commit "file")
-
- (magit-tests--with-temp-clone default-directory
- ;; no tag, return nil
- (should (equal nil (magit-get-next-tag)))
- ;; tag is not annotated, return nil
- (magit-call-git "tag" "FIRST")
- (should (equal "FIRST" (magit-git-string "describe" "--contains" "FIRST")))
- (should (equal nil (magit-get-next-tag)))
- (magit-call-git "tag" "-d" "FIRST"))))
-
-;;;; config
-
-(ert-deftest magit-config-get-boolean ()
- (magit-tests--with-temp-repo
- (magit-call-git "config" "a.b" "true")
- (should (magit-get-boolean "a.b"))
- (should (magit-get-boolean "a" "b"))
-
- (magit-call-git "config" "a.b" "false")
+(ert-deftest magit-get-boolean ()
+ (magit-with-test-repository
+ (magit-git "config" "a.b" "true")
+ (should (magit-get-boolean "a.b"))
+ (should (magit-get-boolean "a" "b"))
+ (magit-git "config" "a.b" "false")
(should-not (magit-get-boolean "a.b"))
(should-not (magit-get-boolean "a" "b"))))
-;;;; branch and remotes
-(ert-deftest magit-list-branch ()
- (magit-tests--with-temp-repo
- (magit-tests--modify-and-commit "file")
- (should (member "master" (magit-list-branch-names)))
- (should (member "master" (magit-list-local-branch-names)))
- (should (null (magit-list-remote-branch-names)))
-
- (magit-tests--with-temp-clone default-directory
- (should (member "origin/master" (magit-list-branch-names)))
- (should-not (member "origin/master" (magit-list-local-branch-names)))
-
- (should (member "origin/master" (magit-list-remote-branch-names)))
- (should (member "origin/master" (magit-list-remote-branch-names "origin")))
-
- (should-not (member "origin/master" (magit-list-remote-branch-names "foo")))
- (should (member "master" (magit-list-remote-branch-names "origin" t)))
-
- (should-not (member "master" (magit-list-remote-branch-names "foo" t))))))
+(ert-deftest magit-get-{current|next}-tag ()
+ (magit-with-test-repository
+ (magit-git "commit" "-m" "1" "--allow-empty")
+ (should (equal (magit-get-current-tag) nil))
+ (should (equal (magit-get-next-tag) nil))
+ (magit-git "tag" "1")
+ (should (equal (magit-get-current-tag) "1"))
+ (should (equal (magit-get-next-tag) nil))
+ (magit-git "commit" "-m" "2" "--allow-empty")
+ (magit-git "tag" "2")
+ (should (equal (magit-get-current-tag) "2"))
+ (should (equal (magit-get-next-tag) nil))
+ (magit-git "commit" "-m" "3" "--allow-empty")
+ (should (equal (magit-get-current-tag) "2"))
+ (should (equal (magit-get-next-tag) nil))
+ (magit-git "commit" "-m" "4" "--allow-empty")
+ (magit-git "tag" "4")
+ (magit-git "reset" "HEAD~")
+ (should (equal (magit-get-current-tag) "2"))
+ (should (equal (magit-get-next-tag) "4"))))
+
+(ert-deftest magit-list-{|local-|remote-}branch-names ()
+ (magit-with-test-repository
+ (magit-git "commit" "-m" "init" "--allow-empty")
+ (magit-git "update-ref" "refs/remotes/foobar/master" "master")
+ (magit-git "update-ref" "refs/remotes/origin/master" "master")
+ (should (equal (magit-list-branch-names)
+ (list "master" "foobar/master" "origin/master")))
+ (should (equal (magit-list-local-branch-names)
+ (list "master")))
+ (should (equal (magit-list-remote-branch-names)
+ (list "foobar/master" "origin/master")))
+ (should (equal (magit-list-remote-branch-names "origin")
+ (list "origin/master")))
+ (should (equal (magit-list-remote-branch-names "origin" t)
+ (list "master")))))
+
+;;; Status
+
+(defun magit-test-get-section (type info)
+ (magit-status-internal default-directory)
+ (--first (equal (magit-section-value it) info)
+ (magit-section-children
+ (magit-get-section `((,type) (status))))))
+
+(ert-deftest magit-status:file-sections ()
+ (magit-with-test-repository
+ (cl-flet ((modify (file) (with-temp-file file
+ (insert (make-temp-name "content")))))
+ (modify "file")
+ (modify "file with space")
+ (modify "file with äöüéλ")
+ (should (magit-test-get-section 'untracked "file"))
+ (should (magit-test-get-section 'untracked "file with space"))
+ (should (magit-test-get-section 'untracked "file with äöüéλ"))
+ (magit-stage-modified t)
+ (should (magit-test-get-section 'staged "file"))
+ (should (magit-test-get-section 'staged "file with space"))
+ (should (magit-test-get-section 'staged "file with äöüéλ"))
+ (magit-git "add" ".")
+ (modify "file")
+ (modify "file with space")
+ (modify "file with äöüéλ")
+ (should (magit-test-get-section 'unstaged "file"))
+ (should (magit-test-get-section 'unstaged "file with space"))
+ (should (magit-test-get-section 'unstaged "file with äöüéλ")))))
+
+(ert-deftest magit-status:log-sections ()
+ (magit-with-test-repository
+ (magit-git "commit" "-m" "common" "--allow-empty")
+ (magit-git "commit" "-m" "unpulled" "--allow-empty")
+ (magit-git "remote" "add" "origin" "/origin")
+ (magit-git "update-ref" "refs/remotes/origin/master" "master")
+ (magit-git "branch" "--set-upstream-to=origin/master")
+ (magit-git "reset" "--hard" "HEAD~")
+ (magit-git "commit" "-m" "unpushed" "--allow-empty")
+ (should (magit-test-get-section 'unpulled
+ (magit-rev-parse "--short" "origin/master")))
+ (should (magit-test-get-section 'unpushed
+ (magit-rev-parse "--short" "master")))))
;;; magit-tests.el ends soon
-
-(defconst magit-tests-font-lock-keywords
- '(("(magit-tests--with-temp-\\(?:clone\\|dir\\|repo\\)\\_>" . 1)))
-
-(font-lock-add-keywords 'emacs-lisp-mode magit-tests-font-lock-keywords)
-
(provide 'magit-tests)
;; Local Variables:
;; indent-tabs-mode: nil