1818Test the :py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class.
1919"""
2020import glob
21+ import importlib .util
2122import io
2223import multiprocessing
2324import os
2829import tempfile
2930import unittest
3031import warnings
32+ from contextlib import redirect_stdout
33+ from io import StringIO
3134from pathlib import Path
3235from unittest import TestCase , mock
3336
@@ -228,14 +231,12 @@ def test_import_function_from_os_as_other_name(self):
228231 stat_result = pyfakefs .tests .import_as_example .file_stat2 (file_path )
229232 self .assertEqual (3 , stat_result .st_size )
230233
231- @unittest .skipIf (sys .version_info >= (3 , 12 ), "Currently not working in 3.12" )
232234 def test_import_open_as_other_name (self ):
233235 file_path = "/foo/bar"
234236 self .fs .create_file (file_path , contents = b"abc" )
235237 contents = pyfakefs .tests .import_as_example .file_contents1 (file_path )
236238 self .assertEqual ("abc" , contents )
237239
238- @unittest .skipIf (sys .version_info >= (3 , 12 ), "Currently not working in 3.12" )
239240 def test_import_io_open_as_other_name (self ):
240241 file_path = "/foo/bar"
241242 self .fs .create_file (file_path , contents = b"abc" )
@@ -398,10 +399,6 @@ def test_fake_path_does_not_exist7(self):
398399 self .fs .create_file ("foo" )
399400 self .assertFalse (pyfakefs .tests .import_as_example .check_if_exists7 ("foo" ))
400401
401- @unittest .skipIf (
402- sys .version_info >= (3 , 12 ),
403- "Skip modules currently not working for open in 3.12" ,
404- )
405402 def test_open_succeeds (self ):
406403 pyfakefs .tests .import_as_example .open_this_file ()
407404
@@ -447,10 +444,6 @@ def test_fake_path_does_not_exist7(self):
447444 self .fs .create_file ("foo" )
448445 self .assertFalse (pyfakefs .tests .import_as_example .check_if_exists7 ("foo" ))
449446
450- @unittest .skipIf (
451- sys .version_info >= (3 , 12 ),
452- "Skip modules currently not working for open in 3.12" ,
453- )
454447 def test_open_succeeds (self ):
455448 pyfakefs .tests .import_as_example .open_this_file ()
456449
@@ -787,6 +780,7 @@ def load_configs(configs):
787780 return retval
788781
789782
783+ @unittest .skipIf (sys .version_info < (3 , 8 ), "open_code new in Python 3.8" )
790784class AutoPatchOpenCodeTestCase (fake_filesystem_unittest .TestCase ):
791785 """Test patching open_code in auto mode, see issue #554."""
792786
@@ -806,6 +800,18 @@ def test_run_path(self):
806800 def test_run_module (self ):
807801 load_configs ([self .config_module ])
808802
803+ def import_foo (self ):
804+ spec = importlib .util .spec_from_file_location ("bar" , "/foo/bar.py" )
805+ mod = importlib .util .module_from_spec (spec )
806+ spec .loader .exec_module (mod )
807+
808+ @unittest .skipIf (sys .platform == "win32" , "Not yet working under Windows" )
809+ def test_exec_module_in_fake_fs (self ):
810+ self .fs .create_file ("/foo/bar.py" , contents = "print('hello')" )
811+ with redirect_stdout (StringIO ()) as stdout :
812+ self .import_foo ()
813+ assert stdout .getvalue () == "hello\n "
814+
809815
810816class TestOtherFS (fake_filesystem_unittest .TestCase ):
811817 def setUp (self ):
0 commit comments