Skip to content

Commit 854c007

Browse files
committed
Refactor to use only a haskell-process-wrapper-function.
Following this discussion - #370 (comment). This permits to one's own wrapper function. For example, here is one to use with nix-shell: ```sh (custom-set-variables '(haskell-process-wrapper-function (lambda (argv) (append (list "nix-shell" "default.nix" "--command" ) (list (shell-quote-argument (mapconcat 'identity argv " "))))))) ``` Unit tests are ok. Need to test in real life now.
1 parent e838266 commit 854c007

File tree

2 files changed

+50
-76
lines changed

2 files changed

+50
-76
lines changed

haskell-process.el

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,15 @@ See `haskell-process-do-cabal' for more details."
111111
:type '(choice (const auto) (const ghci) (const cabal-repl) (const cabal-dev) (const cabal-ghci))
112112
:group 'haskell-interactive)
113113

114-
(defcustom haskell-process-wrapper
115-
nil
116-
"A wrapper to launch the Haskell process defined by `haskell-process-type`.
117-
Nix users may want to use the value (\"nix-shell\" \"--command\"),
118-
Docker users may want to use something like \"run-my-docker\"."
119-
:group 'haskell-interactive
120-
:type '(choice string (repeat string)))
114+
(defcustom haskell-process-wrapper-function
115+
#'identity
116+
"A default wrapper function to deal with an eventual haskell-process-wrapper.
121117
122-
(defun haskell-process-stringify-cmd (cmd &optional args)
123-
"Stringify the CMD with optional ARGS."
124-
(format "%s" (mapconcat 'identity (cons cmd args) " ")))
125-
126-
(defun haskell-process-wrapper-command (cmd &optional cmd-args)
127-
"Compute the haskell command to execute to launch the haskell-process type.
128-
if haskell-process-wrapper is set, return a wrapper of the CMD as list.
129-
Otherwise, return CMD as list.
130-
Deal with optional CMD-ARGS for the CMD."
131-
(if haskell-process-wrapper
132-
(let ((wrapped-cmd (haskell-process-stringify-cmd cmd cmd-args)))
133-
(if (stringp haskell-process-wrapper)
134-
(list haskell-process-wrapper wrapped-cmd)
135-
(append haskell-process-wrapper (list wrapped-cmd))))
136-
(cons cmd cmd-args)))
118+
If no wrapper is needed, then using 'identify function is sufficient.
119+
Otherwise, define a function which takes a list of arguments.
120+
For example: (lambda (argv)
121+
(append '(\"nix-shell\" \"haskell-lab.nix\" \"--command\")
122+
(shell-quote-argument argv)))")
137123

138124
(defcustom haskell-process-log
139125
nil
@@ -1044,25 +1030,25 @@ HPTYPE is the result of calling `'haskell-process-type`' function."
10441030
(append (list (format "Starting inferior GHCi process %s ..." haskell-process-path-ghci)
10451031
session-name
10461032
nil)
1047-
(haskell-process-wrapper-command haskell-process-path-ghci haskell-process-args-ghci)))
1033+
(apply haskell-process-wrapper-function (list (cons haskell-process-path-ghci haskell-process-args-ghci)))))
10481034
('cabal-repl
10491035
(append (list (format "Starting inferior `cabal repl' process using %s ..." haskell-process-path-cabal)
10501036
session-name
10511037
nil)
1052-
(haskell-process-wrapper-command haskell-process-path-cabal (cons "repl" haskell-process-args-cabal-repl))
1038+
(apply haskell-process-wrapper-function (list (cons haskell-process-path-cabal (cons "repl" haskell-process-args-cabal-repl))))
10531039
(let ((target (haskell-session-target session)))
10541040
(if target (list target) nil))))
10551041
('cabal-ghci
10561042
(append (list (format "Starting inferior cabal-ghci process using %s ..." haskell-process-path-cabal-ghci)
10571043
session-name
10581044
nil)
1059-
(haskell-process-wrapper-command haskell-process-path-cabal-ghci)))
1045+
(apply haskell-process-wrapper-function (list (list haskell-process-path-cabal-ghci)))))
10601046
('cabal-dev
10611047
(let ((dir (concat (haskell-session-cabal-dir session) "/cabal-dev")))
10621048
(append (list (format "Starting inferior cabal-dev process %s -s %s ..." haskell-process-path-cabal-dev dir)
10631049
session-name
10641050
nil)
1065-
(haskell-process-wrapper-command haskell-process-path-cabal-dev (list "ghci" "-s" dir))))))))
1051+
(apply haskell-process-wrapper-function (list (cons haskell-process-path-cabal-dev (list "ghci" "-s" dir))))))))))
10661052

