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""" 17Example module that is tested in :py:class`pyfakefs.example_test.TestExample`. 18This demonstrates the usage of the 19:py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class. 20 21The 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 32The `open()` built-in is bound to the fake `open()`: 33 34>>> open #doctest: +ELLIPSIS 35<fake_filesystem.FakeFileOpen object...> 36 37In Python 2 the `file()` built-in is also bound to the fake `open()`. `file()` 38was eliminated in Python 3. 39""" 40 41import os 42import glob 43import shutil 44 45def create_file(path): 46 '''Create the specified file and add some content to it. Use the `open()` 47 built in function. 48 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`: 51 52 >>> os.path.isdir('/test') 53 False 54 >>> os.mkdir('/test') 55 >>> os.path.isdir('/test') 56 True 57 >>> os.path.exists('/test/file.txt') 58 False 59 >>> create_file('/test/file.txt') 60 >>> os.path.exists('/test/file.txt') 61 True 62 >>> with open('/test/file.txt') as f: 63 ... f.readlines() 64 ["This is test file '/test/file.txt'.\\n", 'It was created using the open() function.\\n'] 65 ''' 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") 69 70def delete_file(path): 71 '''Delete the specified file. 72 73 For example: 74 75 >>> os.mkdir('/test') 76 >>> os.path.exists('/test/file.txt') 77 False 78 >>> create_file('/test/file.txt') 79 >>> os.path.exists('/test/file.txt') 80 True 81 >>> delete_file('/test/file.txt') 82 >>> os.path.exists('/test/file.txt') 83 False 84 ''' 85 os.remove(path) 86 87def path_exists(path): 88 '''Return True if the specified file exists. 89 90 For example: 91 92 >>> path_exists('/test') 93 False 94 >>> os.mkdir('/test') 95 >>> path_exists('/test') 96 True 97 >>> 98 >>> path_exists('/test/file.txt') 99 False 100 >>> create_file('/test/file.txt') 101 >>> path_exists('/test/file.txt') 102 True 103 ''' 104 return os.path.exists(path) 105 106def get_glob(glob_path): 107 '''Return the list of paths matching the specified glob expression. 108 109 For example: 110 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'] 116 ''' 117 return glob.glob(glob_path) 118 119def rm_tree(path): 120 '''Delete the specified file hierarchy.''' 121 shutil.rmtree(path) 122