Skip to content

Commit

Permalink
Show warning 206, if a namespace can't be parsed (#258)
Browse files Browse the repository at this point in the history
Add error message if namespace can't be read.
  • Loading branch information
tengstrand authored Dec 8, 2022
1 parent ca7c38e commit 28d8848
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 43 deletions.
2 changes: 1 addition & 1 deletion components/deps/deps.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:paths ["src"]
:deps {org.clojure/tools.deps.alpha {:mvn/version "0.14.1205"}}
:deps {org.clojure/tools.deps.alpha {:mvn/version "0.15.1244"}}
:aliases {:test {:extra-paths ["test"]
:extra-deps {}}}}
2 changes: 1 addition & 1 deletion components/file/deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{:paths ["src"]
:deps {clj-commons/fs {:mvn/version "1.6.310"}
org.clojure/tools.deps.alpha {:mvn/version "0.14.1205"}}
org.clojure/tools.deps.alpha {:mvn/version "0.15.1244"}}
:aliases {:test {:extra-paths ["test"]
:extra-deps {}}}}
7 changes: 6 additions & 1 deletion components/file/src/polylith/clj/core/file/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@
nil)))

(defn read-first-statement [path]
(read-string {:read-cond :allow} (slurp path)))
(try
(read-string {:read-cond :allow} (slurp path))
(catch Exception _
;; When the whole namespace is commented out, we get the runtime exception
;; "EOF while reading", which is okay, and we just skip the namespace.
[])))

(defn copy-resource-file! [source target-path]
(delete-file target-path)
Expand Down
15 changes: 11 additions & 4 deletions components/help/src/polylith/clj/core/help/check.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
" (including test paths).\n"
"\n"
" " (color/error cm "Error 109") " - Invalid test runner configuration for some projects.\n"
" The value of the optional :create-test-runner key under [:test] or\n"
" [:projects \"some-project-name\" :test] in workspace.edn must be either\n"
" nil, :default, or a fully qualified symbol referring to a function on\n"
" The value of the optional :create-test-runner key under [" (s/key ":test" cm) "] or\n"
" [" (s/key ":projects" cm) " \"some-project-name\" " (s/key ":test" cm) "] in workspace.edn must be either\n"
" nil, " (s/key ":default" cm) ", or a fully qualified symbol referring to a function on\n"
" the poly tool's classpath, which can take a single argument and must return\n"
" an instance of polylith.clj.core.test-runner-contract.interface/TestRunner.\n"
"\n"
Expand All @@ -71,7 +71,14 @@
"\n"
" " (color/warning cm "Warning 205") " - Non top namespace was found in brick.\n"
" Triggered if a namespace in a brick doesn't start with the top namespaces\n"
" defined in " (s/key ":top-namespace" cm) " in ./workspace.edn."))
" defined in " (s/key ":top-namespace" cm) " in ./workspace.edn.\n"
"\n"
" " (color/warning cm "Warning 206") " - Unreadable namespace in brick/project.\n"
" Triggered if a namespace can't be parsed for a brick or project."))

(defn print-help [cm]
(-> cm help-text println))

(comment
(print-help "dark")
#__)
2 changes: 1 addition & 1 deletion components/tap/deps.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:paths ["src"]
:deps {djblue/portal {:mvn/version "0.26.0"}}
:deps {djblue/portal {:mvn/version "0.33.0"}}
:aliases {:test {:extra-paths []
:extra-deps {}}}}
2 changes: 1 addition & 1 deletion components/validator/deps.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:paths ["src"]
:deps {metosin/malli {:mvn/version "0.8.4"}}
:deps {metosin/malli {:mvn/version "0.9.2"}}
:aliases {:test {:extra-paths ["test"]
:extra-deps {}}}}
6 changes: 4 additions & 2 deletions components/validator/src/polylith/clj/core/validator/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[polylith.clj.core.validator.m201-mismatching-parameters :as m201]
[polylith.clj.core.validator.m202-missing-paths :as m202]
[polylith.clj.core.validator.m203-path-exists-in-both-dev-and-profile :as m203]
[polylith.clj.core.validator.m205-non-top-namespace :as m205]))
[polylith.clj.core.validator.m205-non-top-namespace :as m205]
[polylith.clj.core.validator.m206-unreadable-namespace :as m206]))

