Skip to content

Commit

Permalink
improve(environment): share common environment for specific commands
Browse files Browse the repository at this point in the history
If the environment was configured using an alist. The default
environment is always merged into the specific command environment.
This makes sure that these don't need to repeat entries.

The values in the specific environment will override those of the
common one for shared keys.
  • Loading branch information
Walheimat committed Jul 13, 2024
1 parent 93243dd commit 2e4f26c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
32 changes: 26 additions & 6 deletions ship-mate.el
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,12 @@ ARG is passed to `ship-mate-command--buffers'."
"Get the last environment for CMD or default.
This is the currently set `compilation-environment' if it's
non-nil. Otherwise it is either the local value of
`ship-mate-environment' of the current CMD or the global
value (corresponding to nil)."
non-nil.
Otherwise the result is based on the local value of
`ship-mate-environment'. If it was defined per-command, the common
environment (denoted by nil) and the command's environment are merged.
If it's a plain list, it is used for all commands."
(if-let* ((buffer-name (funcall compilation-buffer-name-function nil))
(buffer (get-buffer buffer-name))
(local (buffer-local-value 'compilation-environment buffer)))
Expand All @@ -630,9 +633,26 @@ value (corresponding to nil)."

(when-let ((env (ship-mate--local-value 'ship-mate-environment)))

(or (alist-get cmd env)
(alist-get nil env)
env))))
(if (and (alist-get nil env)
(alist-get cmd env))

(let ((keys)
(combined (append (alist-get cmd env) (alist-get nil env)))
(merged))

(dolist (item combined)

(let ((key (car-safe (string-split item "="))))

(unless (member key keys)
(push item merged)
(push key keys))))

(reverse merged))

(or (alist-get cmd env)
(alist-get nil env)
env)))))

;;;; History

Expand Down
9 changes: 6 additions & 3 deletions test/ship-mate-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,15 @@

(setq-local compilation-environment nil)

(bydi ((:mock ship-mate--local-value :return '((test . ("TES=TING")) (nil . ("MOC=KING")))))
(bydi ((:mock ship-mate--local-value :return '((test . ("TES=TING" "MOC=CA"))
(nil . ("MOC=KING" "RE=SEARCH"))
(research . ("LAB=BING")))))

(let ((compilation-buffer-name-function (lambda (_) (buffer-name))))

(should (equal '("MOC=KING") (ship-mate-environment--current-environment 'mock)))
(should (equal '("TES=TING") (ship-mate-environment--current-environment 'test)))))))
(should (equal '("MOC=KING" "RE=SEARCH") (ship-mate-environment--current-environment 'mock)))
(should (equal '("TES=TING" "MOC=CA" "RE=SEARCH") (ship-mate-environment--current-environment 'test)))
(should (equal '("LAB=BING" "MOC=KING" "RE=SEARCH") (ship-mate-environment--current-environment 'research)))))))

(ert-deftest ship-mate-command--has-run-p ()
:tags '(meta)
Expand Down

0 comments on commit 2e4f26c

Please sign in to comment.