From 77e113bfd42d3de36d6028604bf8e9fd371188d6 Mon Sep 17 00:00:00 2001 From: Kurt Grandis Date: Mon, 8 Mar 2010 00:51:12 -0500 Subject: [PATCH] PEP8 refactored and cleaned up some pathing logic added ability to pass in a file option --- README.rst | 24 ++++++++++++++++++- nose_exclude.py | 46 ++++++++++++++++++++++++++++--------- setup.py | 2 +- test_dirs/exclude_dirs.txt | 3 +++ test_dirs/exclude_dirs2.txt | 1 + tests.py | 30 ++++++++++++++++++++++-- 6 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 test_dirs/exclude_dirs.txt create mode 100644 test_dirs/exclude_dirs2.txt diff --git a/README.rst b/README.rst index 00bf87e..2f5b013 100644 --- a/README.rst +++ b/README.rst @@ -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 ==== diff --git a/nose_exclude.py b/nose_exclude.py index 1f71caa..a8f12ed 100644 --- a/nose_exclude.py +++ b/nose_exclude.py @@ -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 - - diff --git a/setup.py b/setup.py index 515b776..4301f46 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/test_dirs/exclude_dirs.txt b/test_dirs/exclude_dirs.txt new file mode 100644 index 0000000..fb411a7 --- /dev/null +++ b/test_dirs/exclude_dirs.txt @@ -0,0 +1,3 @@ +test_dirs/build +test_dirs/test_not_me + diff --git a/test_dirs/exclude_dirs2.txt b/test_dirs/exclude_dirs2.txt new file mode 100644 index 0000000..210f417 --- /dev/null +++ b/test_dirs/exclude_dirs2.txt @@ -0,0 +1 @@ +test_dirs/build diff --git a/tests.py b/tests.py index 2c4e161..743af8a 100644 --- a/tests.py +++ b/tests.py @@ -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