(defn has-errors? [messages]
(->> messages
Expand All @@ -32,7 +33,8 @@
(m201/warnings interfaces components color-mode)
(m202/warnings projects paths color-mode)
(m203/warnings settings projects color-mode)
(m205/warnings components bases color-mode)]
(m205/warnings components bases color-mode)
(m206/warnings components bases projects color-mode)]
(into #{} cat)
(sort-by (juxt :type :code :message))
(vec)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns polylith.clj.core.validator.m206-unreadable-namespace
(:require [polylith.clj.core.util.interface :as util]
[polylith.clj.core.util.interface.color :as color]))

(defn unreadable-ns [{:keys [file-path invalid]} type name color-mode]
(when invalid
(let [message (str "Unreadable namespace in " (color/brick type name color-mode) ": " file-path)]
[(util/ordered-map :type "warning"
:code 206
:message (color/clean-colors message)
:colorized-message message)])))

(defn unreadable-nss [{:keys [type name namespaces]} color-mode]
(concat
(mapcat #(unreadable-ns % type name color-mode) (:src namespaces))
(mapcat #(unreadable-ns % type name color-mode) (:test namespaces))))

(defn warnings [components bases projects color-mode]
(mapcat #(unreadable-nss % color-mode)
(concat components bases projects)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns polylith.clj.core.validator.m206-unreadable-namespace-test
(:require [clojure.test :refer :all]
[polylith.clj.core.validator.m206-unreadable-namespace :as m206]))

(def bricks [{:type "base"
:name "poly-cli"
:namespaces {:src [{:name "core"
:namespace ""
:invalid true
:file-path "/Users/joakimtengstrand/source/polylith/bases/poly-cli/src/polylith/clj/core/poly_cli/core.clj"
:imports ["polylith.clj.core.command.interface"]}
{:name "api"
:namespace "polylith.clj.core.poly-cli.api"
:file-path "/Users/joakimtengstrand/source/polylith/bases/poly-cli/src/polylith/clj/core/poly_cli/api.clj"
:imports ["clojure.string"]}]
:test [{:name "api-argument-mapping-test"
:namespace ""
:invalid true
:file-path "/Users/joakimtengstrand/source/polylith/bases/poly-cli/test/polylith/clj/core/poly_cli/api_argument_mapping_test.clj"
:imports ["clojure.test" "polylith.clj.core.poly-cli.api"]}]}}])

(deftest warning--when-having-unreadable-namespaces--returns-warnings
(is (= [{:type "warning",
:code 206,
:message "Unreadable namespace in poly-cli: /Users/joakimtengstrand/source/polylith/bases/poly-cli/src/polylith/clj/core/poly_cli/core.clj",
:colorized-message "Unreadable namespace in poly-cli: /Users/joakimtengstrand/source/polylith/bases/poly-cli/src/polylith/clj/core/poly_cli/core.clj"}
{:type "warning",
:code 206,
:message "Unreadable namespace in poly-cli: /Users/joakimtengstrand/source/polylith/bases/poly-cli/test/polylith/clj/core/poly_cli/api_argument_mapping_test.clj",
:colorized-message "Unreadable namespace in poly-cli: /Users/joakimtengstrand/source/polylith/bases/poly-cli/test/polylith/clj/core/poly_cli/api_argument_mapping_test.clj"}]
(m206/warnings bricks nil nil "none"))))
11 changes: 7 additions & 4 deletions components/version/src/polylith/clj/core/version/interface.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
(def major 0)
(def minor 2)
(def patch 16)
(def revision "alpha-external-test-runner")
(def revision "alpha-issue254-02")
(def name (str major "." minor "." patch "-" revision))

(def date "2022-11-20")
(def date "2022-12-08")

(defn version
([ws-type]
Expand All @@ -24,12 +24,15 @@
:date date}
:ws {:type :toolsdeps2
:breaking 1
:non-breaking 1}}
:non-breaking 2}}
from (assoc :from from)))))

