Skip to content

Commit

Permalink
Merge pull request #25 from johannes-mueller/correct-transient-repeat…
Browse files Browse the repository at this point in the history
…-cmd

Fixes #24
  • Loading branch information
johannes-mueller authored Jun 1, 2024
2 parents 878ef83 + 0214ea1 commit 22466aa
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 8 deletions.
33 changes: 29 additions & 4 deletions test-cockpit.el
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ a derived class of `test-cockpit--engine'.")
:initform nil)
(last-args :initarg :last-args
:initform nil)
(last-interactive-cmd :initarg :last-interactive-cmd
:initform nil)
(is-dummy-engine :initarg :is-dummy-engine
:initform nil))
"The base class for a test-cockpit engine.
Expand Down Expand Up @@ -208,6 +210,10 @@ The additional arguments are shipped as ARGS."
(oset engine last-function-string last-function))
(oset engine last-args args)))

(defun test-cockpit--update-last-interactive-command (function)
(let ((engine (test-cockpit--retrieve-engine)))
(oset engine last-interactive-cmd function)))

(defun test-cockpit--make-test-project-command (project-string args)
"Call the test-project-command function with ARGS of the current project type.
PROJECT-STRING is usually nil. The argument is here to make the function
Expand All @@ -228,7 +234,7 @@ FUNC-STRING is the string determining the function to test."
(test-cockpit--make-test-command
'test-cockpit--test-function-command func-string args))

(defun test-cockpit-infix ()
(defun test-cockpit--infix ()
"Call the infix function of the current project type and return the infix array."
(test-cockpit--transient-infix
(funcall (alist-get (projectile-project-type) test-cockpit--project-types))))
Expand All @@ -238,7 +244,7 @@ FUNC-STRING is the string determining the function to test."
(unless (equal (aref (transient-get-suffix 'test-cockpit-prefix '(0)) 2)
'(:description "Run test"))
(transient-remove-suffix 'test-cockpit-prefix '(0)))
(if-let (infix (test-cockpit-infix))
(if-let (infix (test-cockpit--infix))
(transient-insert-suffix 'test-cockpit-prefix '(0) infix)))

(defun test-cockpit--run-test (command)
Expand Down Expand Up @@ -268,6 +274,7 @@ ARGS is the UI state for language specific settings."
(list (transient-args 'test-cockpit-prefix)))
(test-cockpit--run-test
(test-cockpit--command 'test-cockpit--make-test-project-command nil args))
(test-cockpit--update-last-interactive-command 'test-cockpit-test-project)
(test-cockpit--update-last-commands args))

;;;###autoload
Expand All @@ -286,6 +293,7 @@ is tested."
(test-cockpit--command 'test-cockpit--make-test-module-command
module-string
args))
(test-cockpit--update-last-interactive-command 'test-cockpit-test-module)
(test-cockpit--update-last-commands args))
(message "Not in a unit test module file")))

Expand All @@ -306,6 +314,7 @@ were in is tested."
(test-cockpit--command 'test-cockpit--make-test-function-command
function-string
args))
(test-cockpit--update-last-interactive-command 'test-cockpit-test-function)
(test-cockpit--update-last-commands args))
(message "Not in a unit test module file")))

Expand Down Expand Up @@ -435,6 +444,18 @@ prompt to type a test command is shown."
(test-cockpit--repeat-projectile-test)
(test-cockpit-repeat-test)))


;;;###autoload
(defun test-cockpit--repeat-interactive-test (&optional args)
"Repeat the last interactive test command.
This is not meant to be called directly but as a result the transient dispatch
in order to call the last test action with modified ARGS."
(interactive
(list (transient-args 'test-cockpit-prefix)))
(when-let ((last-cmd (test-cockpit--last-interactive-test-command)))
(funcall last-cmd args)))


(defun test-cockpit--projectile-build (&optional last-cmd)
"Launch a projectile driven build process.
If last executed command LAST-CMD is given the command is
Expand Down Expand Up @@ -497,6 +518,10 @@ repetition."
"Get the last switches stored in the current engine."
(oref (test-cockpit--retrieve-engine) last-switches))

(defun test-cockpit--last-interactive-test-command ()
"Get the last interactive test command."
(oref (test-cockpit--retrieve-engine) last-interactive-cmd))

(transient-define-prefix test-cockpit-prefix ()
"Test the project."
:value 'test-cockpit--last-switches
Expand All @@ -506,7 +531,7 @@ repetition."
"Setup the main menu common for all projects for testing."
(let ((module-string (or (test-cockpit--current-module-string) (test-cockpit--last-module-string)))
(function-string (or (test-cockpit--current-function-string) (test-cockpit--last-function-string)))
(last-cmd (oref (test-cockpit--real-engine-or-error) last-command)))
(last-cmd (oref (test-cockpit--real-engine-or-error) last-interactive-cmd)))
(vconcat (remove nil (append `("Run tests"
("p" "project" test-cockpit-test-project)
,(if module-string
Expand All @@ -519,7 +544,7 @@ repetition."
test-cockpit-test-function))
("c" "custom" test-cockpit-custom-test-command)
,(if last-cmd
`("r" "repeat" test-cockpit-repeat-test))))))))
`("r" "repeat" test-cockpit--repeat-interactive-test))))))))

