Make use of the ‘:var’ and ‘before|after-each’ forms of buttercup

This removes a bit of boiler-plate code from the tests, hopefully making them
easier to read and write.
This commit is contained in:
Tom Willemse 2020-08-18 23:54:59 -07:00
parent e2c939cb8e
commit d4fd943206

View file

@ -27,146 +27,120 @@
(require 'buttercup) (require 'buttercup)
(require 'git-auto-commit-mode) (require 'git-auto-commit-mode)
(defun git-auto-commit-tests-setup-git () (describe "In a git repository"
(shell-command "git init") :var (temp-dir current-directory)
(shell-command "git config user.email user@example.com")
(shell-command "git config user.name \"User Example\""))
(describe "New files" (before-each
(describe "When gac-automatically-add-new-files is t" (setq temp-dir (make-temp-file "gac-" t)
(it "Should be added to git" current-directory default-directory
(let* ((gac-automatically-add-new-files-p t) default-directory temp-dir)
(temp-dir (make-temp-file "gac-" t)) (shell-command "git init")
(temp-file (expand-file-name "test" temp-dir)) (shell-command "git config user.email user@example.com")
(default-directory temp-dir)) (shell-command "git config user.name \"User Example\""))
(unwind-protect
(progn
(git-auto-commit-tests-setup-git)
(let ((buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer)))
(expect (shell-command-to-string "git log --format=format:%s")
:to-match (rx string-start "test" string-end)))
(delete-directory temp-dir t)))))
(describe "When gac-automatically-add-new-files is nil" (after-each
(it "Shouldnt be added to git" (delete-directory temp-dir t)
(let* ((gac-automatically-add-new-files-p nil) (setq temp-dir nil
(temp-dir (make-temp-file "gac-" t)) default-directory current-directory
(temp-file (expand-file-name "test" temp-dir)) current-directory nil))
(default-directory temp-dir))
(unwind-protect
(progn
(git-auto-commit-tests-setup-git)
(let ((buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer)))
(expect (shell-command-to-string "git log --format=format:%s")
:not :to-match (rx string-start "test" string-end)))
(delete-directory temp-dir t))))))
(describe "Getting a relative path" (describe "New files"
(it "should return the file name for files in the project root" (describe "When gac-automatically-add-new-files is t"
(let* ((temp-dir (make-temp-file "gac-" t)) (it "Should be added to git"
(temp-file (expand-file-name "test" temp-dir)) (let* ((gac-automatically-add-new-files-p t)
(default-directory temp-dir)) (temp-file (expand-file-name "test" temp-dir))
(unwind-protect (buffer (find-file-noselect temp-file)))
(progn (with-current-buffer buffer
(git-auto-commit-tests-setup-git) (git-auto-commit-mode)
(let ((buffer (find-file-noselect temp-file))) (insert "test")
(with-current-buffer buffer (save-buffer))
(git-auto-commit-mode)
(insert "test")
(save-buffer)))
(expect (gac-relative-file-name temp-file)
:to-equal "test"))
(delete-directory temp-dir t))))
(it "should return the file name and directory name for nested files" (expect (shell-command-to-string "git log --format=format:%s")
(let* ((temp-dir (make-temp-file "gac-" t)) :to-match (rx string-start "test" string-end)))))
(temp-file (expand-file-name "test/test.txt" temp-dir))
(default-directory temp-dir)) (describe "When gac-automatically-add-new-files is nil"
(it "Shouldnt be added to git"
(let* ((gac-automatically-add-new-files-p nil)
(temp-file (expand-file-name "test" temp-dir))
(buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer))
(expect (shell-command-to-string "git log --format=format:%s")
:not :to-match (rx string-start "test" string-end))))))
(describe "Getting a relative path"
(it "should return the file name for files in the project root"
(let* ((temp-file (expand-file-name "test" temp-dir))
(buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer))
(expect (gac-relative-file-name temp-file)
:to-equal "test")))
(it "should return the file name and directory name for nested files"
(mkdir (expand-file-name "test" temp-dir)) (mkdir (expand-file-name "test" temp-dir))
(unwind-protect
(progn
(git-auto-commit-tests-setup-git)
(let ((buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer)))
(expect (gac-relative-file-name temp-file)
:to-equal "test/test.txt"))
(delete-directory temp-dir t))))
(it "should handle [] in the filename" (let* ((temp-file (expand-file-name "test/test.txt" temp-dir))
(let* ((temp-dir (make-temp-file "gac-" t)) (buffer (find-file-noselect temp-file)))
(temp-file (expand-file-name "[test]-test.txt" temp-dir)) (with-current-buffer buffer
(default-directory temp-dir)) (git-auto-commit-mode)
(unwind-protect (insert "test")
(progn (save-buffer))
(git-auto-commit-tests-setup-git)
(let ((buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer)))
(expect (gac-relative-file-name temp-file)
:to-equal "[test]-test.txt"))
(delete-directory temp-dir t))))
(it "should handle [] in the directory name" (expect (gac-relative-file-name temp-file)
(let* ((temp-dir (make-temp-file "gac-" t)) :to-equal "test/test.txt")))
(temp-file (expand-file-name "[test]-test/testing.txt" temp-dir))
(default-directory temp-dir)) (it "should handle [] in the filename"
(let* ((temp-file (expand-file-name "[test]-test.txt" temp-dir))
(buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer))
(expect (gac-relative-file-name temp-file)
:to-equal "[test]-test.txt")))
(it "should handle [] in the directory name"
(mkdir (expand-file-name "[test]-test" temp-dir)) (mkdir (expand-file-name "[test]-test" temp-dir))
(unwind-protect
(progn
(git-auto-commit-tests-setup-git)
(let ((buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer)))
(expect (gac-relative-file-name temp-file)
:to-equal "[test]-test/testing.txt"))
(delete-directory temp-dir t))))
(it "should handle [ in the directory name" (let* ((temp-file (expand-file-name "[test]-test/testing.txt" temp-dir))
(let* ((temp-dir (make-temp-file "gac-[test-" t)) (buffer (find-file-noselect temp-file)))
(temp-file (expand-file-name "testing.txt" temp-dir)) (with-current-buffer buffer
(default-directory temp-dir)) (git-auto-commit-mode)
(unwind-protect (insert "test")
(progn (save-buffer))
(git-auto-commit-tests-setup-git)
(let ((buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer)))
(expect (gac-relative-file-name temp-file)
:to-equal "testing.txt"))
(delete-directory temp-dir t))))
(it "should handle [ in the file name" (expect (gac-relative-file-name temp-file)
(let* ((temp-dir (make-temp-file "gac-" t)) :to-equal "[test]-test/testing.txt")))
(temp-file (expand-file-name "[test-test.txt" temp-dir))
(default-directory temp-dir)) (it "should handle [ in the directory name"
(unwind-protect (let* ((temp-file (expand-file-name "testing.txt" temp-dir))
(progn (buffer (find-file-noselect temp-file)))
(git-auto-commit-tests-setup-git) (with-current-buffer buffer
(let ((buffer (find-file-noselect temp-file))) (git-auto-commit-mode)
(with-current-buffer buffer (insert "test")
(git-auto-commit-mode) (save-buffer))
(insert "test")
(save-buffer))) (expect (gac-relative-file-name temp-file)
(expect (gac-relative-file-name temp-file) :to-equal "testing.txt")))
:to-equal "[test-test.txt"))
(delete-directory temp-dir t))))) (it "should handle [ in the file name"
(let* ((temp-file (expand-file-name "[test-test.txt" temp-dir))
(buffer (find-file-noselect temp-file)))
(with-current-buffer buffer
(git-auto-commit-mode)
(insert "test")
(save-buffer))
(expect (gac-relative-file-name temp-file)
:to-equal "[test-test.txt")))))
(provide 'git-auto-commit-mode-tests) (provide 'git-auto-commit-mode-tests)
;;; git-auto-commit-mode-tests.el ends here ;;; git-auto-commit-mode-tests.el ends here