Skip to content

Commit a120efd

Browse files
committed
Eastwood: always use absolute filenames, internally
1 parent df434cc commit a120efd

File tree

7 files changed

+44
-29
lines changed

7 files changed

+44
-29
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
(ns formatting-stack.linters.eastwood
22
(:require
3-
[clojure.java.io :as io]
3+
[clojure.set :as set]
44
[clojure.string :as str]
55
[eastwood.lint]
66
[formatting-stack.linters.eastwood.impl :as impl]
77
[formatting-stack.protocols.linter :as linter]
8+
[formatting-stack.protocols.spec :as protocols.spec]
89
[formatting-stack.util :refer [ns-name-from-filename silence]]
910
[medley.core :refer [assoc-some deep-merge]]
10-
[nedap.utils.modular.api :refer [implement]])
11+
[nedap.speced.def :as speced]
12+
[nedap.utils.modular.api :refer [implement]]
13+
[nedap.utils.spec.api :refer [check!]])
1114
(:import
1215
(java.io File)))
1316

@@ -16,12 +19,20 @@
1619
(assoc :rethrow-exceptions? true)))
1720

1821
(defn lint! [{:keys [options]} filenames]
22+
{:post [(do
23+
(assert (check! (speced/fn [^::protocols.spec/reports xs]
24+
(let [output (->> xs (keep :filename) (set))]
25+
(set/subset? output (set filenames))))
26+
%)
27+
"The `:filename`s returned from Eastwood should be a subset of this function's `filenames`.
28+
Otherwise, it would mean that our filename absolutization out of Eastwood reports is buggy.")
29+
true)]}
1930
(let [namespaces (->> filenames
2031
(remove #(str/ends-with? % ".edn"))
2132
(keep ns-name-from-filename))
2233
reports (atom nil)
2334
exceptions (atom nil)]
24-
35+
2536
(silence
2637
(try
2738
(-> options
@@ -37,9 +48,11 @@
3748
:level :warning
3849
:source (keyword "eastwood" (name linter))
3950
:warning-details-url warning-details-url
40-
:filename (if (string? uri-or-file-name)
41-
uri-or-file-name
42-
(-> ^File uri-or-file-name .getCanonicalPath)))))
51+
:filename (speced/let [^::speced/nilable ^String s (when (string? uri-or-file-name)
52+
uri-or-file-name)
53+
^File file (or (some-> s File.)
54+
uri-or-file-name)]
55+
(-> file .getCanonicalPath)))))
4356
(into (impl/exceptions->reports @exceptions)))))
4457

4558
(defn new [{:keys [eastwood-options]

test/formatting_stack/test_helpers.clj

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

1515
(defn with-mocked-diff-path
1616
"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 "https://github.com/jonase/eastwood#reflection"
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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
(are [dirname filename expected] (= expected
1111
(sut/dir-contains? dirname filename))
1212

13-
"." (-> "src/formatting_stack/strategies/impl.clj" File.) true
14-
(-> "." File. .getAbsolutePath) (File. "project.clj") true
15-
"." (File. "project.clj") true
16-
"." (File. "dev/user.clj") true
17-
"dev" (File. "dev/user.clj") true
18-
"." (File. "LICENSE") true
19-
(-> "." File. .getAbsolutePath) (File. "LICENSE") true
20-
"." (File. "./LICENSE") true
21-
"dev" (File. "LICENSE") false
22-
"dev" (File. "./LICENSE") false
23-
(-> "." File. .getAbsolutePath) (File. "I_dont_exist") false
24-
"." (File. "I_dont_exist") false
25-
"dev" (File. "I_dont_exist") false
26-
"dev" (File. "user.clj") false))
13+
"." (-> "src/formatting_stack/strategies/impl.clj" File.) true
14+
(-> "." File. .getCanonicalPath) (File. "project.clj") true
15+
"." (File. "project.clj") true
16+
"." (File. "dev/user.clj") true
17+
"dev" (File. "dev/user.clj") true
18+
"." (File. "LICENSE") true
19+
(-> "." File. .getCanonicalPath) (File. "LICENSE") true
20+
"." (File. "./LICENSE") true
21+
"dev" (File. "LICENSE") false
22+
"dev" (File. "./LICENSE") false
23+
(-> "." File. .getCanonicalPath) (File. "I_dont_exist") false
24+
"." (File. "I_dont_exist") false
25+
"dev" (File. "I_dont_exist") false
26+
"dev" (File. "user.clj") false))
2727

2828
(deftest absolutize
2929
(are [target] (testing target

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))

0 commit comments

Comments
 (0)