Skip to content

Commit 92bba89

Browse files
authored
Merge pull request #142 from thawn/copilot/improve-pdf-checking-workflow
Fix race condition in PDF generation workflow test
2 parents 46c48fc + 51bd6ae commit 92bba89

File tree

3 files changed

+46
-41
lines changed

3 files changed

+46
-41
lines changed

src/ttmp32gme/print_handler.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,20 +281,10 @@ def create_pdf(
281281
return None
282282

283283
# Create PDF in a temporary file
284-
# Check if E2E test wants to override the temp directory
285-
test_temp_dir = os.environ.get("TTMP32GME_TEST_TEMP_DIR")
286-
if test_temp_dir:
287-
# For E2E testing: create PDF in specified directory
288-
pdf_file = Path(test_temp_dir) / "print.pdf"
289-
# Create empty file for chromium to write to
290-
pdf_file.touch()
291-
temp_fd = None
292-
else:
293-
# Normal operation: create PDF in system temp directory
294-
temp_fd, temp_path = tempfile.mkstemp(suffix=".pdf", prefix="ttmp32gme_print_")
295-
pdf_file = Path(temp_path)
296-
# Close the file descriptor as chromium will write to the file
297-
os.close(temp_fd)
284+
temp_fd, temp_path = tempfile.mkstemp(suffix=".pdf", prefix="ttmp32gme_print_")
285+
pdf_file = Path(temp_path)
286+
# Close the file descriptor as chromium will write to the file
287+
os.close(temp_fd)
298288

299289
args = [
300290
chromium_path,

tests/e2e/conftest.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,30 @@ def chrome_options():
4949

5050

5151
@pytest.fixture
52-
def driver(chrome_options):
53-
"""Create a Chrome WebDriver instance."""
52+
def driver(chrome_options, tmp_path):
53+
"""Create a Chrome WebDriver instance with download directory configured."""
5454
from selenium import webdriver
5555

56+
# Create a temporary download directory for this test session
57+
download_dir = tmp_path / "downloads"
58+
download_dir.mkdir(parents=True, exist_ok=True)
59+
60+
# Configure Chrome to download PDFs automatically to the download directory
61+
prefs = {
62+
"download.default_directory": str(download_dir),
63+
"download.prompt_for_download": False,
64+
"download.directory_upgrade": True,
65+
"plugins.always_open_pdf_externally": True, # Download PDFs instead of opening in viewer
66+
"profile.default_content_settings.popups": 0,
67+
}
68+
chrome_options.add_experimental_option("prefs", prefs)
69+
5670
driver = webdriver.Chrome(options=chrome_options)
5771
driver.implicitly_wait(10)
5872

73+
# Store download directory on driver for tests to access
74+
driver.download_dir = download_dir
75+
5976
yield driver
6077

6178
driver.quit()
@@ -229,9 +246,6 @@ def clean_server(tmp_path, driver, monkeypatch):
229246
230247
This fixture creates temporary database and library paths, starts a server with
231248
those paths, and cleans up everything after the test completes.
232-
233-
Also mocks tempfile.mkstemp to create PDF files in the test library folder
234-
so E2E tests can verify PDF creation.
235249
"""
236250
import os
237251
import subprocess
@@ -247,11 +261,6 @@ def clean_server(tmp_path, driver, monkeypatch):
247261
test_port = 10021
248262
test_host = "127.0.0.1"
249263

250-
# Set environment variable to tell the server to create PDFs in the test library
251-
# This allows E2E tests to verify PDF creation
252-
# (Monkeypatch doesn't work because server runs in a separate process)
253-
os.environ["TTMP32GME_TEST_TEMP_DIR"] = str(test_library)
254-
255264
# Start server with custom paths in background
256265
server_cmd = [
257266
"python",

tests/e2e/test_comprehensive.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,27 +1095,26 @@ def test_pdf_generation_workflow(self, driver, base_config_with_album, caplog):
10951095
pdf_save_button.click()
10961096
logger.info("Clicked PDF save button")
10971097

1098-
# Step 7: Wait for PDF generation
1099-
# The PDF is now created in a temporary file, but we mock tempfile.mkstemp
1100-
# to create it in the library folder so we can check for it
1101-
library_path = server_info["library_path"]
1102-
pdf_file = library_path / "print.pdf"
1098+
# Step 7: Wait for PDF to be downloaded by the browser
1099+
# Check the browser's download directory for the PDF file
1100+
download_dir = driver.download_dir
1101+
expected_pdf = download_dir / "print.pdf"
11031102

1104-
pdf_created = False
1103+
pdf_downloaded = False
11051104
max_wait = 30 # seconds
11061105
start_time = time.time()
11071106

11081107
while time.time() - start_time < max_wait:
1109-
if pdf_file.exists() and pdf_file.stat().st_size > 0:
1110-
pdf_created = True
1111-
logger.info(f"PDF file created at {pdf_file}")
1108+
if expected_pdf.exists() and expected_pdf.stat().st_size > 0:
1109+
pdf_downloaded = True
1110+
logger.info(f"PDF file downloaded to {expected_pdf}")
11121111
break
11131112
time.sleep(1)
11141113

11151114
# Read and log the server output for debugging
1116-
if not pdf_created:
1115+
if not pdf_downloaded:
11171116
logger.error("=" * 80)
1118-
logger.error("PDF WAS NOT CREATED - DUMPING SERVER LOGS")
1117+
logger.error("PDF WAS NOT DOWNLOADED - DUMPING SERVER LOGS")
11191118
logger.error("=" * 80)
11201119

11211120
if server_info.get("log_file") and server_info["log_file"].exists():
@@ -1130,16 +1129,23 @@ def test_pdf_generation_workflow(self, driver, base_config_with_album, caplog):
11301129
else:
11311130
logger.error("No server stderr log file found")
11321131

1132+
# Also check what files exist in the download directory
1133+
if download_dir.exists():
1134+
files_in_download_dir = list(download_dir.iterdir())
1135+
logger.error(
1136+
f"Files in download directory: {files_in_download_dir}"
1137+
)
1138+
else:
1139+
logger.error("Download directory does not exist")
1140+
11331141
logger.error("=" * 80)
11341142

11351143
assert (
1136-
pdf_created
1137-
), f"PDF file not created within {max_wait} seconds at {pdf_file}"
1144+
pdf_downloaded
1145+
), f"PDF file not downloaded within {max_wait} seconds to {expected_pdf}"
11381146

1139-
# The PDF should have been downloaded to the browser's download location
1140-
# In a real test, we'd check the browser's download directory
1141-
# For CI, we just verify the file was created and is valid
1142-
logger.info("PDF generation workflow completed successfully")
1147+
# Verify the downloaded PDF has valid content
1148+
logger.info("PDF generation and download workflow completed successfully")
11431149

11441150
except NoSuchElementException:
11451151
# If chromium is not available, the PDF save button won't be present

0 commit comments

Comments
 (0)