Skip to content

Commit 083236c

Browse files
committed
Eastwood: always use absolute filenames, internally
1 parent ecbe79b commit 083236c

File tree

9 files changed

+46
-26
lines changed

9 files changed

+46
-26
lines changed

e2e/monorepo-support/test/monorepo_support/core_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
(doseq [filename all
2424
:let [file (File. filename)
25-
absolute (-> file .getAbsolutePath)]]
25+
absolute (-> file .getCanonicalPath)]]
2626
(is (= filename
2727
absolute)
2828
"Returns absolutized filenames, which is important for monorepo support")

src/formatting_stack/linters/eastwood.clj

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
(ns formatting-stack.linters.eastwood
22
(:require
33
[clojure.java.io :as io]
4+
[clojure.set :as set]
45
[clojure.string :as str]
56
[eastwood.lint]
67
[formatting-stack.linters.eastwood.impl :as impl]
78
[formatting-stack.protocols.linter :as linter]
9+
[formatting-stack.protocols.spec :as protocols.spec]
810
[formatting-stack.util :refer [ns-name-from-filename]]
911
[medley.core :refer [assoc-some deep-merge]]
10-
[nedap.utils.modular.api :refer [implement]])
12+
[nedap.speced.def :as speced]
13+
[nedap.utils.modular.api :refer [implement]]
14+
[nedap.utils.spec.api :refer [check!]])
1115
(:import
1216
(java.io File)))
1317

@@ -16,7 +20,7 @@
1620
(let [linters (remove #{:suspicious-test :unused-ret-vals :constant-test :wrong-tag}
1721
eastwood.lint/default-linters)]
1822
(-> eastwood.lint/default-opts
19-
(assoc :linters linters
23+
(assoc :linters linters
2024
:rethrow-exceptions? true))))
2125

2226
(def parallelize-linters? (System/getProperty "formatting-stack.eastwood.parallelize-linters"))
@@ -28,6 +32,14 @@
2832
(note that this prefix must not be passed to Eastwood itself).")
2933

3034
(defn lint! [{:keys [options]} filenames]
35+
{:post [(do
36+
(assert (check! (speced/fn [^::protocols.spec/reports xs]
37+
(let [output (->> xs (keep :filename) (set))]
38+
(set/subset? output (set filenames))))
39+
%)
40+
"The `:filename`s returned from Eastwood should be a subset of this function's `filenames`.
41+
Otherwise, it would mean that our filename absolutization out of Eastwood reports is buggy.")
42+
true)]}
3143
(let [namespaces (->> filenames
3244
(remove #(str/ends-with? % ".edn"))
3345
(keep ns-name-from-filename))
@@ -50,9 +62,11 @@
5062
:level :warning
5163
:source (keyword "eastwood" (name linter))
5264
:warning-details-url warning-details-url
53-
:filename (if (string? uri-or-file-name)
54-
uri-or-file-name
55-
(-> ^File uri-or-file-name .getCanonicalPath)))))
65+
:filename (speced/let [^::speced/nilable ^String s (when (string? uri-or-file-name)
66+
uri-or-file-name)
67+
^File file (or (some-> s File.)
68+
uri-or-file-name)]
69+
(-> file .getCanonicalPath)))))
5670
(concat (impl/warnings->reports output)
5771
(impl/exceptions->reports @exceptions)))))
5872

src/formatting_stack/linters/eastwood/impl.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
[clojure.string :as string]
44
[eastwood.reporting-callbacks :as reporting-callbacks]
55
[formatting-stack.protocols.spec :as protocols.spec]
6-
[nedap.speced.def :as speced]))
6+
[nedap.speced.def :as speced])
7+
(:import
8+
(java.io File)))
79

