SF.net SVN: docutils:[9787 ] trunk/docutils/tools

aa-turner--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9787
          http://sourceforge.net/p/docutils/code/9787
Author:   aa-turner
Date:     2024-07-31 03:13:34 +0000 (Wed, 31 Jul 2024)
Log Message:
-----------
Fix test_buildhtml on Windows

When the temporary path is on a different drive 
to the current working directory, the test fails.
Fix by using ``docutils.utils.relative_path()``

Modified Paths:
--------------
    trunk/docutils/tools/buildhtml.py
    trunk/docutils/tools/test/test_buildhtml.py

Modified: trunk/docutils/tools/buildhtml.py
===================================================================
--- trunk/docutils/tools/buildhtml.py	2024-07-31 02:27:55 UTC (rev 9786)
+++ trunk/docutils/tools/buildhtml.py	2024-07-31 03:13:34 UTC (rev 9787)
@@ -30,6 +30,7 @@
 import docutils.io
 from docutils import core, frontend, ApplicationError
 from docutils.parsers import rst
+from docutils.utils import relative_path
 from docutils.readers import standalone, pep
 from docutils.writers import html4css1, html5_polyglot, pep_html
 
@@ -248,13 +249,13 @@
         errout = docutils.io.ErrorOutput(encoding=settings.error_encoding)
         if match_patterns(dirpath, settings.prune):
             errout.write('/// ...Skipping directory (pruned): %s\n'
-                         % os.path.relpath(dirpath))
+                         % relative_path(None, dirpath))
             sys.stderr.flush()
             del dirnames[:]  # modify in-place to control `os.walk()` run
             return
         if not self.initial_settings.silent:
             errout.write('/// Processing directory: %s\n'
-                         % os.path.relpath(dirpath))
+                         % relative_path(None, dirpath))
             sys.stderr.flush()
         for name in sorted(filenames):
             if match_patterns(name, settings.ignore):

Modified: trunk/docutils/tools/test/test_buildhtml.py
===================================================================
--- trunk/docutils/tools/test/test_buildhtml.py	2024-07-31 02:27:55 UTC (rev 9786)
+++ trunk/docutils/tools/test/test_buildhtml.py	2024-07-31 03:13:34 UTC (rev 9787)
@@ -22,30 +22,31 @@
                         "--quiet".
 """
 
-import unittest
-import os
-from subprocess import Popen, PIPE, STDOUT
+import shutil
+import subprocess
 import sys
 import tempfile
+import unittest
+from pathlib import Path
 
+# TOOLS_ROOT is ./tools/ from the docutils root
+TOOLS_ROOT = Path(__file__).resolve().parent.parent
+BUILDHTML_PATH = TOOLS_ROOT / 'buildhtml.py'
 
-buildhtml_path = os.path.abspath(os.path.join(
-                    os.path.dirname(__file__) or os.curdir,
-                    '..', 'buildhtml.py'))
 
-
-def process_and_return_filelist(options):
+def process_and_return_filelist(options: list[str]) -> tuple[list[str], list[str]]:
     dirs = []
     files = []
-    p = Popen([sys.executable, buildhtml_path] + options,
-              stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
-    (cin, cout) = (p.stdin, p.stdout)
-    while True:
-        line = cout.readline()
-        if not line:
-            break
-        # in Py 3x, cout.readline() returns `bytes` and the processing fails
-        line = line.decode('ascii', 'replace')
+    ret = subprocess.run(
+        [sys.executable, BUILDHTML_PATH] + options,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.STDOUT,
+        text=True,
+        encoding='utf-8',
+        errors='replace',
+    )
+    print(ret.stdout)
+    for line in ret.stdout.splitlines():
         # BUG no colon in filename/path allowed
         item = line.split(": ")[-1].strip()
         if line.startswith(" "):
@@ -52,55 +53,39 @@
             files.append(item)
         else:
             dirs.append(item)
-    cin.close()
-    cout.close()
-    p.wait()
     return dirs, files
 
 
 class BuildHtmlTests(unittest.TestCase):
-    tree = ("_tmp_test_tree",
-            "_tmp_test_tree/one.txt",
-            "_tmp_test_tree/two.txt",
-            "_tmp_test_tree/dir1",
-            "_tmp_test_tree/dir1/one.txt",
-            "_tmp_test_tree/dir1/two.txt",
-            "_tmp_test_tree/dir2",
-            "_tmp_test_tree/dir2/one.txt",
-            "_tmp_test_tree/dir2/two.txt",
-            "_tmp_test_tree/dir2/sub",
-            "_tmp_test_tree/dir2/sub/one.txt",
-            "_tmp_test_tree/dir2/sub/two.txt",
-            )
+    tree = (
+        "_tmp_test_tree/one.txt",
+        "_tmp_test_tree/two.txt",
+        "_tmp_test_tree/dir1/one.txt",
+        "_tmp_test_tree/dir1/two.txt",
+        "_tmp_test_tree/dir2/one.txt",
+        "_tmp_test_tree/dir2/two.txt",
+        "_tmp_test_tree/dir2/sub/one.txt",
+        "_tmp_test_tree/dir2/sub/two.txt",
+    )
 
     def setUp(self):
-        self.root = tempfile.mkdtemp()
+        self.root = Path(tempfile.mkdtemp()).resolve()
 
-        for s in self.tree:
-            s = os.path.join(self.root, s)
-            if "." not in s:
-                os.mkdir(s)
-            else:
-                fd_s = open(s, "w", encoding='utf-8')
-                fd_s.write("dummy")
-                fd_s.close()
+        for file in self.tree:
+            path = self.root / file
+            path.parent.mkdir(parents=True, exist_ok=True)
+            path.write_text('dummy', encoding='utf-8')
 
     def tearDown(self):
-        for i in range(len(self.tree) - 1, -1, -1):
-            s = os.path.join(self.root, self.tree[i])
-            if "." not in s:
-                os.rmdir(s)
-            else:
-                os.remove(s)
-        os.rmdir(self.root)
+        shutil.rmtree(self.root)
 
     def test_1(self):
-        opts = ["--dry-run", self.root]
+        opts = ["--dry-run", str(self.root)]
         dirs, files = process_and_return_filelist(opts)
         self.assertEqual(files.count("one.txt"), 4)
 
     def test_local(self):
-        opts = ["--dry-run", "--local", self.root]
+        opts = ["--dry-run", "--local", str(self.root)]
         dirs, files = process_and_return_filelist(opts)
         self.assertEqual(len(dirs), 1)
         self.assertEqual(files, [])

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.