PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/telemetry/third_party/pyfakefs/pyfakefs/fake_filesystem_unittest_test.py

https://gitlab.com/jonnialva90/iridium-browser
Python | 107 lines | 64 code | 14 blank | 29 comment | 8 complexity | ffa3b7a683956cfa1d713073a03c6253 MD5 | raw file
  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. Test the :py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class.
  19. """
  20. import os
  21. import glob
  22. import shutil
  23. import tempfile
  24. import sys
  25. if sys.version_info < (2, 7):
  26. import unittest2 as unittest
  27. else:
  28. import unittest
  29. import fake_filesystem_unittest
  30. import pytest
  31. class TestPyfakefsUnittest(fake_filesystem_unittest.TestCase): # pylint: disable=R0904
  32. '''Test the pyfakefs.fake_filesystem_unittest.TestCase` base class.'''
  33. def setUp(self):
  34. '''Set up the fake file system'''
  35. self.setUpPyfakefs()
  36. def tearDown(self):
  37. '''Tear down the fake file system'''
  38. self.tearDownPyfakefs()
  39. @unittest.skipIf(sys.version_info > (2,), "file() was removed in Python 3")
  40. def test_file(self):
  41. '''Fake `file()` function is bound'''
  42. self.assertFalse(os.path.exists('/fake_file.txt'))
  43. with file('/fake_file.txt', 'w') as f:
  44. f.write("This test file was created using the file() function.\n")
  45. self.assertTrue(self.fs.Exists('/fake_file.txt'))
  46. with file('/fake_file.txt') as f:
  47. content = f.read()
  48. self.assertEqual(content,
  49. 'This test file was created using the file() function.\n')
  50. def test_open(self):
  51. '''Fake `open()` function is bound'''
  52. self.assertFalse(os.path.exists('/fake_file.txt'))
  53. with open('/fake_file.txt', 'w') as f:
  54. f.write("This test file was created using the open() function.\n")
  55. self.assertTrue(self.fs.Exists('/fake_file.txt'))
  56. with open('/fake_file.txt') as f:
  57. content = f.read()
  58. self.assertEqual(content,
  59. 'This test file was created using the open() function.\n')
  60. def test_os(self):
  61. '''Fake os module is bound'''
  62. self.assertFalse(self.fs.Exists('/test/dir1/dir2'))
  63. os.makedirs('/test/dir1/dir2')
  64. self.assertTrue(self.fs.Exists('/test/dir1/dir2'))
  65. def test_glob(self):
  66. '''Fake glob module is bound'''
  67. self.assertCountEqual(glob.glob('/test/dir1/dir*'),
  68. [])
  69. self.fs.CreateDirectory('/test/dir1/dir2a')
  70. self.assertCountEqual(glob.glob('/test/dir1/dir*'),
  71. ['/test/dir1/dir2a'])
  72. self.fs.CreateDirectory('/test/dir1/dir2b')
  73. self.assertCountEqual(glob.glob('/test/dir1/dir*'),
  74. ['/test/dir1/dir2a', '/test/dir1/dir2b'])
  75. def test_shutil(self):
  76. '''Fake shutil module is bound'''
  77. self.fs.CreateDirectory('/test/dir1/dir2a')
  78. self.fs.CreateDirectory('/test/dir1/dir2b')
  79. self.assertTrue(self.fs.Exists('/test/dir1/dir2b'))
  80. self.assertTrue(self.fs.Exists('/test/dir1/dir2a'))
  81. shutil.rmtree('/test/dir1')
  82. self.assertFalse(self.fs.Exists('/test/dir1'))
  83. def test_tempfile(self):
  84. '''Fake tempfile module is bound'''
  85. with tempfile.NamedTemporaryFile() as tf:
  86. tf.write(b'Temporary file contents\n')
  87. name = tf.name
  88. self.assertTrue(self.fs.Exists(tf.name))
  89. def test_pytest(self):
  90. '''Compatibility with the :py:module:`pytest` module.'''
  91. pass
  92. if __name__ == "__main__":
  93. unittest.main()