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

add cli entry point for ide #340

Merged
merged 4 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

:dependencies [[clj-http "3.10.1"]
[cheshire "5.9.0"]
[org.clojure/tools.cli "1.0.194"]
[org.clojure/tools.logging "0.3.1"]
[org.clojure/data.codec "0.1.0"]]

Expand Down
19 changes: 11 additions & 8 deletions src/etaoin/ide/flow.clj
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@
suite-tests))

(defn find-tests
[{:keys [test-id test-ids suite-id suite-ids test-name suite-name]}
[{:keys [test-id test-ids suite-id suite-ids test-name suite-name test-names suite-names]}
{:keys [tests] :as parsed-file}]
(let [test-ids (cond-> #{}
test-id (conj (first (filter #(= test-id (:id %)) tests)))
test-name (conj (first (filter #(= test-name (:name %)) tests)))
suite-id (into (get-tests-by-suite-id suite-id :id parsed-file))
suite-name (into (get-tests-by-suite-id suite-name :name parsed-file))
test-ids (into (filter #((set test-ids) (:id %)) tests))
suite-ids (into (mapcat #(get-tests-by-suite-id % :id parsed-file) suite-ids)))
test-id (conj (first (filter #(= test-id (:id %)) tests)))
test-name (conj (first (filter #(= test-name (:name %)) tests)))
suite-id (into (get-tests-by-suite-id suite-id :id parsed-file))
suite-name (into (get-tests-by-suite-id suite-name :name parsed-file))
test-ids (into (filter #((set test-ids) (:id %)) tests))
suite-ids (into (mapcat #(get-tests-by-suite-id % :id parsed-file) suite-ids))
test-names (into (filter #((set test-names) (:name %)) tests))
suite-names (into (mapcat #(get-tests-by-suite-id % :name parsed-file) suite-names)))
tests-found (filter test-ids tests)]
(if (empty? tests-found)
tests
Expand All @@ -112,7 +114,8 @@
(let [parsed-file (with-open [rdr (io/reader path)]
(parse-stream rdr true))
opt-search (select-keys opt [:test-name :test-id :test-ids
:suite-name :suite-id :suite-ids])
:suite-name :suite-id :suite-ids
:test-names :suite-names])
tests-found (find-tests opt-search parsed-file)
opt (merge {:base-url (:url parsed-file)
:vars (atom {})}
Expand Down
99 changes: 94 additions & 5 deletions src/etaoin/ide/main.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,97 @@
(ns etaoin.ide.main
(:require [etaoin.api :refer :all]
[etaoin.ide.flow :refer [run-ide-script]])
(:gen-class))
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.cli :refer [parse-opts]]
[etaoin.api :as api]
[etaoin.ide.flow :as flow]
[etaoin.util :refer [exit]])
(:gen-class))

(def browsers-set
#{:chrome :safari :firefox :edge :phantom})

(defn -main
[& args])
(defn str->vec
[string]
(str/split string #","))

(def cli-options
[["-d" "--driver-name name" "The name of driver. The default is `chrome`"
:default :chrome
:parse-fn keyword
:validate [browsers-set
(str "Must be one of the list items - " (str/join ", " (map name browsers-set)))]]

["-p" "--params params" "Parameters for the driver represented as edn string"
:default {}
:parse-fn read-string]

["-f" "--file path" "Path to the ide file on disk"]

["-r" "--resource path" "Path to the resource"]

[nil "--test-ids ids" "Comma-separeted test ID(s)"
:parse-fn str->vec]

[nil "--suite-ids ids" "Comma-separeted suite ID(s)"
:parse-fn str->vec]

[nil "--test-names names" "Comma-separeted test name(s)"
:parse-fn str->vec]

[nil "--suite-names names" "Comma-separeted suite name(s)"
:parse-fn str->vec]

[nil "--base-url url" "Base url for test"]
["-h" "--help"]])

(def help
"This is cli interface for running ide files.

Usage examples:

lein run -m etaoin.ide.main -d firefox -p '{:port 8888 :args [\"--no-sandbox\"]} -r ide/test.side

java -cp .../poject.jar -m etaoin.ide.main -d firefox -p '{:port 8888} -f ide/test.side

Options:")

(defn usage [options-summary]
(->> [help options-summary]
(str/join \newline)))

(defn error-msg [errors]
(str "The following errors occurred while parsing your command:\n\n"
(str/join \newline errors)))

(defn run-script
[file-path {:keys [driver-name params] :as options}]
(let [opt (select-keys options [:base-url :test-ids
:test-names :suite-ids
:suite-names])]
(api/with-driver driver-name params driver
(flow/run-ide-script driver file-path opt))))

(defn -main [& args]
(let [{:keys [errors summary options]} (parse-opts args cli-options)
{:keys [help file resource]} options]
(cond
errors
(exit 1 (error-msg errors))

help
(exit 0 (usage summary))

file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

здесь и ниже: немного странно, что в обоих случаях мы передаем в run-script файл и ресурс как строку. В этом случае низлежащий io/reader трактует строку как путь к файлу, то есть с ресурсами из jar это работать не будет. Лучше передавать ide-file и r в run-script. Оба будут прочитаны корректно с помощью io/reader.

Тогда первый параметр в run-script лучше назвать source, источник -- все, что может быть slurp-нуто. И в низлежащей run-ide-script тоже переименовать парамерт и просто сделать slurp на нем.

(let [ide-file (io/file file)]
(when-not (and (.exists ide-file)
(not (.isDirectory ide-file)))
(exit 1 "The IDE file not found"))
(run-script file options))

resource
(if-let [r (io/resource resource)]
(run-script (str r) options)
(exit 1 "Resource not found"))

:else
(exit 1 "Specify the path to the ide file: `--file` or `--resource`"))))
10 changes: 10 additions & 0 deletions src/etaoin/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@
(when (.isConnected socket)
(.close socket)
true)))

(defn exit
[code template & args]
(let [out (if (zero? code)
*out*
*err*)]
(binding [*out* out]
(println (apply format
template args))))
(System/exit code))