810
(speced/defn ^boolean? contains-dynamic-assertions?
911
"Does this linting result refer to a :pre/:post containing dynamic assertions?
@@ -36,8 +38,8 @@ See https://git.io/fhQTx"
3638
[^string? warnings]
3739
(->> warnings
3840
(re-seq #"(.*):(\d+):(\d+): Reflection warning - (.+)\.")
39-
(map (fn [[_, filename, line, column, msg]]
40-
{:filename filename
41+
(map (speced/fn [[_, ^String filename, line, column, msg]]
42+
{:filename (-> filename File. .getCanonicalPath)
4143
:line (Long/parseLong line)
4244
:column (Long/parseLong column)
4345
:msg msg

test/formatting_stack/test_helpers.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
(speced/defn filename-as-resource [^string? filename]
99
(str "file:" (-> filename
1010
File.
11-
.getAbsolutePath)))
11+
.getCanonicalPath)))
1212

1313
(defn with-mocked-diff-path
1414
"Fixture to stub the absolute path in #'util.diff/unified-diff"

test/functional/formatting_stack/formatters/clean_ns/impl.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
(deftest has-duplicate-requires?
88
(are [input assertion] (let [file (io/file "test" "functional" "formatting_stack" "formatters" "clean_ns" input)
99
_ (assert (-> file .exists))
10-
filename (-> file .getAbsolutePath)
10+
filename (-> file .getCanonicalPath)
1111
result (sut/has-duplicate-requires? filename)]
1212
(case assertion
1313
:has-duplicates result

test/functional/formatting_stack/linters/eastwood.clj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
[formatting-stack.linters.eastwood :as sut]
55
[formatting-stack.protocols.linter :as linter]
66
[matcher-combinators.matchers :as matchers]
7-
[matcher-combinators.test :refer [match?]]))
7+
[matcher-combinators.test :refer [match?]])
8+
(:import
9+
(java.io File)))
810

911
(use-fixtures :once (fn [tests]
1012
;; prevent humongous AST representations from being printed:
@@ -14,7 +16,7 @@
1416
(deftest lint!
1517
(let [linter (sut/new {})]
1618
(are [filename expected] (match? expected
17-
(linter/lint! linter [filename]))
19+
(linter/lint! linter [(-> filename File. .getCanonicalPath)]))
1820
"test-resources/valid_syntax.clj"
1921
[]
2022

@@ -32,14 +34,14 @@
3234
:line pos-int?
3335
:column pos-int?
3436
:warning-details-url matchers/absent
35-
:filename "test-resources/eastwood_warning.clj"}
37+
:filename (-> "test-resources/eastwood_warning.clj" File. .getCanonicalPath)}
3638
{:source :eastwood/def-in-def
3739
:line pos-int?
3840
:column pos-int?
3941
:warning-details-url "https://github.com/jonase/eastwood#def-in-def"
40-
:filename "test-resources/eastwood_warning.clj"}
42+
:filename (-> "test-resources/eastwood_warning.clj" File. .getCanonicalPath)}
4143
{:source :eastwood/wrong-pre-post
4244
:line pos-int?
4345
:column pos-int?
4446
:warning-details-url "https://github.com/jonase/eastwood#wrong-pre-post"
45-
:filename "test-resources/eastwood_warning.clj"}]))))
47+
:filename (-> "test-resources/eastwood_warning.clj" File. .getCanonicalPath)}]))))

test/integration/formatting_stack/strategies/impl.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
(sut/dir-contains? dirname filename))
1212

1313
"." (-> "src/formatting_stack/strategies/impl.clj" File.) true
14-
(-> "." File. .getAbsolutePath) (File. "project.clj") true
14+
(-> "." File. .getCanonicalPath) (File. "project.clj") true
1515
"." (File. "project.clj") true
1616
"." (File. "dev/user.clj") true
1717
"dev" (File. "dev/user.clj") true
1818
"." (File. "LICENSE") true
19-
(-> "." File. .getAbsolutePath) (File. "LICENSE") true
19+
(-> "." File. .getCanonicalPath) (File. "LICENSE") true
2020
"." (File. "./LICENSE") true
2121
"dev" (File. "LICENSE") false
2222
"dev" (File. "./LICENSE") false
23-
(-> "." File. .getAbsolutePath) (File. "I_dont_exist") false
23+
(-> "." File. .getCanonicalPath) (File. "I_dont_exist") false
2424
"." (File. "I_dont_exist") false
2525
"dev" (File. "I_dont_exist") false
2626
"dev" (File. "user.clj") false))

test/unit/formatting_stack/formatters/cljfmt/impl/magic_symbols.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
false
1818

1919
(-> (io/file "test-resources" "magic.clj")
20-
(.getAbsolutePath))
20+
(.getCanonicalPath))
2121
true))

test/unit/formatting_stack/linters/eastwood/impl.clj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
[clojure.string :as str]
44
[clojure.test :refer [are deftest is testing]]
55
[formatting-stack.linters.eastwood.impl :as sut]
6-
[matcher-combinators.test :refer [match?]]))
6+
[matcher-combinators.test :refer [match?]])
7+
(:import
8+
(java.io File)))
79

810
(deftest contains-dynamic-assertions?
911
(testing "matches on false-positive wrong-pre-post reports"
@@ -72,7 +74,7 @@
7274
:column 40
7375
:line 6
7476
:msg "reference to field getPath can't be resolved"
75-
:filename "path.clj"}]
77+
:filename (-> "path.clj" File. .getCanonicalPath)}]
7678

7779
(str/join "\n"
7880
["path.clj:6:40: Reflection warning - reference to field getPath can't be resolved."
@@ -82,20 +84,20 @@
8284
:column 40
8385
:line 6
8486
:msg "reference to field getPath can't be resolved"
85-
:filename "path.clj"}
87+
:filename (-> "path.clj" File. .getCanonicalPath)}
8688
{:source :eastwood/warn-on-reflection
8789
:level :warning
8890
:column 12
8991
:line 13
9092
:msg "different message"
91-
:filename "other-path.clj"}]
93+
:filename (-> "other-path.clj" File. .getCanonicalPath)}]
9294

9395
(str/join "\n"
9496
["path.clj:6:40: Reflection warning - reference to field getPath can't be resolved."
9597
"random garbage"
9698
"path.clj: Incomplete warning - different message."
9799
"other-path.clj:13:12: Reflection warning - different message."])
98100
[{:msg "reference to field getPath can't be resolved"
99-
:filename "path.clj"}
101+
:filename (-> "path.clj" File. .getCanonicalPath)}
100102
{:msg "different message"
101-
:filename "other-path.clj"}]))
103+
:filename (-> "other-path.clj" File. .getCanonicalPath)}]))

0 commit comments

Comments
 (0)