Skip to content

Commit

Permalink
added tests for util/config and core/hooks, small mod to core/hooks, …
Browse files Browse the repository at this point in the history
…comments (#162)

* looks OK

* better message
  • Loading branch information
gkspranger committed Mar 15, 2021
1 parent 55deb33 commit 9f6f7b6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/yetibot/core/hooks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require
[yetibot.core.models.admin :as admin]
[clojure.set :refer [intersection]]
[taoensso.timbre :refer [trace info]]
[taoensso.timbre :refer [info]]
[yetibot.core.handler]
[clojure.string :as s]
[metrics.timers :as timers]
Expand Down Expand Up @@ -52,10 +52,13 @@
"Given a top level command prefix look up corresponding sub-cmds by matching
prefix against command regexes in `hooks.`"
[prefix]
(first (filter (fn [[k v]] (re-find (re-pattern k) prefix)) @hooks)))
(->> @hooks
(filter (fn [[k _]] (re-find (re-pattern k) prefix)))
(first)))

(comment
@hooks
(first (filter (fn [[k v]] (re-find (re-pattern k) "channel")) @hooks))
(find-sub-cmds "channel")
)

Expand Down
10 changes: 10 additions & 0 deletions src/yetibot/core/util/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

(defn config-exists? [path] (.exists (as-file path)))

(comment
(config-exists? "nope")
(config-exists? "config/config.sample.edn")
)

(defn load-edn!
[path]
(try
Expand All @@ -21,6 +26,11 @@
(debug "No config found at" (pr-str path))
nil)))

(comment
(load-edn! "nope.edn")
(load-edn! "config/config.sample.edn")
)

(defn load-or-create-edn!
"Attempts to load edn from `config-path`. If no file exists, a new file will
be written with the value of `default-config`."
Expand Down
10 changes: 10 additions & 0 deletions test/yetibot/core/test/hooks.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns yetibot.core.test.hooks
(:require [yetibot.core.hooks :as h]
[yetibot.core.observers.karma :refer [hook-wrapper
message-hook]]
[clojure.test :refer [function?]]
[midje.sweet :refer [=> fact facts
contains every-checker]]))
Expand Down Expand Up @@ -94,3 +96,11 @@
(fact "lockdown prefix will return expected regex result"
(doseq [p parti-pairs]
(h/lockdown-prefix-regex (first p)) => (last p)))))

(facts "about obs-hook"
;; borrowed example usage from observer.karma
(let [hooks (h/obs-hook #{:message}
(partial hook-wrapper message-hook))]
(fact "can load message observer hook without error
and return non-empty collection"
hooks => (every-checker coll? not-empty))))
29 changes: 29 additions & 0 deletions test/yetibot/core/test/util/config.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns yetibot.core.test.util.config
(:require
[midje.sweet :refer [=> fact facts every-checker]]
[yetibot.core.util.config :as c]
[yetibot.core.loader :as ldr]))

(facts "about load-edn!"
(fact "loading non-existent config file returns nil"
(c/load-edn! "nope.edn") => nil)
(let [cfg (c/load-edn! "config/config.sample.edn")]
(fact "loading sample config file returns non-empty map"
cfg => (every-checker map? not-empty))
(fact "loading sample config file returns expected value"
(get-in cfg [:yetibot :url])
=> "http://localhost:3003")))

(facts "about get-config"
(let [cfg (c/load-edn! "config/config.sample.edn")]
(fact "get's valid map path and tests against spec"
(c/get-config cfg ::ldr/url [:yetibot :url])
=> {:value "http://localhost:3003"})
(fact "get's invalid map path and tests against valid spec"
(:error (c/get-config cfg
::ldr/url [:yetibot :doesnotexist]))
=> :not-found)
(fact "get's valid map path and tests against invalid spec"
(:error (c/get-config cfg
::ldr/plugin-config [:yetibot :url]))
=> :invalid)))

0 comments on commit 9f6f7b6

Please sign in to comment.