[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master fd0e5ca 06/14: with-relative-file is now idempotent and te
From: |
Rocky Bernstein |
Subject: |
[elpa] master fd0e5ca 06/14: with-relative-file is now idempotent and tests! |
Date: |
Wed, 25 Feb 2015 01:24:38 +0000 |
branch: master
commit fd0e5ca104f9d861e3cfb12ba52654d8b9d723e3
Author: Phillip Lord <address@hidden>
Commit: Phillip Lord <address@hidden>
with-relative-file is now idempotent and tests!
The first version of with-relative-file left a new buffer around, unless
the file was already open, in which case the existing buffer would be
changed. It was all very stateful and this was not very fixable. This
version uses with-temp-buffer instead, so leaves Emacs as it found it.
This does remove the ability to save to the file from within this
buffer.
---
README.md | 25 ++++++++++++++++++++++++-
load-relative.el | 8 +++++---
test/simple.txt | 1 +
test/test-file.el | 36 ++++++++++++++++++++++++++++++++++++
4 files changed, 66 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index d1d06a9..563db03 100644
--- a/README.md
+++ b/README.md
@@ -78,7 +78,7 @@ Use *require-relative-list* when you have a list of files you
want to
## provide-me
-Finally, macro *provide-me* saves you the trouble of adding a symbol
+The macro *provide-me* saves you the trouble of adding a symbol
after *provide*, by using the file basename (without directory or file
extension) as the name of the thing you want to provide. Using this
forces the *provide* names to be the same as the filename, but I
@@ -111,3 +111,26 @@ this is the same as writing:
```lisp
(provide 'bar-foo)
```
+
+
+## find-file-noselect-relative
+
+The function *find-file-noselect-relative* provides a way of accessing
+resources which are located relative to the currently running Emacs lisp file.
+This is probably most useful when running Emacs as a scripting engine for
+batch processing or with tests cases.
+
+```lisp
+ (find-file-noselect-relative "README.md")
+```
+
+## with-relative-file
+
+The macro *with-relative-file* runs in a buffer with the contents of the given
+relative file.
+
+```lisp
+ (with-relative-file "README.md"
+ (buffer-substring))
+```
+
diff --git a/load-relative.el b/load-relative.el
index 6fac96f..e823ae7 100644
--- a/load-relative.el
+++ b/load-relative.el
@@ -196,11 +196,13 @@ the various files."
;;;###autoload
(defmacro with-relative-file (file &rest body)
- "Read relative FILE into a temporary buffer and evaluated BODY
+ "Read the relative FILE into a temporary buffer and evaluate BODY
in this buffer."
(declare (indent 1) (debug t))
- `(with-current-buffer
- (find-file-noselect-relative ,file)
+ `(with-temp-buffer
+ (insert-file-contents
+ (relative-expand-file-name
+ ,file))
,@body))
;;;###autoload
diff --git a/test/simple.txt b/test/simple.txt
new file mode 100644
index 0000000..ab23474
--- /dev/null
+++ b/test/simple.txt
@@ -0,0 +1 @@
+simple
diff --git a/test/test-file.el b/test/test-file.el
new file mode 100644
index 0000000..2924503
--- /dev/null
+++ b/test/test-file.el
@@ -0,0 +1,36 @@
+(require 'load-relative)
+(require 'ert)
+
+
+(ert-deftest test-name ()
+ (should
+ ;; not sure how I can test the full path here because, well, I need to
+ ;; resolve a relative path to do so...
+ (equal
+ "simple.txt"
+ (let ((bf
+ (find-file-noselect-relative "simple.txt")))
+ (kill-buffer bf)
+ (file-name-nondirectory
+ (buffer-file-name bf))))))
+
+(ert-deftest test-contents ()
+ (should
+ (equal
+ "simple\n"
+ (let* ((bf
+ (find-file-noselect-relative "simple.txt"))
+ (ct
+ (with-current-buffer
+ bf
+ (buffer-string))))
+ (kill-buffer bf)
+ ct))))
+
+(ert-deftest test-contents-with-relative-file ()
+ (should
+ (equal
+ "simple\n"
+ (with-relative-file
+ "simple.txt"
+ (buffer-string)))))
- [elpa] master updated (286ef6a -> a0983c1), Rocky Bernstein, 2015/02/24
- [elpa] master 3fa56d7 01/14: Forgot to add email, Rocky Bernstein, 2015/02/24
- [elpa] master 84151d4 02/14: Merge commit '3fa56d7925d5b24c5cd11c61bd6b3235e753a055', Rocky Bernstein, 2015/02/24
- [elpa] master 267e9c3 04/14: New functions for opening files., Rocky Bernstein, 2015/02/24
- [elpa] master fd0e5ca 06/14: with-relative-file is now idempotent and tests!,
Rocky Bernstein <=
- [elpa] master c72495c 08/14: Change require to path relative load., Rocky Bernstein, 2015/02/24
- [elpa] master 44980ef 07/14: Merge pull request #4 from phillord/feature/find-file-relative, Rocky Bernstein, 2015/02/24
- [elpa] master 933969e 05/14: Merge pull request #3 from phillord/feature/find-file-relative, Rocky Bernstein, 2015/02/24
- [elpa] master c0eb1f2 09/14: Commentary updates for new functions., Rocky Bernstein, 2015/02/24
- [elpa] master 8bacd50 03/14: Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs/elpa, Rocky Bernstein, 2015/02/24
- [elpa] master 4e7b211 11/14: Thank you, Phil Lord!, Rocky Bernstein, 2015/02/24
- [elpa] master 61be439 10/14: Merge pull request #5 from phillord/feature/find-file-relative, Rocky Bernstein, 2015/02/24
- [elpa] master a0983c1 14/14: Merge commit '0e1d1440e4819d76cc68b213431722884af66e89', Rocky Bernstein, 2015/02/24
- [elpa] master 0e1d144 12/14: Bump version number, Rocky Bernstein, 2015/02/24
- [elpa] master 1e9df93 13/14: Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs/elpa, Rocky Bernstein, 2015/02/24