summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrendan Tildesley <mail@brendan.scot>2021-04-29 20:33:08 +1000
committerMaxim Cournoyer <maxim.cournoyer@gmail.com>2022-01-10 11:44:29 -0500
commit7f8a896c5f3ecde60a19f244d5a407ec1033a08d (patch)
treef0964c46b308d6ac9ca4fcc4e60c07ff0b731e97
parent41ec0573b81733e15b70312d81cf7fdf77dd63fe (diff)
utils: Fix wrap-script argument handling.
* guix/build/utils.scm (wrap-script): Don't add (car cl) one too many times, cl its self contains it's car. Split the aguments string with string-tokenize to avoid leaving an empty string argument when there should be none. These two bugs seemed to be partially cancelling each other out so that scripts still worked when ran with no arguments. * tests/build-utils.scm: Adjust wrap-script to above changes. Add two tests to ensure the command line arguments appear identical to a script and its wrapped version. Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com>
-rw-r--r--guix/build/utils.scm8
-rw-r--r--tests/build-utils.scm57
2 files changed, 55 insertions, 10 deletions
diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index 3beb7da67a2..dd5a91f52f8 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -8,6 +8,7 @@
8;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il> 8;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
9;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com> 9;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
10;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be> 10;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
11;;; Copyright © 2021 Brendan Tildesley <mail@brendan.scot>
11;;; 12;;;
12;;; This file is part of GNU Guix. 13;;; This file is part of GNU Guix.
13;;; 14;;;
@@ -1462,10 +1463,9 @@ not supported."
1462 `(let ((cl (command-line))) 1463 `(let ((cl (command-line)))
1463 (apply execl ,interpreter 1464 (apply execl ,interpreter
1464 (car cl) 1465 (car cl)
1465 (cons (car cl) 1466 (append
1466 (append 1467 ',(string-tokenize args char-set:graphic)
1467 ',(string-split args #\space) 1468 cl)))))
1468 cl))))))
1469 (template (string-append prog ".XXXXXX")) 1469 (template (string-append prog ".XXXXXX"))
1470 (out (mkstemp! template)) 1470 (out (mkstemp! template))
1471 (st (stat prog)) 1471 (st (stat prog))
diff --git a/tests/build-utils.scm b/tests/build-utils.scm
index 6b131c0af85..7f4f12ccc7d 100644
--- a/tests/build-utils.scm
+++ b/tests/build-utils.scm
@@ -3,6 +3,7 @@
3;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net> 3;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
4;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com> 4;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
5;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be> 5;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
6;;; Copyright © 2021 Brendan Tildesley <mail@brendan.scot>
6;;; 7;;;
7;;; This file is part of GNU Guix. 8;;; This file is part of GNU Guix.
8;;; 9;;;
@@ -167,9 +168,7 @@ echo hello world"))
167 "/some/path:/some/other/path")))) 168 "/some/path:/some/other/path"))))
168 '(let ((cl (command-line))) 169 '(let ((cl (command-line)))
169 (apply execl "/anything/cabbage-bash-1.2.3/bin/sh" 170 (apply execl "/anything/cabbage-bash-1.2.3/bin/sh"
170 (car cl) 171 (car cl) (append (quote ()) cl))))
171 (cons (car cl)
172 (append '("") cl)))))
173 script-contents) 172 script-contents)
174 (call-with-temporary-directory 173 (call-with-temporary-directory
175 (lambda (directory) 174 (lambda (directory)
@@ -208,8 +207,7 @@ print('hello world')"))
208 `(let ((cl (command-line))) 207 `(let ((cl (command-line)))
209 (apply execl "/anything/cabbage-bash-1.2.3/bin/python3" 208 (apply execl "/anything/cabbage-bash-1.2.3/bin/python3"
210 (car cl) 209 (car cl)
211 (cons (car cl) 210 (append '("-and" "-args") cl))))
212 (append '("" "-and" "-args") cl)))))
213 script-contents) 211 script-contents)
214 (call-with-temporary-directory 212 (call-with-temporary-directory
215 (lambda (directory) 213 (lambda (directory)
@@ -243,6 +241,54 @@ print('hello world')"))
243 "/some/other/path"))) 241 "/some/other/path")))
244 #f))))) 242 #f)))))
245 243
244(define (arg-test bash-args)
245 (call-with-temporary-directory
246 (lambda (directory)
247 (let ((script-file-name (string-append directory "/bash-test.sh")))
248 (call-with-output-file script-file-name
249 (lambda (port)
250 (display (string-append "\
251#!" (which "bash") bash-args "
252echo \"$#$0$*${A}\"")
253 port)))
254
255 (display "Unwrapped script contents:\n")
256 (call-with-input-file script-file-name
257 (lambda (port) (display (get-string-all port))))
258 (newline) (newline)
259 (chmod script-file-name #o777)
260 (setenv "A" "A")
261 (let* ((run-script (lambda _
262 (open-pipe*
263 OPEN_READ
264 script-file-name "1" "2" "3 3" "4")))
265 (pipe (run-script))
266 (unwrapped-output (get-string-all pipe)))
267 (close-pipe pipe)
268
269 (wrap-script script-file-name `("A" = ("A\nA")))
270
271 (display "Wrapped script contents:\n")
272 (call-with-input-file script-file-name
273 (lambda (port) (display (get-string-all port))))
274 (newline) (newline)
275
276 (let* ((pipe (run-script))
277 (wrapped-output (get-string-all pipe)))
278 (close-pipe pipe)
279 (display "./bash-test.sh 1 2 3\\ 3 4 # Output:\n")
280 (display unwrapped-output) (newline)
281 (display "./bash-test.sh 1 2 3\\ 3 4 # Output (wrapped):\n")
282 (display wrapped-output) (newline)
283 (string=? (string-append unwrapped-output "A\n")
284 wrapped-output)))))))
285
286(test-assert "wrap-script, argument handling"
287 (arg-test ""))
288
289(test-assert "wrap-script, argument handling, bash --norc"
290 (arg-test " --norc"))
291
246(test-equal "substitute*, text contains a NUL byte, UTF-8" 292(test-equal "substitute*, text contains a NUL byte, UTF-8"
247 "c\0d" 293 "c\0d"
248 (with-fluids ((%default-port-encoding "UTF-8") 294 (with-fluids ((%default-port-encoding "UTF-8")
@@ -287,5 +333,4 @@ print('hello world')"))
287 ("guile/bin" . ,(dirname (which "guile")))) 333 ("guile/bin" . ,(dirname (which "guile"))))
288 "guile")))) 334 "guile"))))
289 335
290
291(test-end) 336(test-end)