Skip to content

Instantly share code, notes, and snippets.

@snowman
Forked from twlz0ne/test-magit-stage-minimal.el
Created January 21, 2022 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snowman/3ee311e1494b57f7f043a1be4cd20dc0 to your computer and use it in GitHub Desktop.
Save snowman/3ee311e1494b57f7f043a1be4cd20dc0 to your computer and use it in GitHub Desktop.
Minimal configuration for testing magit stage #Emacs
;;; Usage: /path/to/emacs -nw -Q -l /path/to/test-magit-stage-minimal.el --eval '(load-magit "/path/to/magit/lisp")'
;;; Created: 2020-03-04 03.05.24
;;; Updated: 2020-03-04 21.17.19
;;; Version: 2
(toggle-debug-on-error)
;; ------------------------------------------------------------------
;; elpa
(setq user-emacs-directory (format "~/.emacs.d/%s/%s/" (file-name-base load-file-name) emacs-version))
(setq package-user-dir (concat user-emacs-directory "elpa/"))
(unless (load "~/.emacs.d/elpa.el" t)
(setq package-archives
'(("gnu" . "https://elpa.gnu.org/packages/")
("melpa" . "https://melpa.org/packages/"))))
(package-initialize)
(defun require-packages (&rest packages)
(dolist (pkg packages)
(unless (package-installed-p pkg)
(package-refresh-contents)
(package-install pkg))
(require pkg)))
;; ------------------------------------------------------------------
;; code
(require-packages
'async
'dash
'git-commit
'transient
'with-editor)
(define-advice magit-refresh-buffer (:after () apply-ansi-color)
"Apply ANSI color to current buffer."
(let ((inhibit-read-only t))
(ansi-color-apply-on-region (point-min) (point-max))))
(defun load-magit (magit-lisp-dir)
(add-to-list 'load-path (expand-file-name magit-lisp-dir))
(setq magit-git-debug t)
(require 'magit)
(let ((default-directory magit-lisp-dir))
;; 1 Prepare ----------------------------------------------------
;; 1.1 Reset changes
(shell-command "git restore --source=HEAD --staged --worktree magit.el")
;; 1.2 Change file
(shell-command
(mapconcat 'identity
(list
(format "echo %S >> magit.el" ";; Change 1")
(format "echo %S >> magit.el" ";; Change 2")
(format "echo %S >> magit.el" ";; Change 3"))
" && "))
;; 2 Start tests ------------------------------------------------
(kill-buffer "*Messages*")
(magit-version t)
(princ "\n")
(find-file (expand-file-name "magit.el" magit-lisp-dir))
(magit-status)
;; 2.1 Assert sections
(let ((results '((t . "found") (nil . "not found"))))
(mapc
(pcase-lambda (`(,section ,expected))
(goto-char (point-min))
(message "==> [Section] %-20s actual: %-10s, expected: %-10s"
(format "%s" section)
(assoc-default (re-search-forward (concat "^" section) nil t) results)
(assoc-default expected results)))
'(;; section expected
;; ---------------- -----------
("Head:" t)
("Merge:" t)
("Tag:" t)
("Unstaged changes" t)
("Recent commits" t))))
;; 2.2 Stage a region
(goto-char (point-min))
(when (re-search-forward "^\\+;; Change 1$")
(set-mark (match-beginning 0))
(forward-line)
(message "==> [Stage region] %s"
(condition-case err
(progn (magit-stage) "success")
(user-error
(cadr err)))))
;; 2.3 Stage a hunk
(goto-char (point-min))
(when (re-search-forward "^@@" nil t)
(deactivate-mark)
(message "==> [Stage hunk] %s"
(condition-case err
(progn (magit-stage) "success")
(user-error
(cadr err)))))
(view-echo-area-messages)))
;;; test-magit-stage-minimal.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment