OLD | NEW |
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 # | 2 # |
3 # Copyright 2014 Altera Corporation. All Rights Reserved. | 3 # Copyright 2014 Altera Corporation. All Rights Reserved. |
4 # Author: John McGehee | 4 # Author: John McGehee |
5 # | 5 # |
6 # Licensed under the Apache License, Version 2.0 (the "License"); | 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
7 # you may not use this file except in compliance with 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 | 8 # You may obtain a copy of the License at |
9 # | 9 # |
10 # http://www.apache.org/licenses/LICENSE-2.0 | 10 # http://www.apache.org/licenses/LICENSE-2.0 |
(...skipping 11 matching lines...) Expand all Loading... |
22 import os | 22 import os |
23 import glob | 23 import glob |
24 import shutil | 24 import shutil |
25 import tempfile | 25 import tempfile |
26 import sys | 26 import sys |
27 if sys.version_info < (2, 7): | 27 if sys.version_info < (2, 7): |
28 import unittest2 as unittest | 28 import unittest2 as unittest |
29 else: | 29 else: |
30 import unittest | 30 import unittest |
31 import fake_filesystem_unittest | 31 import fake_filesystem_unittest |
32 | 32 import pytest |
33 | 33 |
34 class TestPyfakefsUnittest(fake_filesystem_unittest.TestCase): # pylint: disable
=R0904 | 34 class TestPyfakefsUnittest(fake_filesystem_unittest.TestCase): # pylint: disable
=R0904 |
35 '''Test the pyfakefs.fake_filesystem_unittest.TestCase` base class.''' | 35 '''Test the pyfakefs.fake_filesystem_unittest.TestCase` base class.''' |
36 | 36 |
37 def setUp(self): | 37 def setUp(self): |
38 '''Set up the fake file system''' | 38 '''Set up the fake file system''' |
39 self.setUpPyfakefs() | 39 self.setUpPyfakefs() |
40 | 40 |
41 def tearDown(self): | 41 def tearDown(self): |
42 '''Tear down the fake file system''' | 42 '''Tear down the fake file system''' |
43 self.tearDownPyfakefs() | 43 self.tearDownPyfakefs() |
44 | 44 |
| 45 @unittest.skipIf(sys.version_info > (2,), "file() was removed in Python 3") |
45 def test_file(self): | 46 def test_file(self): |
46 '''Test that the fake `file()` function is bound''' | 47 '''Fake `file()` function is bound''' |
47 self.assertFalse(os.path.exists('/fake_file.txt')) | 48 self.assertFalse(os.path.exists('/fake_file.txt')) |
48 with file('/fake_file.txt', 'w') as f: | 49 with file('/fake_file.txt', 'w') as f: |
49 f.write("This test file was created using the file() function.\n") | 50 f.write("This test file was created using the file() function.\n") |
50 self.assertTrue(self.fs.Exists('/fake_file.txt')) | 51 self.assertTrue(self.fs.Exists('/fake_file.txt')) |
51 with file('/fake_file.txt') as f: | 52 with file('/fake_file.txt') as f: |
52 content = f.read() | 53 content = f.read() |
53 self.assertEqual(content, | 54 self.assertEqual(content, |
54 'This test file was created using the file() function.\
n') | 55 'This test file was created using the file() function.\
n') |
55 | 56 |
56 def test_open(self): | 57 def test_open(self): |
57 '''Test that the fake `open()` function is bound''' | 58 '''Fake `open()` function is bound''' |
58 self.assertFalse(os.path.exists('/fake_file.txt')) | 59 self.assertFalse(os.path.exists('/fake_file.txt')) |
59 with open('/fake_file.txt', 'w') as f: | 60 with open('/fake_file.txt', 'w') as f: |
60 f.write("This test file was created using the open() function.\n") | 61 f.write("This test file was created using the open() function.\n") |
61 self.assertTrue(self.fs.Exists('/fake_file.txt')) | 62 self.assertTrue(self.fs.Exists('/fake_file.txt')) |
62 with open('/fake_file.txt') as f: | 63 with open('/fake_file.txt') as f: |
63 content = f.read() | 64 content = f.read() |
64 self.assertEqual(content, | 65 self.assertEqual(content, |
65 'This test file was created using the open() function.\
n') | 66 'This test file was created using the open() function.\
n') |
66 | 67 |
67 def test_os(self): | 68 def test_os(self): |
68 '''Test that the fake os module is bound''' | 69 '''Fake os module is bound''' |
69 self.assertFalse(self.fs.Exists('/test/dir1/dir2')) | 70 self.assertFalse(self.fs.Exists('/test/dir1/dir2')) |
70 os.makedirs('/test/dir1/dir2') | 71 os.makedirs('/test/dir1/dir2') |
71 self.assertTrue(self.fs.Exists('/test/dir1/dir2')) | 72 self.assertTrue(self.fs.Exists('/test/dir1/dir2')) |
72 | 73 |
73 def test_glob(self): | 74 def test_glob(self): |
74 '''Test that the fake glob module is bound''' | 75 '''Fake glob module is bound''' |
75 self.assertItemsEqual(glob.glob('/test/dir1/dir*'), | 76 self.assertCountEqual(glob.glob('/test/dir1/dir*'), |
76 []) | 77 []) |
77 self.fs.CreateDirectory('/test/dir1/dir2a') | 78 self.fs.CreateDirectory('/test/dir1/dir2a') |
78 self.assertItemsEqual(glob.glob('/test/dir1/dir*'), | 79 self.assertCountEqual(glob.glob('/test/dir1/dir*'), |
79 ['/test/dir1/dir2a']) | 80 ['/test/dir1/dir2a']) |
80 self.fs.CreateDirectory('/test/dir1/dir2b') | 81 self.fs.CreateDirectory('/test/dir1/dir2b') |
81 self.assertItemsEqual(glob.glob('/test/dir1/dir*'), | 82 self.assertCountEqual(glob.glob('/test/dir1/dir*'), |
82 ['/test/dir1/dir2a', '/test/dir1/dir2b']) | 83 ['/test/dir1/dir2a', '/test/dir1/dir2b']) |
83 | 84 |
84 def test_shutil(self): | 85 def test_shutil(self): |
85 '''Test that the fake shutil module is bound''' | 86 '''Fake shutil module is bound''' |
86 self.fs.CreateDirectory('/test/dir1/dir2a') | 87 self.fs.CreateDirectory('/test/dir1/dir2a') |
87 self.fs.CreateDirectory('/test/dir1/dir2b') | 88 self.fs.CreateDirectory('/test/dir1/dir2b') |
88 self.assertTrue(self.fs.Exists('/test/dir1/dir2b')) | 89 self.assertTrue(self.fs.Exists('/test/dir1/dir2b')) |
89 self.assertTrue(self.fs.Exists('/test/dir1/dir2a')) | 90 self.assertTrue(self.fs.Exists('/test/dir1/dir2a')) |
90 | 91 |
91 shutil.rmtree('/test/dir1') | 92 shutil.rmtree('/test/dir1') |
92 self.assertFalse(self.fs.Exists('/test/dir1')) | 93 self.assertFalse(self.fs.Exists('/test/dir1')) |
93 | 94 |
94 def test_tempfile(self): | 95 def test_tempfile(self): |
95 '''Test that the fake tempfile module is bound''' | 96 '''Fake tempfile module is bound''' |
96 with tempfile.NamedTemporaryFile() as tf: | 97 with tempfile.NamedTemporaryFile() as tf: |
97 tf.write('Temporary file contents\n') | 98 tf.write(b'Temporary file contents\n') |
98 name = tf.name | 99 name = tf.name |
99 self.assertTrue(self.fs.Exists(tf.name)) | 100 self.assertTrue(self.fs.Exists(tf.name)) |
100 | 101 |
| 102 def test_pytest(self): |
| 103 '''Compatibility with the :py:module:`pytest` module.''' |
| 104 pass |
101 | 105 |
102 if __name__ == "__main__": | 106 if __name__ == "__main__": |
103 unittest.main() | 107 unittest.main() |
OLD | NEW |