Skip to content

Commit

Permalink
Add xappend and xprepend options for editing path, add edit error che…
Browse files Browse the repository at this point in the history
…cking and tests
  • Loading branch information
justone committed May 26, 2020
1 parent 1ca78ea commit 26573a0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
3 changes: 2 additions & 1 deletion deps.edn
Expand Up @@ -4,7 +4,8 @@
:clj {:extra-deps {;; Additional libs for clojure to match babashka includes
cheshire {:mvn/version "5.10.0"}
org.clojure/tools.cli {:mvn/version "1.0.194"}}
:extra-paths ["dev"]}}
:extra-paths ["dev"]}
:test {:extra-paths ["test"]}}
:paths ["src"]
:deps {comb {:mvn/version "0.1.1"}
doric {:mvn/version "0.9.0"}}}
6 changes: 6 additions & 0 deletions run-tests.sh
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
bb -cp $(clojure -A:test -Spath) \
-e "(require '[clojure.test :as t]
'[empath-test])
(let [{:keys [:fail :error]} (t/run-tests 'empath-test)]
(System/exit (+ fail error)))"
26 changes: 20 additions & 6 deletions src/empath.clj
Expand Up @@ -85,11 +85,14 @@
" "
"Edit elements of a path.
Takes a list of actions. Valid actions are:
Takes a list of action/element pairs. Valid actions are:
append [element]
remove [element]
prepend [element]"))
append [element] - append element to end of path
remove [element] - remove element from path
prepend [element] - prepsent element to beginning of path
xappend [element] - append element after removing from rest of path
xprepend [element] - prepend element after removing from rest of path
"))

(defn munge-path
[path args]
Expand All @@ -101,14 +104,25 @@
(case op
"prepend" (into [arg] result)
"append" (conj result arg)
"remove" (into [] (remove #(= arg %) result))))
"remove" (into [] (remove #(= arg %)) result)
"xappend" (conj (into [] (remove #(= arg %)) result) arg)
"xprepend" (into [arg] (remove #(= arg %) result))))
parts (partition 2 args)))))

(defn find-edit-errors
[parsed]
(or (opts/find-errors parsed)
(let [{:keys [arguments]} parsed]
(cond
(odd? (count arguments))
{:message "Even number of arguments expected."
:exit 1}))))

(defn handle-edit
[global-options subargs]
(let [parsed (parse-opts subargs edit-options :in-order true)
{:keys [options arguments]} parsed]
(or (when-some [errors (opts/find-errors parsed)]
(or (when-some [errors (find-edit-errors parsed)]
(->> (opts/format-help (str progname " edit") edit-help parsed errors)
(opts/print-and-exit)))
(let [path (if (:empty options)
Expand Down
24 changes: 24 additions & 0 deletions test/empath_test.clj
@@ -0,0 +1,24 @@
(ns empath-test
(:require
[clojure.test :refer :all]
[empath :refer :all]
))

(deftest edit
(testing "munge"
(is (= "/usr/bin"
(munge-path ":" ["append" "/usr/bin"])))
(is (= "/usr/bin:/usr/local/bin:/home/nate/bin"
(munge-path "/usr/bin:/usr/local/bin" ["append" "/home/nate/bin"])))
(is (= "/home/nate/bin:/usr/bin:/usr/local/bin"
(munge-path "/usr/bin:/usr/local/bin" ["prepend" "/home/nate/bin"])))
(is (= "/usr/local/bin:/usr/bin"
(munge-path "/usr/bin:/usr/local/bin" ["xprepend" "/usr/local/bin"])))
(is (= "/usr/local/bin:/usr/bin"
(munge-path "/usr/bin:/usr/local/bin" ["xappend" "/usr/bin"])))
(is (= "/usr/local/bin"
(munge-path "/usr/bin:/usr/local/bin" ["remove" "/usr/bin"])))
(is (= "/usr/local/bin:/home/nate/bin"
(munge-path "/usr/bin:/usr/local/bin" ["remove" "/usr/bin" "append" "/home/nate/bin"])))
)
)

0 comments on commit 26573a0

Please sign in to comment.