Skip to content

Commit

Permalink
bugfix #17
Browse files Browse the repository at this point in the history
Stall program when change read-only file line.
  • Loading branch information
mhayashi1120 committed Dec 9, 2013
1 parent 3963a80 commit 968288e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
34 changes: 27 additions & 7 deletions wgrep-test.el
Expand Up @@ -20,6 +20,12 @@
(let ((coding-system-for-write cs))
(write-region contents nil file)))

(defun wgrep-test--cleanup-file (file)
(when (file-exists-p file)
(delete-file file))
(when (file-exists-p (concat file "~"))
(delete-file (concat file "~"))))

(ert-deftest wgrep-normal ()
:tags '(wgrep)
(let (wgrep-auto-save-buffer)
Expand All @@ -46,7 +52,7 @@
(wgrep-save-all-buffers)
;; compare file contents is valid
(should (equal "FOO2\nBAZ\n" (wgrep-test--contents "test-data.txt")))
(delete-file "test-data.txt")))
(wgrep-test--cleanup-file "test-data.txt")))

(ert-deftest wgrep-normal-with-newline ()
:tags '(wgrep)
Expand All @@ -66,7 +72,7 @@
(wgrep-save-all-buffers)
;; compare file contents is valid
(should (equal "FOO\nBAZ\n" (wgrep-test--contents "test-data.txt")))
(delete-file "test-data.txt")))
(wgrep-test--cleanup-file "test-data.txt")))

(ert-deftest wgrep-bom-with-multibyte ()
:tags '(wgrep)
Expand All @@ -88,7 +94,7 @@
(wgrep-save-all-buffers)
;; compare file contents is valid
(should (equal "へのへのも\nへじ\n\n" (wgrep-test--contents "test-data.txt")))
(delete-file "test-data.txt")))
(wgrep-test--cleanup-file "test-data.txt")))

(ert-deftest wgrep-bom-with-unibyte ()
:tags '(wgrep)
Expand All @@ -106,7 +112,7 @@
(wgrep-save-all-buffers)
;; compare file contents is valid
(should (equal "ABCD\nb\n" (wgrep-test--contents "test-data.txt")))
(delete-file "test-data.txt")))
(wgrep-test--cleanup-file "test-data.txt")))

(ert-deftest wgrep-with-modify ()
:tags '(wgrep)
Expand Down Expand Up @@ -137,13 +143,27 @@
(wgrep-save-all-buffers)
;; compare file contents is valid
(should (equal "hoge\nfoo\nC\n" (wgrep-test--contents "test-data.txt")))
(delete-file "test-data.txt")))
(wgrep-test--cleanup-file "test-data.txt")))

(ert-deftest wgrep-with-readonly-file ()
:tags '(wgrep)
(let (wgrep-auto-save-buffer)
(wgrep-test--prepare "test-data.txt" "a\nb\nc\n")
;; make readonly
(set-file-modes "test-data.txt" ?\400)
(wgrep-test--grep "grep -nH -e 'a' test-data.txt")
(wgrep-change-to-wgrep-mode)
(goto-char (point-min))
(should (re-search-forward "test-data\\.txt:[0-9]+:.*\\(a\\)$" nil t))
(replace-match "A" nil nil nil 1)
;; only check with no error
(wgrep-finish-edit))
(wgrep-test--cleanup-file "test-data.txt"))

;; TODO
;; TODO (Not implemented testcase)
;; * wgrep-toggle-readonly-area
;; ** sort-lines
;; * wgrep-abort-changes
;; * wgrep-exit
;; * broken file contents (invalid coding system)
;; * new text contains newline
;; * wgrep-change-readonly-file
19 changes: 13 additions & 6 deletions wgrep.el
Expand Up @@ -4,7 +4,7 @@
;; Keywords: grep edit extensions
;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep.el
;; Emacs: GNU Emacs 22 or later
;; Version: 2.1.3
;; Version: 2.1.4

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -888,7 +888,12 @@ This change will be applied when \\[wgrep-finish-edit]."
(start (next-single-property-change
(point) 'wgrep-line-filename nil (line-end-position)))
(file (expand-file-name name default-directory))
(buffer (wgrep-get-file-buffer file))
(file-error nil)
(buffer (condition-case err
(wgrep-get-file-buffer file)
(wgrep-error
(setq file-error (cdr err))
nil)))
(old (overlay-get ov 'wgrep-old-text))
(new (overlay-get ov 'wgrep-edit-text))
result)
Expand All @@ -902,10 +907,12 @@ This change will be applied when \\[wgrep-finish-edit]."
;; create overlay to show result of committing
(setq result (wgrep-make-overlay start (overlay-end ov)))
(overlay-put result 'wgrep-result t))
(setq res
(cons
(list buffer linum old new result ov)
res))))))
(if file-error
(wgrep-put-reject-result result file-error)
(setq res
(cons
(list buffer linum old new result ov)
res)))))))
(nreverse res)))

(defun wgrep-compute-transaction ()
Expand Down

0 comments on commit 968288e

Please sign in to comment.