;; === workspace attributes (ws) ===
;;
;; ws release action attribute
;; ----- ------------- ------- ------------------------------------
;; ----- ------------- ------- -----------------------------------------------------------------
;; 1.2 0.2.16-alpha added :bases > BASE > :namespaces > :src/test > [] > :invalid
;; added :projects > PROJECT > :namespaces > :src/test > [] > :invalid
;; added :components > COMPONENT > :namespaces > :src/test > [] > :invalid
;; 1.1 0.2.14-alpha added :settings > :vcs > :is-git-repo
;; deleted :projects > PROJECT > :is-run-tests
2 changes: 1 addition & 1 deletion components/workspace-clj/deps.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:paths ["src"]
:deps {org.clojure/tools.deps.alpha {:mvn/version "0.14.1205"}}
:deps {org.clojure/tools.deps.alpha {:mvn/version "0.15.1244"}}
:aliases {:test {:extra-paths ["test"]
:extra-deps {}}}}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,20 @@
statement-body)))

(defn imports [ns-statements suffixed-top-ns interface-ns]
(vec (sort (mapcat #(import % suffixed-top-ns interface-ns)
(filterv import? ns-statements)))))
(if (sequential? ns-statements)
(vec (sort (mapcat #(import % suffixed-top-ns interface-ns)
(filterv import? ns-statements))))
[]))

(comment
(def ns-statements 'x)
(def suffixed-top-ns "polylith.clj.core.")
(def interface-ns "interface")
(imports ns-statements suffixed-top-ns interface-ns)

(import? ns-statements)

#__)

(defn skip-slash [path]
(or (str-util/skip-until path "/")
Expand All @@ -102,14 +114,40 @@
(str/replace "/" ".")
(str/replace "_" "-")))))

(defn empty-ns? [content]
(and (sequential? content)
(empty? content)))

(defn valid-ns? [content]
(and (sequential? content)
(second content)))

(defn ->namespace [source-dir suffixed-top-ns interface-ns file-path]
(let [content (file/read-first-statement file-path)
ns-name (namespace-name source-dir file-path)
imports (imports content suffixed-top-ns interface-ns)]
{:name ns-name
:namespace (-> content second str)
:file-path file-path
:imports imports}))
ns-name (namespace-name source-dir file-path)]
(if (empty-ns? content)
{:name ns-name
:namespace ""
:file-path file-path
:imports []}
(let [imports (imports content suffixed-top-ns interface-ns)
valid? (valid-ns? content)]
(cond-> {:name ns-name
:namespace (if valid?
(-> content second str)
"")
:file-path file-path
:imports imports}
(not valid?) (assoc :invalid true))))))

