From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
From: Maxim Cournoyer <maxim.cournoyer@gmail.com>
Newsgroups: gmane.lisp.guile.devel
Subject: [PATCH v3 2/6] tests: Add new compile-file tests.
Date: Sat, 18 Nov 2023 01:05:34 -0500
Message-ID: <20231118060621.24675-2-maxim.cournoyer@gmail.com>
References: <20231118060621.24675-1-maxim.cournoyer@gmail.com>
Mime-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214";
	logging-data="30910"; mail-complaints-to="usenet@ciao.gmane.io"
Cc: Maxim Cournoyer <maxim.cournoyer@gmail.com>
To: guile-devel@gnu.org
Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Sat Nov 18 07:07:06 2023
Return-path: <guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org>
Envelope-to: guile-devel@m.gmane-mx.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.92)
	(envelope-from <guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org>)
	id 1r4ETx-0007sr-CY
	for guile-devel@m.gmane-mx.org; Sat, 18 Nov 2023 07:07:05 +0100
Original-Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <guile-devel-bounces@gnu.org>)
	id 1r4ETR-0006yB-Ja; Sat, 18 Nov 2023 01:06:33 -0500
Original-Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <maxim.cournoyer@gmail.com>)
 id 1r4ETP-0006xX-V5
 for guile-devel@gnu.org; Sat, 18 Nov 2023 01:06:31 -0500
Original-Received: from mail-qv1-xf35.google.com ([2607:f8b0:4864:20::f35])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <maxim.cournoyer@gmail.com>)
 id 1r4ETN-00005w-NT
 for guile-devel@gnu.org; Sat, 18 Nov 2023 01:06:31 -0500
Original-Received: by mail-qv1-xf35.google.com with SMTP id
 6a1803df08f44-6707401e22eso1287256d6.2
 for <guile-devel@gnu.org>; Fri, 17 Nov 2023 22:06:29 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1700287588; x=1700892388; darn=gnu.org;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:from:to:cc:subject:date
 :message-id:reply-to;
 bh=Ot9KKkRpbWcNN/Dvl+qhCzPkbcohxfrnTX+B64frd58=;
 b=KByapFaZ6lFplj+cKvXVNjXFPMbiGWTNgkaBD+jOeuIttr0GmoA2sZjzzgcDKmfG63
 XcxdQ6b11dxxuW5OwWEkel3pvoWYEbFamGNyp71e0zZ5CIDC5ZeXqcJJ/TO5zoOEr3Hu
 rZhrk51WZsOQPsmgTwG4RdmQJ9KW34RD3RcKFXpRcLmUMNfSP64gwbq6OF2lzpH8TvV+
 dMcNeE1G64nil/563vQVgKNpkBs3SaixNbgGSTcE/lcSzRzdJ4YdVuFSfeV/GhQl7rdK
 KT1Oow07z30BFb9qvCxTx7XKEFwJ0xVZYt688KQUjYRKev/N/6ZUwaWuYAVRGiaymOsu
 xURA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1700287588; x=1700892388;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=Ot9KKkRpbWcNN/Dvl+qhCzPkbcohxfrnTX+B64frd58=;
 b=coIxwwR5EfTQPGLamP8AC8BbZy27sAQmuxHxL+KySZ7Dn4DMAeX8A2sRXyYH8zEIBZ
 IQRbBtIA9lFVVcCtrrs2jFzHhL25tidGYf49uWwtHPxn7odwDgyM4IZeU4ClDLhN9Dfy
 /uVwnAYzKkSnsUeYpVWG2+UFRF1ZFQJDD1tA+x7js+oBWamQgky8mrH6xBCqtRdHNB1S
 vrOB+ANuoXB+xmDOZ7DB4iHgHch2oclFQ1vr1nY4R0uYIimOlWYq3LCBAEd/fW5f70zA
 F14CvU5BOT4NBW75uEKSDUoS7DAswn38bRltsbOfI+5vIpsTjMiJxw+goRU8jZd1NeOP
 2iSg==
