(search-forward "diff --git" nil 'move)
(move-beginning-of-line 1)
(setq file-diff (buffer-substring pos (point)))
- (if (and (setq file-beg (string-search
- file-diff vc-git-patch-string))
- ;; Check that file diff ends with an empty string
- ;; or the beginning of the next file diff.
- (string-match-p "\\`\\'\\|\\`diff --git"
- (substring
- vc-git-patch-string
- (+ file-beg (length file-diff)))))
- (setq vc-git-patch-string
- (string-replace file-diff "" vc-git-patch-string))
- (user-error "Index not empty"))
+ (cond ((and (setq file-beg (string-search
+ file-diff vc-git-patch-string))
+ ;; Check that file diff ends with an empty string
+ ;; or the beginning of the next file diff.
+ (string-match-p "\\`\\'\\|\\`diff --git"
+ (substring
+ vc-git-patch-string
+ (+ file-beg (length file-diff)))))
+ (setq vc-git-patch-string
+ (string-replace file-diff "" vc-git-patch-string)))
+ ((string-match (format "^%s" (regexp-quote file-header))
+ vc-git-patch-string)
+ (if (and file-name
+ (yes-or-no-p
+ (format "Unstage already-staged changes to %s?"
+ file-name)))
+ (vc-git-command nil 0 file-name "reset" "-q" "--")
+ (user-error "Index not empty")))
+ (t (push file-name to-stash)))
(setq pos (point))))))
(unless (string-empty-p vc-git-patch-string)
- (let ((patch-file (make-nearby-temp-file "git-patch")))
+ (let ((patch-file (make-nearby-temp-file "git-patch"))
+ ;; Temporarily countermand the let-binding at the
+ ;; beginning of this function.
+ (coding-system-for-write
+ (coding-system-change-eol-conversion
+ ;; On DOS/Windows, it is important for the patch file
+ ;; to have the Unix EOL format, because Git expects
+ ;; that, even on Windows.
+ (or pcsw vc-git-commits-coding-system) 'unix)))
(with-temp-file patch-file
(insert vc-git-patch-string))
(unwind-protect
"Test that `eshell-stringify' correctly stringifies complex objects."
(should (equal (eshell-stringify (list 'quote 'hello)) "'hello")))
+(ert-deftest esh-util-test/eshell-convert-to-number/integer ()
+ "Test that `eshell-convert-to-number' correctly converts integers."
+ (should (equal (eshell-convert-to-number "123") 123))
+ (should (equal (eshell-convert-to-number "-123") -123))
+ ;; These are technially integers, since Emacs Lisp requires at least
+ ;; one digit after the "." to be a float:
+ (should (equal (eshell-convert-to-number "123.") 123))
+ (should (equal (eshell-convert-to-number "-123.") -123)))
+
+(ert-deftest esh-util-test/eshell-convert-to-number/floating-point ()
+ "Test that `eshell-convert-to-number' correctly converts floats."
+ (should (equal (eshell-convert-to-number "1.23") 1.23))
+ (should (equal (eshell-convert-to-number "-1.23") -1.23))
+ (should (equal (eshell-convert-to-number ".1") 0.1))
+ (should (equal (eshell-convert-to-number "-.1") -0.1)))
+
+(ert-deftest esh-util-test/eshell-convert-to-number/floating-point-exponent ()
+ "Test that `eshell-convert-to-number' correctly converts exponent notation."
+ ;; Positive exponent:
+ (dolist (exp '("e2" "e+2" "E2" "E+2"))
+ (should (equal (eshell-convert-to-number (concat "123" exp)) 12300.0))
+ (should (equal (eshell-convert-to-number (concat "-123" exp)) -12300.0))
+ (should (equal (eshell-convert-to-number (concat "1.23" exp)) 123.0))
+ (should (equal (eshell-convert-to-number (concat "-1.23" exp)) -123.0))
+ (should (equal (eshell-convert-to-number (concat "1." exp)) 100.0))
+ (should (equal (eshell-convert-to-number (concat "-1." exp)) -100.0))
+ (should (equal (eshell-convert-to-number (concat ".1" exp)) 10.0))
+ (should (equal (eshell-convert-to-number (concat "-.1" exp)) -10.0)))
+ ;; Negative exponent:
+ (dolist (exp '("e-2" "E-2"))
+ (should (equal (eshell-convert-to-number (concat "123" exp)) 1.23))
+ (should (equal (eshell-convert-to-number (concat "-123" exp)) -1.23))
+ (should (equal (eshell-convert-to-number (concat "1.23" exp)) 0.0123))
+ (should (equal (eshell-convert-to-number (concat "-1.23" exp)) -0.0123))
+ (should (equal (eshell-convert-to-number (concat "1." exp)) 0.01))
+ (should (equal (eshell-convert-to-number (concat "-1." exp)) -0.01))
+ (should (equal (eshell-convert-to-number (concat ".1" exp)) 0.001))
+ (should (equal (eshell-convert-to-number (concat "-.1" exp)) -0.001))))
+
+(ert-deftest esh-util-test/eshell-convert-to-number/floating-point/infinite ()
+ "Test that `eshell-convert-to-number' correctly converts infinite floats."
+ (should (equal (eshell-convert-to-number "1.0e+INF") 1.0e+INF))
+ (should (equal (eshell-convert-to-number "2.e+INF") 1.0e+INF))
+ (should (equal (eshell-convert-to-number "-1.0e+INF") -1.0e+INF))
+ (should (equal (eshell-convert-to-number "-2.e+INF") -1.0e+INF)))
+
+(ert-deftest esh-util-test/eshell-convert-to-number/floating-point/nan ()
+ "Test that `eshell-convert-to-number' correctly converts NaNs."
+ (should (equal (eshell-convert-to-number "1.0e+NaN") 1.0e+NaN))
+ (should (equal (eshell-convert-to-number "2.e+NaN") 2.0e+NaN))
+ (should (equal (eshell-convert-to-number "-1.0e+NaN") -1.0e+NaN))
+ (should (equal (eshell-convert-to-number "-2.e+NaN") -2.0e+NaN)))
+
+(ert-deftest esh-util-test/eshell-convert-to-number/non-numeric ()
+ "Test that `eshell-convert-to-number' does nothing to non-numeric values."
+ (should (equal (eshell-convert-to-number "foo") "foo"))
+ (should (equal (eshell-convert-to-number "") ""))
+ (should (equal (eshell-convert-to-number "123foo") "123foo")))
+
+(ert-deftest esh-util-test/eshell-convert-to-number/no-convert ()
+ "Test that `eshell-convert-to-number' does nothing when disabled."
+ (let ((eshell-convert-numeric-arguments nil))
+ (should (equal (eshell-convert-to-number "123") "123"))
+ (should (equal (eshell-convert-to-number "1.23") "1.23"))))
+
+(ert-deftest esh-util-test/eshell-printable-size ()
+ (should (equal (eshell-printable-size (expt 2 16)) "65536"))
+ (should (equal (eshell-printable-size (expt 2 32)) "4294967296")))
+
+(ert-deftest esh-util-test/eshell-printable-size/zero ()
+ (should (equal (eshell-printable-size 0 1000 nil t) "0")))
+
+(ert-deftest esh-util-test/eshell-printable-size/terabyte ()
+ (should (equal (eshell-printable-size (1- (expt 2 40)) 1024 nil t) "1024G"))
+ (should (equal (eshell-printable-size (expt 2 40) 1024 nil t) "1T"))
+ (should (equal (eshell-printable-size (1- (expt 10 12)) 1000 nil t) "1000G"))
+ (should (equal (eshell-printable-size (expt 10 12) 1000 nil t) "1T")))
+
+(ert-deftest esh-util-test/eshell-printable-size/use-colors ()
+ (should (equal-including-properties
+ (eshell-printable-size (1- (expt 2 20)) 1024 nil t)
+ "1024k"))
+ (should (equal-including-properties
+ (eshell-printable-size (1- (expt 2 30)) 1024 nil t)
+ (propertize "1024M" 'face 'bold)))
+ (should (equal-including-properties
+ (eshell-printable-size (1- (expt 2 40)) 1024 nil t)
+ (propertize "1024G" 'face 'bold-italic))))
+
+(ert-deftest esh-util-test/eshell-printable-size/block-size ()
+ (should (equal (eshell-printable-size (1- (expt 2 20)) nil 4096) "256"))
+ (should (equal (eshell-printable-size (1- (expt 2 30)) nil 4096) "262144")))
+
+(ert-deftest esh-util-test/eshell-printable-size/human-readable-arg ()
+ (should-error (eshell-printable-size 0 999 nil t)))
+
+ (ert-deftest esh-util-test/path/get ()
+ "Test that getting the Eshell path returns the expected results."
+ (let ((expected-path (butlast (exec-path))))
+ (should (equal (eshell-get-path)
+ (if (eshell-under-windows-p)
+ (cons "." expected-path)
+ expected-path)))
+ (should (equal (eshell-get-path 'literal)
+ expected-path))))
+
+ (ert-deftest esh-util-test/path/get-remote ()
+ "Test that getting the remote Eshell path returns the expected results."
+ (let* ((default-directory ert-remote-temporary-file-directory)
+ (expected-path (butlast (exec-path))))
+ ;; Make sure we don't have a doubled directory separator.
+ (should (seq-every-p (lambda (i) (not (string-match-p "//" i)))
+ (eshell-get-path)))
+ (should (equal (eshell-get-path)
+ (mapcar (lambda (i)
+ (concat (file-remote-p default-directory) i))
+ expected-path)))
+ (should (equal (eshell-get-path 'literal)
+ expected-path))))
+
;;; esh-util-tests.el ends here