-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconftest.py
More file actions
28 lines (24 loc) · 886 Bytes
/
conftest.py
File metadata and controls
28 lines (24 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import builtins
import io
import os
import pytest
def patch_open(open_func, files):
def open_patched(path, mode='r', buffering=-1, encoding=None,
errors=None, newline=None, closefd=True,
opener=None):
if 'w' in mode and not os.path.isfile(path):
files.append(path)
return open_func(path, mode=mode, buffering=buffering,
encoding=encoding, errors=errors,
newline=newline, closefd=closefd,
opener=opener)
return open_patched
@pytest.fixture(autouse=True)
def cleanup_files(monkeypatch):
files = []
monkeypatch.setattr(builtins, 'open', patch_open(builtins.open, files))
monkeypatch.setattr(io, 'open', patch_open(io.open, files))
yield
for f in files:
if type(f) is not int:
os.remove(f)