1"""Provide base classes for the test system"""
2from unittest import TestCase
3import os
4import tempfile
5
6__all__ = ['TestBase', 'FileCreator']
7
8
9#{ Utilities
10
11class FileCreator(object):
12
13    """A instance which creates a temporary file with a prefix and a given size
14    and provides this info to the user.
15    Once it gets deleted, it will remove the temporary file as well."""
16    __slots__ = ("_size", "_path")
17
18    def __init__(self, size, prefix=''):
19        assert size, "Require size to be larger 0"
20
21        self._path = tempfile.mktemp(prefix=prefix)
22        self._size = size
23
24        with open(self._path, "wb") as fp:
25            fp.seek(size - 1)
26            fp.write(b'1')
27
28        assert os.path.getsize(self.path) == size
29
30    def __del__(self):
31        try:
32            os.remove(self.path)
33        except OSError:
34            pass
35        # END exception handling
36
37    def __enter__(self):
38        return self
39
40    def __exit__(self, exc_type, exc_value, traceback):
41        self.__del__()
42
43    @property
44    def path(self):
45        return self._path
46
47    @property
48    def size(self):
49        return self._size
50
51#} END utilities
52
53
54class TestBase(TestCase):
55
56    """Foundation used by all tests"""
57
58    #{ Configuration
59    k_window_test_size = 1000 * 1000 * 8 + 5195
60    #} END configuration
61
62    #{ Overrides
63    @classmethod
64    def setUpAll(cls):
65        # nothing for now
66        pass
67
68    # END overrides
69
70    #{ Interface
71
72    #} END interface
73