Skip to content

Commit 82e4af4

Browse files
[clojuredocs] Refactoring
1 parent 9fb3df8 commit 82e4af4

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

src/orchard/clojuredocs.clj

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
(:require
66
[clojure.edn :as edn]
77
[clojure.java.io :as io]
8-
[clojure.string :as string]
98
[orchard.misc :refer [with-lock]]
109
[orchard.util.os :as os])
1110
(:import
11+
(java.io File)
1212
(java.net URI)
1313
(javax.net.ssl HttpsURLConnection)
1414
(java.util.concurrent.locks ReentrantLock)))
@@ -22,11 +22,8 @@
2222
(ReentrantLock.))
2323
(def default-edn-file-url
2424
"https://github.com/clojure-emacs/clojuredocs-export-edn/raw/master/exports/export.compact.edn")
25-
(def cache-file-name
26-
(string/join os/file-separator [(os/cache-dir)
27-
"orchard"
28-
"clojuredocs"
29-
"export.edn"]))
25+
(def ^File cache-file
26+
(io/file (os/cache-dir) "orchard" "clojuredocs" "export.edn"))
3027

3128
(def http-timeout
3229
"Timeout in milliseconds for connecting to a URL and reading the content."
@@ -50,13 +47,13 @@
5047
(slurp resource)))
5148

5249
(defn- write-cache-file! [url]
53-
(.. (io/file cache-file-name)
50+
(.. cache-file
5451
getParentFile
5552
mkdirs)
56-
(spit cache-file-name (slurp-with-timeout url http-timeout)))
53+
(spit cache-file (slurp-with-timeout url http-timeout)))
5754

58-
(defn- load-cache-file! [cache-file]
59-
(reset! cache (-> cache-file
55+
(defn- load-cache-file! [file]
56+
(reset! cache (-> file
6057
slurp
6158
edn/read-string))
6259
true)
@@ -69,11 +66,10 @@
6966
;; Prevent multiple threads from trying to load the cache simultaneously.
7067
(with-lock lock
7168
(when (empty? @cache)
72-
(let [cache-file (io/file cache-file-name)]
73-
(load-cache-file!
74-
(if (.exists cache-file)
75-
cache-file
76-
(io/resource "clojuredocs/export.edn")))))))
69+
(load-cache-file!
70+
(if (.exists cache-file)
71+
cache-file
72+
(io/resource "clojuredocs/export.edn"))))))
7773

7874
(defn update-cache!
7975
"Load exported docs file from ClojureDocs, and store it as a cache.
@@ -83,16 +79,15 @@
8379
([]
8480
(update-cache! default-edn-file-url))
8581
([export-edn-url]
86-
(let [cache-file (io/file cache-file-name)]
87-
(with-lock lock
88-
(write-cache-file! export-edn-url)
89-
(load-cache-file! cache-file)))))
82+
(with-lock lock
83+
(write-cache-file! export-edn-url)
84+
(load-cache-file! cache-file))))
9085

9186
(defn clean-cache!
9287
"Clean the cached ClojureDocs export file and the in memory cache."
9388
{:added "0.5"}
9489
[]
95-
(.delete (io/file cache-file-name))
90+
(.delete cache-file)
9691
(reset! cache {}))
9792

9893
(defn get-doc

test/orchard/clojuredocs_test.clj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
(-> (Instant/now) .getEpochSecond (* 1000)))
1414

1515
(defn- create-dummy-cache-file [& [timestamp]]
16-
(let [cache-file (io/file docs/cache-file-name)]
16+
(let [cache-file docs/cache-file]
1717
(.. cache-file
1818
getParentFile
1919
mkdirs)
@@ -22,15 +22,15 @@
2222
(cond-> timestamp (.setLastModified timestamp)))))
2323

2424
(defn- clojuredocs-test-fixture [f]
25-
(with-redefs [docs/cache-file-name "target/clojuredocs/export.edn"]
25+
(with-redefs [docs/cache-file (io/file "target/clojuredocs/export.edn")]
2626
(docs/clean-cache!)
2727
(f)
2828
(docs/clean-cache!)))
2929

3030
(use-fixtures :each clojuredocs-test-fixture)
3131

3232
(deftest load-docs-if-not-loaded!-test
33-
(let [cache-file (io/file docs/cache-file-name)]
33+
(let [cache-file docs/cache-file]
3434
(testing "bundled"
3535
(is (not (.exists cache-file)))
3636
(is (empty? @docs/cache))
@@ -66,7 +66,7 @@
6666
(docs/clean-cache!))))
6767

6868
(deftest update-cache!-no-cache-file-test
69-
(let [cache-file (io/file docs/cache-file-name)]
69+
(let [cache-file docs/cache-file]
7070
(testing "accessible to remote export.edn"
7171
(is (not (.exists cache-file)))
7272
(is (empty? @docs/cache))
@@ -86,15 +86,15 @@
8686
(is (empty? @docs/cache)))))
8787

8888
(deftest update-cache!-non-existing-url-test
89-
(let [cache-file (io/file docs/cache-file-name)]
89+
(let [cache-file docs/cache-file]
9090
(is (not (.exists cache-file)))
9191
(is (empty? @docs/cache))
9292
(is (thrown? Exception (docs/update-cache! "file:/not/existing/file.edn")))
9393
(is (not (.exists cache-file)))
9494
(is (empty? @docs/cache))))
9595

9696
(deftest update-cache!-existing-cache-file-test
97-
(let [cache-file (io/file docs/cache-file-name)]
97+
(let [cache-file docs/cache-file]
9898
(testing "no cached documentation"
9999
(create-dummy-cache-file now)
100100
(reset! docs/cache {})
@@ -119,7 +119,7 @@
119119
(deftest clean-cache!-test
120120
(create-dummy-cache-file)
121121
(reset! docs/cache {:dummy "not-empty-dummy-data"})
122-
(let [cache-file (io/file docs/cache-file-name)]
122+
(let [cache-file docs/cache-file]
123123
(is (.exists cache-file))
124124
(is (seq @docs/cache))
125125
(docs/clean-cache!)

0 commit comments

Comments
 (0)