本文整理汇总了Python中test.test_support.temp_cwd函数的典型用法代码示例。如果您正苦于以下问题:Python temp_cwd函数的具体用法?Python temp_cwd怎么用?Python temp_cwd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了temp_cwd函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_compileall
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!")
示例2: test_options_no_site_import
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']")
示例3: test_getcwd
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)
示例4: test_readlink_nonexistent
def test_readlink_nonexistent(self):
with test_support.temp_cwd() as new_cwd:
nonexistent_file = os.path.join(new_cwd, "nonexistent-file")
with self.assertRaises(OSError) as cm:
os.readlink(nonexistent_file)
self.assertEqual(cm.exception.errno, errno.ENOENT)
self.assertEqual(cm.exception.filename, nonexistent_file)
示例5: test_property_no_site_import
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']")
示例6: test_directory
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)
示例7: test_link
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")
示例8: test_bad_symlink
def test_bad_symlink(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")
with self.assertRaises(OSError) as cm:
os.symlink(source, target) # reversed args!
self.assertEqual(cm.exception.errno, errno.EEXIST)
示例9: test_getcwdu
def test_getcwdu(self):
with test_support.temp_cwd(name=u"tempcwd-中文") as temp_cwd:
# os.getcwdu reports the working directory as unicode,
# which must be encoded for subprocess communication.
p = subprocess.Popen([
sys.executable, "-c",
'import sys,os;' \
'sys.stdout.write(os.getcwdu().encode(sys.getfilesystemencoding()))'],
stdout=subprocess.PIPE)
self.assertEqual(p.stdout.read(), temp_cwd)
示例10: test_readlink
def test_readlink(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.symlink(target, source)
self.assertEqual(os.readlink(source), target)
self.assertEqual(os.readlink(unicode(source)), unicode(target))
self.assertIsInstance(os.readlink(unicode(source)), unicode)
示例11: test_empty_python_home
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")
示例12: test_env
def test_env(self):
with test_support.temp_cwd(name=u"tempcwd-中文"):
newenv = os.environ.copy()
newenv["TEST_HOME"] = u"首页"
p = subprocess.Popen([sys.executable, "-c",
'import sys,os;' \
'sys.stdout.write(os.getenv("TEST_HOME").encode("utf-8"))'],
stdout=subprocess.PIPE,
env=newenv)
self.assertEqual(p.stdout.read().decode("utf-8"), u"首页")
示例13: test_getcwd
def test_getcwd(self):
with test_support.temp_cwd(name=u"tempcwd-中文") as temp_cwd:
# os.getcwd reports the working directory as an FS-encoded str,
# which is also the encoding used in subprocess communication.
p = subprocess.Popen([
sys.executable, "-c",
'import sys,os;' \
'sys.stdout.write(os.getcwd())'],
stdout=subprocess.PIPE)
self.assertEqual(p.stdout.read(), temp_cwd)
示例14: test_readlink_non_symlink
def test_readlink_non_symlink(self):
"""os.readlink of a non symbolic link should raise an error"""
# test for http://bugs.jython.org/issue2292
with test_support.temp_cwd() as new_cwd:
target = os.path.join(new_cwd, "target")
with open(target, "w") as f:
f.write("TARGET")
with self.assertRaises(OSError) as cm:
os.readlink(target)
self.assertEqual(cm.exception.errno, errno.EINVAL)
self.assertEqual(cm.exception.filename, target)
示例15: test_unicode_argv
def test_unicode_argv(self):
# Unicode roundtrips successfully through sys.argv arguments
zhongwen = u'\u4e2d\u6587'
with test_support.temp_cwd(name=u"tempcwd-%s" % zhongwen):
p = subprocess.Popen(
[sys.executable, '-c',
'import sys;' \
'sys.stdout.write(sys.argv[1].encode("utf-8"))',
zhongwen],
stdout=subprocess.PIPE)
self.assertEqual(p.stdout.read().decode("utf-8"), zhongwen)