本文整理汇总了Python中test.test_support.temp_cwd方法的典型用法代码示例。如果您正苦于以下问题:Python test_support.temp_cwd方法的具体用法?Python test_support.temp_cwd怎么用?Python test_support.temp_cwd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类test.test_support
的用法示例。
在下文中一共展示了test_support.temp_cwd方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_relpath
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_relpath(self):
tester('ntpath.relpath("a")', 'a')
tester('ntpath.relpath(os.path.abspath("a"))', 'a')
tester('ntpath.relpath("a/b")', 'a\\b')
tester('ntpath.relpath("../a/b")', '..\\a\\b')
with test_support.temp_cwd(test_support.TESTFN) as cwd_dir:
currentdir = os.path.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("//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")', '.')
示例2: test_abspath_issue3426
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
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 (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
self.assertIsInstance(abspath(path), unicode)
unicwd = u'\xe7w\xf0'
try:
fsencoding = test_support.TESTFN_ENCODING or "ascii"
unicwd.encode(fsencoding)
except (AttributeError, UnicodeEncodeError):
# FS encoding is probably ASCII
pass
else:
with test_support.temp_cwd(unicwd):
for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
self.assertIsInstance(abspath(path), unicode)
示例3: test_env
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_env(self):
with test_support.temp_cwd(name=u"tempcwd-中文"):
# os.environ is constructed with FS-encoded values (as in CPython),
# but it will accept unicode additions.
newenv = os.environ.copy()
newenv["TEST_HOME"] = expected = u"首页"
# Environment passed as UTF-16 String[] by Java, arrives FS-encoded.
for encoding in ('utf-8', 'gbk'):
# Emit the value of TEST_HOME explicitly encoded.
p = subprocess.Popen(
[sys.executable, "-c",
'import sys, os;' \
'sys.stdout.write(os.getenv("TEST_HOME")' \
'.decode(sys.getfilesystemencoding())' \
'.encode("%s"))' \
% encoding],
stdout=subprocess.PIPE,
env=newenv)
# Decode with chosen encoding
self.assertEqual(p.stdout.read().decode(encoding), u"首页")
示例4: test_env_naively
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_env_naively(self):
with test_support.temp_cwd(name=u"tempcwd-中文"):
# os.environ is constructed with FS-encoded values (as in CPython),
# but it will accept unicode additions.
newenv = os.environ.copy()
newenv["TEST_HOME"] = expected = u"首页"
# Environment passed as UTF-16 String[] by Java, arrives FS-encoded.
# However, emit TEST_HOME without thinking about the encoding.
p = subprocess.Popen(
[sys.executable, "-c",
'import sys, os;' \
'sys.stdout.write(os.getenv("TEST_HOME"))'],
stdout=subprocess.PIPE,
env=newenv)
# Decode with default encoding utf-8 (because ... ?)
self.assertEqual(p.stdout.read().decode('utf-8'), expected)
示例5: test_system_no_site_import
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_system_no_site_import(self):
# If not importing site (-S), importing traceback previously
# would fail with an import error due to creating a circular
# import chain. This root cause is because the os module
# imports the subprocess module for the system function; but
# the subprocess module imports from os. Verifies that this is
# managed by making the import late; also verify the
# monkeypatching optimization is successful by calling
# os.system twice.
with test_support.temp_cwd() as temp_cwd:
self.assertEqual(
subprocess.check_output(
[sys.executable, "-S", "-c",
"import traceback; import os; os.system('echo 42'); os.system('echo 47')"])\
.replace("\r", ""), # in case of running on Windows
"42\n47\n")
示例6: test_system_uses_os_environ
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_system_uses_os_environ(self):
"""Writing to os.environ is made available as env vars in os.system subprocesses"""
# This test likely requires additional customization for
# environments like AS/400, but I do not have current access.
# Verifies fix for http://bugs.jython.org/issue2416
if os._name == "nt":
echo_command = 'echo %TEST_ENVVAR%'
else:
echo_command = 'echo $TEST_ENVVAR'
with test_support.temp_cwd() as temp_cwd:
self.assertEqual(
subprocess.check_output(
[sys.executable, "-c",
"import os; os.environ['TEST_ENVVAR'] = 'works on 2.7.1+'; os.system('%s')" % echo_command])\
.replace("\r", ""), # in case of running on Windows
"works on 2.7.1+\n")
示例7: test_directory
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_directory(self):
dirname = os.path.join(test_support.TESTFN,
u'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
filename = u'\xdf-\u66e8\u66e9\u66eb'
with test_support.temp_cwd(dirname):
with open(filename, 'w') as f:
f.write((filename + '\n').encode("utf-8"))
os.access(filename,os.R_OK)
os.remove(filename)
示例8: test_nonascii_abspath
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_nonascii_abspath(self):
# Test non-ASCII, non-UTF8 bytes in the path.
with test_support.temp_cwd('\xe7w\xf0'):
self.test_abspath()
示例9: test_empty_python_home
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_empty_python_home(self):
# http://bugs.jython.org/issue2283
with test_support.temp_cwd() as temp_cwd:
# using a new directory ensures no Lib/ directory is available
self.assertEqual(
subprocess.check_output(
[sys.executable, "-Dpython.home=", "-c",
"import os; os.system('echo 42'); os.system('echo 47')"])\
.replace("\r", ""), # in case of running on Windows
"42\n47\n")
示例10: test_bad_python_home
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_bad_python_home(self):
# http://bugs.jython.org/issue2283
with test_support.temp_cwd() as temp_cwd:
os.makedirs(os.path.join(temp_cwd, "Lib"))
with self.assertRaises(subprocess.CalledProcessError) as cm:
subprocess.check_output(
[sys.executable, "-Dpython.home=%s" % temp_cwd, "-c",
"print 42"],
stderr=subprocess.STDOUT)
self.assertIn(
'Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site',
cm.exception.output)
示例11: test_property_no_site_import
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_property_no_site_import(self):
# only the minimal set of modules are imported
with test_support.temp_cwd() as temp_cwd:
self.assertEqual(
subprocess.check_output(
[sys.executable, "-Dpython.import.site=false", "-c",
"import sys; print sorted(sys.modules.keys())"]).strip(),
"['__builtin__', '__main__', 'exceptions', 'sys']")
示例12: test_options_no_site_import
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_options_no_site_import(self):
with test_support.temp_cwd() as temp_cwd:
self.assertEqual(
subprocess.check_output(
[sys.executable, "-S", "-c",
"import sys; print sorted(sys.modules.keys())"]).strip(),
"['__builtin__', '__main__', 'exceptions', 'sys']")
示例13: test_compileall
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_compileall(self):
with temp_cwd():
PACKAGE = os.path.realpath("./greetings")
PYC_GREETER = os.path.join(PACKAGE, "greeter.pyc")
PYCLASS_GREETER = os.path.join(PACKAGE, "greeter$py.class")
PYCLASS_TEST = os.path.join(PACKAGE, "test$py.class")
os.mkdir(PACKAGE)
self.write_code(
PACKAGE, "greeter.py",
"""
def greet():
print 'Hello world!'
""")
self.write_code(
PACKAGE, "test.py",
"""
from greeter import greet
greet()
""")
# pretend we have a Python bytecode compiler by touching this file
open(PYC_GREETER, "a").close()
compileall.compile_dir(PACKAGE, quiet=True)
self.assertTrue(os.path.exists(PYC_GREETER)) # still exists
self.assertTrue(os.path.exists(PYCLASS_TEST)) # along with these new compiled files
self.assertTrue(os.path.exists(PYCLASS_GREETER))
# verify we can work with just compiled files
os.unlink(os.path.join(PACKAGE, "greeter.py"))
self.assertEqual(
subprocess.check_output([sys.executable, os.path.join(PACKAGE, "test.py")]).rstrip(),
"Hello world!")
示例14: test_getcwd
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_getcwd(self):
with test_support.temp_cwd(name=u"tempcwd-中文") as temp_cwd:
p = subprocess.Popen([sys.executable, "-c",
'import sys,os;' \
'sys.stdout.write(os.getcwd().encode("utf-8"))'],
stdout=subprocess.PIPE)
self.assertEqual(p.stdout.read().decode("utf-8"), temp_cwd)
示例15: test_link
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import temp_cwd [as 别名]
def test_link(self):
with test_support.temp_cwd() as new_cwd:
target = os.path.join(new_cwd, "target")
with open(target, "w") as f:
f.write("TARGET")
source = os.path.join(new_cwd, "source")
os.link(target, source)
with open(source, "r") as f:
self.assertEqual(f.read(), "TARGET")