1 # Copyright 2014 Altera Corporation. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 Example module that is tested in :py:class`pyfakefs.example_test.TestExample`.
18 This demonstrates the usage of the
19 :py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class.
21 The modules related to file handling are bound to the respective fake modules:
23 >>> os #doctest: +ELLIPSIS
24 <fake_filesystem.FakeOsModule object...>
25 >>> os.path #doctest: +ELLIPSIS
26 <fake_filesystem.FakePathModule object...>
27 >>> glob #doctest: +ELLIPSIS
28 <fake_filesystem_glob.FakeGlobModule object...>
29 >>> shutil #doctest: +ELLIPSIS
30 <fake_filesystem_shutil.FakeShutilModule object...>
32 The `open()` built-in is bound to the fake `open()`:
34 >>> open #doctest: +ELLIPSIS
35 <fake_filesystem.FakeFileOpen object...>
37 In Python 2 the `file()` built-in is also bound to the fake `open()`. `file()`
38 was eliminated in Python 3.
45 def create_file(path
):
46 '''Create the specified file and add some content to it. Use the `open()`
49 For example, the following file operations occur in the fake file system.
50 In the real file system, we would not even have permission to write `/test`:
52 >>> os.path.isdir('/test')
55 >>> os.path.isdir('/test')
57 >>> os.path.exists('/test/file.txt')
59 >>> create_file('/test/file.txt')
60 >>> os.path.exists('/test/file.txt')
62 >>> with open('/test/file.txt') as f:
64 ["This is test file '/test/file.txt'.\\n", 'It was created using the open() function.\\n']
66 with
open(path
, 'w') as f
:
67 f
.write("This is test file '{}'.\n".format(path
))
68 f
.write("It was created using the open() function.\n")
70 def delete_file(path
):
71 '''Delete the specified file.
76 >>> os.path.exists('/test/file.txt')
78 >>> create_file('/test/file.txt')
79 >>> os.path.exists('/test/file.txt')
81 >>> delete_file('/test/file.txt')
82 >>> os.path.exists('/test/file.txt')
87 def path_exists(path
):
88 '''Return True if the specified file exists.
92 >>> path_exists('/test')
95 >>> path_exists('/test')
98 >>> path_exists('/test/file.txt')
100 >>> create_file('/test/file.txt')
101 >>> path_exists('/test/file.txt')
104 return os
.path
.exists(path
)
106 def get_glob(glob_path
):
107 '''Return the list of paths matching the specified glob expression.
111 >>> os.mkdir('/test')
112 >>> create_file('/test/file1.txt')
113 >>> create_file('/test/file2.txt')
114 >>> get_glob('/test/file*.txt')
115 ['/test/file1.txt', '/test/file2.txt']
117 return glob
.glob(glob_path
)
120 '''Delete the specified file hierarchy.'''