(defun test-cockpit--strip-project-root (path)
"Strip the project root path from a given PATH."
Expand Down
2 changes: 1 addition & 1 deletion test/test-cargo.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ mod bar {
(ert-deftest test-cargo-infix ()
(mocker-let
((projectile-project-type () ((:output 'rust-cargo))))
(let ((infix (test-cockpit-infix)))
(let ((infix (test-cockpit--infix)))
(should (and (equal (aref (aref infix 0) 0) "Targets")
(equal (aref (aref infix 0) 1) '("-t" "tests" "--tests"))
(equal (aref (aref infix 0) 2) '("-b" "with benchmarks" "--benches"))
Expand Down
2 changes: 1 addition & 1 deletion test/test-cask.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
(setq test-cockpit--project-engines nil)
(mocker-let
((projectile-project-type () ((:output 'emacs-cask))))
(let ((infix (test-cockpit-infix)))
(let ((infix (test-cockpit--infix)))
(should
(and (equal (aref infix 0) "Cask specific switches")
(equal (aref infix 1) '("-i" "Run `cask install` before test" "install")))))))
Expand Down
69 changes: 69 additions & 0 deletions test/test-cockpit.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,37 @@
(test-cockpit-test-function '("foo" "bar"))
(should (equal (test-cockpit--last-switches) '("foo" "bar")))))


(ert-deftest test-transient-repeat-command-no-last-command ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-root (&optional _dir) ((:input-matcher (lambda (_) t) :output "foo-project"))))
(should (eq (test-cockpit--last-interactive-test-command) nil))))


(ert-deftest test-transient-repeat-command-last-cmd-project ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-root (&optional _dir) ((:input-matcher (lambda (_) t) :output "foo-project")))
(compile (command) ((:input '("test project") :output 'success))))
(test-cockpit-test-project)
(should (equal (test-cockpit--last-interactive-test-command) 'test-cockpit-test-project))))


(ert-deftest test-transient-repeat-command-last-cmd-module ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-root (&optional _dir) ((:input-matcher (lambda (_) t) :output "foo-project")))
(compile (command) ((:input '("test module foo-module-string") :output 'success))))
(test-cockpit-test-module)
(should (equal (test-cockpit--last-interactive-test-command) 'test-cockpit-test-module))))


(ert-deftest test-transient-repeat-command-last-cmd-function ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-root (&optional _dir) ((:input-matcher (lambda (_) t) :output "foo-project")))
(compile (command) ((:input '("test function foo-function-string") :output 'success))))
(test-cockpit-test-function)
(should (equal (test-cockpit--last-interactive-test-command) 'test-cockpit-test-function))))


(ert-deftest test-repeat-test-foo-engine ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-root (&optional _dir) ((:input-matcher (lambda (_) t) :output "foo-project")))
Expand Down Expand Up @@ -581,6 +612,44 @@
(test-cockpit-repeat-test)
(should (equal (test-cockpit--last-custom-command) "some custom command"))))))

(ert-deftest test-interactive-repeat-test-no-last-cmd ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-type () ((:output 'foo-project-type)))
(compile (command) ((:input '(_) :output nil :occur 0))))
(test-cockpit--repeat-interactive-test '())))

(ert-deftest test-interactive-repeat-test-project ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-type () ((:output 'foo-project-type)))
(compile (command) ((:input '("test project foo bar") :output 'success :occur 1)
(:input '("test project other args") :output 'success :occur 1)
(:input '("test project even other args") :output 'success :occur 1))))
(test-cockpit-test-project '("foo" "bar"))
(test-cockpit--repeat-interactive-test '("other" "args"))
(test-cockpit--repeat-interactive-test '("even" "other" "args"))))

(ert-deftest test-interactive-repeat-test-module ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-root (&optional _dir) ((:input-matcher (lambda (_) t) :output "foo-project")))
(compile (command) ((:input '("test module foo-module-string foo bar") :output 'success :occur 1)
(:input '("test module foo-module-string other args") :output 'success :occur 1)
(:input '("test module foo-module-string even other args") :output 'success :occur 1))))
(test-cockpit-test-module '("foo" "bar"))
(test-cockpit--repeat-interactive-test '("other" "args"))
(test-cockpit--repeat-interactive-test '("even" "other" "args"))
))

(ert-deftest test-interactive-repeat-test-function ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-root (&optional _dir) ((:input-matcher (lambda (_) t) :output "foo-project")))
(compile (command) ((:input '("test function foo-function-string foo bar") :output 'success :occur 1)
(:input '("test function foo-function-string other args") :output 'success :occur 1)
(:input '("test function foo-function-string even other args") :output 'success :occur 1))))
(test-cockpit-test-function '("foo" "bar"))
(test-cockpit--repeat-interactive-test '("other" "args"))
(test-cockpit--repeat-interactive-test '("even" "other" "args"))
))

(ert-deftest test-custom-test-command-default-directory ()
(tc--register-foo-project "foo")
(mocker-let ((projectile-project-root (&optional _dir) ((:input-matcher (lambda (_) t) :output "foo-project"))))
Expand Down
2 changes: 1 addition & 1 deletion test/test-mix.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
(setq test-cockpit--project-engines nil)
(mocker-let
((projectile-project-type () ((:output 'elixir))))
(let ((infix (test-cockpit-infix)))
(let ((infix (test-cockpit--infix)))
(should
(and (equal (aref infix 0) "Mix specific switches")
(equal (aref infix 1) '("-r" "Reset Ecto before test" "reset"))
Expand Down
2 changes: 1 addition & 1 deletion test/test-python.el-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ async def test_first_outer():

(ert-deftest test-python-python-infix ()
(mocker-let ((projectile-project-type () ((:output 'python-pip))))
(let ((infix (test-cockpit-infix)))
(let ((infix (test-cockpit--infix)))
(should (equal (aref (aref infix 0) 0) "Switches"))
(should (equal (car (aref (aref infix 0) 1)) "-k"))
(should (equal (aref (aref infix 0) 2) '("-f" "only lastly failed tests" "--last-failed")))
Expand Down

0 comments on commit 22466aa

Please sign in to comment.