Skip to content

Instantly share code, notes, and snippets.

@twlz0ne
Last active January 21, 2022 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twlz0ne/1e19f295c0a29caefef77ee38accbe4b to your computer and use it in GitHub Desktop.
Save twlz0ne/1e19f295c0a29caefef77ee38accbe4b 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
@twlz0ne
Copy link
Author

twlz0ne commented Mar 4, 2020

Steps:

$ git clone https://github.com/magit/magit ~/repos/magit
$ emacs -nw -Q -l test-magti-state-minimal.el --eval '(load-magit "~/repos/magit/lisp")'

test-magit-stage-minimal_v2

  • Macos 10.12.6
  • Emacs 26.3 / 28.0.50 (2020-01-29)
  • Magit c8cd22e2 (2020-02-28)
  • async-20200113.1745
  • dash-20200119.2310
  • git-commit-20200207.1819
  • transient-20200226.1612
  • with-editor-20200217.1015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment