unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Maxim Cournoyer <maxim.cournoyer@gmail.com>
To: 66046@debbugs.gnu.org
Cc: Timothy Sample <samplet@ngyro.com>,
	Amirouche <amirouche@hyper.dev>,
	Maxim Cournoyer <maxim.cournoyer@gmail.com>,
	Daphne Preston-Kendal <dpk@nonceword.org>
Subject: bug#66046: [PATCH v4 2/3] tests: Add new compile-file tests.
Date: Sat, 14 Sep 2024 10:34:28 +0900	[thread overview]
Message-ID: <20240914013501.6445-2-maxim.cournoyer@gmail.com> (raw)
In-Reply-To: <20240914013501.6445-1-maxim.cournoyer@gmail.com>

Add a test for bug #66046.

To run just the compiler tests:

  ./meta/guile -L test-suite -L . test-suite/tests/compiler.test

* test-suite/tests/compiler.test (with-temporary-directory): New syntax.
(delete-file-recursively): New procedure.
("compile-file: relative include works")
("compile-file: relative include works with load path
canonicalization"): New tests.
---

(no changes since v1)

 test-suite/tests/compiler.test | 84 ++++++++++++++++++++++++++++++++--
 1 file changed, 81 insertions(+), 3 deletions(-)

diff --git a/test-suite/tests/compiler.test b/test-suite/tests/compiler.test
index 0b47d0e32..5cb7a8ef6 100644
--- a/test-suite/tests/compiler.test
+++ b/test-suite/tests/compiler.test
@@ -1,6 +1,6 @@
 ;;;; compiler.test --- tests for the compiler      -*- scheme -*-
-;;;; Copyright (C) 2008-2014, 2018, 2021-2022, 2024 Free Software Foundation, Inc.
-;;;; 
+;;;; Copyright (C) 2008-2014, 2018, 2021-2024 Free Software Foundation, Inc.
+;;;;
 ;;;; This library is free software; you can redistribute it and/or
 ;;;; modify it under the terms of the GNU Lesser General Public
 ;;;; License as published by the Free Software Foundation; either
@@ -18,15 +18,50 @@
 (define-module (tests compiler)
   #:use-module (test-suite lib)
   #:use-module (test-suite guile-test)
+  #:use-module (ice-9 ftw)
   #:use-module (system base compile)
   #:use-module ((language tree-il)
                 #:select (tree-il-src call-args))
   #:use-module ((system vm loader) #:select (load-thunk-from-memory))
-  #:use-module ((system vm program) #:select (program-sources source:addr)))
+  #:use-module ((system vm program) #:select (program-sources source:addr))
+  #:use-module (srfi srfi-26))
 
 (define read-and-compile
   (@@ (system base compile) read-and-compile))
 
+;;; Based on 'with-directory-excursion', from (guix build utils).
+(define-syntax-rule (with-temporary-directory body ...)
+  "Run BODY with DIR as the process's current directory."
+  (let ((init (getcwd))
+        (dir (mkdtemp "tempdir.XXXXXX")))
+   (dynamic-wind
+     (lambda ()
+       (chdir dir))
+     (lambda ()
+       body ...)
+     (lambda ()
+       (chdir init)
+       (delete-file-recursively dir)))))
+
+;;; XXX: Adapted from (guix build utils).
+(define* (delete-file-recursively dir)
+  "Delete DIR recursively, like `rm -rf', without following symlinks."
+  (file-system-fold (const #t)          ;enter
+                    (lambda (file stat result)   ; leaf
+                      (delete-file file))
+                    (const #t)                   ; down
+                    (lambda (dir stat result)    ; up
+                      (rmdir dir))
+                    (const #t)                   ; skip
+                    (lambda (file stat errno result)
+                      (format (current-error-port)
+                              "warning: failed to delete ~a: ~a~%"
+                              file (strerror errno)))
+                    #t
+                    dir
+
+                    ;; Don't follow symlinks.
+                    lstat))
 
 \f
 (with-test-prefix "basic"
@@ -441,3 +476,46 @@
         (set! proc ((load-thunk-from-memory bytecode)))
         (procedure? proc)))
     (pass-if-equal "proc executes" 42 (proc))))
+
+(with-test-prefix "compile-file"
+  ;; Setup test library sources in a temporary directory.
+  (let ((hello-sexp '(define-library (hello)
+                       (import (scheme base)
+                               (scheme write))
+                       (export hello)
+                       (include "hello/hello-impl.scm")))
+        (hello-impl-sexp '(begin
+                            (include "../external/nothing.scm")
+                            (include "body.scm")))
+        (hello-body-sexp '(define (hello)
+                            (display "hello!\n"))))
+    (with-temporary-directory
+     (mkdir "module")
+     (call-with-output-file "module/hello.scm"
+       (cut write hello-sexp <>))
+     (mkdir "module/hello")
+     (call-with-output-file "module/hello/hello-impl.scm"
+       (cut write hello-impl-sexp <>))
+     (call-with-output-file "module/hello/body.scm"
+       (cut write hello-body-sexp <>))
+     (mkdir "module/external")
+     (call-with-output-file "module/external/nothing.scm" (const #t))
+     (mkdir "build")
+     (chdir "build")
+
+     (pass-if "relative include works"
+       (compile-file "../module/hello.scm" #:output-file "hello.go")
+       #t)
+
+     ;; This used to fail, because compile-file's #:canonicalization
+     ;; defaults to 'relative, which causes 'scm_relativize_path' to
+     ;; strip the prefix not in the load path, to avoid baking an
+     ;; invalid source file reference in the byte compiled output file
+     ;; (see: https://bugs.gnu.org/66046).  This was fixed by having a
+     ;; '%file-port-stripped-prefixes' fluid to preserve the stripped
+     ;; prefix, to be used by 'include' to reconstruct the original
+     ;; complete relative file name.
+     (pass-if "relative include works with load path canonicalization"
+       (add-to-load-path (string-append (getcwd) "/../module"))
+       (compile-file "../module/hello.scm" #:output-file "hello.go")
+       #t))))
-- 
2.46.0






  reply	other threads:[~2024-09-14  1:34 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-17  8:22 bug#66046: Relative includes in R7RS define-library seem broken Daphne Preston-Kendal
2023-11-06 18:31 ` Timothy Sample
2023-11-06 18:48   ` Maxim Cournoyer
2023-11-06 19:57     ` Maxim Cournoyer
2023-11-07  4:42     ` Maxim Cournoyer
2023-11-10  3:36       ` bug#66046: [PATCH 1/2] tests: Add new compile-file tests Maxim Cournoyer
2023-11-10  3:36         ` bug#66046: [PATCH 2/2] ice-9: Fix 'include' when used in compilation contexts Maxim Cournoyer
2023-11-11 11:58 ` bug#66046: Relative includes in R7RS define-library seem broken Amirouche
2023-11-14 13:57   ` Maxim Cournoyer
2023-11-18 22:56     ` Maxim Cournoyer
2023-11-22 16:11 ` bug#66046: [PATCH v2 1/3] libguile/fports.c: Remove extraneous include Maxim Cournoyer
2023-11-22 16:11   ` bug#66046: [PATCH v2 2/3] tests: Add new compile-file tests Maxim Cournoyer
2023-11-22 16:11   ` bug#66046: [PATCH v2 3/3] ice-9: Fix 'include' when used in compilation contexts Maxim Cournoyer
2023-11-22 16:17 ` bug#66046: [PATCH v3 1/3] libguile/fports.c: Remove extraneous include Maxim Cournoyer
2023-11-22 16:17   ` bug#66046: [PATCH v3 2/3] tests: Add new compile-file tests Maxim Cournoyer
2023-11-22 16:17   ` bug#66046: [PATCH v3 3/3] ice-9: Fix 'include' when used in compilation contexts Maxim Cournoyer
2024-09-14  1:34 ` bug#66046: [PATCH v4 1/3] libguile/fports.c: Remove extraneous include Maxim Cournoyer
2024-09-14  1:34   ` Maxim Cournoyer [this message]
2024-09-14  1:34   ` bug#66046: [PATCH v4 3/3] ice-9: Fix 'include' when used in compilation contexts Maxim Cournoyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240914013501.6445-2-maxim.cournoyer@gmail.com \
    --to=maxim.cournoyer@gmail.com \
    --cc=66046@debbugs.gnu.org \
    --cc=amirouche@hyper.dev \
    --cc=dpk@nonceword.org \
    --cc=samplet@ngyro.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).