Skip to content

Commit

Permalink
Merge pull request #441 from ivarref/bugfix
Browse files Browse the repository at this point in the history
Fix beholder watch functionality. Improve docs.
  • Loading branch information
plexus authored Aug 15, 2024
2 parents 49e98ed + 98996ba commit 15e553d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Fixed

- Fix beholder watch functionality that would cause a NullPointerException earlier.

## Changed

# 1.91.1392 (2024-05-23 / c2d7e1f)
Expand Down
4 changes: 3 additions & 1 deletion doc/07_watch_mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ interface, the provided links describes how they work in detail.

``` clojure
#kaocha/v1
{:kaocha.watch/ignore ["*.tmp"]}
{:kaocha.watch/ignore ["**.tmp"]}
; this will match all files ending in .tmp in the current directory and
; any subdirectory
```

When running in watch mode you can press the Enter (Return) key to manually
Expand Down
17 changes: 10 additions & 7 deletions src/kaocha/watch.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
[lambdaisland.tools.namespace.track :as ctn-track]
[slingshot.slingshot :refer [try+]]
[nextjournal.beholder :as beholder])
(:import (java.nio.file FileSystems)
(:import (java.io File)
(java.nio.file FileSystems Path PathMatcher)
(java.util.concurrent ArrayBlockingQueue BlockingQueue)))

(defn make-queue []
Expand Down Expand Up @@ -93,9 +94,10 @@
for a description of the patterns, these are similar but not the same as
typical shell glob patterns."
[path patterns]
(assert (instance? Path path))
(let [fs (FileSystems/getDefault)
patterns (map #(.getPathMatcher fs (str "glob:" %)) patterns)]
(some #(.matches % path) patterns)))
(some #(.matches ^PathMatcher % path) patterns)))

(defn convert
"Converts a Git-style ignore pattern into the equivalent pattern that Java PathMatcher uses."
Expand Down Expand Up @@ -153,7 +155,7 @@
"Finds ignore files in the local directory and the system."
[dir]
(let [absolute-files [(io/file (str (System/getProperty "user.home") "/.config/git/ignore"))]
relative-files (filter #(glob? (.toPath %) ["**.gitignore" "**.ignore"]) (file-seq (io/file dir)))]
relative-files (filter #(glob? (.toPath ^File %) ["**.gitignore" "**.ignore"]) (file-seq (io/file dir)))]
(into absolute-files relative-files)))

(defn merge-ignore-files
Expand All @@ -169,7 +171,7 @@
(defn wait-and-rescan! [q tracker watch-paths ignore]
(let [f (qtake q)]
(cond
(and (file? f) (glob? (.toPath f) ignore))
(and (file? f) (glob? (.toPath ^File f) ignore))
(recur q tracker watch-paths ignore)

(directory? f)
Expand Down Expand Up @@ -281,7 +283,7 @@ errors as test errors."
(map io/file))
(:kaocha/tests config))
;; Without this, if any path doesn't exist the watching doesn't work.
(filter (fn [x] (.exists ^java.io.File x)))))
(filter (fn [x] (.exists ^File x)))))

(defmulti watch! :type)

Expand All @@ -295,8 +297,9 @@ errors as test errors."
(defmethod watch! :beholder [{:keys [q watch-paths]}]
(apply beholder/watch
(fn [{:keys [type path]}]
(assert (instance? Path path))
(when (contains? #{:modify :create} type)
(qput q path)))
(qput q (.toFile ^Path path))))
(map str watch-paths)))

(defn run* [config finish? q]
Expand All @@ -307,7 +310,7 @@ errors as test errors."
{})
watch-paths (if (:kaocha.watch/use-ignore-file config)
(set/union (watch-paths config)
(set (map #(.getParentFile (.getCanonicalFile %)) (find-ignore-files "."))))
(set (map #(.getParentFile (.getCanonicalFile ^File %)) (find-ignore-files "."))))
(watch-paths config))
tracker (ctn-track/tracker)
;; if t.n fails due to circular dependencies, do not track-reload.
Expand Down

0 comments on commit 15e553d

Please sign in to comment.