OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 Altera Corporation. All Rights Reserved. |
| 2 # Author: John McGehee |
| 3 # |
| 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 |
| 7 # |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 # |
| 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. |
| 15 |
| 16 """ |
| 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. |
| 20 |
| 21 The modules related to file handling are bound to the respective fake modules: |
| 22 |
| 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...> |
| 31 |
| 32 The `file()` and `open()` built-ins are bound to the fake `open()`: |
| 33 |
| 34 >>> file #doctest: +ELLIPSIS |
| 35 <fake_filesystem.FakeFileOpen object...> |
| 36 >>> open #doctest: +ELLIPSIS |
| 37 <fake_filesystem.FakeFileOpen object...> |
| 38 """ |
| 39 |
| 40 import os |
| 41 import glob |
| 42 import shutil |
| 43 |
| 44 def create_file(path): |
| 45 '''Create the specified file and add some content to it. Use the `open()` |
| 46 built in function. |
| 47 |
| 48 For example, the following file operations occur in the fake file system. |
| 49 In the real file system, we would not even have permission to write `/test`: |
| 50 |
| 51 >>> os.path.isdir('/test') |
| 52 False |
| 53 >>> os.mkdir('/test') |
| 54 >>> os.path.isdir('/test') |
| 55 True |
| 56 >>> os.path.exists('/test/file.txt') |
| 57 False |
| 58 >>> create_file('/test/file.txt') |
| 59 >>> os.path.exists('/test/file.txt') |
| 60 True |
| 61 >>> with open('/test/file.txt') as f: |
| 62 ... f.readlines() |
| 63 ["This is test file '/test/file.txt'.\\n", 'It was created using the open()
function.\\n'] |
| 64 ''' |
| 65 with open(path, 'w') as f: |
| 66 f.write("This is test file '{}'.\n".format(path)) |
| 67 f.write("It was created using the open() function.\n") |
| 68 |
| 69 def delete_file(path): |
| 70 '''Delete the specified file. |
| 71 |
| 72 For example: |
| 73 |
| 74 >>> os.mkdir('/test') |
| 75 >>> os.path.exists('/test/file.txt') |
| 76 False |
| 77 >>> create_file('/test/file.txt') |
| 78 >>> os.path.exists('/test/file.txt') |
| 79 True |
| 80 >>> delete_file('/test/file.txt') |
| 81 >>> os.path.exists('/test/file.txt') |
| 82 False |
| 83 ''' |
| 84 os.remove(path) |
| 85 |
| 86 def path_exists(path): |
| 87 '''Return True if the specified file exists. |
| 88 |
| 89 For example: |
| 90 |
| 91 >>> path_exists('/test') |
| 92 False |
| 93 >>> os.mkdir('/test') |
| 94 >>> path_exists('/test') |
| 95 True |
| 96 >>> |
| 97 >>> path_exists('/test/file.txt') |
| 98 False |
| 99 >>> create_file('/test/file.txt') |
| 100 >>> path_exists('/test/file.txt') |
| 101 True |
| 102 ''' |
| 103 return os.path.exists(path) |
| 104 |
| 105 def get_glob(glob_path): |
| 106 '''Return the list of paths matching the specified glob expression. |
| 107 |
| 108 For example: |
| 109 |
| 110 >>> os.mkdir('/test') |
| 111 >>> create_file('/test/file1.txt') |
| 112 >>> create_file('/test/file2.txt') |
| 113 >>> get_glob('/test/file*.txt') |
| 114 ['/test/file1.txt', '/test/file2.txt'] |
| 115 ''' |
| 116 return glob.glob(glob_path) |
| 117 |
| 118 def rm_tree(path): |
| 119 '''Delete the specified file hierarchy.''' |
| 120 shutil.rmtree(path) |
OLD | NEW |