Skip to content

Commit

Permalink
Add optional class validations and fix exclude option
Browse files Browse the repository at this point in the history
  • Loading branch information
logseq-cldwalker committed Jun 2, 2023
1 parent fb2bfd0 commit f37814b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Fix `assets-exist-and-are-used` validation - asset subdirectories caused a false negative
* Fix `tags-and-page-refs-have-pages` validation - whiteboard page refs weren't recognized
* Add var to allow validations to access graph's config
* Add optional class validations
* Fix exclude option unable to exclude tests from other namespaces

## 0.4.0
* Add support for custom validations
Expand Down
14 changes: 9 additions & 5 deletions src/logseq/graph_validator.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[logseq.graph-validator.config :as config]
[logseq.graph-validator.default-validations]
[clojure.edn :as edn]
[clojure.string :as string]
[babashka.cli :as cli]
[promesa.core :as p]
[nbb.classpath :as classpath]
Expand All @@ -28,9 +29,12 @@
"Hacky way to exclude tests because t/run-tests doesn't give us test level control"
[tests]
(doseq [t tests]
(when-let [var (get (ns-publics 'logseq.graph-validator.default-validations) (symbol t))]
(println "Excluded test" var)
(alter-meta! var dissoc :test))))
(let [[test-ns test-name]
(if (string/includes? (str t) "/")
(string/split t #"/") ["logseq.graph-validator.default-validations" t])]
(when-let [var (get (ns-publics (symbol test-ns)) (symbol test-name))]
(println "Excluded test" var)
(alter-meta! var dissoc :test)))))

(def spec
"Options spec"
Expand Down Expand Up @@ -68,13 +72,13 @@
user-config' (into {} (keep (fn [[k v]] (when (seq v) [k v])) user-config))
{:keys [exclude add-namespaces] :as config} (get-validator-config dir user-config')]
(reset! state/config config)
(when (seq exclude)
(exclude-tests exclude))
(when (seq add-namespaces)
(classpath/add-classpath (path/join dir ".graph-validator")))
(-> (p/all (map #(require (symbol %)) add-namespaces))
(p/then
(fn [_promise-results]
(when (seq exclude)
(exclude-tests exclude))
(setup-graph dir)
(apply t/run-tests (into ['logseq.graph-validator.default-validations]
(map symbol add-namespaces))))))))
Expand Down
29 changes: 29 additions & 0 deletions src/logseq/graph_validator/validations/class.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns logseq.graph-validator.validations.class
"Validations related to managing classes"
(:require [clojure.test :refer [deftest is]]
[logseq.graph-validator.state :as state]
[logseq.db.rules :as rules]
[datascript.core :as d]))

(defn- get-classes []
(->> (d/q
'[:find (pull ?b [*])
:in $ %
:where (page-property ?b :type "Class")]
@state/db-conn
(vals rules/query-dsl-rules))
(map first)
(map #(assoc (:block/properties %) :block/original-name (:block/original-name %)))))

(deftest classes-have-parents
(is (empty? (remove #(or (some? (:parent %)) (= "Thing" (:block/original-name %)))
(get-classes)))
"All classes have parent property except Thing"))

(deftest classes-have-urls
(is (empty? (remove :url (get-classes)))
"All classes have urls"))

(deftest classes-are-capitalized
(is (empty? (remove #(re-find #"^[A-Z]" (:block/original-name %)) (get-classes)))
"All classes start with a capital letter"))

0 comments on commit f37814b

Please sign in to comment.