|
1 | 1 | (ns refactor-nrepl.rename-file-or-dir-test
|
2 | 2 | (:require
|
| 3 | + [clojure.data :as data] |
| 4 | + [clojure.java.io :as io] |
3 | 5 | [clojure.string :as string]
|
4 | 6 | [clojure.test :refer [deftest is testing]]
|
5 | 7 | [refactor-nrepl.core :refer [get-ns-component ns-form-from-string]]
|
|
27 | 29 | refactor-nrepl.rename-file-or-dir/update-ns! (fn [_path _old-ns])
|
28 | 30 | refactor-nrepl.rename-file-or-dir/update-dependents! (fn [_dependents])
|
29 | 31 | refactor-nrepl.rename-file-or-dir/file-or-symlink-exists? (constantly true)]
|
30 |
| - (let [res (sut/rename-file-or-dir from-file-path to-file-path ignore-errors?)] |
31 |
| - (is (or (list? res) |
32 |
| - (instance? clojure.lang.Cons res)) |
33 |
| - (pr-str [pr-str res, (class res)])) |
34 |
| - (is (= 4 (count res)) |
35 |
| - (pr-str res)))));; currently not tracking :require-macros!! |
| 32 | + (let [files->absolute-path (fn [files] |
| 33 | + (->> files |
| 34 | + (map #(-> % io/file .getAbsolutePath)) |
| 35 | + (into #{}))) |
| 36 | + clj ["testproject/src/com/move/dependent_ns1.clj" |
| 37 | + "testproject/src/com/move/subdir/dependent_ns_3.clj" |
| 38 | + "testproject/src/com/move/dependent_ns2.clj"] |
| 39 | + cljs ["testproject/src/com/move/dependent_ns1_cljs.cljs" |
| 40 | + "testproject/src/com/move/subdir/dependent_ns_3_cljs.cljs"] |
| 41 | + common-referencing-files (files->absolute-path (into clj cljs)) |
| 42 | + files-referencing-old-ns (conj common-referencing-files from-file-path) |
| 43 | + res (sut/rename-file-or-dir from-file-path to-file-path ignore-errors?) |
| 44 | + [old-file-name new-file-name files-in-both] |
| 45 | + (data/diff files-referencing-old-ns (files->absolute-path res))] |
| 46 | + (is (= old-file-name #{from-file-path}) "That the old filename is not present") |
| 47 | + (is (= new-file-name #{to-file-path}) "That the new filename is present") |
| 48 | + (is (= files-in-both common-referencing-files) |
| 49 | + "That the files referencing the old & the new namespace are the same")))) |
36 | 50 |
|
37 | 51 | (deftest replaces-ns-references-in-dependents
|
38 | 52 | (let [dependents (atom [])]
|
|
133 | 147 | (is (not (.contains content old-package-prefix-dir)))))))
|
134 | 148 |
|
135 | 149 | (deftest calls-rename-file!-on-the-right-files-when-moving-dirs
|
136 |
| - (let [files (atom [])] |
| 150 | + (let [files (atom []) |
| 151 | + original-files ["testproject/src/com/move/dependent_ns1.clj" |
| 152 | + "testproject/src/com/move/dependent_ns1_cljs.cljs" |
| 153 | + "testproject/src/com/move/dependent_ns2.clj" |
| 154 | + "testproject/src/com/move/dependent_ns2_cljs.cljs" |
| 155 | + "testproject/src/com/move/non_clj_file" |
| 156 | + "testproject/src/com/move/ns_to_be_moved.clj" |
| 157 | + "testproject/src/com/move/ns_to_be_moved_cljs.cljs" |
| 158 | + "testproject/src/com/move/subdir/dependent_ns_3.clj" |
| 159 | + "testproject/src/com/move/subdir/dependent_ns_3_cljs.cljs"]] |
137 | 160 | (with-redefs [refactor-nrepl.rename-file-or-dir/rename-file!
|
138 | 161 | (fn [old new]
|
139 | 162 | (swap! files conj [old new]))
|
140 | 163 | refactor-nrepl.rename-file-or-dir/update-ns! (fn [_path _old-ns])
|
141 | 164 | refactor-nrepl.rename-file-or-dir/update-dependents! (fn [_deps])]
|
142 | 165 | (sut/rename-file-or-dir from-dir-path to-dir-path ignore-errors?)
|
143 |
| - (is (= (count @files) 9)) |
| 166 | + (is (= (count @files) (count original-files))) |
144 | 167 | (doseq [[^String old ^String new] @files]
|
145 | 168 | (is (.contains old "/move/"))
|
146 | 169 | (is (.contains new "/moved/"))))))
|
|
166 | 189 | refactor-nrepl.rename-file-or-dir/update-ns! (fn [_path _old-ns])
|
167 | 190 | refactor-nrepl.rename-file-or-dir/update-dependents! (fn [_dependents])
|
168 | 191 | refactor-nrepl.rename-file-or-dir/file-or-symlink-exists? (constantly true)]
|
169 |
| - (let [res (sut/rename-file-or-dir from-file-path-cljs to-file-path-cljs ignore-errors?)] |
170 |
| - (is (or (list? res) (instance? clojure.lang.Cons res))) |
171 |
| - (is (= 4 (count res)))))) |
| 192 | + (let [[_ _ file-with-string-requires :as files] |
| 193 | + (->> [to-file-path-cljs |
| 194 | + "testproject/src/com/move/dependent_ns1_cljs.cljs" |
| 195 | + "testproject/src/com/move/dependent_ns2_cljs.cljs" |
| 196 | + "testproject/src/com/move/subdir/dependent_ns_3_cljs.cljs"] |
| 197 | + (mapv (fn [^String s] |
| 198 | + (-> s File. .getAbsolutePath)))) |
| 199 | + res (sut/rename-file-or-dir from-file-path-cljs to-file-path-cljs ignore-errors?)] |
| 200 | + (is (seq? res)) |
| 201 | + (is (= (count files) (count res))) |
| 202 | + (assert (-> file-with-string-requires slurp read-string last last (= '["string-require-some-javascript-library" |
| 203 | + :as |
| 204 | + foo]))) |
| 205 | + (testing "a .cljs file with string requires in it was not excluded from the rename, |
| 206 | +and the string requires remain there as-is" |
| 207 | + (is (some #{file-with-string-requires} res)))))) |
172 | 208 |
|
173 | 209 | (deftest replaces-ns-references-in-dependent-for-cljs
|
174 | 210 | (let [dependents (atom [])]
|
|
179 | 215 | (doseq [[_f content] deps]
|
180 | 216 | (swap! dependents conj content)))]
|
181 | 217 | (sut/rename-file-or-dir from-file-path-cljs to-file-path-cljs ignore-errors?)
|
| 218 | + (assert (seq @dependents)) |
182 | 219 | (doseq [content @dependents
|
183 | 220 | :let [ns-form (ns-form-from-string content)
|
184 | 221 | require-form (get-ns-component ns-form :require)
|
185 | 222 | libspec (second require-form)
|
186 |
| - required-ns (if (sequential? libspec) (first libspec) libspec)]] |
187 |
| - (is (= 'com.move.moved-ns-cljs required-ns)))))) |
| 223 | + required-ns (if (sequential? libspec) |
| 224 | + (first libspec) |
| 225 | + libspec)]] |
| 226 | + (if (string? required-ns) |
| 227 | + (testing "a .cljs file with string requires in it was not excluded from the rename, |
| 228 | +and the string requires remain there as-is" |
| 229 | + (is (= "string-require-some-javascript-library" required-ns))) |
| 230 | + (is (= 'com.move.moved-ns-cljs required-ns))))))) |
188 | 231 |
|
189 | 232 | (deftest replaces-fully-qualified-vars-in-dependents-for-cljs
|
190 | 233 | (let [dependents (atom [])]
|
|
216 | 259 | (let [dependents (atom [])]
|
217 | 260 | (with-redefs [refactor-nrepl.rename-file-or-dir/rename-file! (fn [_old _new])
|
218 | 261 | refactor-nrepl.rename-file-or-dir/update-ns! (fn [_path _old-ns])
|
219 |
| - |
220 | 262 | refactor-nrepl.rename-file-or-dir/update-dependents!
|
221 | 263 | (fn [deps]
|
222 | 264 | (doseq [[^String path content] deps]
|
223 | 265 | (when (.endsWith path ".cljs")
|
224 | 266 | (swap! dependents conj content))))]
|
225 | 267 | (sut/rename-file-or-dir from-dir-path to-dir-path ignore-errors?)
|
| 268 | + (assert (seq @dependents)) |
226 | 269 | (doseq [content @dependents
|
227 | 270 | :let [ns-form (ns-form-from-string content)
|
228 | 271 | require-form (get-ns-component ns-form :require)
|
|
232 | 275 | required-macro-ns (-> require-macros-form second first)]]
|
233 | 276 | ;; This is a little gross, but the changes are done in two
|
234 | 277 | ;; passes, so each file has one of them.
|
235 |
| - (is (or (= 'com.moved.ns-to-be-moved-cljs required-ns) |
| 278 | + (is (or (= required-ns 'com.moved.ns-to-be-moved-cljs) |
| 279 | + (= required-ns "string-require-some-javascript-library") |
236 | 280 | (when required-macro-ns
|
237 |
| - (= 'com.moved.ns-to-be-moved required-macro-ns)))))))) |
| 281 | + (= 'com.moved.ns-to-be-moved required-macro-ns))) |
| 282 | + (pr-str [(second ns-form) required-ns required-macro-ns])))))) |
238 | 283 |
|
239 | 284 | (deftest returns-list-of-affected-files-when-moving-dirs-for-cljs
|
240 | 285 | (with-redefs [refactor-nrepl.rename-file-or-dir/rename-file! (fn [_old _new])
|
|
0 commit comments