OLD | NEW |
(Empty) | |
| 1 #! /usr/bin/env python |
| 2 # |
| 3 # Copyright 2014 Altera Corporation. All Rights Reserved. |
| 4 # Author: John McGehee |
| 5 # |
| 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 # you may not use this file except in compliance with the License. |
| 8 # You may obtain a copy of the License at |
| 9 # |
| 10 # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 # |
| 12 # Unless required by applicable law or agreed to in writing, software |
| 13 # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 # See the License for the specific language governing permissions and |
| 16 # limitations under the License. |
| 17 |
| 18 """ |
| 19 Test the :py:class`pyfakefs.example` module to demonstrate the usage of the |
| 20 :py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class. |
| 21 """ |
| 22 |
| 23 import os |
| 24 import sys |
| 25 if sys.version_info < (2, 7): |
| 26 import unittest2 as unittest |
| 27 else: |
| 28 import unittest |
| 29 |
| 30 import fake_filesystem_unittest |
| 31 # The module under test is pyfakefs.example |
| 32 import example |
| 33 |
| 34 |
| 35 def load_tests(loader, tests, ignore): |
| 36 '''Load the pyfakefs/example.py doctest tests into unittest.''' |
| 37 return fake_filesystem_unittest.load_doctests(loader, tests, ignore, example
) |
| 38 |
| 39 |
| 40 class TestExample(fake_filesystem_unittest.TestCase): # pylint: disable=R0904 |
| 41 '''Test the pyfakefs.example module.''' |
| 42 |
| 43 def setUp(self): |
| 44 '''Invoke the :py:class:`pyfakefs.fake_filesystem_unittest.TestCase` |
| 45 `self.setUp()` method. This defines: |
| 46 |
| 47 * Attribute `self.fs`, an instance of \ |
| 48 :py:class:`pyfakefs.fake_filesystem.FakeFilesystem`. This is useful \ |
| 49 for creating test files. |
| 50 * Attribute `self.stubs`, an instance of \ |
| 51 :py:class:`mox.stubout.StubOutForTesting`. Use this if you need to |
| 52 define additional stubs. |
| 53 ''' |
| 54 self.setUpPyfakefs() |
| 55 |
| 56 def tearDown(self): |
| 57 # No longer need self.tearDownPyfakefs() |
| 58 pass |
| 59 |
| 60 def test_create_file(self): |
| 61 '''Test example.create_file()''' |
| 62 # The os module has been replaced with the fake os module so all of the |
| 63 # following occurs in the fake filesystem. |
| 64 self.assertFalse(os.path.isdir('/test')) |
| 65 os.mkdir('/test') |
| 66 self.assertTrue(os.path.isdir('/test')) |
| 67 |
| 68 self.assertFalse(os.path.exists('/test/file.txt')) |
| 69 example.create_file('/test/file.txt') |
| 70 self.assertTrue(os.path.exists('/test/file.txt')) |
| 71 |
| 72 def test_delete_file(self): |
| 73 '''Test example.delete_file() |
| 74 |
| 75 `self.fs.CreateFile()` is convenient because it automatically creates |
| 76 directories in the fake file system and allows you to specify the file |
| 77 contents. |
| 78 |
| 79 You could also use `open()` or `file()`. |
| 80 ''' |
| 81 self.fs.CreateFile('/test/full.txt', |
| 82 contents='First line\n' |
| 83 'Second Line\n') |
| 84 self.assertTrue(os.path.exists('/test/full.txt')) |
| 85 example.delete_file('/test/full.txt') |
| 86 self.assertFalse(os.path.exists('/test/full.txt')) |
| 87 |
| 88 def test_file_exists(self): |
| 89 '''Test example.path_exists() |
| 90 |
| 91 `self.fs.CreateFile()` is convenient because it automatically creates |
| 92 directories in the fake file system and allows you to specify the file |
| 93 contents. |
| 94 |
| 95 You could also use `open()` or `file()` if you wanted. |
| 96 ''' |
| 97 self.assertFalse(example.path_exists('/test/empty.txt')) |
| 98 self.fs.CreateFile('/test/empty.txt') |
| 99 self.assertTrue(example.path_exists('/test/empty.txt')) |
| 100 |
| 101 def test_get_globs(self): |
| 102 '''Test example.get_glob() |
| 103 |
| 104 `self.fs.CreateDirectory()` creates directories. However, you might |
| 105 prefer the familiar `os.makedirs()`, which also works fine on the fake |
| 106 file system. |
| 107 ''' |
| 108 self.assertFalse(os.path.isdir('/test')) |
| 109 self.fs.CreateDirectory('/test/dir1/dir2a') |
| 110 self.assertTrue(os.path.isdir('/test/dir1/dir2a')) |
| 111 # os.mkdirs() works, too. |
| 112 os.makedirs('/test/dir1/dir2b') |
| 113 self.assertTrue(os.path.isdir('/test/dir1/dir2b')) |
| 114 |
| 115 self.assertItemsEqual(example.get_glob('/test/dir1/nonexistent*'), |
| 116 []) |
| 117 self.assertItemsEqual(example.get_glob('/test/dir1/dir*'), |
| 118 ['/test/dir1/dir2a', '/test/dir1/dir2b']) |
| 119 |
| 120 def test_rm_tree(self): |
| 121 '''Test example.rm_tree() |
| 122 |
| 123 `self.fs.CreateDirectory()` creates directories. However, you might |
| 124 prefer the familiar `os.makedirs()`, which also works fine on the fake |
| 125 file system. |
| 126 ''' |
| 127 self.fs.CreateDirectory('/test/dir1/dir2a') |
| 128 # os.mkdirs() works, too. |
| 129 os.makedirs('/test/dir1/dir2b') |
| 130 self.assertTrue(os.path.isdir('/test/dir1/dir2b')) |
| 131 self.assertTrue(os.path.isdir('/test/dir1/dir2a')) |
| 132 |
| 133 example.rm_tree('/test/dir1') |
| 134 self.assertFalse(os.path.exists('/test/dir1')) |
| 135 |
| 136 if __name__ == "__main__": |
| 137 unittest.main() |
OLD | NEW |