1# Copyright 2014 Altera Corporation. All Rights Reserved. 2# Copyright 2015-2017 John McGehee 3# Author: John McGehee 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17""" 18Test the :py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class. 19""" 20import glob 21import io 22import multiprocessing 23import os 24import pathlib 25import runpy 26import shutil 27import sys 28import tempfile 29import unittest 30import warnings 31from distutils.dir_util import copy_tree, remove_tree 32from pathlib import Path 33from unittest import TestCase, mock 34 35import pyfakefs.tests.import_as_example 36import pyfakefs.tests.logsio 37from pyfakefs import fake_filesystem_unittest, fake_filesystem 38from pyfakefs.fake_filesystem import OSType 39from pyfakefs.fake_filesystem_unittest import ( 40 Patcher, Pause, patchfs, PatchMode 41) 42from pyfakefs.tests.fixtures import module_with_attributes 43 44 45class TestPatcher(TestCase): 46 def test_context_manager(self): 47 with Patcher() as patcher: 48 patcher.fs.create_file('/foo/bar', contents='test') 49 with open('/foo/bar') as f: 50 contents = f.read() 51 self.assertEqual('test', contents) 52 53 @patchfs 54 def test_context_decorator(self, fake_fs): 55 fake_fs.create_file('/foo/bar', contents='test') 56 with open('/foo/bar') as f: 57 contents = f.read() 58 self.assertEqual('test', contents) 59 60 61class TestPatchfsArgumentOrder(TestCase): 62 @patchfs 63 @mock.patch('os.system') 64 def test_argument_order1(self, fake_fs, patched_system): 65 fake_fs.create_file('/foo/bar', contents='test') 66 with open('/foo/bar') as f: 67 contents = f.read() 68 self.assertEqual('test', contents) 69 os.system("foo") 70 patched_system.assert_called_with("foo") 71 72 @mock.patch('os.system') 73 @patchfs 74 def test_argument_order2(self, patched_system, fake_fs): 75 fake_fs.create_file('/foo/bar', contents='test') 76 with open('/foo/bar') as f: 77 contents = f.read() 78 self.assertEqual('test', contents) 79 os.system("foo") 80 patched_system.assert_called_with("foo") 81 82 83class TestPyfakefsUnittestBase(fake_filesystem_unittest.TestCase): 84 def setUp(self): 85 """Set up the fake file system""" 86 self.setUpPyfakefs() 87 88 89class TestPyfakefsUnittest(TestPyfakefsUnittestBase): # pylint: disable=R0904 90 """Test the `pyfakefs.fake_filesystem_unittest.TestCase` base class.""" 91 92 def test_open(self): 93 """Fake `open()` function is bound""" 94 self.assertFalse(os.path.exists('/fake_file.txt')) 95 with open('/fake_file.txt', 'w') as f: 96 f.write("This test file was created using the open() function.\n") 97 self.assertTrue(self.fs.exists('/fake_file.txt')) 98 with open('/fake_file.txt') as f: 99 content = f.read() 100 self.assertEqual('This test file was created using the ' 101 'open() function.\n', content) 102 103 def test_io_open(self): 104 """Fake io module is bound""" 105 self.assertFalse(os.path.exists('/fake_file.txt')) 106 with io.open('/fake_file.txt', 'w') as f: 107 f.write("This test file was created using the" 108 " io.open() function.\n") 109 self.assertTrue(self.fs.exists('/fake_file.txt')) 110 with open('/fake_file.txt') as f: 111 content = f.read() 112 self.assertEqual('This test file was created using the ' 113 'io.open() function.\n', content) 114 115 def test_os(self): 116 """Fake os module is bound""" 117 self.assertFalse(self.fs.exists('/test/dir1/dir2')) 118 os.makedirs('/test/dir1/dir2') 119 self.assertTrue(self.fs.exists('/test/dir1/dir2')) 120 121 def test_glob(self): 122 """Fake glob module is bound""" 123 is_windows = sys.platform.startswith('win') 124 self.assertEqual([], glob.glob('/test/dir1/dir*')) 125 self.fs.create_dir('/test/dir1/dir2a') 126 matching_paths = glob.glob('/test/dir1/dir*') 127 if is_windows: 128 self.assertEqual([r'/test/dir1\dir2a'], matching_paths) 129 else: 130 self.assertEqual(['/test/dir1/dir2a'], matching_paths) 131 self.fs.create_dir('/test/dir1/dir2b') 132 matching_paths = sorted(glob.glob('/test/dir1/dir*')) 133 if is_windows: 134 self.assertEqual([r'/test/dir1\dir2a', r'/test/dir1\dir2b'], 135 matching_paths) 136 else: 137 self.assertEqual(['/test/dir1/dir2a', '/test/dir1/dir2b'], 138 matching_paths) 139 140 def test_shutil(self): 141 """Fake shutil module is bound""" 142 self.fs.create_dir('/test/dir1/dir2a') 143 self.fs.create_dir('/test/dir1/dir2b') 144 self.assertTrue(self.fs.exists('/test/dir1/dir2b')) 145 self.assertTrue(self.fs.exists('/test/dir1/dir2a')) 146 147 shutil.rmtree('/test/dir1') 148 self.assertFalse(self.fs.exists('/test/dir1')) 149 150 def test_fakepathlib(self): 151 with pathlib.Path('/fake_file.txt') as p: 152 with p.open('w') as f: 153 f.write('text') 154 is_windows = sys.platform.startswith('win') 155 if is_windows: 156 self.assertTrue(self.fs.exists(r'\fake_file.txt')) 157 else: 158 self.assertTrue(self.fs.exists('/fake_file.txt')) 159 160 161class TestPatchingImports(TestPyfakefsUnittestBase): 162 def test_import_as_other_name(self): 163 file_path = '/foo/bar/baz' 164 self.fs.create_file(file_path) 165 self.assertTrue(self.fs.exists(file_path)) 166 self.assertTrue( 167 pyfakefs.tests.import_as_example.check_if_exists1(file_path)) 168 169 def test_import_path_from_os(self): 170 """Make sure `from os import path` patches `path`.""" 171 file_path = '/foo/bar/baz' 172 self.fs.create_file(file_path) 173 self.assertTrue(self.fs.exists(file_path)) 174 self.assertTrue( 175 pyfakefs.tests.import_as_example.check_if_exists2(file_path)) 176 177 def test_import_path_from_pathlib(self): 178 file_path = '/foo/bar' 179 self.fs.create_dir(file_path) 180 self.assertTrue( 181 pyfakefs.tests.import_as_example.check_if_exists3(file_path)) 182 183 def test_import_function_from_os_path(self): 184 file_path = '/foo/bar' 185 self.fs.create_dir(file_path) 186 self.assertTrue( 187 pyfakefs.tests.import_as_example.check_if_exists5(file_path)) 188 189 def test_import_function_from_os_path_as_other_name(self): 190 file_path = '/foo/bar' 191 self.fs.create_dir(file_path) 192 self.assertTrue( 193 pyfakefs.tests.import_as_example.check_if_exists6(file_path)) 194 195 def test_import_pathlib_path(self): 196 file_path = '/foo/bar' 197 self.fs.create_dir(file_path) 198 self.assertTrue( 199 pyfakefs.tests.import_as_example.check_if_exists7(file_path)) 200 201 def test_import_function_from_os(self): 202 file_path = '/foo/bar' 203 self.fs.create_file(file_path, contents=b'abc') 204 stat_result = pyfakefs.tests.import_as_example.file_stat1(file_path) 205 self.assertEqual(3, stat_result.st_size) 206 207 def test_import_function_from_os_as_other_name(self): 208 file_path = '/foo/bar' 209 self.fs.create_file(file_path, contents=b'abc') 210 stat_result = pyfakefs.tests.import_as_example.file_stat2(file_path) 211 self.assertEqual(3, stat_result.st_size) 212 213 def test_import_open_as_other_name(self): 214 file_path = '/foo/bar' 215 self.fs.create_file(file_path, contents=b'abc') 216 contents = pyfakefs.tests.import_as_example.file_contents1(file_path) 217 self.assertEqual('abc', contents) 218 219 def test_import_io_open_as_other_name(self): 220 file_path = '/foo/bar' 221 self.fs.create_file(file_path, contents=b'abc') 222 contents = pyfakefs.tests.import_as_example.file_contents2(file_path) 223 self.assertEqual('abc', contents) 224 225 226class TestPatchingDefaultArgs(fake_filesystem_unittest.TestCase): 227 def setUp(self): 228 self.setUpPyfakefs(patch_default_args=True) 229 230 def test_path_exists_as_default_arg_in_function(self): 231 file_path = '/foo/bar' 232 self.fs.create_dir(file_path) 233 self.assertTrue( 234 pyfakefs.tests.import_as_example.check_if_exists4(file_path)) 235 236 def test_path_exists_as_default_arg_in_method(self): 237 file_path = '/foo/bar' 238 self.fs.create_dir(file_path) 239 sut = pyfakefs.tests.import_as_example.TestDefaultArg() 240 self.assertTrue(sut.check_if_exists(file_path)) 241 242 def test_fake_path_exists4(self): 243 self.fs.create_file('foo') 244 self.assertTrue( 245 pyfakefs.tests.import_as_example.check_if_exists4('foo')) 246 247 248class TestAttributesWithFakeModuleNames(TestPyfakefsUnittestBase): 249 """Test that module attributes with names like `path` or `io` are not 250 stubbed out. 251 """ 252 253 def test_attributes(self): 254 """Attributes of module under test are not patched""" 255 self.assertEqual(module_with_attributes.os, 'os attribute value') 256 self.assertEqual(module_with_attributes.path, 'path attribute value') 257 self.assertEqual(module_with_attributes.pathlib, 258 'pathlib attribute value') 259 self.assertEqual(module_with_attributes.shutil, 260 'shutil attribute value') 261 self.assertEqual(module_with_attributes.io, 'io attribute value') 262 263 264import math as path # noqa: E402 wanted import not at top 265 266 267class TestPathNotPatchedIfNotOsPath(TestPyfakefsUnittestBase): 268 """Tests that `path` is not patched if it is not `os.path`. 269 An own path module (in this case an alias to math) can be imported 270 and used. 271 """ 272 273 def test_own_path_module(self): 274 self.assertEqual(2, path.floor(2.5)) 275 276 277class FailedPatchingTest(TestPyfakefsUnittestBase): 278 """Negative tests: make sure the tests for `modules_to_reload` and 279 `modules_to_patch` fail if not providing the arguments. 280 """ 281 282 @unittest.expectedFailure 283 def test_system_stat(self): 284 file_path = '/foo/bar' 285 self.fs.create_file(file_path, contents=b'test') 286 self.assertEqual( 287 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size) 288 289 290class ReloadModuleTest(fake_filesystem_unittest.TestCase): 291 """Make sure that reloading a module allows patching of classes not 292 patched automatically. 293 """ 294 295 def setUp(self): 296 """Set up the fake file system""" 297 self.setUpPyfakefs( 298 modules_to_reload=[pyfakefs.tests.import_as_example]) 299 300 301class NoSkipNamesTest(fake_filesystem_unittest.TestCase): 302 """Reference test for additional_skip_names tests: 303 make sure that the module is patched by default.""" 304 305 def setUp(self): 306 self.setUpPyfakefs() 307 308 def test_path_exists(self): 309 self.assertFalse( 310 pyfakefs.tests.import_as_example.exists_this_file()) 311 312 def test_fake_path_exists1(self): 313 self.fs.create_file('foo') 314 self.assertTrue( 315 pyfakefs.tests.import_as_example.check_if_exists1('foo')) 316 317 def test_fake_path_exists2(self): 318 self.fs.create_file('foo') 319 self.assertTrue( 320 pyfakefs.tests.import_as_example.check_if_exists2('foo')) 321 322 def test_fake_path_exists3(self): 323 self.fs.create_file('foo') 324 self.assertTrue( 325 pyfakefs.tests.import_as_example.check_if_exists3('foo')) 326 327 def test_fake_path_exists5(self): 328 self.fs.create_file('foo') 329 self.assertTrue( 330 pyfakefs.tests.import_as_example.check_if_exists5('foo')) 331 332 def test_fake_path_exists6(self): 333 self.fs.create_file('foo') 334 self.assertTrue( 335 pyfakefs.tests.import_as_example.check_if_exists6('foo')) 336 337 def test_fake_path_exists7(self): 338 self.fs.create_file('foo') 339 self.assertTrue( 340 pyfakefs.tests.import_as_example.check_if_exists7('foo')) 341 342 def test_open_fails(self): 343 with self.assertRaises(OSError): 344 pyfakefs.tests.import_as_example.open_this_file() 345 346 def test_open_patched_in_module_ending_with_io(self): 347 # regression test for #569 348 file_path = '/foo/bar' 349 self.fs.create_file(file_path, contents=b'abc') 350 contents = pyfakefs.tests.logsio.file_contents(file_path) 351 self.assertEqual(b'abc', contents) 352 353 354class AdditionalSkipNamesTest(fake_filesystem_unittest.TestCase): 355 """Make sure that modules in additional_skip_names are not patched. 356 Passes module name to `additional_skip_names`.""" 357 358 def setUp(self): 359 self.setUpPyfakefs( 360 additional_skip_names=['pyfakefs.tests.import_as_example']) 361 362 def test_path_exists(self): 363 self.assertTrue( 364 pyfakefs.tests.import_as_example.exists_this_file()) 365 366 def test_fake_path_does_not_exist1(self): 367 self.fs.create_file('foo') 368 self.assertFalse( 369 pyfakefs.tests.import_as_example.check_if_exists1('foo')) 370 371 def test_fake_path_does_not_exist2(self): 372 self.fs.create_file('foo') 373 self.assertFalse( 374 pyfakefs.tests.import_as_example.check_if_exists2('foo')) 375 376 def test_fake_path_does_not_exist3(self): 377 self.fs.create_file('foo') 378 self.assertFalse( 379 pyfakefs.tests.import_as_example.check_if_exists3('foo')) 380 381 def test_fake_path_does_not_exist4(self): 382 self.fs.create_file('foo') 383 self.assertFalse( 384 pyfakefs.tests.import_as_example.check_if_exists4('foo')) 385 386 def test_fake_path_does_not_exist5(self): 387 self.fs.create_file('foo') 388 self.assertFalse( 389 pyfakefs.tests.import_as_example.check_if_exists5('foo')) 390 391 def test_fake_path_does_not_exist6(self): 392 self.fs.create_file('foo') 393 self.assertFalse( 394 pyfakefs.tests.import_as_example.check_if_exists6('foo')) 395 396 def test_fake_path_does_not_exist7(self): 397 self.fs.create_file('foo') 398 self.assertFalse( 399 pyfakefs.tests.import_as_example.check_if_exists7('foo')) 400 401 def test_open_succeeds(self): 402 pyfakefs.tests.import_as_example.open_this_file() 403 404 def test_path_succeeds(self): 405 pyfakefs.tests.import_as_example.return_this_file_path() 406 407 408class AdditionalSkipNamesModuleTest(fake_filesystem_unittest.TestCase): 409 """Make sure that modules in additional_skip_names are not patched. 410 Passes module to `additional_skip_names`.""" 411 412 def setUp(self): 413 self.setUpPyfakefs( 414 additional_skip_names=[pyfakefs.tests.import_as_example]) 415 416 def test_path_exists(self): 417 self.assertTrue( 418 pyfakefs.tests.import_as_example.exists_this_file()) 419 420 def test_fake_path_does_not_exist1(self): 421 self.fs.create_file('foo') 422 self.assertFalse( 423 pyfakefs.tests.import_as_example.check_if_exists1('foo')) 424 425 def test_fake_path_does_not_exist2(self): 426 self.fs.create_file('foo') 427 self.assertFalse( 428 pyfakefs.tests.import_as_example.check_if_exists2('foo')) 429 430 def test_fake_path_does_not_exist3(self): 431 self.fs.create_file('foo') 432 self.assertFalse( 433 pyfakefs.tests.import_as_example.check_if_exists3('foo')) 434 435 def test_fake_path_does_not_exist4(self): 436 self.fs.create_file('foo') 437 self.assertFalse( 438 pyfakefs.tests.import_as_example.check_if_exists4('foo')) 439 440 def test_fake_path_does_not_exist5(self): 441 self.fs.create_file('foo') 442 self.assertFalse( 443 pyfakefs.tests.import_as_example.check_if_exists5('foo')) 444 445 def test_fake_path_does_not_exist6(self): 446 self.fs.create_file('foo') 447 self.assertFalse( 448 pyfakefs.tests.import_as_example.check_if_exists6('foo')) 449 450 def test_fake_path_does_not_exist7(self): 451 self.fs.create_file('foo') 452 self.assertFalse( 453 pyfakefs.tests.import_as_example.check_if_exists7('foo')) 454 455 def test_open_succeeds(self): 456 pyfakefs.tests.import_as_example.open_this_file() 457 458 def test_path_succeeds(self): 459 pyfakefs.tests.import_as_example.return_this_file_path() 460 461 462class FakeExampleModule: 463 """Used to patch a function that uses system-specific functions that 464 cannot be patched automatically.""" 465 _orig_module = pyfakefs.tests.import_as_example 466 467 def __init__(self, fs): 468 pass 469 470 def system_stat(self, filepath): 471 return os.stat(filepath) 472 473 def __getattr__(self, name): 474 """Forwards any non-faked calls to the standard module.""" 475 return getattr(self._orig_module, name) 476 477 478class PatchModuleTest(fake_filesystem_unittest.TestCase): 479 """Make sure that reloading a module allows patching of classes not 480 patched automatically. 481 """ 482 483 def setUp(self): 484 """Set up the fake file system""" 485 self.setUpPyfakefs( 486 modules_to_patch={ 487 'pyfakefs.tests.import_as_example': FakeExampleModule}) 488 489 def test_system_stat(self): 490 file_path = '/foo/bar' 491 self.fs.create_file(file_path, contents=b'test') 492 self.assertEqual( 493 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size) 494 495 496class PatchModuleTestUsingDecorator(unittest.TestCase): 497 """Make sure that reloading a module allows patching of classes not 498 patched automatically - use patchfs decorator with parameter. 499 """ 500 501 @patchfs 502 @unittest.expectedFailure 503 def test_system_stat_failing(self, fake_fs): 504 file_path = '/foo/bar' 505 fake_fs.create_file(file_path, contents=b'test') 506 self.assertEqual( 507 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size) 508 509 @patchfs(modules_to_patch={ 510 'pyfakefs.tests.import_as_example': FakeExampleModule}) 511 def test_system_stat(self, fake_fs): 512 file_path = '/foo/bar' 513 fake_fs.create_file(file_path, contents=b'test') 514 self.assertEqual( 515 4, pyfakefs.tests.import_as_example.system_stat(file_path).st_size) 516 517 518class NoRootUserTest(fake_filesystem_unittest.TestCase): 519 """Test allow_root_user argument to setUpPyfakefs.""" 520 521 def setUp(self): 522 self.setUpPyfakefs(allow_root_user=False) 523 524 def test_non_root_behavior(self): 525 """Check that fs behaves as non-root user regardless of actual 526 user rights. 527 """ 528 self.fs.is_windows_fs = False 529 dir_path = '/foo/bar' 530 self.fs.create_dir(dir_path, perm_bits=0o555) 531 file_path = dir_path + 'baz' 532 with self.assertRaises(OSError): 533 self.fs.create_file(file_path) 534 535 file_path = '/baz' 536 self.fs.create_file(file_path) 537 os.chmod(file_path, 0o400) 538 with self.assertRaises(OSError): 539 open(file_path, 'w') 540 541 542class PauseResumeTest(fake_filesystem_unittest.TestCase): 543 def setUp(self): 544 self.real_temp_file = None 545 self.setUpPyfakefs() 546 547 def tearDown(self): 548 if self.real_temp_file is not None: 549 self.real_temp_file.close() 550 551 def test_pause_resume(self): 552 fake_temp_file = tempfile.NamedTemporaryFile() 553 self.assertTrue(self.fs.exists(fake_temp_file.name)) 554 self.assertTrue(os.path.exists(fake_temp_file.name)) 555 self.pause() 556 self.assertTrue(self.fs.exists(fake_temp_file.name)) 557 self.assertFalse(os.path.exists(fake_temp_file.name)) 558 self.real_temp_file = tempfile.NamedTemporaryFile() 559 self.assertFalse(self.fs.exists(self.real_temp_file.name)) 560 self.assertTrue(os.path.exists(self.real_temp_file.name)) 561 self.resume() 562 self.assertFalse(os.path.exists(self.real_temp_file.name)) 563 self.assertTrue(os.path.exists(fake_temp_file.name)) 564 565 def test_pause_resume_fs(self): 566 fake_temp_file = tempfile.NamedTemporaryFile() 567 self.assertTrue(self.fs.exists(fake_temp_file.name)) 568 self.assertTrue(os.path.exists(fake_temp_file.name)) 569 # resume does nothing if not paused 570 self.fs.resume() 571 self.assertTrue(os.path.exists(fake_temp_file.name)) 572 self.fs.pause() 573 self.assertTrue(self.fs.exists(fake_temp_file.name)) 574 self.assertFalse(os.path.exists(fake_temp_file.name)) 575 self.real_temp_file = tempfile.NamedTemporaryFile() 576 self.assertFalse(self.fs.exists(self.real_temp_file.name)) 577 self.assertTrue(os.path.exists(self.real_temp_file.name)) 578 # pause does nothing if already paused 579 self.fs.pause() 580 self.assertFalse(self.fs.exists(self.real_temp_file.name)) 581 self.assertTrue(os.path.exists(self.real_temp_file.name)) 582 self.fs.resume() 583 self.assertFalse(os.path.exists(self.real_temp_file.name)) 584 self.assertTrue(os.path.exists(fake_temp_file.name)) 585 586 def test_pause_resume_contextmanager(self): 587 fake_temp_file = tempfile.NamedTemporaryFile() 588 self.assertTrue(self.fs.exists(fake_temp_file.name)) 589 self.assertTrue(os.path.exists(fake_temp_file.name)) 590 with Pause(self): 591 self.assertTrue(self.fs.exists(fake_temp_file.name)) 592 self.assertFalse(os.path.exists(fake_temp_file.name)) 593 self.real_temp_file = tempfile.NamedTemporaryFile() 594 self.assertFalse(self.fs.exists(self.real_temp_file.name)) 595 self.assertTrue(os.path.exists(self.real_temp_file.name)) 596 self.assertFalse(os.path.exists(self.real_temp_file.name)) 597 self.assertTrue(os.path.exists(fake_temp_file.name)) 598 599 def test_pause_resume_fs_contextmanager(self): 600 fake_temp_file = tempfile.NamedTemporaryFile() 601 self.assertTrue(self.fs.exists(fake_temp_file.name)) 602 self.assertTrue(os.path.exists(fake_temp_file.name)) 603 with Pause(self.fs): 604 self.assertTrue(self.fs.exists(fake_temp_file.name)) 605 self.assertFalse(os.path.exists(fake_temp_file.name)) 606 self.real_temp_file = tempfile.NamedTemporaryFile() 607 self.assertFalse(self.fs.exists(self.real_temp_file.name)) 608 self.assertTrue(os.path.exists(self.real_temp_file.name)) 609 self.assertFalse(os.path.exists(self.real_temp_file.name)) 610 self.assertTrue(os.path.exists(fake_temp_file.name)) 611 612 def test_pause_resume_without_patcher(self): 613 fs = fake_filesystem.FakeFilesystem() 614 with self.assertRaises(RuntimeError): 615 fs.resume() 616 617 618class PauseResumePatcherTest(fake_filesystem_unittest.TestCase): 619 def test_pause_resume(self): 620 with Patcher() as p: 621 fake_temp_file = tempfile.NamedTemporaryFile() 622 self.assertTrue(p.fs.exists(fake_temp_file.name)) 623 self.assertTrue(os.path.exists(fake_temp_file.name)) 624 p.pause() 625 self.assertTrue(p.fs.exists(fake_temp_file.name)) 626 self.assertFalse(os.path.exists(fake_temp_file.name)) 627 real_temp_file = tempfile.NamedTemporaryFile() 628 self.assertFalse(p.fs.exists(real_temp_file.name)) 629 self.assertTrue(os.path.exists(real_temp_file.name)) 630 p.resume() 631 self.assertFalse(os.path.exists(real_temp_file.name)) 632 self.assertTrue(os.path.exists(fake_temp_file.name)) 633 real_temp_file.close() 634 635 def test_pause_resume_contextmanager(self): 636 with Patcher() as p: 637 fake_temp_file = tempfile.NamedTemporaryFile() 638 self.assertTrue(p.fs.exists(fake_temp_file.name)) 639 self.assertTrue(os.path.exists(fake_temp_file.name)) 640 with Pause(p): 641 self.assertTrue(p.fs.exists(fake_temp_file.name)) 642 self.assertFalse(os.path.exists(fake_temp_file.name)) 643 real_temp_file = tempfile.NamedTemporaryFile() 644 self.assertFalse(p.fs.exists(real_temp_file.name)) 645 self.assertTrue(os.path.exists(real_temp_file.name)) 646 self.assertFalse(os.path.exists(real_temp_file.name)) 647 self.assertTrue(os.path.exists(fake_temp_file.name)) 648 real_temp_file.close() 649 650 651class TestPyfakefsTestCase(unittest.TestCase): 652 def setUp(self): 653 class TestTestCase(fake_filesystem_unittest.TestCase): 654 def runTest(self): 655 pass 656 657 self.test_case = TestTestCase('runTest') 658 659 def test_test_case_type(self): 660 self.assertIsInstance(self.test_case, unittest.TestCase) 661 662 self.assertIsInstance(self.test_case, 663 fake_filesystem_unittest.TestCaseMixin) 664 665 666class TestTempFileReload(unittest.TestCase): 667 """Regression test for #356 to make sure that reloading the tempfile 668 does not affect other tests.""" 669 670 def test_fakefs(self): 671 with Patcher() as patcher: 672 patcher.fs.create_file('/mytempfile', contents='abcd') 673 674 def test_value(self): 675 v = multiprocessing.Value('I', 0) 676 self.assertEqual(v.value, 0) 677 678 679class TestPyfakefsTestCaseMixin(unittest.TestCase, 680 fake_filesystem_unittest.TestCaseMixin): 681 def test_set_up_pyfakefs(self): 682 self.setUpPyfakefs() 683 684 self.assertTrue(hasattr(self, 'fs')) 685 self.assertIsInstance(self.fs, fake_filesystem.FakeFilesystem) 686 687 688class TestShutilWithZipfile(fake_filesystem_unittest.TestCase): 689 """Regression test for #427.""" 690 691 def setUp(self): 692 self.setUpPyfakefs() 693 self.fs.create_file('foo/bar') 694 695 def test_a(self): 696 shutil.make_archive('archive', 'zip', root_dir='foo') 697 698 def test_b(self): 699 # used to fail because 'bar' could not be found 700 shutil.make_archive('archive', 'zip', root_dir='foo') 701 702 703class TestDistutilsCopyTree(fake_filesystem_unittest.TestCase): 704 """Regression test for #501.""" 705 706 def setUp(self): 707 self.setUpPyfakefs() 708 self.fs.create_dir("./test/subdir/") 709 self.fs.create_dir("./test/subdir2/") 710 self.fs.create_file("./test2/subdir/1.txt") 711 712 def test_file_copied(self): 713 copy_tree("./test2/", "./test/") 714 remove_tree("./test2/") 715 716 self.assertTrue(os.path.isfile('./test/subdir/1.txt')) 717 self.assertFalse(os.path.isdir('./test2/')) 718 719 def test_file_copied_again(self): 720 # used to fail because 'test2' could not be found 721 self.assertTrue(os.path.isfile('./test2/subdir/1.txt')) 722 723 copy_tree("./test2/", "./test/") 724 remove_tree("./test2/") 725 726 self.assertTrue(os.path.isfile('./test/subdir/1.txt')) 727 self.assertFalse(os.path.isdir('./test2/')) 728 729 730class PathlibTest(TestCase): 731 """Regression test for #527""" 732 733 @patchfs 734 def test_cwd(self, fs): 735 """Make sure fake file system is used for os in pathlib""" 736 self.assertEqual(os.path.sep, str(pathlib.Path.cwd())) 737 dot_abs = pathlib.Path(".").absolute() 738 self.assertEqual(os.path.sep, str(dot_abs)) 739 self.assertTrue(dot_abs.exists()) 740 741 742class TestDeprecationSuppression(fake_filesystem_unittest.TestCase): 743 @unittest.skipIf(sys.version_info[1] == 6, 744 'Test fails for Python 3.6 for unknown reason') 745 def test_no_deprecation_warning(self): 746 """Ensures that deprecation warnings are suppressed during module 747 lookup, see #542. 748 """ 749 750 from pyfakefs.tests.fixtures.deprecated_property import \ 751 DeprecationTest # noqa: F401 752 753 with warnings.catch_warnings(record=True) as w: 754 warnings.simplefilter("error", DeprecationWarning) 755 self.setUpPyfakefs() 756 self.assertEqual(0, len(w)) 757 758 759def load_configs(configs): 760 """ Helper code for patching open_code in auto mode, see issue #554. """ 761 retval = [] 762 for config in configs: 763 if len(config) > 3 and config[-3:] == ".py": 764 retval += runpy.run_path(config) 765 else: 766 retval += runpy.run_module(config) 767 return retval 768 769 770class AutoPatchOpenCodeTestCase(fake_filesystem_unittest.TestCase): 771 """ Test patching open_code in auto mode, see issue #554.""" 772 773 def setUp(self): 774 self.setUpPyfakefs(patch_open_code=PatchMode.AUTO) 775 776 self.configpy = 'configpy.py' 777 self.fs.create_file( 778 self.configpy, 779 contents="configurable_value='yup'") 780 self.config_module = 'pyfakefs.tests.fixtures.config_module' 781 782 def test_both(self): 783 load_configs([self.configpy, self.config_module]) 784 785 def test_run_path(self): 786 load_configs([self.configpy]) 787 788 def test_run_module(self): 789 load_configs([self.config_module]) 790 791 792class TestOtherFS(fake_filesystem_unittest.TestCase): 793 def setUp(self): 794 self.setUpPyfakefs() 795 796 def test_real_file_with_home(self): 797 """Regression test for #558""" 798 self.fs.is_windows_fs = os.name != 'nt' 799 self.fs.add_real_file(__file__) 800 with open(__file__) as f: 801 self.assertTrue(f.read()) 802 home = Path.home() 803 if sys.version_info < (3, 6): 804 # fspath support since Python 3.6 805 home = str(home) 806 os.chdir(home) 807 with open(__file__) as f: 808 self.assertTrue(f.read()) 809 810 def test_windows(self): 811 self.fs.os = OSType.WINDOWS 812 path = r'C:\foo\bar' 813 self.assertEqual(path, os.path.join('C:\\', 'foo', 'bar')) 814 self.assertEqual(('C:', r'\foo\bar'), os.path.splitdrive(path)) 815 self.fs.create_file(path) 816 self.assertTrue(os.path.exists(path)) 817 self.assertTrue(os.path.exists(path.upper())) 818 self.assertTrue(os.path.ismount(r'\\share\foo')) 819 self.assertTrue(os.path.ismount(r'C:')) 820 self.assertEqual('\\', os.sep) 821 self.assertEqual('\\', os.path.sep) 822 self.assertEqual('/', os.altsep) 823 self.assertEqual(';', os.pathsep) 824 self.assertEqual('\r\n', os.linesep) 825 self.assertEqual('nul', os.devnull) 826 827 def test_linux(self): 828 self.fs.os = OSType.LINUX 829 path = '/foo/bar' 830 self.assertEqual(path, os.path.join('/', 'foo', 'bar')) 831 self.assertEqual(('', 'C:/foo/bar'), os.path.splitdrive('C:/foo/bar')) 832 self.fs.create_file(path) 833 self.assertTrue(os.path.exists(path)) 834 self.assertFalse(os.path.exists(path.upper())) 835 self.assertTrue(os.path.ismount('/')) 836 self.assertFalse(os.path.ismount('//share/foo')) 837 self.assertEqual('/', os.sep) 838 self.assertEqual('/', os.path.sep) 839 self.assertEqual(None, os.altsep) 840 self.assertEqual(':', os.pathsep) 841 self.assertEqual('\n', os.linesep) 842 self.assertEqual('/dev/null', os.devnull) 843 844 def test_macos(self): 845 self.fs.os = OSType.MACOS 846 path = '/foo/bar' 847 self.assertEqual(path, os.path.join('/', 'foo', 'bar')) 848 self.assertEqual(('', 'C:/foo/bar'), os.path.splitdrive('C:/foo/bar')) 849 self.fs.create_file(path) 850 self.assertTrue(os.path.exists(path)) 851 self.assertTrue(os.path.exists(path.upper())) 852 self.assertTrue(os.path.ismount('/')) 853 self.assertFalse(os.path.ismount('//share/foo')) 854 self.assertEqual('/', os.sep) 855 self.assertEqual('/', os.path.sep) 856 self.assertEqual(None, os.altsep) 857 self.assertEqual(':', os.pathsep) 858 self.assertEqual('\n', os.linesep) 859 self.assertEqual('/dev/null', os.devnull) 860 861 def test_drivelike_path(self): 862 self.fs.os = OSType.LINUX 863 folder = Path('/test') 864 file_path = folder / 'C:/testfile' 865 file_path.parent.mkdir(parents=True) 866 file_path.touch() 867 # use str() to be Python 3.5 compatible 868 os.chdir(str(folder)) 869 self.assertTrue(os.path.exists(str(file_path.relative_to(folder)))) 870 871 872if __name__ == "__main__": 873 unittest.main() 874