aboutsummaryrefslogtreecommitdiff
path: root/pyfakefs/tests/test_utils.py
blob: da52fbed53f6bc54ec6ec0e1cbc3451527f29270 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Copyright 2009 Google Inc. All Rights Reserved.
# Copyright 2015-2017 John McGehee
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Common helper classes used in tests, or as test class base."""

import errno
import os
import platform
import shutil
import stat
import sys
import tempfile
import unittest

from pyfakefs import fake_filesystem
from pyfakefs.helpers import is_byte_string, to_string


class DummyTime:
    """Mock replacement for time.time. Increases returned time on access."""

    def __init__(self, curr_time, increment):
        self.curr_time = curr_time
        self.increment = increment
        self.started = False

    def start(self):
        self.started = True

    def __call__(self, *args, **kwargs):
        if self.started:
            self.curr_time += self.increment
        return self.curr_time


class TestCase(unittest.TestCase):
    """Test base class with some convenience methods and attributes"""
    is_windows = sys.platform == 'win32'
    is_cygwin = sys.platform == 'cygwin'
    is_macos = sys.platform == 'darwin'
    symlinks_can_be_tested = None

    def assert_mode_equal(self, expected, actual):
        return self.assertEqual(stat.S_IMODE(expected), stat.S_IMODE(actual))

    def assert_raises_os_error(self, subtype, expression, *args, **kwargs):
        """Asserts that a specific subtype of OSError is raised."""
        try:
            expression(*args, **kwargs)
            self.fail('No exception was raised, OSError expected')
        except OSError as exc:
            self.assertEqual(subtype, exc.errno)

    def assert_equal_paths(self, actual, expected):
        if self.is_windows:
            self.assertEqual(actual.replace('\\\\?\\', ''),
                             expected.replace('\\\\?\\', ''))
        elif self.is_macos:
            self.assertEqual(actual.replace('/private/var/', '/var/'),
                             expected.replace('/private/var/', '/var/'))
        else:
            self.assertEqual(actual, expected)


class RealFsTestMixin:
    """Test mixin to allow tests to run both in the fake filesystem and in the
    real filesystem.
    To run tests in the real filesystem, a new test class can be derived from
    the test class testing the fake filesystem which overwrites
    `use_real_fs()` to return `True`.
    All tests in the real file system operate inside the local temp path.

    In order to make a test able to run in the real FS, it must not use the
    fake filesystem functions directly. For access to `os` and `open`,
    the respective attributes must be used, which point either to the native
    or to the fake modules. A few convenience methods allow to compose
    paths, create files and directories.
    """

    def __init__(self):
        self.filesystem = None
        self.open = open
        self.os = os
        self.base_path = None
        if self.use_real_fs():
            self.base_path = tempfile.mkdtemp()

    @property
    def is_windows_fs(self):
        return TestCase.is_windows

    def set_windows_fs(self, value):
        if self.filesystem is not None:
            self.filesystem.is_windows_fs = value
            if value:
                self.filesystem.is_macos = False
            self.create_basepath()

    @property
    def is_macos(self):
        return TestCase.is_macos

    @property
    def is_pypy(self):
        return platform.python_implementation() == 'PyPy'

    def use_real_fs(self):
        """Return True if the real file system shall be tested."""
        return False

    def path_separator(self):
        """Can be overwritten to use a specific separator in the
        fake filesystem."""
        if self.use_real_fs():
            return os.path.sep
        return '/'

    def check_windows_only(self):
        """If called at test start, the real FS test is executed only under
        Windows, and the fake filesystem test emulates a Windows system.
        """
        if self.use_real_fs():
            if not TestCase.is_windows:
                raise unittest.SkipTest(
                    'Testing Windows specific functionality')
        else:
            self.set_windows_fs(True)

    def check_linux_only(self):
        """If called at test start, the real FS test is executed only under
        Linux, and the fake filesystem test emulates a Linux system.
        """
        if self.use_real_fs():
            if TestCase.is_macos or TestCase.is_windows:
                raise unittest.SkipTest(
                    'Testing Linux specific functionality')
        else:
            self.set_windows_fs(False)
            self.filesystem.is_macos = False

    def check_macos_only(self):
        """If called at test start, the real FS test is executed only under
        MacOS, and the fake filesystem test emulates a MacOS system.
        """
        if self.use_real_fs():
            if not TestCase.is_macos:
                raise unittest.SkipTest(
                    'Testing MacOS specific functionality')
        else:
            self.set_windows_fs(False)
            self.filesystem.is_macos = True

    def check_linux_and_windows(self):
        """If called at test start, the real FS test is executed only under
        Linux and Windows, and the fake filesystem test emulates a Linux
        system under MacOS.
        """
        if self.use_real_fs():
            if TestCase.is_macos:
                raise unittest.SkipTest(
                    'Testing non-MacOs functionality')
        else:
            self.filesystem.is_macos = False

    def check_case_insensitive_fs(self):
        """If called at test start, the real FS test is executed only in a
        case-insensitive FS (e.g. Windows or MacOS), and the fake filesystem
        test emulates a case-insensitive FS under the running OS.
        """
        if self.use_real_fs():
            if not TestCase.is_macos and not TestCase.is_windows:
                raise unittest.SkipTest(
                    'Testing case insensitive specific functionality')
        else:
            self.filesystem.is_case_sensitive = False

    def check_case_sensitive_fs(self):
        """If called at test start, the real FS test is executed only in a
        case-sensitive FS (e.g. under Linux), and the fake file system test
        emulates a case-sensitive FS under the running OS.
        """
        if self.use_real_fs():
            if TestCase.is_macos or TestCase.is_windows:
                raise unittest.SkipTest(
                    'Testing case sensitive specific functionality')
        else:
            self.filesystem.is_case_sensitive = True

    def check_posix_only(self):
        """If called at test start, the real FS test is executed only under
        Linux and MacOS, and the fake filesystem test emulates a Linux
        system under Windows.
        """
        if self.use_real_fs():
            if TestCase.is_windows:
                raise unittest.SkipTest(
                    'Testing Posix specific functionality')
        else:
            self.set_windows_fs(False)

    def skip_real_fs(self):
        """If called at test start, no real FS test is executed."""
        if self.use_real_fs():
            raise unittest.SkipTest('Only tests fake FS')

    def skip_real_fs_failure(self, skip_windows=True, skip_posix=True,
                             skip_macos=True, skip_linux=True):
        """If called at test start, no real FS test is executed for the given
        conditions. This is used to mark tests that do not pass correctly under
        certain systems and shall eventually be fixed.
        """
        if True:
            if (self.use_real_fs() and
                    (TestCase.is_windows and skip_windows or
                     not TestCase.is_windows
                     and skip_macos and skip_linux or
                     TestCase.is_macos and skip_macos or
                     not TestCase.is_windows and
                     not TestCase.is_macos and skip_linux or
                     not TestCase.is_windows and skip_posix)):
                raise unittest.SkipTest(
                    'Skipping because FakeFS does not match real FS')

    def symlink_can_be_tested(self):
        """Used to check if symlinks and hard links can be tested under
        Windows. All tests are skipped under Windows for Python versions
        not supporting links, and real tests are skipped if running without
        administrator rights.
        """
        if not TestCase.is_windows or not self.use_real_fs():
            return True
        if TestCase.symlinks_can_be_tested is None:
            link_path = self.make_path('link')
            try:
                self.os.symlink(self.base_path, link_path)
                TestCase.symlinks_can_be_tested = True
                self.os.remove(link_path)
            except (OSError, NotImplementedError):
                TestCase.symlinks_can_be_tested = False
        return TestCase.symlinks_can_be_tested

    def skip_if_symlink_not_supported(self):
        """If called at test start, tests are skipped if symlinks are not
        supported."""
        if not self.symlink_can_be_tested():
            raise unittest.SkipTest(
                'Symlinks under Windows need admin privileges')

    def make_path(self, *args):
        """Create a path with the given component(s). A base path is prepended
        to the path which represents a temporary directory in the real FS,
        and a fixed path in the fake filesystem.
        Always use to compose absolute paths for tests also running in the
        real FS.
        """
        if isinstance(args[0], (list, tuple)):
            path = self.base_path
            for arg in args[0]:
                path = self.os.path.join(path, to_string(arg))
            return path
        args = [to_string(arg) for arg in args]
        return self.os.path.join(self.base_path, *args)

    def create_dir(self, dir_path):
        """Create the directory at `dir_path`, including subdirectories.
        `dir_path` shall be composed using `make_path()`.
        """
        existing_path = dir_path
        components = []
        while existing_path and not self.os.path.exists(existing_path):
            existing_path, component = self.os.path.split(existing_path)
            components.insert(0, component)
        for component in components:
            existing_path = self.os.path.join(existing_path, component)
            self.os.mkdir(existing_path)
            self.os.chmod(existing_path, 0o777)

    def create_file(self, file_path, contents=None, encoding=None):
        """Create the given file at `file_path` with optional contents,
        including subdirectories. `file_path` shall be composed using
        `make_path()`.
        """
        self.create_dir(self.os.path.dirname(file_path))
        mode = ('wb' if encoding is not None or is_byte_string(contents)
                else 'w')

        if encoding is not None and contents is not None:
            contents = contents.encode(encoding)
        with self.open(file_path, mode) as f:
            if contents is not None:
                f.write(contents)
        self.os.chmod(file_path, 0o666)

    def create_symlink(self, link_path, target_path):
        """Create the path at `link_path`, and a symlink to this path at
        `target_path`. `link_path` shall be composed using `make_path()`.
        """
        self.create_dir(self.os.path.dirname(link_path))
        self.os.symlink(target_path, link_path)

    def check_contents(self, file_path, contents):
        """Compare `contents` with the contents of the file at `file_path`.
        Asserts equality.
        """
        mode = 'rb' if is_byte_string(contents) else 'r'
        with self.open(file_path, mode) as f:
            self.assertEqual(contents, f.read())

    def not_dir_error(self):
        error = errno.ENOTDIR
        return error

    def create_basepath(self):
        """Create the path used as base path in `make_path`."""
        if self.filesystem is not None:
            old_base_path = self.base_path
            self.base_path = self.filesystem.path_separator + 'basepath'
            if self.is_windows_fs:
                self.base_path = 'C:' + self.base_path
            if old_base_path != self.base_path:
                if old_base_path is not None:
                    self.filesystem.reset()
                if not self.filesystem.exists(self.base_path):
                    self.filesystem.create_dir(self.base_path)
                if old_base_path is not None:
                    self.setUpFileSystem()


class RealFsTestCase(TestCase, RealFsTestMixin):
    """Can be used as base class for tests also running in the real
    file system."""

    def __init__(self, methodName='runTest'):
        TestCase.__init__(self, methodName)
        RealFsTestMixin.__init__(self)

    def setUp(self):
        self.cwd = os.getcwd()
        if not self.use_real_fs():
            self.filesystem = fake_filesystem.FakeFilesystem(
                path_separator=self.path_separator())
            self.open = fake_filesystem.FakeFileOpen(self.filesystem)
            self.os = fake_filesystem.FakeOsModule(self.filesystem)
            self.create_basepath()
        elif not os.environ.get('TEST_REAL_FS'):
            self.skip_real_fs()

        self.setUpFileSystem()

    def setUpFileSystem(self):
        pass

    @property
    def is_windows_fs(self):
        if self.use_real_fs():
            return self.is_windows
        return self.filesystem.is_windows_fs

    @property
    def is_macos(self):
        if self.use_real_fs():
            return TestCase.is_macos
        return self.filesystem.is_macos

    def tearDown(self):
        if self.use_real_fs():
            self.os.chdir(os.path.dirname(self.base_path))
            shutil.rmtree(self.base_path, ignore_errors=True)
            self.os.chdir(self.cwd)