• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2009 Google Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""A fake shutil module implementation that uses fake_filesystem for
16unit tests.
17Note that only `shutildisk_usage()` is faked, the rest of the functions shall
18work fine with the fake file system if `os`/`os.path` are patched.
19
20:Includes:
21  FakeShutil: Uses a FakeFilesystem to provide a fake replacement for the
22    shutil module.
23
24:Usage:
25  The fake implementation is automatically involved if using
26  `fake_filesystem_unittest.TestCase`, pytest fs fixture,
27  or directly `Patcher`.
28"""
29
30import shutil
31
32
33class FakeShutilModule:
34    """Uses a FakeFilesystem to provide a fake replacement for shutil module.
35    """
36
37    @staticmethod
38    def dir():
39        """Return the list of patched function names. Used for patching
40        functions imported from the module.
41        """
42        return 'disk_usage',
43
44    def __init__(self, filesystem):
45        """Construct fake shutil module using the fake filesystem.
46
47        Args:
48          filesystem:  FakeFilesystem used to provide file system information
49        """
50        self.filesystem = filesystem
51        self._shutil_module = shutil
52
53    def disk_usage(self, path):
54        """Return the total, used and free disk space in bytes as named tuple
55        or placeholder holder values simulating unlimited space if not set.
56
57        Args:
58          path: defines the filesystem device which is queried
59        """
60        return self.filesystem.get_disk_usage(path)
61
62    def __getattr__(self, name):
63        """Forwards any non-faked calls to the standard shutil module."""
64        return getattr(self._shutil_module, name)
65