Skip to content

Commit

Permalink
Add exception handling to usage of tempfile.TemporaryDirectory() (#111)
Browse files Browse the repository at this point in the history
I couldn't replicate this locally at all (something specific to the runner?).

I eventually found this [pytest issue](pytest-dev/pytest#7491) that discusses
similar(ish) behaviors. This lead me to attempt to catch the PermissionError exception and essentially ignore it.

I'm not sure if this is a good approach, but it appears to resolve the issue.

Co-authored-by: Jacob Campbell <jacob.campbell@hca.wa.gov>
  • Loading branch information
Snotpus and Jacob Campbell committed May 11, 2024
1 parent 47700ed commit 1aed064
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
8 changes: 5 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

@pytest.fixture()
def tmpdir():
with tempfile.TemporaryDirectory() as d:
yield Path(d)

try:
with tempfile.TemporaryDirectory() as d:
yield Path(d)
except PermissionError:
pass

@pytest.fixture()
def basic() -> dict:
Expand Down
55 changes: 29 additions & 26 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import List, Union, Tuple
from unittest.mock import DEFAULT


import openpyxl

from rumydata import Layout, CsvFile, ExcelFile
Expand All @@ -25,32 +26,34 @@ def func(*args):
def file_row_harness(row: List[Union[str, int]], layout: dict):
""" Write row to file for testing in ingest """
lay = Layout(layout, no_header=True)

with tempfile.TemporaryDirectory() as d:
csv_p = Path(d, 'file_test.csv')

with csv_p.open('w', newline='') as f:
writer = csv.writer(f)
writer.writerow(row)

xl_p = Path(d, 'excel_test.xlsx')
wb = openpyxl.Workbook()
sheet = wb['Sheet']
for ix, value in enumerate(row, start=1):
sheet.cell(row=1, column=ix).value = value
wb.save(filename=xl_p)

to_check = [
('CsvFile', CsvFile(lay), csv_p),
('ExcelFile', ExcelFile(lay), xl_p)
]
aes = {}
for nm, obj, p in to_check:
try:
assert not obj.check(p)
except AssertionError as e:
aes[nm] = e
new_line = '\n'
try:
with tempfile.TemporaryDirectory() as d:
csv_p = Path(d, 'file_test.csv')

with csv_p.open('w', newline='') as f:
writer = csv.writer(f)
writer.writerow(row)

xl_p = Path(d, 'excel_test.xlsx')
wb = openpyxl.Workbook()
sheet = wb['Sheet']
for ix, value in enumerate(row, start=1):
sheet.cell(row=1, column=ix).value = value
wb.save(filename=xl_p)

to_check = [
('CsvFile', CsvFile(lay), csv_p),
('ExcelFile', ExcelFile(lay), xl_p)
]
aes = {}
for nm, obj, p in to_check:
try:
assert not obj.check(p)
except AssertionError as e:
aes[nm] = e
new_line = '\n'
assert not aes, f'Write test failed for:\n {new_line.join([f"{k}:{v}" for k, v in aes.items()])}'
except PermissionError:
assert not aes, f'Write test failed for:\n {new_line.join([f"{k}:{v}" for k, v in aes.items()])}'


Expand Down

0 comments on commit 1aed064

Please sign in to comment.