Skip to content

Commit ef3c242

Browse files
[test] Make tests pass on Windows
1 parent 1f81a88 commit ef3c242

File tree

7 files changed

+93
-83
lines changed

7 files changed

+93
-83
lines changed

src/orchard/java/resource.clj

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,28 @@
77
(:import (java.io File)
88
(java.net URL)))
99

10-
(defn- trim-leading-separator
11-
"Trim the java.io.File/separator at the beginning of s."
12-
[^String s]
13-
(if (.startsWith s java.io.File/separator)
14-
(subs s 1)
15-
s))
16-
1710
(defn project-resources
1811
"Get a list of classpath resources, i.e. files that are not clojure/java source
1912
or class files. Only consider classpath entries that are directories, does not
2013
consider jars."
2114
[]
22-
(mapcat
23-
(fn [^File directory]
24-
(->> directory
25-
(file-seq)
26-
(filter (memfn ^File isFile))
27-
(map (fn [^File file]
28-
(let [relpath (-> file
29-
(.getPath)
30-
(.replaceFirst
31-
(.getPath directory)
32-
"")
33-
(trim-leading-separator))]
34-
{:root directory
35-
:file file
36-
:relpath relpath
37-
:url (io/as-url file)})))
38-
(remove #(.startsWith ^String (:relpath %) "META-INF/"))
39-
(remove #(re-matches #".*\.(clj[cs]?|java|class)" (:relpath %)))))
40-
(filter (memfn ^File isDirectory) (map io/as-file (cp/classpath (cp/context-classloader))))))
15+
(->> (cp/classpath (cp/context-classloader))
16+
(map io/as-file)
17+
(filter (memfn ^File isDirectory))
18+
(mapcat
19+
(fn [^File directory]
20+
(->> (file-seq directory)
21+
(filter (memfn ^File isFile))
22+
(map (fn [^File file]
23+
(let [relpath (.getPath
24+
(.relativize (.toURI directory)
25+
(.toURI file)))]
26+
{:root directory
27+
:file file
28+
:relpath relpath
29+
:url (io/as-url file)})))
30+
(remove #(.startsWith ^String (:relpath %) "META-INF/"))
31+
(remove #(re-matches #".*\.(clj[cs]?|java|class)" (:relpath %))))))))
4132

4233
(defn resource-full-path ^URL [relative-path]
4334
(io/resource relative-path (cp/context-classloader)))

test/orchard/java/classpath_test.clj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
[clojure.set :as set]
55
[clojure.string :as string]
66
[clojure.test :refer [deftest is testing]]
7+
[matcher-combinators.matchers :as m]
78
[orchard.java]
89
[orchard.java.classpath :as cp]
910
[orchard.misc :as misc])
1011
(:import
1112
(java.io File)
1213
(java.net URL)))
1314

15+
(require 'matcher-combinators.test) ;; for `match?`
16+
1417
;; Simple normalization for string compare. Classpath URLs have trailing
1518
;; slashes; classpath entries specified at JVM launch may or may not.
1619
(defn- trim-trailing-slash [x]
@@ -33,14 +36,11 @@
3336
(let [project-root (System/getProperty "user.dir")
3437
directory-with-jar-extension (some #(when (re-find #"not-a\.jar" (.getPath ^URL %)) %)
3538
(cp/classpath))]
36-
(is (some #(= (str (io/file project-root "src")) %)
37-
(map trim-trailing-slash (cp/classpath))))
38-
(is (some #(= (str (io/file project-root "test")) %)
39-
(map trim-trailing-slash (cp/classpath))))
40-
(is (some #(re-find #".*/clojure-.*\.jar" (.getPath ^URL %))
41-
(cp/classpath)))
42-
(is (some #(re-find #".*/clojure-.*-sources\.jar" (.getPath ^URL %))
43-
(cp/classpath)))
39+
(is (match? (m/embeds [(trim-trailing-slash (io/as-url (io/file project-root "src")))
40+
(trim-trailing-slash (.getPath (io/as-url (io/file project-root "test"))))
41+
#".*/clojure-.*\.jar"
42+
#".*/clojure-.*-sources\.jar"])
43+
(map trim-trailing-slash (cp/classpath))))
4444
(testing "Directories with .jar extension"
4545
(is directory-with-jar-extension "Is present in the classpath")
4646
(is (-> directory-with-jar-extension io/file .isDirectory)
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
(ns orchard.java.resource-test
22
(:require
3+
[clojure.string :as string]
34
[clojure.test :refer [deftest is testing]]
5+
[matcher-combinators.matchers :as m]
46
[orchard.java.resource :as resource]))
57

8+
(require 'matcher-combinators.test) ;; for `match?`
9+
610
(deftest resource-path-tuple-test
711
(is (nil? (resource/resource-path-tuple "jar:file:fake.jar!/fake/file.clj"))))
812

913
(deftest project-resources-test
1014
(testing "get the correct resources for the orchard project"
11-
(let [resources (->> (resource/project-resources)
12-
(filter (fn [{:keys [relpath]}]
13-
(= relpath "clojuredocs/test_export.edn"))))]
14-
(is (= "clojuredocs/test_export.edn" (-> resources first :relpath)))
15-
(is (= java.net.URL (-> resources first :url class))))))
15+
(let [test-export (some (fn [{:keys [relpath] :as res}]
16+
(when (string/ends-with? relpath "test_export.edn")
17+
res))
18+
(resource/project-resources))]
19+
(is (match? {:relpath "clojuredocs/test_export.edn"
20+
:url (m/via class java.net.URL)}
21+
test-export)))))
Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns orchard.java.source-files-test
22
(:require [clojure.test :refer [deftest is testing]]
33
[orchard.java.source-files :as src-files]
4-
[orchard.test.util :as util]))
4+
[orchard.test.util :as util]
5+
[orchard.util.os :as os]))
56

67
(when util/jdk-sources-present?
78
(deftest class->source-file-url-test
@@ -25,30 +26,34 @@
2526
(#'src-files/find-jar-file-for-class klass))))
2627

2728
;; TODO: test for Clojure CLI and invoke tool at some point.
28-
(deftest test-download-sources-jar-using-lein
29-
(let [f (sources-jar-file clojure.lang.Ref)]
30-
(.delete f)
31-
(is (not (.exists f))))
29+
;; TODO: doesn't currently pass on Windows because it can't find "lein".
30+
;; Probably a CI setup problem.
31+
(when-not (= os/os-type ::os/windows)
32+
(deftest test-download-sources-jar-using-lein
33+
(let [f (sources-jar-file clojure.lang.Ref)]
34+
(.delete f)
35+
(is (not (.exists f))))
3236

33-
(is (#'src-files/download-sources-using-lein
34-
(src-files/infer-maven-coordinates-for-class clojure.lang.Ref)))
37+
(is (#'src-files/download-sources-using-lein
38+
(src-files/infer-maven-coordinates-for-class clojure.lang.Ref)))
3539

36-
(is (.exists (sources-jar-file clojure.lang.Ref)))
40+
(is (.exists (sources-jar-file clojure.lang.Ref)))
3741

38-
(testing "downloaded source jars contain actual source files"
39-
(is (> (count (slurp (src-files/class->source-file-url clojure.lang.Ref)))
40-
200))))
42+
(testing "downloaded source jars contain actual source files"
43+
(is (> (count (slurp (src-files/class->source-file-url clojure.lang.Ref)))
44+
200)))))
4145

42-
(deftest test-download-sources-jar
43-
(let [f (sources-jar-file clojure.lang.Ref)]
44-
(.delete f)
45-
(is (not (.exists f))))
46+
(when-not (= os/os-type ::os/windows)
47+
(deftest test-download-sources-jar
48+
(let [f (sources-jar-file clojure.lang.Ref)]
49+
(.delete f)
50+
(is (not (.exists f))))
4651

47-
(is (src-files/download-sources-jar-for-coordinates
48-
(src-files/infer-maven-coordinates-for-class clojure.lang.Ref)))
52+
(is (src-files/download-sources-jar-for-coordinates
53+
(src-files/infer-maven-coordinates-for-class clojure.lang.Ref)))
4954

50-
(is (.exists (sources-jar-file clojure.lang.Ref)))
55+
(is (.exists (sources-jar-file clojure.lang.Ref)))
5156

52-
(testing "downloaded source jars contain actual source files"
53-
(is (> (count (slurp (src-files/class->source-file-url clojure.lang.Ref)))
54-
200))))
57+
(testing "downloaded source jars contain actual source files"
58+
(is (> (count (slurp (src-files/class->source-file-url clojure.lang.Ref)))
59+
200)))))

test/orchard/test/util.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns orchard.test.util
2-
(:require [orchard.java.source-files :as src-files]))
2+
(:require clojure.string
3+
[orchard.java.source-files :as src-files]))
34

45
(def jdk-sources-present?
56
(boolean (src-files/class->source-file-url Thread)))
@@ -8,3 +9,9 @@
89
{:post [(seq %)]}
910
(->> (ns-imports ns-sym)
1011
(map #(-> % ^Class val .getName symbol))))
12+
13+
(defmacro with-out-str-rn
14+
"Like `with-out-str`, but replaces Windows' CR+LF with LF."
15+
[& body]
16+
`(let [s# (with-out-str ~@body)]
17+
(clojure.string/replace s# "\r\n" "\n")))

test/orchard/trace_test.clj

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require
33
[clojure.edn]
44
[clojure.test :as t :refer [is are deftest]]
5+
[orchard.test.util :refer [with-out-str-rn]]
56
[orchard.trace :as sut]
67
[orchard.trace-test.sample-ns :as sample-ns]))
78

@@ -53,24 +54,24 @@
5354

5455
(deftest basic-test
5556
(run! sut/trace-var* vars)
56-
(is (= expected-trace-result (with-out-str (sample-ns/qux arg1 arg2))))
57+
(is (= expected-trace-result (with-out-str-rn (sample-ns/qux arg1 arg2))))
5758

5859
(run! sut/untrace-var* vars)
59-
(is (= "" (with-out-str (sample-ns/qux arg1 arg2))))
60+
(is (= "" (with-out-str-rn (sample-ns/qux arg1 arg2))))
6061

6162
(run! sut/trace-var* vars)
6263
(sut/untrace-var* #'sample-ns/foo)
63-
(is (= expected-trace-result-no-foo (with-out-str (sample-ns/qux arg1 arg2))))
64+
(is (= expected-trace-result-no-foo (with-out-str-rn (sample-ns/qux arg1 arg2))))
6465

6566
(sut/trace-ns* 'orchard.trace-test.sample-ns)
66-
(is (= expected-trace-result (with-out-str (sample-ns/qux arg1 arg2))))
67+
(is (= expected-trace-result (with-out-str-rn (sample-ns/qux arg1 arg2))))
6768

6869
(sut/untrace-ns* 'orchard.trace-test.sample-ns)
69-
(is (= "" (with-out-str (sample-ns/qux arg1 arg2))))
70+
(is (= "" (with-out-str-rn (sample-ns/qux arg1 arg2))))
7071

7172
(sut/trace-ns* 'orchard.trace-test.sample-ns)
7273
(sut/untrace-all)
73-
(is (= "" (with-out-str (sample-ns/qux arg1 arg2)))))
74+
(is (= "" (with-out-str-rn (sample-ns/qux arg1 arg2)))))
7475

7576
(deftest fibo-test
7677
(sut/trace-ns* 'orchard.trace-test.sample-ns)
@@ -136,7 +137,7 @@
136137
137138
└─→ 8
138139
"
139-
(with-out-str (sample-ns/fibo 5))))
140+
(with-out-str-rn (sample-ns/fibo 5))))
140141

141142
(is (= "
142143
(orchard.trace-test.sample-ns/fibo 5)
@@ -199,7 +200,7 @@
199200
200201
└─→ 8
201202
"
202-
(with-out-str (sample-ns/fibo 5))))
203+
(with-out-str-rn (sample-ns/fibo 5))))
203204

204205
(is (= "
205206
(orchard.trace-test.sample-ns/fibo2 0 1 10)
@@ -246,7 +247,7 @@
246247
247248
└─→ 89
248249
"
249-
(with-out-str (sample-ns/fibo2 0 1 10)))))
250+
(with-out-str-rn (sample-ns/fibo2 0 1 10)))))
250251

251252
(defn function-that-prints []
252253
(let [example-data (into {} (map #(vector % :val) (range 1000)))

test/orchard/util/os_test.clj

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
[clojure.test :as test :refer [deftest is testing]]
55
[orchard.util.os :as os]))
66

7-
(deftest cache-dir-test
8-
(testing "BSD"
9-
(with-redefs [os/os-type ::os/bsd]
10-
(is (string/ends-with? (os/cache-dir) "/.cache"))))
7+
(when-not (= os/os-type ::os/windows)
8+
(deftest cache-dir-test
9+
(testing "BSD"
10+
(with-redefs [os/os-type ::os/bsd]
11+
(is (string/ends-with? (os/cache-dir) "/.cache"))))
1112

12-
(testing "Linux"
13-
(with-redefs [os/os-type ::os/linux]
14-
(is (string/ends-with? (os/cache-dir) "/.cache"))))
13+
(testing "Linux"
14+
(with-redefs [os/os-type ::os/linux]
15+
(is (string/ends-with? (os/cache-dir) "/.cache"))))
1516

16-
(testing "Mac"
17-
(with-redefs [os/os-type ::os/mac]
18-
(is (string/ends-with? (os/cache-dir) "/Library/Caches")))))
17+
(testing "Mac"
18+
(with-redefs [os/os-type ::os/mac]
19+
(is (string/ends-with? (os/cache-dir) "/Library/Caches"))))))
1920

20-
;; NOTE: This test case targets AppVeyor mainly
21-
(deftest cache-dir-windows-test
22-
(when (= ::os/windows os/os-type)
21+
(when (= os/os-type ::os/windows)
22+
(deftest cache-dir-windows-test
2323
(is (re-seq #"^C:\\Users\\.+\\AppData\\Local" (os/cache-dir)))))

0 commit comments

Comments
 (0)