Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

road to bb: more babashka fs usage #437

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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