* tests/build-utils.scm (call-after-patch-shebang): New procedure.
("patch-shebang: with simple shebang")
("patch-shebang: with \"env CMD\" form")
("patch-shebang: with \"env -S CMD\" form")
("patch-shebang: handle whitespace")
("patch-shebang: fail when no binary found")
("patch-shebang: fail with \"env -S CMD\" form when \"env\" not found")
("patch-shebang: fail with \"env -S CMD\" form when CMD not found"): New tests.

Change-Id: I11b7289513b762b030abdceb9a0bbe8a700ec242
---
 tests/build-utils.scm | 90 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)

diff --git a/tests/build-utils.scm b/tests/build-utils.scm
index 3babf5d544..8bdbc5008b 100644
--- a/tests/build-utils.scm
+++ b/tests/build-utils.scm
@@ -4,6 +4,7 @@
 ;;; Copyright © 2021 Maxim Cournoyer <maxim.courno...@gmail.com>
 ;;; Copyright © 2021 Maxime Devos <maximede...@telenet.be>
 ;;; Copyright © 2021 Brendan Tildesley <m...@brendan.scot>
+;;; Copyright © 2025 aurtzy <aur...@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -28,6 +29,7 @@ (define-module (test build-utils)
                 #:select (%current-system call-with-temporary-directory))
   #:use-module (gnu packages)
   #:use-module (gnu packages bootstrap)
+  #:use-module (srfi srfi-26)
   #:use-module (srfi srfi-34)
   #:use-module (srfi srfi-35)
   #:use-module (srfi srfi-64)
@@ -333,4 +335,92 @@ (define (arg-test bash-args)
           ("guile/bin" . ,(dirname (which "guile"))))
         "guile"))))
 
+(define (call-after-patch-shebang initial-contents bin-names proc)
+  "Set up a test environment with BIN-NAMES as \"binary files\" to include in
+path (exclusively) and run patch-shebang on a mock script with 
INITIAL-CONTENTS.
+Then, call PROC with the script's contents (re-read), the binaries directory,
+and the return value of patch-shebang as arguments."
+  (define (touch file)
+    (call-with-output-file file (const #t)))
+
+  (call-with-temporary-directory
+   (lambda (directory)
+     (let ((bin-path (string-append directory "/bin"))
+           (script-file (string-append directory "/test.sh")))
+       (mkdir bin-path)
+       (for-each (lambda (bin-name)
+                   (touch (string-append bin-path "/" bin-name)))
+                 bin-names)
+
+       (call-with-output-file script-file
+         (cut display initial-contents <>))
+
+       (let* ((success? (patch-shebang script-file (list bin-path)))
+              (contents (call-with-input-file script-file get-string-all)))
+         (format #t "Contents after patch-shebang:\n~a\n" contents)
+         (format #t "patch-shebang return: ~a\n" success?)
+         (proc contents bin-path success?))))))
+
+(test-assert "patch-shebang: with simple shebang"
+  (call-after-patch-shebang
+   "#!/bin/binary1\n"
+   '("binary1")
+   (lambda (contents bin-path success?)
+     (and (equal? (string-append "#!" bin-path "/binary1\n")
+                  contents)
+          success?))))
+
+(test-assert "patch-shebang: with \"env CMD\" form"
+  (call-after-patch-shebang
+   "#!/example/path/to/env test-command\n"
+   '("env" "test-command")
+   (lambda (contents bin-path success?)
+     (and (equal? (string-append "#!" bin-path "/test-command\n")
+                  contents)
+          success?))))
+
+(test-assert "patch-shebang: with \"env -S CMD\" form"
+  (call-after-patch-shebang
+   "#!/usr/bin/env -S test-command arg1 arg2\n"
+   '("env" "test-command")
+   (lambda (contents bin-path success?)
+     (and (equal? (string-append
+                   "#!" bin-path "/env -S " bin-path "/test-command arg1 
arg2\n")
+                  contents)
+          success?))))
+
+(test-assert "patch-shebang: handle whitespace"
+  (call-after-patch-shebang
+   "#! \t /usr/bin/env \t -S \t test-command \t arg\n"
+   '("env" "test-command")
+   (lambda (contents bin-path success?)
+     (and (equal? (string-append
+                   "#!" bin-path "/env -S " bin-path "/test-command arg\n")
+                  contents)
+          success?))))
+
+(test-assert "patch-shebang: fail when no binary found"
+  (call-after-patch-shebang
+   "#!/nonexistent/test-command\n"
+   '()
+   (lambda (contents bin-path success?)
+     (and (equal? "#!/nonexistent/test-command\n" contents)
+          (not success?)))))
+
+(test-assert "patch-shebang: fail with \"env -S CMD\" form when \"env\" not 
found"
+  (call-after-patch-shebang
+   "#!/usr/bin/env -S test-command arg\n"
+   '("test-command")
+   (lambda (contents bin-path success?)
+     (and (equal? "#!/usr/bin/env -S test-command arg\n" contents)
+          (not success?)))))
+
+(test-assert "patch-shebang: fail with \"env -S CMD\" form when CMD not found"
+  (call-after-patch-shebang
+   "#!/usr/bin/env -S test-command arg\n"
+   '("env")
+   (lambda (contents bin-path success?)
+     (and (equal? "#!/usr/bin/env -S test-command arg\n" contents)
+          (not success?)))))
+
 (test-end)
-- 
2.49.0




Reply via email to