refactored and cleaned up some pathing logic
added ability to pass in a file option
This commit is contained in:
Kurt Grandis
2010-03-08 00:51:12 -05:00
parent 5cfaee0056
commit 77e113bfd4
6 changed files with 91 additions and 15 deletions

View File

@@ -10,7 +10,7 @@ directories to be excluded from testing.
Exclude Directories
===================
the ``--exclude-dir=`` option is made available after installation of the
The ``--exclude-dir=`` option is made available after installation of the
plugin. The option may be used multiple times to exclude multiple directories
from testing. The directory paths provided may be absolute or relative.
@@ -27,6 +27,28 @@ Example::
This example will exclude the directories test_dirs/build and
test_dirs/test_not_me from nosetests' test searching.
Using File-Based Exclusion List
-------------------------------
The ``--exclude-dir-file=`` option can be used to pass in a predefined
list of directories contained within a file. ``nose-exclude`` expects each
directory to be excluded to be on its own line.
Example::
$ nosetests --exclude-dir-file=test_dirs/exclude_dirs.txt \
test_dirs
....
----------------------------------------------------------------------
Ran 4 tests in 0.006s
OK
where ``exclude_dirs.txt`` might look like: ::
test_dirs/build
test_dirs/test_not_me
Bugs
====

View File

@@ -16,12 +16,45 @@ class NoseExclude(Plugin):
Path can be relative to current working directory \
or an absolute path. May be specified multiple \
times. [NOSE_EXCLUDE_DIRS]")
parser.add_option(
"--exclude-dir-file", type="string",
dest="exclude_dir_file",
help="A file containing a list of directories to exclude \
from test discovery. Paths can be relative to current \
working directory or an absolute path. \
[NOSE_EXCLUDE_DIRS_FILE]")
def _force_to_abspath(self, pathname):
if os.path.isabs(pathname):
abspath = pathname
else:
abspath = os.path.abspath(pathname)
if os.path.exists(abspath):
return abspath
else:
raise ValueError("invalid path: %s" % pathname)
def _load_from_file(self, filename):
infile = open(filename)
new_list = [l.strip() for l in infile.readlines() if l.strip()]
return new_list
def configure(self, options, conf):
"""Configure plugin based on command line options"""
super(NoseExclude, self).configure(options, conf)
self.exclude_dirs = {}
# preload directories from file
if options.exclude_dir_file:
if not options.exclude_dirs:
options.exclude_dirs = []
new_dirs = self._load_from_file(options.exclude_dir_file)
options.exclude_dirs.extend(new_dirs)
if not options.exclude_dirs:
self.enabled = False
@@ -33,15 +66,8 @@ class NoseExclude(Plugin):
# Normalize excluded directory names for lookup
for d in options.exclude_dirs:
if os.path.isabs(d):
self.exclude_dirs[d] = True
elif os.path.isdir(d):
#see if it's relative
new_abs_d = os.path.join(root,d)
self.exclude_dirs[new_abs_d] = True
else:
#bad path
raise ValueError("invalid path: %s" % d)
abs_d = self._force_to_abspath(d)
self.exclude_dirs[abs_d] = True
exclude_str = "excluding dirs: %s" % ",".join(self.exclude_dirs.keys())
log.debug(exclude_str)
@@ -55,5 +81,3 @@ class NoseExclude(Plugin):
return True

View File

@@ -5,7 +5,7 @@ f = open(os.path.join(os.path.dirname(__file__), 'README.rst'))
long_description = f.read().strip()
f.close()
VERSION = '0.1.2'
VERSION = '0.1.4'
setup(
name = "nose-exclude",

View File

@@ -0,0 +1,3 @@
test_dirs/build
test_dirs/test_not_me

View File

@@ -0,0 +1 @@
test_dirs/build

View File

@@ -5,7 +5,8 @@ from nose_exclude import NoseExclude
class TestNoseExcludeDirs_Relative_Args(PluginTester, unittest.TestCase):
"""Test nose-exclude directories using relative paths passed
on the commandline via --exclude-dir"""
on the commandline via --exclude-dir
"""
activate = "--exclude-dir=test_dirs/build"
args = ['--exclude-dir=test_dirs/test_not_me']
@@ -17,7 +18,8 @@ class TestNoseExcludeDirs_Relative_Args(PluginTester, unittest.TestCase):
class TestNoseExcludeDirs_Absolute_Args(PluginTester, unittest.TestCase):
"""Test nose-exclude directories using absolute paths passed
on the commandline via --exclude-dir"""
on the commandline via --exclude-dir
"""
plugins = [NoseExclude()]
suitepath = os.path.join(os.getcwd(), 'test_dirs')
@@ -29,6 +31,30 @@ class TestNoseExcludeDirs_Absolute_Args(PluginTester, unittest.TestCase):
self.args = ['--exclude-dir=%s' % arg_path]
super(TestNoseExcludeDirs_Absolute_Args, self).__init__(*args, **kwargs)
def test_proper_dirs_omitted(self):
assert "FAILED" not in self.output
class TestNoseExcludeDirs_Relative_Args_File(PluginTester, unittest.TestCase):
"""Test nose-exclude directories using relative paths passed
by file using --exclude-dir-file
"""
activate = "--exclude-dir-file=test_dirs/exclude_dirs.txt"
plugins = [NoseExclude()]
suitepath = os.path.join(os.getcwd(), 'test_dirs')
def test_proper_dirs_omitted(self):
assert "FAILED" not in self.output
class TestNoseExcludeDirs_Relative_Args_Mixed(PluginTester, unittest.TestCase):
"""Test nose-exclude directories using paths passed
by file and commandline
"""
activate = "--exclude-dir-file=test_dirs/exclude_dirs2.txt"
args = ["--exclude-dir=test_dirs/test_not_me"]
plugins = [NoseExclude()]
suitepath = os.path.join(os.getcwd(), 'test_dirs')
def test_proper_dirs_omitted(self):
assert "FAILED" not in self.output