def test_relpath(self): tester('ntpath.relpath("a")', 'a') tester('ntpath.relpath(ntpath.abspath("a"))', 'a') tester('ntpath.relpath("a/b")', 'a\\b') tester('ntpath.relpath("../a/b")', '..\\a\\b') with os_helper.temp_cwd(os_helper.TESTFN) as cwd_dir: currentdir = ntpath.basename(cwd_dir) tester('ntpath.relpath("a", "../b")', '..\\' + currentdir + '\\a') tester('ntpath.relpath("a/b", "../c")', '..\\' + currentdir + '\\a\\b') tester('ntpath.relpath("a", "b/c")', '..\\..\\a') tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat') tester( 'ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a') tester('ntpath.relpath("a", "a")', '.') tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat') tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat') tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat') tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..') tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat') tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x') tester('ntpath.relpath("/", "/")', '.') tester('ntpath.relpath("/a", "/a")', '.') tester('ntpath.relpath("/a/b", "/a/b")', '.') tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
def test_isolatedmode(self): self.verify_valid_flag('-I') self.verify_valid_flag('-IEPs') rc, out, err = assert_python_ok( '-I', '-c', 'from sys import flags as f; ' 'print(f.no_user_site, f.ignore_environment, f.isolated, f.safe_path)', # dummyvar to prevent extraneous -E dummyvar="") self.assertEqual(out.strip(), b'1 1 1 True') with os_helper.temp_cwd() as tmpdir: fake = os.path.join(tmpdir, "uuid.py") main = os.path.join(tmpdir, "main.py") with open(fake, "w", encoding="utf-8") as f: f.write("raise RuntimeError('isolated mode test')\n") with open(main, "w", encoding="utf-8") as f: f.write("import uuid\n") f.write("print('ok')\n") # Use -E to ignore PYTHONSAFEPATH env var self.assertRaises(subprocess.CalledProcessError, subprocess.check_output, [sys.executable, '-E', main], cwd=tmpdir, stderr=subprocess.DEVNULL) out = subprocess.check_output([sys.executable, "-I", main], cwd=tmpdir) self.assertEqual(out.strip(), b"ok")
def test_set_pycache_prefix(self): # sys.pycache_prefix can be set from either -X pycache_prefix or # PYTHONPYCACHEPREFIX env var, with the former taking precedence. NO_VALUE = object() # `-X pycache_prefix` with no `=PATH` cases = [ # (PYTHONPYCACHEPREFIX, -X pycache_prefix, sys.pycache_prefix) (None, None, None), ('foo', None, 'foo'), (None, 'bar', 'bar'), ('foo', 'bar', 'bar'), ('foo', '', None), ('foo', NO_VALUE, None), ] for envval, opt, expected in cases: exp_clause = "is None" if expected is None else f'== "{expected}"' code = f"import sys; sys.exit(not sys.pycache_prefix {exp_clause})" args = ['-c', code] env = {} if envval is None else {'PYTHONPYCACHEPREFIX': envval} if opt is NO_VALUE: args[:0] = ['-X', 'pycache_prefix'] elif opt is not None: args[:0] = ['-X', f'pycache_prefix={opt}'] with self.subTest(envval=envval, opt=opt): with os_helper.temp_cwd(): assert_python_ok(*args, **env)
def test_files_list(self): """Make sure the directories are inspected for source files bpo-31920 """ text1 = 'Text to translate1' text2 = 'Text to translate2' text3 = 'Text to ignore' with temp_cwd(None), temp_dir(None) as sdir: os.mkdir(os.path.join(sdir, 'pypkg')) with open(os.path.join(sdir, 'pypkg', 'pymod.py'), 'w', encoding='utf-8') as sfile: sfile.write(f'_({text1!r})') os.mkdir(os.path.join(sdir, 'pkg.py')) with open(os.path.join(sdir, 'pkg.py', 'pymod2.py'), 'w', encoding='utf-8') as sfile: sfile.write(f'_({text2!r})') os.mkdir(os.path.join(sdir, 'CVS')) with open(os.path.join(sdir, 'CVS', 'pymod3.py'), 'w', encoding='utf-8') as sfile: sfile.write(f'_({text3!r})') assert_python_ok(self.script, sdir) with open('messages.pot', encoding='utf-8') as fp: data = fp.read() self.assertIn(f'msgid "{text1}"', data) self.assertIn(f'msgid "{text2}"', data) self.assertNotIn(text3, data)
def main(self, tests=None, **kwargs): self.parse_args(kwargs) self.set_temp_dir() if self.ns.cleanup: self.cleanup() sys.exit(0) test_cwd = self.create_temp_dir() try: # Run the tests in a context manager that temporarily changes the CWD # to a temporary and writable directory. If it's not possible to # create or change the CWD, the original CWD will be used. # The original CWD is available from os_helper.SAVEDCWD. with os_helper.temp_cwd(test_cwd, quiet=True): # When using multiprocessing, worker processes will use test_cwd # as their parent temporary directory. So when the main process # exit, it removes also subdirectories of worker processes. self.ns.tempdir = test_cwd self._main(tests, kwargs) except SystemExit as exc: # bpo-38203: Python can hang at exit in Py_Finalize(), especially # on threading._shutdown() call: put a timeout faulthandler.dump_traceback_later(EXIT_TIMEOUT, exit=True) sys.exit(exc.code)
def test_reload_namespace_changed(self): name = 'spam' with os_helper.temp_cwd(None) as cwd: with test_util.uncache('spam'): with import_helper.DirsOnSysPath(cwd): # Start as a namespace package. self.init.invalidate_caches() bad_path = os.path.join(cwd, name, '__init.py') cached = self.util.cache_from_source(bad_path) expected = { '__name__': name, '__package__': name, '__doc__': None, '__file__': None, } os.mkdir(name) with open(bad_path, 'w') as init_file: init_file.write('eggs = None') module = self.init.import_module(name) ns = vars(module).copy() loader = ns.pop('__loader__') path = ns.pop('__path__') spec = ns.pop('__spec__') ns.pop('__builtins__', None) # An implementation detail. self.assertEqual(spec.name, name) self.assertIsNotNone(spec.loader) self.assertIsNotNone(loader) self.assertEqual(spec.loader, loader) self.assertEqual(set(path), set([os.path.dirname(bad_path)])) with self.assertRaises(AttributeError): # a NamespaceLoader loader.path self.assertEqual(ns, expected) # Change to a regular package. self.init.invalidate_caches() init_path = os.path.join(cwd, name, '__init__.py') cached = self.util.cache_from_source(init_path) expected = { '__name__': name, '__package__': name, '__file__': init_path, '__cached__': cached, '__path__': [os.path.dirname(init_path)], '__doc__': None, 'eggs': None, } os.rename(bad_path, init_path) reloaded = self.init.reload(module) ns = vars(reloaded).copy() loader = ns.pop('__loader__') spec = ns.pop('__spec__') ns.pop('__builtins__', None) # An implementation detail. self.assertEqual(spec.name, name) self.assertEqual(spec.loader, loader) self.assertIs(reloaded, module) self.assertEqual(loader.path, init_path) self.assertEqual(ns, expected)
def test_abspath(self): tester('ntpath.abspath("C:\\")', "C:\\") with os_helper.temp_cwd(os_helper.TESTFN) as cwd_dir: # bpo-31047 tester('ntpath.abspath("")', cwd_dir) tester('ntpath.abspath(" ")', cwd_dir + "\\ ") tester('ntpath.abspath("?")', cwd_dir + "\\?") drive, _ = ntpath.splitdrive(cwd_dir) tester('ntpath.abspath("/abc/")', drive + "\\abc")
def test_temp_cwd__name_none(self): """Test passing None to temp_cwd().""" original_cwd = os.getcwd() with os_helper.temp_cwd(name=None) as new_cwd: self.assertNotEqual(new_cwd, original_cwd) self.assertTrue(os.path.isdir(new_cwd)) self.assertEqual(os.getcwd(), new_cwd) self.assertEqual(os.getcwd(), original_cwd)
def test_directory(self): dirname = os.path.join(os_helper.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb') filename = '\xdf-\u66e8\u66e9\u66eb' with os_helper.temp_cwd(dirname): with open(filename, 'wb') as f: f.write((filename + '\n').encode("utf-8")) os.access(filename, os.R_OK) os.remove(filename)
def test_filename_in_syntaxerror(self): # see issue 38964 with temp_cwd() as cwd: file_path = os.path.join(cwd, 't.py') with open(file_path, 'w') as f: f.write('f"{a b}"') # This generates a SyntaxError _, _, stderr = assert_python_failure(file_path, PYTHONIOENCODING='ascii') self.assertIn(file_path.encode('ascii', 'backslashreplace'), stderr)
def extract_docstrings_from_str(self, module_content): """ utility: return all msgids extracted from module_content """ filename = 'test_docstrings.py' with temp_cwd(None) as cwd: with open(filename, 'w', encoding='utf-8') as fp: fp.write(module_content) assert_python_ok(self.script, '-D', filename) with open('messages.pot', encoding='utf-8') as fp: data = fp.read() return self.get_msgids(data)
def test_script_abspath(self): # pass the script using the relative path, expect the absolute path # in __file__ with os_helper.temp_cwd() as script_dir: self.assertTrue(os.path.isabs(script_dir), script_dir) script_name = _make_test_script(script_dir, 'script') relative_name = os.path.basename(script_name) self._check_script(relative_name, script_name, relative_name, script_dir, None, importlib.machinery.SourceFileLoader)
def test_reload_location_changed(self): name = 'spam' with os_helper.temp_cwd(None) as cwd: with test_util.uncache('spam'): with import_helper.DirsOnSysPath(cwd): # Start as a plain module. self.init.invalidate_caches() path = os.path.join(cwd, name + '.py') cached = self.util.cache_from_source(path) expected = { '__name__': name, '__package__': '', '__file__': path, '__cached__': cached, '__doc__': None, } os_helper.create_empty_file(path) module = self.init.import_module(name) ns = vars(module).copy() loader = ns.pop('__loader__') spec = ns.pop('__spec__') ns.pop('__builtins__', None) # An implementation detail. self.assertEqual(spec.name, name) self.assertEqual(spec.loader, loader) self.assertEqual(loader.path, path) self.assertEqual(ns, expected) # Change to a package. self.init.invalidate_caches() init_path = os.path.join(cwd, name, '__init__.py') cached = self.util.cache_from_source(init_path) expected = { '__name__': name, '__package__': name, '__file__': init_path, '__cached__': cached, '__path__': [os.path.dirname(init_path)], '__doc__': None, } os.mkdir(name) os.rename(path, init_path) reloaded = self.init.reload(module) ns = vars(reloaded).copy() loader = ns.pop('__loader__') spec = ns.pop('__spec__') ns.pop('__builtins__', None) # An implementation detail. self.assertEqual(spec.name, name) self.assertEqual(spec.loader, loader) self.assertIs(reloaded, module) self.assertEqual(loader.path, init_path) self.maxDiff = None self.assertEqual(ns, expected)
def test_find_and_load_checked_pyc(self): # issue 34056 with os_helper.temp_cwd(): with open('mymod.py', 'wb') as fp: fp.write(b'x = 42\n') py_compile.compile( 'mymod.py', doraise=True, invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH, ) file, path, description = imp.find_module('mymod', path=['.']) mod = imp.load_module('mymod', file, path, description) self.assertEqual(mod.x, 42)
def create_modules(modules): with os_helper.temp_cwd(): sys.path.append(os.getcwd()) try: for m in modules: fname = m + '.py' with open(fname, 'w', encoding="utf-8") as f: f.write(textwrap.dedent(modules[m])) linecache.checkcache(fname) importlib.invalidate_caches() yield finally: for m in modules: import_helper.forget(m) sys.path.pop()
def test_basic_discovery(self): """ When findall is called with no parameters or with '.' as the parameter, the dot should be omitted from the results. """ with os_helper.temp_cwd(): os.mkdir('foo') file1 = os.path.join('foo', 'file1.txt') os_helper.create_empty_file(file1) os.mkdir('bar') file2 = os.path.join('bar', 'file2.txt') os_helper.create_empty_file(file2) expected = [file2, file1] self.assertEqual(sorted(filelist.findall()), expected)
def test_POT_Creation_Date(self): """ Match the date format from xgettext for POT-Creation-Date """ from datetime import datetime with temp_cwd(None) as cwd: assert_python_ok(self.script) with open('messages.pot', encoding='utf-8') as fp: data = fp.read() header = self.get_header(data) creationDate = header['POT-Creation-Date'] # peel off the escaped newline at the end of string if creationDate.endswith('\\n'): creationDate = creationDate[:-len('\\n')] # This will raise if the date format does not exactly match. datetime.strptime(creationDate, '%Y-%m-%d %H:%M%z')
def test_nonascii_abspath(self): if (os_helper.TESTFN_UNDECODABLE # Mac OS X denies the creation of a directory with an invalid # UTF-8 name. Windows allows creating a directory with an # arbitrary bytes name, but fails to enter this directory # (when the bytes name is used). and sys.platform not in ('win32', 'darwin')): name = os_helper.TESTFN_UNDECODABLE elif os_helper.TESTFN_NONASCII: name = os_helper.TESTFN_NONASCII else: self.skipTest("need os_helper.TESTFN_NONASCII") with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) with os_helper.temp_cwd(name): self.test_abspath()
def test_abspath_issue3426(self): # Check that abspath returns unicode when the arg is unicode # with both ASCII and non-ASCII cwds. abspath = self.pathmodule.abspath for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): self.assertIsInstance(abspath(path), str) unicwd = '\xe7w\xf0' try: os.fsencode(unicwd) except (AttributeError, UnicodeEncodeError): # FS encoding is probably ASCII pass else: with os_helper.temp_cwd(unicwd): for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): self.assertIsInstance(abspath(path), str)
def test_abspath(self): tester('ntpath.abspath("C:\\")', "C:\\") tester('ntpath.abspath("\\\\?\\C:////spam////eggs. . .")', "\\\\?\\C:\\spam\\eggs") tester('ntpath.abspath("\\\\.\\C:////spam////eggs. . .")', "\\\\.\\C:\\spam\\eggs") tester('ntpath.abspath("//spam//eggs. . .")', "\\\\spam\\eggs") tester('ntpath.abspath("\\\\spam\\\\eggs. . .")', "\\\\spam\\eggs") tester('ntpath.abspath("C:/spam. . .")', "C:\\spam") tester('ntpath.abspath("C:\\spam. . .")', "C:\\spam") tester('ntpath.abspath("C:/nul")', "\\\\.\\nul") tester('ntpath.abspath("C:\\nul")', "\\\\.\\nul") tester('ntpath.abspath("//..")', "\\\\") tester('ntpath.abspath("//../")', "\\\\..\\") tester('ntpath.abspath("//../..")', "\\\\..\\") tester('ntpath.abspath("//../../")', "\\\\..\\..\\") tester('ntpath.abspath("//../../../")', "\\\\..\\..\\") tester('ntpath.abspath("//../../../..")', "\\\\..\\..\\") tester('ntpath.abspath("//../../../../")', "\\\\..\\..\\") tester('ntpath.abspath("//server")', "\\\\server") tester('ntpath.abspath("//server/")', "\\\\server\\") tester('ntpath.abspath("//server/..")', "\\\\server\\") tester('ntpath.abspath("//server/../")', "\\\\server\\..\\") tester('ntpath.abspath("//server/../..")', "\\\\server\\..\\") tester('ntpath.abspath("//server/../../")', "\\\\server\\..\\") tester('ntpath.abspath("//server/../../..")', "\\\\server\\..\\") tester('ntpath.abspath("//server/../../../")', "\\\\server\\..\\") tester('ntpath.abspath("//server/share")', "\\\\server\\share") tester('ntpath.abspath("//server/share/")', "\\\\server\\share\\") tester('ntpath.abspath("//server/share/..")', "\\\\server\\share\\") tester('ntpath.abspath("//server/share/../")', "\\\\server\\share\\") tester('ntpath.abspath("//server/share/../..")', "\\\\server\\share\\") tester('ntpath.abspath("//server/share/../../")', "\\\\server\\share\\") tester('ntpath.abspath("C:\\nul. . .")', "\\\\.\\nul") tester('ntpath.abspath("//... . .")', "\\\\") tester('ntpath.abspath("//.. . . .")', "\\\\") tester('ntpath.abspath("//../... . .")', "\\\\..\\") tester('ntpath.abspath("//../.. . . .")', "\\\\..\\") with os_helper.temp_cwd(os_helper.TESTFN) as cwd_dir: # bpo-31047 tester('ntpath.abspath("")', cwd_dir) tester('ntpath.abspath(" ")', cwd_dir + "\\ ") tester('ntpath.abspath("?")', cwd_dir + "\\?") drive, _ = ntpath.splitdrive(cwd_dir) tester('ntpath.abspath("/abc/")', drive + "\\abc")
def test_security(self): # This test is incomplete since we are normally not run as root and # therefore can't test the file ownership being wrong. with os_helper.temp_cwd(None) as d: fn = os.path.join(d, '.netrc') with open(fn, 'wt') as f: f.write("""\ machine foo.domain.com login bar password pass default login foo password pass """) with os_helper.EnvironmentVarGuard() as environ: environ.set('HOME', d) os.chmod(fn, 0o600) nrc = netrc.netrc() self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, 'pass')) os.chmod(fn, 0o622) self.assertRaises(netrc.NetrcParseError, netrc.netrc)
def test_build(self): # save/restore os.environ def restore_env(old_env): os.environ.clear() os.environ.update(old_env) self.addCleanup(restore_env, dict(os.environ)) def restore_sysconfig_vars(old_config_vars): distutils.sysconfig._config_vars.clear() distutils.sysconfig._config_vars.update(old_config_vars) self.addCleanup(restore_sysconfig_vars, dict(distutils.sysconfig._config_vars)) # Build in a temporary directory with os_helper.temp_cwd(): self.build()
def test_header(self): """Make sure the required fields are in the header, according to: http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry """ with temp_cwd(None) as cwd: assert_python_ok(self.script) with open('messages.pot', encoding='utf-8') as fp: data = fp.read() header = self.get_header(data) self.assertIn("Project-Id-Version", header) self.assertIn("POT-Creation-Date", header) self.assertIn("PO-Revision-Date", header) self.assertIn("Last-Translator", header) self.assertIn("Language-Team", header) self.assertIn("MIME-Version", header) self.assertIn("Content-Type", header) self.assertIn("Content-Transfer-Encoding", header) self.assertIn("Generated-By", header)
def temp_module(name, content='', *, pkg=False): conflicts = [n for n in sys.modules if n.partition('.')[0] == name] with os_helper.temp_cwd(None) as cwd: with uncache(name, *conflicts): with import_helper.DirsOnSysPath(cwd): invalidate_caches() location = os.path.join(cwd, name) if pkg: modpath = os.path.join(location, '__init__.py') os.mkdir(name) else: modpath = location + '.py' if content is None: # Make sure the module file gets created. content = '' if content is not None: # not a namespace package with open(modpath, 'w') as modfile: modfile.write(content) yield location
def main(self, tests=None, **kwargs): global TEMPDIR self.ns = self.parse_args(kwargs) if self.ns.tempdir: TEMPDIR = self.ns.tempdir elif self.ns.worker_args: ns_dict, _ = json.loads(self.ns.worker_args) TEMPDIR = ns_dict.get("tempdir") or TEMPDIR os.makedirs(TEMPDIR, exist_ok=True) # Define a writable temp dir that will be used as cwd while running # the tests. The name of the dir includes the pid to allow parallel # testing (see the -j option). test_cwd = 'test_python_{}'.format(os.getpid()) test_cwd = os.path.join(TEMPDIR, test_cwd) # Run the tests in a context manager that temporarily changes the CWD to a # temporary and writable directory. If it's not possible to create or # change the CWD, the original CWD will be used. The original CWD is # available from os_helper.SAVEDCWD. with os_helper.temp_cwd(test_cwd, quiet=True): self._main(tests, kwargs)
def test_home_not_set(self): with os_helper.temp_cwd(None) as fake_home: fake_netrc_path = os.path.join(fake_home, '.netrc') with open(fake_netrc_path, 'w') as f: f.write('machine foo.domain.com login bar password pass') os.chmod(fake_netrc_path, 0o600) orig_expanduser = os.path.expanduser called = [] def fake_expanduser(s): called.append(s) with os_helper.EnvironmentVarGuard() as environ: environ.set('HOME', fake_home) environ.set('USERPROFILE', fake_home) result = orig_expanduser(s) return result with support.swap_attr(os.path, 'expanduser', fake_expanduser): nrc = netrc.netrc() login, account, password = nrc.authenticators('foo.domain.com') self.assertEqual(login, 'bar') self.assertTrue(called)
def test_temp_cwd(self): here = os.getcwd() with os_helper.temp_cwd(name=TESTFN): self.assertEqual(os.path.basename(os.getcwd()), TESTFN) self.assertFalse(os.path.exists(TESTFN)) self.assertEqual(os.getcwd(), here)
def test_bug7732(self): with os_helper.temp_cwd(): source = os_helper.TESTFN + '.py' os.mkdir(source) self.assertRaisesRegex(ImportError, '^No module', imp.find_module, os_helper.TESTFN, ["."])
def test_build(self): # Build in a temporary directory with os_helper.temp_cwd(): self._test_build()
def check_build(self, std_cpp03, extension_name): # Build in a temporary directory with os_helper.temp_cwd(): self._check_build(std_cpp03, extension_name)