• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5#      http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10# See the License for the specific language governing permissions and
11# limitations under the License.
12
13"""
14Example module that is used for testing modules that import file system modules
15to be patched under another name.
16"""
17
18import os as my_os
19import pathlib
20import sys
21from builtins import open as bltn_open
22from io import open as io_open
23from os import path
24from os import stat
25from os import stat as my_stat
26from os.path import exists, isfile, isdir, islink
27from os.path import exists as my_exists
28from pathlib import Path
29
30
31def check_if_exists1(filepath):
32    # test patching module imported under other name
33    return my_os.path.exists(filepath)
34
35
36def check_if_exists2(filepath):
37    # tests patching path imported from os
38    return path.exists(filepath)
39
40
41def check_if_exists3(filepath):
42    # tests patching Path imported from pathlib
43    return Path(filepath).exists()
44
45
46def check_if_exists4(filepath, file_exists=my_os.path.exists):
47    return file_exists(filepath)
48
49
50def check_if_exists5(filepath):
51    # tests patching `exists` imported from os.path
52    return exists(filepath)
53
54
55def check_if_exists6(filepath):
56    # tests patching `exists` imported from os.path as other name
57    return my_exists(filepath)
58
59
60def check_if_exists7(filepath):
61    # tests patching pathlib
62    return pathlib.Path(filepath).exists()
63
64
65def check_if_isfile(filepath):
66    # tests patching `isfile` imported from os.path
67    return isfile(filepath)
68
69
70def check_if_isdir(filepath):
71    # tests patching `isdir` imported from os.path
72    return isdir(filepath)
73
74
75def check_if_islink(filepath):
76    # tests patching `islink` imported from os.path
77    return islink(filepath)
78
79
80def file_stat1(filepath):
81    # tests patching `stat` imported from os
82    return stat(filepath)
83
84
85def file_stat2(filepath):
86    # tests patching `stat` imported from os as other name
87    return my_stat(filepath)
88
89
90def system_stat(filepath):
91    if sys.platform == "win32":
92        from nt import stat as system_stat
93    else:
94        from posix import stat as system_stat
95    return system_stat(filepath)
96
97
98def file_contents1(filepath):
99    with bltn_open(filepath, encoding="utf8") as f:
100        return f.read()
101
102
103def file_contents2(filepath):
104    with io_open(filepath, encoding="utf8") as f:
105        return f.read()
106
107
108def exists_this_file():
109    """Returns True in real fs only"""
110    return exists(__file__)
111
112
113def open_this_file():
114    """Works only in real fs"""
115    with open(__file__, encoding="utf8"):
116        pass
117
118
119def return_this_file_path():
120    """Works only in real fs"""
121    return Path(__file__)
122
123
124class TestDefaultArg:
125    def check_if_exists(self, filepath, file_exists=my_os.path.exists):
126        # this is a similar case as in the tempfile implementation under Posix
127        return file_exists(filepath)
128