(comment
(imports content suffixed-top-ns interface-ns)

(def source-dir "/Users/joakimtengstrand/source/polylith/components/version/src/polylith/clj/core/")
(def file-path "/Users/joakimtengstrand/source/polylith/components/version/src/polylith/clj/core/version/testing.clj")
(->namespace source-dir "polylith.clj.core." "interface" file-path)
(file/read-first-statement file-path)
#__)

(defn source-namespaces-from-disk [source-dir suffixed-top-ns interface-ns]
(mapv #(->namespace source-dir suffixed-top-ns interface-ns %)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns polylith.clj.core.workspace-clj.readimportsfromdisk-test
(ns polylith.clj.core.workspace-clj.namespaces-from-disk-test
(:require [clojure.test :refer :all]
[polylith.clj.core.file.interface :as file]
[polylith.clj.core.workspace-clj.namespaces-from-disk :as from-disk]))

(def suffixed-top-ns "polylith.clj.core.")
Expand Down Expand Up @@ -66,3 +67,10 @@
(deftest import-when-using-as-alias-if-implementation-ns
(is (= ["asalias.comp-a.core"]
(from-disk/import '(:require [asalias.comp-a.core :as-alias comp-a]) "asalias." "interface"))))

(deftest ->namespace--read-invalid-namespace
(with-redefs [file/read-first-statement (fn [_] '--)
from-disk/namespace-name (fn [_ _] "")]
(from-disk/->namespace "" "" "" "")
(is (= {:name "", :namespace "", :file-path "path", :imports [], :invalid true}
(from-disk/->namespace "" "" "" "path")))))
12 changes: 6 additions & 6 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
"bases/poly-cli/src"]

:extra-deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/tools.deps.alpha {:mvn/version "0.14.1205"}
org.slf4j/slf4j-nop {:mvn/version "1.7.36"}
org.clojure/tools.deps.alpha {:mvn/version "0.15.1244"}
org.slf4j/slf4j-nop {:mvn/version "2.0.3"}
org.jline/jline {:mvn/version "3.21.0"}
djblue/portal {:mvn/version "0.26.0"}
io.github.seancorfield/build-clj {:git/tag "v0.8.2" :git/sha "0ffdb4c"}
djblue/portal {:mvn/version "0.33.0"}
io.github.seancorfield/build-clj {:git/tag "v0.8.3" :git/sha "7ac1f8d"}
clj-commons/fs {:mvn/version "1.6.310"}
metosin/malli {:mvn/version "0.8.4"}
metosin/malli {:mvn/version "0.9.2"}
mount/mount {:mvn/version "0.1.16"}
mvxcvi/puget {:mvn/version "1.3.2"}
slipset/deps-deploy {:mvn/version "0.2.0"}
Expand Down Expand Up @@ -81,7 +81,7 @@
:extra-deps {polylith/clj-poly-migrator {:local/root "projects/poly-migrator"}}}

:build {:deps {io.github.seancorfield/build-clj
{:git/tag "v0.8.2" :git/sha "0ffdb4c"}
{:git/tag "v0.8.3" :git/sha "7ac1f8d"}
poly/version {:local/root "components/version"}}
:paths ["build/resources"]
:ns-default build}}}
5 changes: 4 additions & 1 deletion doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ poly help
```

```
Poly 0.2.16-alpha-issue247 (2022-10-27) - https://github.com/polyfy/polylith
Poly 0.2.16-alpha-issue254 (2022-11-04) - https://github.com/polyfy/polylith
poly CMD [ARGS] - where CMD [ARGS] are:
Expand Down Expand Up @@ -250,6 +250,9 @@ poly help
Warning 205 - Non top namespace was found in brick.
Triggered if a namespace in a brick doesn't start with the top namespaces
defined in :top-namespace in ./workspace.edn.
Warning 206 - Unreadable namespace in brick/project.
Triggered if a namespace can't be parsed for a brick or project.
```

### create
Expand Down
2 changes: 1 addition & 1 deletion projects/poly/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
poly/poly-cli {:local/root "../../bases/poly-cli"}

org.clojure/clojure {:mvn/version "1.11.1"}
org.slf4j/slf4j-nop {:mvn/version "1.7.36"}}
org.slf4j/slf4j-nop {:mvn/version "2.0.3"}}

;; so poly can be installed as a tool:
:tools/usage {:ns-default polylith.clj.core.poly-cli.api}
Expand Down
10 changes: 5 additions & 5 deletions projects/poly/test/project/poly/workspace_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@
" library version type KB api poly dev s e r l p r j r"
" --------------------------------------------------------- --------- --- ----------------------"
" clj-commons/fs 1.6.310 maven 12 x x x . x . . . . . ."
" djblue/portal 0.26.0 maven 1,136 - x x . . . . x . . ."
" io.github.seancorfield/build-clj 0ffdb4c git 41 - - x . . . . . . . ."
" metosin/malli 0.8.4 maven 63 x x x . . . . . x . ."
" djblue/portal 0.33.0 maven 1,150 - x x . . . . x . . ."
" io.github.seancorfield/build-clj 7ac1f8d git 41 - - x . . . . . . . ."
" metosin/malli 0.9.2 maven 73 x x x . . . . . x . ."
" mount/mount 0.1.16 maven 8 - - x . . . . . . . ."
" mvxcvi/puget 1.3.2 maven 15 x x x . . . . . . . x"
" org.clojure/clojure 1.11.1 maven 4,008 x x x . . . . . . . ."
" org.clojure/tools.deps.alpha 0.14.1205 maven 64 x x x x x . . . . x ."
" org.clojure/tools.deps.alpha 0.15.1244 maven 64 x x x x x . . . . x ."
" org.jline/jline 3.21.0 maven 971 - x x . . . x . . . ."
" org.slf4j/slf4j-nop 1.7.36 maven 3 - x x . . . . . . . ."
" org.slf4j/slf4j-nop 2.0.3 maven 3 - x x . . . . . . . ."
" rewrite-clj/rewrite-clj 1.1.45 maven 71 - - x . . . . . . . ."
" slipset/deps-deploy 0.2.0 maven 7 - - x . . . . . . . ."
" zprint/zprint 1.2.4 maven 185 - x x . . x . . . . ."]
Expand Down
2 changes: 1 addition & 1 deletion scripts/output/help/01-help.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Poly 0.2.16-alpha-issue247 (2022-10-27) - https://github.com/polyfy/polylith
Poly 0.2.16-alpha-issue254 (2022-11-04) - https://github.com/polyfy/polylith

poly CMD [ARGS] - where CMD [ARGS] are:

Expand Down
3 changes: 3 additions & 0 deletions scripts/output/help/02-check.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@
Warning 205 - Non top namespace was found in brick.
Triggered if a namespace in a brick doesn't start with the top namespaces
defined in :top-namespace in ./workspace.edn.

Warning 206 - Unreadable namespace in brick/project.
Triggered if a namespace can't be parsed for a brick or project.
4 changes: 2 additions & 2 deletions scripts/output/local-dep/libs-compact.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
e i
library version type KB inv dev r l
---------------------------------------------------- --- --- ---
clj-time/clj-time d9ed4e4 git 133 x x . x
clj-time/clj-time d9ed4e4 git - x x . x
migrate-me/migrate-me - local 0 t t t .
org.clojure/clojure 1.10.1 maven 3,816 x x . .
org.clojure/tools.deps.alpha 0.12.985 maven 59 x x . .
uncomplicate/neanderthal 0.41.0 maven 219 - x . .
uncomplicate/neanderthal 0.41.0 maven 0 - x . .
2 changes: 1 addition & 1 deletion scripts/output/polylith1/libs-migrated.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
org.slf4j/slf4j-nop 1.7.36 maven 3 - x x . . . . . . . .
rewrite-clj/rewrite-clj 1.1.45 maven 71 - - x . . . . . . . .
slipset/deps-deploy 0.2.0 maven 7 - - x . . . . . . . .
zprint/zprint 1.2.3 maven 176 - x x . . x . . . . .
zprint/zprint 1.2.4 maven 185 - x x . . x . . . . .
2 changes: 1 addition & 1 deletion scripts/output/polylith1/libs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
org.slf4j/slf4j-nop 1.7.36 maven 3 - x x . . . . . . . .
rewrite-clj/rewrite-clj 1.1.45 maven 71 - - x . . . . . . . .
slipset/deps-deploy 0.2.0 maven 7 - - x . . . . . . . .
zprint/zprint 1.2.3 maven 176 - x x . . x . . . . .
zprint/zprint 1.2.4 maven 185 - x x . . x . . . . .

0 comments on commit 28d8848

Please sign in to comment.