File: filesystem_mock.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 3,286 bytes parent folder | download | duplicates (6)
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
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import errno
import fnmatch
import os
import re
from io import StringIO


def _RaiseNotFound(path):
    raise IOError(errno.ENOENT, path, os.strerror(errno.ENOENT))


class MockFileSystem(object):
    """Stripped-down version of WebKit's webkitpy.common.system.filesystem_mock

    Implements a filesystem-like interface on top of a dict of filenames ->
    file contents. A file content value of None indicates that the file should
    not exist (IOError will be raised if it is opened;
    reading from a missing key raises a KeyError, not an IOError."""
    def __init__(self, files=None):
        self.files = files or {}
        self.written_files = {}
        self._sep = '/'

    @property
    def sep(self):
        return self._sep

    def abspath(self, path):
        if path.endswith(self.sep):
            return path[:-1]
        return path

    def basename(self, path):
        if self.sep not in path:
            return ''
        return self.split(path)[-1] or self.sep

    def dirname(self, path):
        if self.sep not in path:
            return ''
        return self.split(path)[0] or self.sep

    def exists(self, path):
        return self.isfile(path) or self.isdir(path)

    def isabs(self, path):
        return path.startswith(self.sep)

    def isfile(self, path):
        return path in self.files and self.files[path] is not None

    def isdir(self, path):
        if path in self.files:
            return False
        if not path.endswith(self.sep):
            path += self.sep

        # We need to use a copy of the keys here in order to avoid switching
        # to a different thread and potentially modifying the dict in
        # mid-iteration.
        files = list(self.files.keys())[:]
        return any(f.startswith(path) for f in files)

    def join(self, *comps):
        # TODO: Might want tests for this and/or a better comment about how
        # it works.
        return re.sub(re.escape(os.path.sep), self.sep, os.path.join(*comps))

    def glob(self, path):
        return fnmatch.filter(self.files.keys(), path)

    def open_for_reading(self, path):
        return StringIO(self.read_binary_file(path))

    def normpath(self, path):
        # This is not a complete implementation of normpath. Only covers what we
        # use in tests.
        result = []
        for part in path.split(self.sep):
            if part == '..':
                result.pop()
            elif part == '.':
                continue
            else:
                result.append(part)
        return self.sep.join(result)

    def read_binary_file(self, path):
        # Intentionally raises KeyError if we don't recognize the path.
        if self.files[path] is None:
            _RaiseNotFound(path)
        return self.files[path]

    def relpath(self, path, base):
        # This implementation is wrong in many ways; assert to check them for
        # now.
        if not base.endswith(self.sep):
            base += self.sep
        assert path.startswith(base)
        return path[len(base):]

    def split(self, path):
        return path.rsplit(self.sep, 1)