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.fake_filesystem_unittest.TestCase` base class. |
| 20 """ |
| 21 |
| 22 import os |
| 23 import glob |
| 24 import shutil |
| 25 import tempfile |
| 26 import sys |
| 27 if sys.version_info < (2, 7): |
| 28 import unittest2 as unittest |
| 29 else: |
| 30 import unittest |
| 31 import fake_filesystem_unittest |
| 32 |
| 33 |
| 34 class TestPyfakefsUnittest(fake_filesystem_unittest.TestCase): # pylint: disable
=R0904 |
| 35 '''Test the pyfakefs.fake_filesystem_unittest.TestCase` base class.''' |
| 36 |
| 37 def setUp(self): |
| 38 '''Set up the fake file system''' |
| 39 self.setUpPyfakefs() |
| 40 |
| 41 def tearDown(self): |
| 42 '''Tear down the fake file system''' |
| 43 self.tearDownPyfakefs() |
| 44 |
| 45 def test_file(self): |
| 46 '''Test that the fake `file()` function is bound''' |
| 47 self.assertFalse(os.path.exists('/fake_file.txt')) |
| 48 with file('/fake_file.txt', 'w') as f: |
| 49 f.write("This test file was created using the file() function.\n") |
| 50 self.assertTrue(self.fs.Exists('/fake_file.txt')) |
| 51 with file('/fake_file.txt') as f: |
| 52 content = f.read() |
| 53 self.assertEqual(content, |
| 54 'This test file was created using the file() function.\
n') |
| 55 |
| 56 def test_open(self): |
| 57 '''Test that the fake `open()` function is bound''' |
| 58 self.assertFalse(os.path.exists('/fake_file.txt')) |
| 59 with open('/fake_file.txt', 'w') as f: |
| 60 f.write("This test file was created using the open() function.\n") |
| 61 self.assertTrue(self.fs.Exists('/fake_file.txt')) |
| 62 with open('/fake_file.txt') as f: |
| 63 content = f.read() |
| 64 self.assertEqual(content, |
| 65 'This test file was created using the open() function.\
n') |
| 66 |
| 67 def test_os(self): |
| 68 '''Test that the fake os module is bound''' |
| 69 self.assertFalse(self.fs.Exists('/test/dir1/dir2')) |
| 70 os.makedirs('/test/dir1/dir2') |
| 71 self.assertTrue(self.fs.Exists('/test/dir1/dir2')) |
| 72 |
| 73 def test_glob(self): |
| 74 '''Test that the fake glob module is bound''' |
| 75 self.assertItemsEqual(glob.glob('/test/dir1/dir*'), |
| 76 []) |
| 77 self.fs.CreateDirectory('/test/dir1/dir2a') |
| 78 self.assertItemsEqual(glob.glob('/test/dir1/dir*'), |
| 79 ['/test/dir1/dir2a']) |
| 80 self.fs.CreateDirectory('/test/dir1/dir2b') |
| 81 self.assertItemsEqual(glob.glob('/test/dir1/dir*'), |
| 82 ['/test/dir1/dir2a', '/test/dir1/dir2b']) |
| 83 |
| 84 def test_shutil(self): |
| 85 '''Test that the fake shutil module is bound''' |
| 86 self.fs.CreateDirectory('/test/dir1/dir2a') |
| 87 self.fs.CreateDirectory('/test/dir1/dir2b') |
| 88 self.assertTrue(self.fs.Exists('/test/dir1/dir2b')) |
| 89 self.assertTrue(self.fs.Exists('/test/dir1/dir2a')) |
| 90 |
| 91 shutil.rmtree('/test/dir1') |
| 92 self.assertFalse(self.fs.Exists('/test/dir1')) |
| 93 |
| 94 def test_tempfile(self): |
| 95 '''Test that the fake tempfile module is bound''' |
| 96 with tempfile.NamedTemporaryFile() as tf: |
| 97 tf.write('Temporary file contents\n') |
| 98 name = tf.name |
| 99 self.assertTrue(self.fs.Exists(tf.name)) |
| 100 |
| 101 |
| 102 if __name__ == "__main__": |
| 103 unittest.main() |
OLD | NEW |