Permalink
Browse files

io/copy from the pushbackreader; added integration test of `reconstru…

…ct-in-place`
  • Loading branch information...
1 parent bedb943 commit 92892f6eba2ff801e5a72ac6d98c42ddc754545f Alex Baranosky committed Dec 26, 2012
Showing with 39 additions and 24 deletions.
  1. +5 −0 resources/reconstructed_namespace.clj
  2. +7 −0 resources/test_namespace.clj
  3. +15 −21 src/slam/hound.clj
  4. +12 −3 test/slam/hound_test.clj
@@ -0,0 +1,5 @@
+(ns foo.bar
+ (:require [clojure.string :as str]))
+
+(defn replace-commas [replacement]
+ (str/replace "asdf" #"," replacement))
@@ -0,0 +1,7 @@
+(ns foo.bar
+ (:require [clojure.string :as str]
+ [clojure.java.io :as io]
+ [clojure.set :as set]))
+
+(defn replace-commas [replacement]
+ (str/replace "asdf" #"," replacement))
View
@@ -3,7 +3,7 @@
[slam.hound.asplode :refer [asplode]]
[slam.hound.regrow :refer [regrow]]
[slam.hound.stitch :refer [stitch-up]])
- (:import (java.io File FileReader PushbackReader))))
+ (:import (java.io File FileReader PushbackReader)))
(defn reconstruct [filename]
@@ -18,30 +18,24 @@
(cons (.getMessage e)
(map #(str % "\n") (.getStackTrace e))))
-(defn- non-whitespace-char? [ch]
- (re-matches #"\S" (str ch)))
-
-(defn- body-from-file [file-name old-ns-form]
- (let [file-contents (slurp file-name)
- num-non-white-chars-in-old-ns-form (count (filter non-whitespace-char? (str old-ns-form)))
- non-white-so-far (atom 0)]
- (apply str (drop-while (fn [ch]
- (when (non-whitespace-char? ch)
- (swap! non-white-so-far inc))
- (< @non-white-so-far num-non-white-chars-in-old-ns-form))
- file-contents))))
-
-(defn- swap-in-reconstructed-ns-form [file]
- (let [new-ns (.trim (reconstruct file))
- old-ns-form (read (PushbackReader. (FileReader. file)))
- body (body-from-file file old-ns-form)]
- (spit file (str new-ns body))))
+(defn- swap-in-reconstructed-ns-form [filename]
+ (let [new-ns (.trim (reconstruct filename))
+ rdr (PushbackReader. (FileReader. filename))]
+ ;; scan past the namespace form
+ (read rdr)
+ ;; copy in the reconstructed ns form
+ (io/copy new-ns (File. filename))
+ ;; append the body
+ (with-open [writer (io/writer filename :append true)]
+ (io/copy rdr writer))))
(defn reconstruct-in-place
"Takes a file or directory and rewrites the files
with reconstructed ns forms."
[file-or-dir]
- (doseq [^File f (file-seq (File. file-or-dir))
+ (doseq [^File f (file-seq (if (string? file-or-dir)
+ (File. file-or-dir)
+ file-or-dir))
:let [^String filename (.getName f)
^String file-path (.getAbsolutePath f)]
:when (and (.endsWith filename ".clj")
@@ -54,4 +48,4 @@
"\nException: " (stacktrace-to-str ex)))))))
(defn -main [file-or-dir]
- (reconstruct-in-place file-or-dir))
+ (reconstruct-in-place file-or-dir))
View
@@ -1,7 +1,8 @@
(ns slam.hound-test
- (:require [clojure.test :refer [deftest is testing]]
- [slam.hound :refer [reconstruct]])
- (:import java.io.StringReader))
+ (:require [clojure.java.io :as io]
+ [clojure.test :refer [deftest is testing]]
+ [slam.hound :refer [reconstruct reconstruct-in-place]])
+ (:import (java.io File StringReader)))
(def basic-ns (str '(ns slamhound.sample
@@ -77,3 +78,11 @@
'(defn do-it! []
(join "," ["a" "b" "c"])))))))))
+(deftest ^:integration test-reconstruct-in-place
+ (let [tmp (doto (File/createTempFile "test_namespace_copy" ".clj")
+ .deleteOnExit)]
+ (io/copy (io/reader (io/resource "test_namespace.clj")) tmp)
+ (reconstruct-in-place tmp)
+
+ (is (= (slurp (io/resource "reconstructed_namespace.clj"))
+ (slurp tmp)))))

0 comments on commit 92892f6

Please sign in to comment.