X-Gm-Message-State: AOJu0YyzQxisa18QfLHCLKy+kqyNiZH/o3M0t6YedOxjcVpz4u83wWDr
 95AQEuXrRIjlVBPzjg19eeT4qMgY1Uw=
X-Google-Smtp-Source: AGHT+IGJ+1P4cizVhUgOllKNRVqxmwNNouwbaPUwL0Hgab4gsrA0PWG7X/+UPI8oJzToP8SvRUJWAw==
X-Received: by 2002:a05:6214:1305:b0:66f:bc3f:bd7 with SMTP id
 pn5-20020a056214130500b0066fbc3f0bd7mr1577964qvb.27.1700287588073; 
 Fri, 17 Nov 2023 22:06:28 -0800 (PST)
Original-Received: from localhost.localdomain (dsl-154-55.b2b2c.ca. [66.158.154.55])
 by smtp.gmail.com with ESMTPSA id
 m9-20020a0ce6e9000000b00677a12f11bcsm1179262qvn.24.2023.11.17.22.06.27
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 17 Nov 2023 22:06:27 -0800 (PST)
X-Mailer: git-send-email 2.41.0
In-Reply-To: <20231118060621.24675-1-maxim.cournoyer@gmail.com>
Received-SPF: pass client-ip=2607:f8b0:4864:20::f35;
 envelope-from=maxim.cournoyer@gmail.com; helo=mail-qv1-xf35.google.com
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
 T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: guile-devel@gnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: "Developers list for Guile,
 the GNU extensibility library" <guile-devel.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/guile-devel>,
 <mailto:guile-devel-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/guile-devel>
List-Post: <mailto:guile-devel@gnu.org>
List-Help: <mailto:guile-devel-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/guile-devel>,
 <mailto:guile-devel-request@gnu.org?subject=subscribe>
Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org
Original-Sender: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org
Xref: news.gmane.io gmane.lisp.guile.devel:22086
Archived-At: <http://permalink.gmane.org/gmane.lisp.guile.devel/22086>

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 | 75 +++++++++++++++++++++++++++++++++-
 1 file changed, 74 insertions(+), 1 deletion(-)

diff --git a/test-suite/tests/compiler.test b/test-suite/tests/compiler.test
index a018e0c41..2026d5ff3 100644
--- a/test-suite/tests/compiler.test
+++ b/test-suite/tests/compiler.test
@@ -1,5 +1,5 @@
 ;;;; compiler.test --- tests for the compiler      -*- scheme -*-
-;;;; Copyright (C) 2008-2014, 2018, 2021-2022 Free Software Foundation, Inc.
+;;;; Copyright (C) 2008-2014, 2018, 2021-2023 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
@@ -18,6 +18,7 @@
 (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))
@@ -27,6 +28,39 @@
 (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))
 
 
 (with-test-prefix "basic"
@@ -434,3 +468,42 @@
         (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 ((top-sexp '(define-library (hello)
+                     (import (scheme base)
+                             (scheme write))
+                     (export hello)
+                     (include "hello/hello-impl.scm")))
+        (included-sexp '(define (hello)
+                          (display "hello!\n"))))
+    (with-temporary-directory
+     (mkdir "module")
+     (call-with-output-file "module/hello.scm"
+       (lambda (port)
+         (write top-sexp port)))
+     (mkdir "module/hello")
+     (call-with-output-file "module/hello/hello-impl.scm"
+       (lambda (port)
+         (write included-sexp port)))
+     (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 caused '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
+     ;; 'compilation-source-file' fluid that preserves the file name
+     ;; passed to 'compile-file', used by 'include' instead of the file
+     ;; name of the port.
+     (pass-if "relative include works with load path canonicalization"
+       (begin
+         (add-to-load-path (string-append (getcwd) "/../module"))
+         (compile-file "../module/hello.scm" #:output-file "hello.go")
+         #t)))))
-- 
2.41.0