Hi Bruno,

On 5/13/24 8:47 PM, Bruno Haible wrote:
>> Perhaps both implementations should quickly exit in this situation, come 
>> to think of it.
>
> I agree. I've made the same mistake a number of times. There is no valid use
> for continuing if $destdir already exists, since the call is guaranteed to
> overwrite configure.ac, Makefile.am, and other files.

I've applied the attached patch. I can do the same in gnulib-tool.sh
if you would like since it should be pretty easy there too.


  $ gnulib-tool --create-testdir --dir foo -h stdbit 
  [...]
  executing automake --add-missing --copy
  parallel-tests: installing '../build-aux/test-driver'
  patching file build-aux/test-driver
  $ gnulib-tool --create-testdir --dir foo -h stdbit 
  not overwriting destination directory: foo
  /home/collin/.local/src/gnulib/gnulib-tool.py: *** Stop.

Likewise for --create-megatestdir.

Collin
From 719ce7d39e58b9311b51ac93d828fb26c2bcfbaf Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Mon, 13 May 2024 21:36:25 -0700
Subject: [PATCH] gnulib-tool.py: Don't continue creating testdirs when destdir
 exists.

* pygnulib/GLError.py (GLError.__init__, GLError.__repr__): Add a new
error number for destination directories that already exist.
* pygnulib/main.py (main_with_exception_handling): Print the message.
* pygnulib/GLTestDir.py (GLTestDir.__init__, GLMegaTestdir.__init__):
Fail if the destination directory exists instead of creating files and
failing to patch test driver.
---
 ChangeLog             | 10 ++++++++++
 pygnulib/GLError.py   |  3 +++
 pygnulib/GLTestDir.py | 26 ++++++++++++++++----------
 pygnulib/main.py      |  2 ++
 4 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 327ea5273b..23fd42e6e0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2024-05-13  Collin Funk  <collin.fu...@gmail.com>
+
+	gnulib-tool.py: Don't continue creating testdirs when destdir exists.
+	* pygnulib/GLError.py (GLError.__init__, GLError.__repr__): Add a new
+	error number for destination directories that already exist.
+	* pygnulib/main.py (main_with_exception_handling): Print the message.
+	* pygnulib/GLTestDir.py (GLTestDir.__init__, GLMegaTestdir.__init__):
+	Fail if the destination directory exists instead of creating files and
+	failing to patch test driver.
+
 2024-05-13  Paul Eggert  <egg...@cs.ucla.edu>
 
 	stdbit: redo clzll without lookcup table
diff --git a/pygnulib/GLError.py b/pygnulib/GLError.py
index 4288820c97..a6d7d8437d 100644
--- a/pygnulib/GLError.py
+++ b/pygnulib/GLError.py
@@ -55,6 +55,7 @@ def __init__(self, errno: int, errinfo: Any = None) -> None:
          19: could not create destination directory: <directory>
          20: could not patch test-driver script
          21: Option --automake-subdir is only supported if the definition of AUTOMAKE_OPTIONS in Makefile.am contains 'subdir-objects'.
+         22: not overwriting destination directory: <directory>
         errinfo: additional information'''
         self.errno = errno
         self.errinfo = errinfo
@@ -107,5 +108,7 @@ def __repr__(self) -> str:
                 message = ('Option --automake-subdir/--automake-subdir-tests are only '
                            'supported if the definition of AUTOMAKE_OPTIONS in '
                            'Makefile.am contains \'subdir-objects\'.')
+            elif errno == 22:
+                message = 'not overwriting destination directory: %s' % repr(errinfo)
             self.message = '[Errno %d] %s' % (errno, message)
         return self.message
diff --git a/pygnulib/GLTestDir.py b/pygnulib/GLTestDir.py
index d3b819bd0d..ad4549f734 100644
--- a/pygnulib/GLTestDir.py
+++ b/pygnulib/GLTestDir.py
@@ -107,11 +107,14 @@ def __init__(self, config: GLConfig, testdir: str) -> None:
                             % type(testdir).__name__)
         self.config = config
         self.testdir = os.path.normpath(testdir)
-        if not os.path.exists(self.testdir):
-            try:  # Try to create directory
-                os.mkdir(self.testdir)
-            except Exception as exc:
-                raise GLError(19, self.testdir) from exc
+        # Don't overwrite the directory.
+        if os.path.exists(self.testdir):
+            raise GLError(22, self.testdir)
+        # Try to create directory.
+        try:
+            os.mkdir(self.testdir)
+        except Exception as exc:
+            raise GLError(19, self.testdir) from exc
         self.emitter = GLEmiter(self.config)
         self.filesystem = GLFileSystem(self.config)
         self.modulesystem = GLModuleSystem(self.config)
@@ -863,11 +866,14 @@ def __init__(self, config: GLConfig, megatestdir: str) -> None:
                             % type(megatestdir).__name__)
         self.config = config
         self.megatestdir = os.path.normpath(megatestdir)
-        if not os.path.exists(self.megatestdir):
-            try:  # Try to create directory
-                os.mkdir(self.megatestdir)
-            except Exception as exc:
-                raise GLError(19, self.megatestdir) from exc
+        # Don't overwrite the directory.
+        if os.path.exists(self.megatestdir):
+            raise GLError(22, self.megatestdir)
+        # Try to create directory.
+        try:
+            os.mkdir(self.megatestdir)
+        except Exception as exc:
+            raise GLError(19, self.megatestdir) from exc
         self.modulesystem = GLModuleSystem(self.config)
 
     def execute(self) -> None:
diff --git a/pygnulib/main.py b/pygnulib/main.py
index 585f7df43f..aeb78ad2c7 100644
--- a/pygnulib/main.py
+++ b/pygnulib/main.py
@@ -1438,6 +1438,8 @@ def main_with_exception_handling() -> None:
                 message += ('Option --automake-subdir/--automake-subdir-tests are only '
                             'supported if the definition of AUTOMAKE_OPTIONS in '
                             'Makefile.am contains \'subdir-objects\'.')
+            elif errno == 22:
+                message = 'not overwriting destination directory: %s' % errinfo
             message += '\n%s: *** Stop.\n' % APP['name']
             sys.stderr.write(message)
             sys.exit(1)
-- 
2.45.0

Reply via email to