1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
|
"""Test chdir
Made for Jython.
"""
import imp
import os
import py_compile
import shutil
import subprocess
import sys
import tempfile
import unittest
import zipfile
import zipimport
from test import test_support
COMPILED_SUFFIX = [suffix for suffix, mode, type in imp.get_suffixes()
if type == imp.PY_COMPILED][0]
WINDOWS = (os._name if test_support.is_jython else os.name) == 'nt'
CODE1 = """result = 'result is %r' % (100.0 * (3.0 / 5.0))"""
CODE1_RESULT = 'result is %r' % (100.0 * (3.0 / 5.0))
CODE2 = 'raise NotImplementedError()'
# The following are their corresponding Java code compiled with javac 1.5
"""
public class ChdirFooTest {
public static String foo = "bar";
}
"""
CHDIR_FOO_CLASS = """\
eJxdjr1uwjAUhc8F8ktaQspKJTZgAHUuYkFiQnQAdXeCoUYhlkzoe1UdKnXoA/BQFddR1VYs91wf
fz4+5+/PLwAP6IRw0PIRB2ig7SHxcEeob7UmJIu9eBXjXBS78ao0qtg9EtyJKlQ5ZaY/eCY0Znoj
Ca2FKuTydEilWYs0Z8efZPkPGa70yWRyrqzfnr1slJlrvZbHcmQ/iOAj4LxUmAguPEL0nyHEfzWe
0r3MSvS4rMP9CYF9wVuNd47hGfKpy0qszvAD9FaBTZ5uZVow+kXvK4/vkto76tesjb2p9PYCbxA0
PQ==
""".decode('base64').decode('zlib')
"""
package chdir_bar_test;
public class ChdirBarTest {
public static String bar = "foo";
}
"""
CHDIR_BAR_CLASS = """\
eJxdj71Ow0AQhGfjf2NIYugQSHRAgUWdiIJIVBEUiWijs3OEi4wtHQ7vhSiQKHgAHgoxthAgmt29
uW/n5j4+394BnGMvhod+iEEEF8MAaYBdgZMrK0ina/WkslJVq2zWWFOtRgJ/bCrTXJA5PrkVuJN6
qQX9qan09eYh13au8pJKOC7KbzKe1Rtb6CvT6sPJ/dLYS2Xn+rE5ax9IECKi311dJ/ARCPaLllkw
xKIhlf1dEQx+U93ka100OGJ2j98RRK0Bpx5nurLGPB2wC7t3+gp57sAtVr8THYLJD3rYabxLey9w
/rMu63Znv/MFLEM6Hg==
""".decode('base64').decode('zlib')
# This is a jar archive of the following Java code compiled with javac 1.5
"""
public class ChdirJyTest {
public static String jy = "thon";
}
"""
CHDIR_JY_JAR = """\
eJwL8GZmEWHgAMLaXf3mDEiAk4GFwdc1xFHX089N/98pBgZmhgBvdg6QFBNUSQBOzSJADNfs6+jn
6eYaHKLn6/bZ98xpH29dvYu83rpa586c3xxkcMX4wdMiPS9fHU/fi6WrWDhnvJI8Ij1DO8NCTOTJ
Ei2L58/FpkxbIvZSo+KZ6uvMT0UfixjB7liq2l/iBrTFDcUdP3agukMQiJ0zUjKLvCpDUotL9JJz
EouLY/v2eh1xEGk9fzavzvx38pbQDcHczV/1zBi5I7fcSJif0p7iNMdhDsfviCTFiB8MdlMq1E9o
mX2/Y/n57uGff6r+MxS0LmYweyYnZnfMokBwp4VKxNc+gbWv31h2z/aZ+tGtPGiqd9PmSV6vsg+9
ePOn0cDv1lqFM9eDOnf/MxSPnfWU1/reqoy6EwmXv8Uq5i35KO65derdt3qi/r8uPWn/8KB4wroo
0UON3jP+JP54tP1obVy30+Mpq2/dmfdgNvM9nXuWbekH1ieHJW3Vv+egz2zx8CXPMsbDtlVGFX/E
I9dL/E33fVNy4aY5KHj+n9nN+QDoeUNGUPAwMokwoEYULApBsYwKUOIcXStyqIugaLPFEePIJoBi
B9lhgigmHMUWVwHerGwgSWYg3AOknzCCeAAyK9gW
""".decode('base64').decode('zlib')
class BaseChdirTestCase(unittest.TestCase):
"""Base TestCase for chdir tests.
TEST_FILES is the number of available test files allocated for the
test (residing in the first TEST_DIR).
Their names are safe for use as a Python module name and available
at self.filename1 - self.filenameN. os.path.basename(self.filenameN)
is available at self.basename1 - self.basenameN.
TEST_DIRS is the number of directories to be created by the fixture,
available at self.dir1 - self.dirN. At least 1 is always created.
FILE_SUFFIX is a suffix passed to mktemp for TEST_FILES.
FIXTURE_CHDIR is whether or not the fixture should chdir to the
first dir. It always changes back to the original working dir after
the test regardless of this setting.
SYSPATH is an iterable of entries to be added (if they don't already
exist) to sys.path for the duration of the test.
"""
TEST_FILES = 1
TEST_DIRS = 1
FILE_SUFFIX = ''
FIXTURE_CHDIR = True
SYSPATH = ()
def setUp(self):
for i in max(1, range(self.TEST_DIRS)):
setattr(self, 'dir%i' % (i + 1), tempfile.mkdtemp())
for i in range(self.TEST_FILES):
filename = safe_mktemp(dir=self.dir1, suffix=self.FILE_SUFFIX)
basename = os.path.basename(filename)
setattr(self, 'filename%i' % (i + 1), filename)
setattr(self, 'basename%i' % (i + 1), basename)
self.orig_cwd = os.getcwd()
self.orig_syspath = sys.path[:]
sys.path.extend([path for path in self.SYSPATH
if path not in sys.path])
if self.FIXTURE_CHDIR:
os.chdir(self.dir1)
def tearDown(self):
try:
try:
for i in range(self.TEST_DIRS):
dir = getattr(self, 'dir%i' % (i + 1))
if os.path.exists(dir):
shutil.rmtree(dir)
finally:
for i in range(self.TEST_FILES):
filename = getattr(self, 'filename%i' % (i + 1))
if os.path.exists(filename):
os.remove(filename)
finally:
sys.path = self.orig_syspath
os.chdir(self.orig_cwd)
class ChdirTestCase(BaseChdirTestCase):
FIXTURE_CHDIR = False
def setUp(self):
super(ChdirTestCase, self).setUp()
self.subdir1 = os.path.join(self.dir1, 'subdir1')
self.subdir2 = os.path.join(self.dir1, 'subdir1', 'subdir2')
os.makedirs(self.subdir2)
def test_chdir(self):
orig = os.path.realpath(os.getcwd())
self.assertNotEqual(orig, self.dir1)
os.chdir(self.dir1)
cwd = os.path.realpath(os.getcwd())
self.assertEqual(os.path.realpath(self.dir1), cwd)
self.assertNotEqual(orig, cwd)
def test_relative_chdir(self):
top = os.path.dirname(self.dir1)
os.chdir(top)
os.chdir(os.path.basename(self.dir1))
self.assertEqual(os.getcwd(), os.path.realpath(self.dir1))
os.chdir('subdir1')
self.assertEqual(os.getcwd(), os.path.realpath(self.subdir1))
os.chdir('subdir2')
self.assertEqual(os.getcwd(), os.path.realpath(self.subdir2))
os.chdir('..')
self.assertEqual(os.getcwd(), os.path.realpath(self.subdir1))
os.chdir('..%s..' % os.sep)
self.assertEqual(os.getcwd(), os.path.realpath(top))
def test_invalid_chdir(self):
raises(OSError,
'[Errno 2] %s: %r' % (os.strerror(2), self.filename1),
os.chdir, self.filename1)
open(self.filename1, 'w').close()
raises(OSError,
'[Errno 20] %s: %r' % (os.strerror(20), self.filename1),
os.chdir, self.filename1)
class WindowsChdirTestCase(BaseChdirTestCase):
FIXTURE_CHDIR = False
def setUp(self):
super(WindowsChdirTestCase, self).setUp()
self.subdir = os.path.join(self.dir1, 'Program Files')
os.makedirs(self.subdir)
def test_windows_chdir_dos_path(self):
dos_name = os.path.join(self.dir1, 'progra~1')
os.chdir(dos_name)
self.assertEqual(os.getcwd(), os.path.realpath(dos_name))
def test_windows_getcwd_ensures_drive_letter(self):
drive = os.path.splitdrive(self.subdir)[0]
os.chdir('\\')
self.assertEqual(os.path.normcase(os.getcwd()),
os.path.normcase(os.path.join(drive, '\\')))
def test_windows_chdir_slash_isabs(self):
drive = os.path.splitdrive(os.getcwd())[0]
os.chdir('/')
self.assertEqual(os.path.normcase(os.getcwd()),
os.path.normcase(os.path.join(drive, '\\')))
class BaseImportTestCase(BaseChdirTestCase):
FILE_SUFFIX = '.py'
SYSPATH = ('',)
def setUp(self):
super(BaseImportTestCase, self).setUp()
write(self.filename1, CODE1)
self.mod_name = self.basename1[:-3]
if self.mod_name in sys.modules:
del sys.modules[self.mod_name]
def tearDown(self):
super(BaseImportTestCase, self).tearDown()
if self.mod_name in sys.modules:
del sys.modules[self.mod_name]
class ImportTestCase(BaseImportTestCase):
def setUp(self):
super(ImportTestCase, self).setUp()
self.bytecode = os.path.join(self.dir1,
self.mod_name + COMPILED_SUFFIX)
def test_import_module(self):
__import__(self.mod_name)
self.assert_(self.mod_name in sys.modules)
mod = sys.modules[self.mod_name]
self.assertEqual(mod.__file__, self.basename1)
self.assert_(os.path.exists(self.bytecode))
def test_import_bytecode(self):
py_compile.compile(self.basename1)
self.assert_(os.path.exists(self.bytecode))
os.remove(self.filename1)
__import__(self.mod_name)
self.assert_(self.mod_name in sys.modules)
mod = sys.modules[self.mod_name]
self.assertEqual(mod.__file__, self.mod_name + COMPILED_SUFFIX)
def test_imp_find_module(self):
module_info = imp.find_module(self.mod_name)
self.assertEqual(module_info[1], self.basename1)
module_info[0].close()
def test_imp_load_module(self):
module_info = imp.find_module(self.mod_name)
self.assertEqual(module_info[1], self.basename1)
mod = imp.load_module(self.mod_name, *module_info)
self.assertEqual(mod.__file__, self.basename1)
self.assert_(os.path.exists(self.bytecode))
module_info[0].close()
def test_imp_load_source(self):
mod = imp.load_source(self.mod_name, self.basename1)
self.assertEqual(mod.__file__, self.basename1)
self.assert_(os.path.exists(self.bytecode))
def test_imp_load_compiled(self):
__import__(self.mod_name)
self.assertTrue(os.path.exists(self.bytecode))
basename = os.path.basename(self.bytecode)
mod = imp.load_compiled(self.mod_name, basename)
self.assertEqual(mod.__file__, basename)
class ImportPackageTestCase(BaseChdirTestCase):
SYSPATH = ('',)
def setUp(self):
super(ImportPackageTestCase, self).setUp()
self.package_name = 'chdir_test'
self.package_path = os.path.join(self.dir1, self.package_name)
os.mkdir(self.package_path)
write(os.path.join(self.package_path, '__init__.py'), CODE1)
self.bytecode = os.path.join(self.package_path,
'__init__' + COMPILED_SUFFIX)
self.relative_source = os.path.join(self.package_name, '__init__.py')
if self.package_name in sys.modules:
del sys.modules[self.package_name]
def tearDown(self):
super(ImportPackageTestCase, self).tearDown()
if self.package_name in sys.modules:
del sys.modules[self.package_name]
def test_import_package(self):
__import__(self.package_name)
self.assert_(self.package_name in sys.modules)
package = sys.modules[self.package_name]
self.assertEqual(package.__path__, [self.package_name])
self.assertEqual(package.__file__, self.relative_source)
self.assert_(os.path.exists(self.bytecode))
def test_imp_find_module_package(self):
module_info = imp.find_module(self.package_name)
self.assertEqual(module_info[1], self.package_name)
def test_imp_load_module_package(self):
module_info = imp.find_module(self.package_name)
mod = imp.load_module(self.package_name, *module_info)
self.assertEqual(mod.__file__, self.relative_source)
self.assert_(os.path.exists(self.bytecode))
def test_imp_load_source_package(self):
mod = imp.load_source(self.package_name, self.relative_source)
self.assertEqual(mod.__file__, self.relative_source)
self.assert_(os.path.exists(self.bytecode))
class ZipimportTestCase(BaseImportTestCase):
def setUp(self):
super(ZipimportTestCase, self).setUp()
zip = zipfile.ZipFile(self.filename1 + '.zip', 'w')
zip.write(self.basename1)
zip.close()
os.remove(self.filename1)
def test_zipimport(self):
importer = zipimport.zipimporter(self.basename1 + '.zip')
self.assertEqual(importer.archive, self.basename1 + '.zip')
self.assertEqual(importer.prefix, '')
self.assert_(self.basename1 in importer._files)
# Ensure correct cache entry paths
entry = importer._files[self.basename1]
self.assertEqual(entry[0], os.path.join(self.basename1 + '.zip',
self.basename1))
# Ensure sane import machinery
self.assertEqual(importer.find_module(self.mod_name), importer)
# Ensure data lookup from the zip file
self.assertEqual(importer.get_source(self.mod_name), CODE1)
class PyCompileTestCase(BaseImportTestCase):
def test_compile(self):
py_compile.compile(self.basename1)
self.assert_(os.path.exists(self.filename1[:-3] + COMPILED_SUFFIX))
__import__(self.mod_name)
self.assert_(self.mod_name in sys.modules)
mod = sys.modules[self.mod_name]
self.assertEqual(mod.__file__, self.mod_name + COMPILED_SUFFIX)
def test_compile_dest(self):
py_compile.compile(self.basename1,
self.basename1[:-3] +
'chdir_test' + COMPILED_SUFFIX)
self.assert_(os.path.exists(self.filename1[:-3] + 'chdir_test' +
COMPILED_SUFFIX))
mod_name = self.mod_name + 'chdir_test'
__import__(mod_name)
self.assert_(mod_name in sys.modules)
mod = sys.modules[mod_name]
self.assertEqual(mod.__file__, mod_name + COMPILED_SUFFIX)
class SubprocessTestCase(BaseChdirTestCase):
TEST_DIRS = 2
# Write out the external app's cwd to a file we'll specify in setUp
COMMAND = '''\
"%s" -c "import os; fp = open(r'%%s', 'w'); fp.write(os.getcwd())"''' % \
sys.executable
def test_popen(self):
os.popen(self._command()).read()
self.assertEqual(read(self.filename1), os.getcwd())
os.chdir(self.dir2)
os.popen(self._command()).read()
self.assertEqual(read(self.filename1), os.getcwd())
def test_system(self):
self.assertEqual(os.system(self._command()), 0)
self.assertEqual(read(self.filename1), os.getcwd())
os.chdir(self.dir2)
self.assertEqual(os.system(self._command()), 0)
self.assertEqual(read(self.filename1), os.getcwd())
def test_subprocess(self):
self.assertEqual(subprocess.call(self._command(), shell=True), 0)
self.assertEqual(read(self.filename1), os.getcwd())
os.chdir(self.dir2)
self.assertEqual(subprocess.call(self._command(), shell=True), 0)
self.assertEqual(read(self.filename1), os.getcwd())
def _command(self):
command = self.COMMAND % self.filename1
if WINDOWS:
command = '"%s"' % command
return command
class ExecfileTestCase(BaseChdirTestCase):
FIXTURE_CHDIR = False
TEST_DIRS = 2
def setUp(self):
super(ExecfileTestCase, self).setUp()
write(self.filename1, CODE1)
def test_execfile(self):
globals = {}
execfile(self.filename1, globals)
self.assertEqual(globals['result'], CODE1_RESULT)
# filename1 lives in dir1
os.chdir(self.dir1)
globals = {}
execfile(self.basename1, globals)
self.assertEqual(globals['result'], CODE1_RESULT)
os.chdir(self.dir2)
raises(IOError, 2, execfile, self.basename1, globals)
class ExecfileTracebackTestCase(BaseChdirTestCase):
def setUp(self):
super(ExecfileTracebackTestCase, self).setUp()
write(self.filename1, CODE2)
def test_execfile_traceback(self):
globals = {}
try:
execfile(self.basename1, globals)
except NotImplementedError:
tb = sys.exc_info()[2]
self.assertEqual(tb.tb_next.tb_frame.f_code.co_filename,
self.basename1)
class ListdirTestCase(BaseChdirTestCase):
TEST_DIRS = 2
FIXTURE_CHDIR = False
def setUp(self):
super(ListdirTestCase, self).setUp()
open(os.path.join(self.dir1, 'chdir_test1'), 'w').close()
open(os.path.join(self.dir2, 'chdir_test2'), 'w').close()
def test_listdir(self):
dir1 = os.listdir(self.dir1)
dir2 = os.listdir(self.dir2)
os.chdir(self.dir1)
self.assertEqual(os.listdir('.'), dir1, os.listdir(self.dir1))
os.chdir(self.dir2)
self.assertEqual(os.listdir('.'), dir2, os.listdir(self.dir2))
class DirsTestCase(BaseChdirTestCase):
def test_makedirs(self):
self.assert_(not os.path.exists(self.filename1))
subdir = os.path.join(self.basename1, 'chdir_test')
os.makedirs(subdir)
self.assert_(os.path.isdir(self.filename1))
self.assert_(os.path.isdir(os.path.join(self.filename1, 'chdir_test')))
def test_mkdir(self):
self.assert_(not os.path.exists(self.filename1))
os.mkdir(self.basename1)
self.assert_(os.path.isdir(self.filename1))
def test_rmdir(self):
os.mkdir(self.filename1)
self.assert_(os.path.exists(self.filename1))
self.assert_(os.path.exists(self.basename1))
os.rmdir(self.basename1)
self.assert_(not os.path.exists(self.filename1))
def test_isdir(self):
self.assert_(not os.path.isdir(self.basename1))
os.mkdir(self.filename1)
self.assert_(os.path.isdir(self.basename1))
class FilesTestCase(BaseChdirTestCase):
def setUp(self):
super(FilesTestCase, self).setUp()
write(self.filename1, 'test')
def test_remove(self):
os.remove(self.basename1)
self.assert_(not os.path.exists(self.filename1))
def test_rename(self):
os.rename(self.basename1, 'chdir_test')
self.assert_(not os.path.exists(self.filename1))
self.assert_(os.path.exists(os.path.join(self.dir1, 'chdir_test')))
def test_stat(self):
self.assertEqual(os.stat(self.basename1).st_size, 4)
def test_utime(self):
new_utime = os.stat(self.basename1).st_mtime + 100
os.utime(self.basename1, (new_utime, new_utime))
self.assertEqual(os.stat(self.basename1).st_mtime,
os.stat(self.filename1).st_mtime)
def test_open(self):
fp = open(self.basename1)
self.assertEqual(fp.name, self.basename1)
self.assert_(repr(fp).startswith("<open file '%s'" % self.basename1))
self.assertEqual(fp.read(), 'test')
fp.close()
def test_os_open(self):
fd = os.open(self.basename1, os.O_RDONLY)
self.assertEqual(os.read(fd, 4), 'test')
os.close(fd)
def test_exists(self):
self.assert_(os.path.exists(self.basename1))
def test_isfile(self):
self.assert_(os.path.isfile(self.basename1))
def test_abspath(self):
self.assertEqual(os.path.join(os.getcwd(), self.basename1),
os.path.abspath(self.basename1))
def test_realpath(self):
self.assertEqual(os.path.realpath(self.filename1),
os.path.realpath(self.basename1))
def test_getsize(self):
self.assertEqual(os.path.getsize(self.basename1), 4)
def test_getmtime(self):
self.assertEqual(os.path.getmtime(self.filename1),
os.path.getmtime(self.basename1))
def test_getatime(self):
self.assertEqual(os.path.getatime(self.filename1),
os.path.getatime(self.basename1))
class SymlinkTestCase(BaseChdirTestCase):
TEST_DIRS = 2
def setUp(self):
super(SymlinkTestCase, self).setUp()
self.relsrc = 'src'
self.src = os.path.join(self.dir1, self.relsrc)
self.link = os.path.join(self.dir2, 'link')
def test_symlink_src_is_relative(self):
write(self.src, 'foo')
# Ensure that linking to os.path.basename(self.src) creates a
# dead link (as self.src lives in a different dir than
# self.link)
os.symlink(self.relsrc, self.link)
# If the cwd (self.dir1) was applied to os.link's src arg then
# the link would not be dead
self.assertTrue(self.isdeadlink(self.link))
def isdeadlink(self, link):
return os.path.lexists(link) and not os.path.exists(link)
class ImportJavaClassTestCase(BaseChdirTestCase):
SYSPATH = ('',)
def setUp(self):
super(ImportJavaClassTestCase, self).setUp()
write(os.path.join(self.dir1, 'ChdirFooTest.class'), CHDIR_FOO_CLASS)
package_path = os.path.join(self.dir1, 'chdir_bar_test')
os.mkdir(package_path)
write(os.path.join(package_path, 'ChdirBarTest.class'),
CHDIR_BAR_CLASS)
def tearDown(self):
super(ImportJavaClassTestCase, self).tearDown()
if 'ChdirFooTest' in sys.modules:
del sys.modules['ChdirFooTest']
if 'chdir_bar_test' in sys.modules:
del sys.modules['chdir_bar_test']
def test_import_java_class(self):
import ChdirFooTest
self.assertEqual(ChdirFooTest.foo, 'bar')
def test_import_java_package(self):
import chdir_bar_test
self.assertEqual(chdir_bar_test.ChdirBarTest.bar, 'foo')
class ImportJarTestCase(BaseChdirTestCase):
SYSPATH = ('chdir-test.jar',)
def setUp(self):
super(ImportJarTestCase, self).setUp()
write(os.path.join(self.dir1, 'chdir-test.jar'), CHDIR_JY_JAR)
def tearDown(self):
try:
super(ImportJarTestCase, self).tearDown()
except OSError:
# XXX: Windows raises an error here when deleting the jar
# because SyspathArchive holds onto its file handle (and you
# can't delete a file in use on Windows). We may not want to
# change this
self.assert_(WINDOWS)
if 'ChdirJyTest' in sys.modules:
del sys.modules['ChdirJyTest']
def test_import_jar(self):
import ChdirJyTest
self.assertEqual(ChdirJyTest.jy, 'thon')
def safe_mktemp(*args, **kwargs):
"""Return a temp filename similar to tempfile.mktemp()
The 'safety' features:
o ensures the filename is safe to use as a Python module name
o ensures a file of the same name does not exist in the current
working directory (to avoid false chdir positives -- extra paranoia)
"""
while True:
dir, basename = os.path.split(tempfile.mktemp(*args, **kwargs))
basename = basename.replace('-', '')
name = os.path.join(dir, basename)
if not os.path.exists(name) and \
not os.path.exists(os.path.join(os.getcwd(), basename)):
return name
def raises(exc, expected, callable, *args):
"""Ensure the expected exception is raised"""
try:
callable(*args)
except exc, msg:
if expected is not None:
if isinstance(expected, str):
if str(msg) != expected:
raise test_support.TestFailed(
"Message %r, expected %r" % (str(msg), expected))
elif isinstance(expected, int):
if msg.errno != expected:
raise test_support.TestFailed(
"errno %r, expected %r" % (msg, expected))
else:
raise test_support.TestFailed("Expected %s" % exc)
def read(filename):
"""Read data from filename"""
fp = open(filename)
data = fp.read()
fp.close()
return data
def write(filename, data):
"""Write data to filename"""
fp = open(filename, 'wb')
fp.write(data)
fp.close()
def test_main():
tests = [ChdirTestCase,
ImportTestCase,
ImportPackageTestCase,
ZipimportTestCase,
PyCompileTestCase,
ExecfileTestCase,
ExecfileTracebackTestCase,
ListdirTestCase,
DirsTestCase,
FilesTestCase,
SymlinkTestCase]
if WINDOWS:
tests.append(WindowsChdirTestCase)
if test_support.is_jython:
tests.extend((ImportJavaClassTestCase,
ImportJarTestCase))
if test_support.is_resource_enabled('subprocess'):
tests.append(SubprocessTestCase)
test_support.run_unittest(*tests)
if __name__ == '__main__':
test_main()
|