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 # pylint: disable=redefined-outer-name
from dataclasses import dataclass
from secrets import token_urlsafe from secrets import token_urlsafe
from os.path import join from os.path import join
from os import chdir, mkdir, symlink from os import chdir, mkdir, symlink
@ -10,11 +11,10 @@ import pytest
from file_manager import FileManager from file_manager import FileManager
@dataclass
class ManagerContext: class ManagerContext:
def __init__(self, folder, manager): folder: str
self.folder = folder manager: FileManager
self.manager = manager
def join(self, path): def join(self, path):
return join(self.folder, path) return join(self.folder, path)
@ -22,20 +22,24 @@ class ManagerContext:
@pytest.fixture() @pytest.fixture()
def context(): def context():
dirs = [] dirs = {}
with TemporaryDirectory() as workdir: with TemporaryDirectory() as workdir:
chdir(workdir) chdir(workdir)
for name in ["allowed", "excluded", "invis"]: for name in ['allowed', 'excluded', 'invis']:
node = join(workdir, name) node = join(workdir, name)
mkdir(node) mkdir(node)
Path(join(node, 'empty.txt')).touch() Path(join(node, 'empty.txt')).touch()
Path(join(node, 'empty.bin')).touch() Path(join(node, 'empty.bin')).touch()
dirs.append(node) dirs[name] = node
yield ManagerContext( yield ManagerContext(
workdir, workdir,
FileManager(dirs[0], dirs[:-1], exclude=['*/excluded/*']) FileManager(
dirs['allowed'],
[dirs['allowed'], dirs['excluded']],
exclude=['*/excluded/*']
)
) )
@pytest.mark.parametrize('subdir', ['allowed/', 'excluded/']) @pytest.mark.parametrize('subdir', ['allowed/', 'excluded/'])
@ -99,5 +103,22 @@ def test_read_write_file(context):
content = token_urlsafe(32) content = token_urlsafe(32)
context.manager.file_contents = content context.manager.file_contents = content
assert 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 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