From 9392d4a2d94bf2097d82b0f1c6b3f62ec85ff0aa Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Wed, 17 Dec 2025 22:10:32 +0000 Subject: [PATCH] fixed tempfile bleed through --- core/TESTS/test_caves.py | 6 +++--- core/TESTS/test_drawings.py | 29 +++++++++++++---------------- parsers/drawings.py | 5 +++-- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/core/TESTS/test_caves.py b/core/TESTS/test_caves.py index fb1d6d5..61a3baa 100644 --- a/core/TESTS/test_caves.py +++ b/core/TESTS/test_caves.py @@ -228,9 +228,9 @@ class FixturePageTests(TestCase): content = response.content.decode() ph = r"Seetrichter" ph_alt = r"1623-284" - phmatch = re.search(ph, content) or re.search(ph_alt, content) - with open('_cave_caves284.html', 'w') as f: - f.write(content) + # phmatch = re.search(ph, content) or re.search(ph_alt, content) + # with open('_cave_caves284.html', 'w') as f: + # f.write(content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "' or '" + ph_alt + "'") # Although the Cave object exists, it looks like we get a bad slug error when trying to get a QM page. diff --git a/core/TESTS/test_drawings.py b/core/TESTS/test_drawings.py index 693a59e..107d934 100644 --- a/core/TESTS/test_drawings.py +++ b/core/TESTS/test_drawings.py @@ -1,6 +1,7 @@ import os import pathlib import tempfile +from unittest.mock import patch from django.test import TestCase @@ -27,10 +28,10 @@ class DrawingsPathlibTests(TestCase): (sub2 / 'abc.th2').write_text('th2') (sub2 / 'abc.th').write_text('th') - # point the module at our tempdir - settings.DRAWINGS_DATA = td - - drawings.load_drawings_files() + # point the module at our tempdir using a temporary setting on the + # local settings module (the parsers import `settings` directly) + with patch.object(settings, "DRAWINGS_DATA", td): + drawings.load_drawings_files() # all files should be present self.assertTrue(DrawingFile.objects.filter(dwgpath='one.pdf').exists()) @@ -44,9 +45,8 @@ class DrawingsPathlibTests(TestCase): p = pathlib.Path(td) (p / '.hidden').write_text('hid') (p / 'file~').write_text('bak') - settings.DRAWINGS_DATA = td - - drawings.load_drawings_files() + with patch.object(settings, "DRAWINGS_DATA", td): + drawings.load_drawings_files() # Should not import hidden or backup files self.assertFalse(DrawingFile.objects.filter(dwgpath='.hidden').exists()) @@ -56,9 +56,8 @@ class DrawingsPathlibTests(TestCase): with tempfile.TemporaryDirectory() as td: p = pathlib.Path(td) (p / 'noext').write_text('data') - settings.DRAWINGS_DATA = td - - drawings.load_drawings_files() + with patch.object(settings, "DRAWINGS_DATA", td): + drawings.load_drawings_files() self.assertTrue(DrawingFile.objects.filter(dwgpath='noext').exists()) @@ -68,9 +67,8 @@ class DrawingsPathlibTests(TestCase): g = p / '.git' g.mkdir() (g / 'secret.txt').write_text('top secret') - settings.DRAWINGS_DATA = td - - drawings.load_drawings_files() + with patch.object(settings, "DRAWINGS_DATA", td): + drawings.load_drawings_files() self.assertFalse(DrawingFile.objects.filter(dwgpath='.git/secret.txt').exists()) @@ -81,9 +79,8 @@ class DrawingsPathlibTests(TestCase): p = pathlib.Path(td) for i in range(count): (p / f'file{i}.txt').write_text('x') - settings.DRAWINGS_DATA = td - - drawings.load_drawings_files() + with patch.object(settings, "DRAWINGS_DATA", td): + drawings.load_drawings_files() self.assertEqual(DrawingFile.objects.count(), count) diff --git a/parsers/drawings.py b/parsers/drawings.py index c9600a2..1ea7df9 100644 --- a/parsers/drawings.py +++ b/parsers/drawings.py @@ -256,9 +256,10 @@ def _handle_obsolete_wallets(old_wallet, dwgfile, scanfilename, parser_label): if w_renamed := _find_renamed_x_wallet(old_wallet, scanfilename, parser_label): return w_renamed - message = f" - Warning {parser_label} XFILES {old_wallet} {(old_wallet==x.group(1))} in {path}" + # 'path' is not in scope here; use the drawing file's path for messages + message = f" - Warning {parser_label} XFILES {old_wallet} {(old_wallet==x.group(1))} in {dwgfile.dwgpath}" print(message) - DataIssue.objects.update_or_create(parser=parser_label, message=message, url=f"/dwgdataraw/{path}") + DataIssue.objects.update_or_create(parser=parser_label, message=message, url=f"/dwgdataraw/{dwgfile.dwgpath}") return False if old_wallet in old_wallets: