emacs-elpa-diffs
[Top][All Lists]
Advanced

[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)))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]