Skip to content

Commit

Permalink
road to bb: more babashka fs usage
Browse files Browse the repository at this point in the history
Replaced direct JDK references with babashka fs abstractions.

Of note:
- turfed etaoin.util/with-tmp-dir and replaced its usages with babashka
fs equivalent (Files/createTempDirectory is available in bb, I think).
We've decided that etaoin.util is not part of our public API clj-commons#430, so not
worried about deleting this macro.
- some references to io/file replaced with fs/file for more flexible coercion.

Contributes to clj-commons#380
  • Loading branch information
lread committed May 23, 2022
1 parent d851877 commit 2d94402
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 36 deletions.
14 changes: 7 additions & 7 deletions src/etaoin/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
[clojure.java.io :as io]
[clojure.string :as str]

[babashka.fs :as fs]
[cheshire.core :refer [generate-stream]]
[slingshot.slingshot :refer [try+ throw+]])

Expand Down Expand Up @@ -2825,7 +2826,7 @@

(defmethod upload-file String
[driver q path]
(upload-file driver q (io/file path)))
(upload-file driver q (fs/file path)))

(defmethod upload-file java.io.File
[driver q ^java.io.File file]
Expand Down Expand Up @@ -2995,7 +2996,6 @@

;; TODO add w3c screenshot
(defmulti screenshot-element

{:arglists '([driver q file])}
dispatch-driver)

Expand All @@ -3021,7 +3021,7 @@
(->> (.getTime (java.util.Date.))
(format "-%d.png")
(str (name driver-type))
(io/file dir)
(fs/file dir)
str))

(defmacro with-screenshots
Expand All @@ -3041,7 +3041,7 @@
(defn join-path
"Joins two and more path components into a single file path OS-wisely."
[p1 p2 & more]
(.getPath ^java.io.File (apply io/file p1 p2 more)))
(.getPath ^java.io.File (apply fs/file p1 p2 more)))

(defn format-date
[date pattern]
Expand Down Expand Up @@ -3072,9 +3072,9 @@
path-src (join-path dir-src file-src)
path-log (join-path dir-log file-log)]

(clojure.java.io/make-parents path-img)
(clojure.java.io/make-parents path-src)
(clojure.java.io/make-parents path-log)
(io/make-parents path-img)
(io/make-parents path-src)
(io/make-parents path-log)

(log/debugf "Writing screenshot: %s" path-img)
(screenshot driver path-img)
Expand Down
16 changes: 8 additions & 8 deletions src/etaoin/driver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/firefox/options.py
"
(:require [etaoin.util :refer [defmethods deep-merge]]
[babashka.fs :as fs]
[clojure.string :as string]
[clojure.tools.logging :as log])
(:import (java.io File)))
[clojure.tools.logging :as log]))

(defn dispatch-driver
[driver & _]
Expand Down Expand Up @@ -157,12 +157,12 @@
;; Chrome adds the trailing `/Default` part to the profile path.
;; To prevent duplication, let's clear the given path manually.
[driver ^String profile]
(let [profile (File. profile)
^File profile (if (= "Default" (.getName profile))
(.getParent profile)
profile)
user-data-dir (str (.getParent profile))
profile-dir (str (.getName profile))]
(let [profile (fs/file profile)
profile (if (= "Default" (fs/file-name profile))
(fs/parent profile)
profile)
user-data-dir (str (fs/parent profile))
profile-dir (str (fs/file-name profile))]
(set-options-args driver [(format "--user-data-dir=%s" user-data-dir)
(format "--profile-directory=%s" profile-dir)])))

Expand Down
10 changes: 0 additions & 10 deletions src/etaoin/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,3 @@
~@body
(finally
(.delete tmp#)))))

(defmacro with-tmp-dir [prefix bind & body]
`(let [tmp# (str (Files/createTempDirectory
~prefix
(into-array FileAttribute [])))
~bind tmp#]
(try
~@body
(finally
(fs/delete-tree tmp#)))))
8 changes: 4 additions & 4 deletions test/etaoin/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[clojure.test :refer :all]
[etaoin.api :refer :all]
[etaoin.test-report :as test-report]
[etaoin.util :refer [with-tmp-dir with-tmp-file]]
[etaoin.util :refer [with-tmp-file]]
[slingshot.slingshot :refer [try+]]))

(defn numeric? [val]
Expand Down Expand Up @@ -588,12 +588,12 @@
(is (valid-image? path))))

(deftest test-with-screenshots
(with-tmp-dir "screenshots" dir
(fs/with-temp-dir [dir {:prefix "screenshots"}]
(with-screenshots *driver* dir
(fill *driver* :simple-input "1")
(fill *driver* :simple-input "1")
(fill *driver* :simple-input "1"))
(is (= 3 (count (.listFiles (io/file dir)))))))
(is (= 3 (count (fs/list-dir dir))))))

(deftest test-screenshot-element
(when (or (chrome? *driver*)
Expand Down Expand Up @@ -711,7 +711,7 @@
(is false "should be caught")
(catch Exception _e
(is true "caught")
(let [files (file-seq (io/file dir-tmp))
(let [files (file-seq (fs/file dir-tmp))
expected-file-count (if (supports-logs? *driver*) 3 2)]
(is (= (-> files rest count)
expected-file-count))))))))
Expand Down
12 changes: 5 additions & 7 deletions test/etaoin/unit/unit_test.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
(ns etaoin.unit.unit-test
(:require [clojure.spec.alpha :as s]
(:require [babashka.fs :as fs]
[clojure.spec.alpha :as s]
[clojure.test :refer :all]
[etaoin.api :refer :all]
[etaoin.ide.flow :as ide]
[etaoin.ide.spec :as spec]
[etaoin.util :refer [with-tmp-dir]]
[etaoin.test-report]
etaoin.proc)
(:import java.io.File))

etaoin.proc))

(deftest test-firefox-driver-args
(with-redefs
Expand Down Expand Up @@ -37,8 +35,8 @@
(:args driver)))))))

(deftest test-chrome-profile
(with-tmp-dir "chrome-dir" chrome-dir
(let [profile-path (str (File. chrome-dir "chrome-profile"))]
(fs/with-temp-dir [chrome-dir {:prefix "chrome-dir"}]
(let [profile-path (str (fs/file chrome-dir "chrome-profile"))]
(with-chrome {:profile profile-path :args ["--no-sandbox"]} driver
(go driver "chrome://version")
(is profile-path
Expand Down

0 comments on commit 2d94402

Please sign in to comment.