Commit 64a84702 authored by Brett Cannon's avatar Brett Cannon

Restructure testing of .pth files. Move previous functions into a class and

create a testing method that can be called to make sure that the handling of
the .pth file was correct.
parent f6067ec3
...@@ -5,7 +5,7 @@ executing have not been removed. ...@@ -5,7 +5,7 @@ executing have not been removed.
""" """
import unittest import unittest
from test.test_support import TestSkipped, run_unittest, TESTFN from test.test_support import TestSkipped, TestFailed, run_unittest, TESTFN
import __builtin__ import __builtin__
import os import os
import sys import sys
...@@ -58,51 +58,96 @@ class HelperFunctionsTests(unittest.TestCase): ...@@ -58,51 +58,96 @@ class HelperFunctionsTests(unittest.TestCase):
def test_addpackage(self): def test_addpackage(self):
# Make sure addpackage() imports if the line starts with 'import', # Make sure addpackage() imports if the line starts with 'import',
# otherwise add a directory combined from sitedir and 'name'. # adds directories to sys.path for any line in the file that is not a
# Must also skip comment lines. # comment or import that is a valid directory name for where the .pth
dir_path, file_name, new_dir = createpth() # file resides; invalid directories are not added
pth_file = PthFile()
pth_file.cleanup() # to make sure that nothing is pre-existing that
# shouldn't be
try: try:
site.addpackage(dir_path, file_name, set()) pth_file.create()
self.failUnless(site.makepath(os.path.join(dir_path, new_dir))[0] in site.addpackage(pth_file.base_dir, pth_file.filename, set())
sys.path) unittest.FunctionTestCase(pth_file.test)
finally: finally:
cleanuppth(dir_path, file_name, new_dir) pth_file.cleanup()
def test_addsitedir(self): def test_addsitedir(self):
dir_path, file_name, new_dir = createpth() # Same tests for test_addpackage since addsitedir() essentially just
# calls addpackage() for every .pth file in the directory
pth_file = PthFile()
pth_file.cleanup() # Make sure that nothing is pre-existing that is
# tested for
try: try:
site.addsitedir(dir_path, set()) site.addsitedir(pth_file.base_dir, set())
self.failUnless(site.makepath(os.path.join(dir_path, new_dir))[0] in unittest.FunctionTestCase(pth_file.test)
sys.path)
finally: finally:
cleanuppth(dir_path, file_name, new_dir) pth_file.cleanup()
class PthFile(object):
"""Helper class for handling testing of .pth files"""
def __init__(self, filename_base=TESTFN, imported="time",
good_dirname="__testdir__", bad_dirname="__bad"):
"""Initialize instance variables"""
self.filename = filename_base + ".pth"
self.base_dir = os.path.abspath('')
self.file_path = os.path.join(self.base_dir, self.filename)
self.imported = "time"
self.good_dirname = good_dirname
self.bad_dirname = bad_dirname
self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
def create(self):
"""Create a .pth file with a comment, blank lines, an ``import
<self.imported>``, a line with self.good_dirname, and a line with
self.bad_dirname.
Creation of the directory for self.good_dir_path (based off of
self.good_dirname) is also performed.
Make sure to call self.cleanup() to undo anything done by this method.
"""
FILE = open(self.file_path, 'wU')
try:
print>>FILE, "#import @bad module name"
print>>FILE, "\n"
print>>FILE, "import %s" % self.imported
print>>FILE, self.good_dirname
print>>FILE, self.bad_dirname
finally:
FILE.close()
os.mkdir(self.good_dir_path)
def createpth(): def cleanup(self):
"""Create a temporary .pth file at the returned location and return the """Make sure that the .pth file is deleted, self.imported is not in
directory where it was created, the pth file name, and the directory sys.modules, and that both self.good_dirname and self.bad_dirname are
specified in the pth file. not existing directories."""
try:
os.remove(self.file_path)
except OSError:
pass
try:
del sys.modules[self.imported]
except KeyError:
pass
try:
os.rmdir(self.good_dir_path)
except OSError:
pass
try:
os.rmdir(self.bad_dir_path)
except OSError:
pass
Make sure to delete the file when finished. def test(self):
"""Test to make sure that was and was not supposed to be created by
using the .pth file occurred"""
assert site.makepath(self.good_dir_path)[0] in sys.path
assert self.imported in sys.modules
assert not os.path.exists(self.bad_dir_path)
"""
pth_dirname = "__testdir__"
file_name = TESTFN + ".pth"
full_dirname = os.path.dirname(os.path.abspath(file_name))
FILE = file(os.path.join(full_dirname, file_name), 'w')
try:
print>>FILE, "#import @bad module name"
print>>FILE, ''
print>>FILE, "import os"
print>>FILE, pth_dirname
finally:
FILE.close()
os.mkdir(os.path.join(full_dirname, pth_dirname))
return full_dirname, file_name, pth_dirname
def cleanuppth(full_dirname, file_name, pth_dirname):
"""Clean up what createpth() made"""
os.remove(os.path.join(full_dirname, file_name))
os.rmdir(os.path.join(full_dirname, pth_dirname))
class ImportSideEffectTests(unittest.TestCase): class ImportSideEffectTests(unittest.TestCase):
"""Test side-effects from importing 'site'.""" """Test side-effects from importing 'site'."""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment