Turn context into a dataclass and add new test case

This commit is contained in:
R. Richard 2019-06-06 09:58:18 +02:00
parent b44fd200c6
commit 8cbe737d2f

View File

@ -1,5 +1,6 @@
# pylint: disable=redefined-outer-name
from dataclasses import dataclass
from secrets import token_urlsafe
from os.path import join
from os import chdir, mkdir, symlink
@ -10,11 +11,10 @@ import pytest
from file_manager import FileManager
@dataclass
class ManagerContext:
def __init__(self, folder, manager):
self.folder = folder
self.manager = manager
folder: str
manager: FileManager
def join(self, path):
return join(self.folder, path)
@ -22,20 +22,24 @@ class ManagerContext:
@pytest.fixture()
def context():
dirs = []
dirs = {}
with TemporaryDirectory() as workdir:
chdir(workdir)
for name in ["allowed", "excluded", "invis"]:
for name in ['allowed', 'excluded', 'invis']:
node = join(workdir, name)
mkdir(node)
Path(join(node, 'empty.txt')).touch()
Path(join(node, 'empty.bin')).touch()
dirs.append(node)
dirs[name] = node
yield ManagerContext(
workdir,
FileManager(dirs[0], dirs[:-1], exclude=['*/excluded/*'])
FileManager(
dirs['allowed'],
[dirs['allowed'], dirs['excluded']],
exclude=['*/excluded/*']
)
)
@pytest.mark.parametrize('subdir', ['allowed/', 'excluded/'])
@ -99,5 +103,22 @@ def test_read_write_file(context):
content = token_urlsafe(32)
context.manager.file_contents = content
assert context.manager.file_contents == content
with open(context.join('allowed/empty.txt'), "r") as ifile:
with open(context.join('allowed/empty.txt'), 'r') as ifile:
assert ifile.read() == content
def test_regular_ide_actions(context):
context.manager.workdir = context.join('allowed')
newfile1, newfile2 = token_urlsafe(16), token_urlsafe(16)
Path(context.join(f'allowed/{newfile1}')).touch()
Path(context.join(f'allowed/{newfile2}')).touch()
for _ in range(8):
context.manager.filename = newfile1
content1 = token_urlsafe(32)
context.manager.file_contents = content1
context.manager.filename = newfile2
content2 = token_urlsafe(32)
context.manager.file_contents = content2
context.manager.filename = newfile1
assert context.manager.file_contents == content1
context.manager.filename = newfile2
assert context.manager.file_contents == content2