10671053
;;;###autoload
10681054
(defun haskell-process-start (session)

tests/haskell-process-tests.el

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,102 +14,90 @@
1414

1515
(require 'el-mock)
1616

17-
(ert-deftest haskell-process-stringify-cmd-no-arg ()
18-
"No wrapper, return directly the command (between string quote)."
19-
(should (equal "run"
20-
(let ((haskell-process-wrapper nil))
21-
(haskell-process-stringify-cmd "run")))))
22-
23-
(ert-deftest haskell-process-stringify-cmd-with-args ()
24-
"No wrapper, return directly the command."
25-
(should (equal "run -a b -c d"
26-
(let ((haskell-process-wrapper nil))
27-
(haskell-process-stringify-cmd "run" '("-a" "b" "-c" "d"))))))
28-
29-
(ert-deftest haskell-process-wrapper-command-nil ()
17+
(ert-deftest haskell-process-wrapper-command-function-identity ()
3018
"No wrapper, return directly the command."
3119
(should (equal '("ghci")
32-
(let ((haskell-process-wrapper nil))
33-
(haskell-process-wrapper-command "ghci")))))
34-
35-
(ert-deftest haskell-process-wrapper-command-with-string ()
36-
"Wrapper as a string, return the wrapping command as a string."
37-
(should (equal '("nix-shell" "cabal run")
38-
(let ((haskell-process-wrapper "nix-shell"))
39-
(haskell-process-wrapper-command "cabal run")))))
20+
(progn
21+
(custom-set-variables '(haskell-process-wrapper-function #'identity))
22+
(apply haskell-process-wrapper-function (list '("ghci")))))))
4023

41-
(ert-deftest haskell-process-wrapper-command-with-string-2 ()
24+
(ert-deftest haskell-process-wrapper-function-non-identity ()
4225
"Wrapper as a string, return the wrapping command as a string."
43-
(should (equal '("nix-shell" "cabal repl")
44-
(let ((haskell-process-wrapper "nix-shell"))
45-
(haskell-process-wrapper-command "cabal" '("repl"))))))
46-
47-
(ert-deftest haskell-process-wrapper-command-with-repeat-string ()
48-
"Wrapper as a list of string, return the wrapping command as a string."
49-
(should (equal '("nix-shell" "default.nix" "--command" "cabal build")
50-
(let ((haskell-process-wrapper '("nix-shell" "default.nix" "--command")))
51-
(haskell-process-wrapper-command "cabal" '("build"))))))
26+
(should (equal '("nix-shell" "default.nix" "--command" "cabal\\ run")
27+
(progn
28+
(custom-set-variables '(haskell-process-wrapper-function (lambda (argv)
29+
(append '("nix-shell" "default.nix" "--command")
30+
(list (shell-quote-argument argv))))))
31+
(apply haskell-process-wrapper-function (list "cabal run"))))))
5232

5333
(ert-deftest test-haskell-process--compute-process-log-and-command-ghci ()
5434
(should (equal '("Starting inferior GHCi process ghci ..." "dumses1" nil "ghci" "-ferror-spans")
5535
(let ((haskell-process-path-ghci "ghci")
56-
(haskell-process-args-ghci '("-ferror-spans"))
57-
(haskell-process-wrapper nil))
36+
(haskell-process-args-ghci '("-ferror-spans")))
37+
(custom-set-variables '(haskell-process-wrapper-function #'identity))
5838
(mocklet (((haskell-session-name "dummy-session") => "dumses1"))
5939
(haskell-process-compute-process-log-and-command "dummy-session" 'ghci))))))
6040

6141
(ert-deftest test-haskell-process--with-wrapper-compute-process-log-and-command-ghci ()
62-
(should (equal '("Starting inferior GHCi process ghci ..." "dumses1" nil "nix-shell" "default.nix" "--command" "ghci -ferror-spans")
42+
(should (equal '("Starting inferior GHCi process ghci ..." "dumses1" nil "nix-shell" "default.nix" "--command" "ghci\\ -ferror-spans")
6343
(let ((haskell-process-path-ghci "ghci")
64-
(haskell-process-args-ghci '("-ferror-spans"))
65-
(haskell-process-wrapper '("nix-shell" "default.nix" "--command")))
44+
(haskell-process-args-ghci '("-ferror-spans")))
45+
(custom-set-variables '(haskell-process-wrapper-function
46+
(lambda (argv) (append (list "nix-shell" "default.nix" "--command" )
47+
(list (shell-quote-argument (mapconcat 'identity argv " ")))))))
6648
(mocklet (((haskell-session-name "dummy-session") => "dumses1"))
6749
(haskell-process-compute-process-log-and-command "dummy-session" 'ghci))))))
6850

6951
(ert-deftest test-haskell-process--compute-process-log-and-command-cabal-repl ()
7052
(should (equal '("Starting inferior `cabal repl' process using cabal ..." "dumses2" nil "cabal" "repl" "--ghc-option=-ferror-spans" "dumdum-session")
7153
(let ((haskell-process-path-cabal "cabal")
72-
(haskell-process-args-cabal-repl '("--ghc-option=-ferror-spans"))
73-
(haskell-process-wrapper nil))
54+
(haskell-process-args-cabal-repl '("--ghc-option=-ferror-spans")))
55+
(custom-set-variables '(haskell-process-wrapper-function #'identity))
7456
(mocklet (((haskell-session-name "dummy-session2") => "dumses2")
7557
((haskell-session-target "dummy-session2") => "dumdum-session"))
7658
(haskell-process-compute-process-log-and-command "dummy-session2" 'cabal-repl))))))
7759

7860
(ert-deftest test-haskell-process--with-wrapper-compute-process-log-and-command-cabal-repl ()
79-
(should (equal '("Starting inferior `cabal repl' process using cabal ..." "dumses2" nil "nix-shell" "default.nix" "--command" "cabal repl --ghc-option=-ferror-spans" "dumdum-session")
61+
(should (equal '("Starting inferior `cabal repl' process using cabal ..." "dumses2" nil "nix-shell" "default.nix" "--command" "cabal\\ repl\\ --ghc-option\\=-ferror-spans" "dumdum-session")
8062
(let ((haskell-process-path-cabal "cabal")
81-
(haskell-process-args-cabal-repl '("--ghc-option=-ferror-spans"))
82-
(haskell-process-wrapper '("nix-shell" "default.nix" "--command")))
63+
(haskell-process-args-cabal-repl '("--ghc-option=-ferror-spans")))
64+
(custom-set-variables '(haskell-process-wrapper-function
65+
(lambda (argv) (append (list "nix-shell" "default.nix" "--command" )
66+
(list (shell-quote-argument (mapconcat 'identity argv " ")))))))
8367
(mocklet (((haskell-session-name "dummy-session2") => "dumses2")
8468
((haskell-session-target "dummy-session2") => "dumdum-session"))
8569
(haskell-process-compute-process-log-and-command "dummy-session2" 'cabal-repl))))))
8670

8771
(ert-deftest test-haskell-process--compute-process-log-and-command-cabal-ghci ()
8872
(should (equal '("Starting inferior cabal-ghci process using cabal-ghci ..." "dumses3" nil "cabal-ghci")
89-
(let ((haskell-process-path-ghci "ghci")
90-
(haskell-process-wrapper nil))
73+
(let ((haskell-process-path-ghci "ghci"))
74+
(custom-set-variables '(haskell-process-wrapper-function #'identity))
9175
(mocklet (((haskell-session-name "dummy-session3") => "dumses3"))
9276
(haskell-process-compute-process-log-and-command "dummy-session3" 'cabal-ghci))))))
9377

9478
(ert-deftest test-haskell-process--with-wrapper-compute-process-log-and-command-cabal-ghci ()
9579
(should (equal '("Starting inferior cabal-ghci process using cabal-ghci ..." "dumses3" nil "nix-shell" "default.nix" "--command" "cabal-ghci")
96-
(let ((haskell-process-path-ghci "ghci")
97-
(haskell-process-wrapper '("nix-shell" "default.nix" "--command")))
80+
(let ((haskell-process-path-ghci "ghci"))
81+
(custom-set-variables '(haskell-process-wrapper-function
82+
(lambda (argv) (append (list "nix-shell" "default.nix" "--command" )
83+
(list (shell-quote-argument (mapconcat 'identity argv " ")))))))
9884
(mocklet (((haskell-session-name "dummy-session3") => "dumses3"))
9985
(haskell-process-compute-process-log-and-command "dummy-session3" 'cabal-ghci))))))
10086

10187
(ert-deftest test-haskell-process--compute-process-log-and-command-cabal-dev ()
10288
(should (equal '("Starting inferior cabal-dev process cabal-dev -s directory/cabal-dev ..." "dumses4" nil "cabal-dev" "ghci" "-s" "directory/cabal-dev")
103-
(let ((haskell-process-path-cabal-dev "cabal-dev")
104-
(haskell-process-wrapper nil))
89+
(let ((haskell-process-path-cabal-dev "cabal-dev"))
90+
(custom-set-variables '(haskell-process-wrapper-function #'identity))
10591
(mocklet (((haskell-session-name "dummy-session4") => "dumses4")
10692
((haskell-session-cabal-dir "dummy-session4") => "directory"))
10793
(haskell-process-compute-process-log-and-command "dummy-session4" 'cabal-dev))))))
10894

10995
(ert-deftest test-haskell-process--with-wrapper-compute-process-log-and-command-cabal-dev ()
110-
(should (equal '("Starting inferior cabal-dev process cabal-dev -s directory/cabal-dev ..." "dumses4" nil "run-with-docker" "cabal-dev ghci -s directory/cabal-dev")
111-
(let ((haskell-process-path-cabal-dev "cabal-dev")
112-
(haskell-process-wrapper "run-with-docker"))
96+
(should (equal '("Starting inferior cabal-dev process cabal-dev -s directory/cabal-dev ..." "dumses4" nil "run-with-docker" "cabal-dev\\ ghci\\ -s\\ directory/cabal-dev")
97+
(let ((haskell-process-path-cabal-dev "cabal-dev"))
98+
(custom-set-variables '(haskell-process-wrapper-function
99+
(lambda (argv) (append (list "run-with-docker")
100+
(list (shell-quote-argument (mapconcat 'identity argv " ")))))))
113101
(mocklet (((haskell-session-name "dummy-session4") => "dumses4")
114102
((haskell-session-cabal-dir "dummy-session4") => "directory"))
115103
(haskell-process-compute-process-log-and-command "dummy-session4" 'cabal-dev))))))

0 commit comments

Comments
 (0)