changeset 183:fda952491beb

* Normalize separators in file paths to "/" in `FileSet`s, so that pattern matching against `include`/`exclude` patterns also works on Windows. * Add more unit tests for the `FileSet` class.
author cmlenz
date Wed, 31 Aug 2005 15:29:04 +0000
parents cafe294fa835
children fbf949f4c706
files bitten/build/api.py bitten/build/tests/api.py
diffstat 2 files changed, 59 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/bitten/build/api.py
+++ b/bitten/build/api.py
@@ -7,6 +7,7 @@
 # you should have received as part of this distribution. The terms
 # are also available at http://bitten.cmlenz.net/wiki/License.
 
+from itertools import ifilterfalse
 import logging
 import fnmatch
 import os
@@ -212,20 +213,24 @@
         if include is not None:
             self.include = shlex.split(include)
 
-        self.exclude = self.DEFAULT_EXCLUDES
+        self.exclude = self.DEFAULT_EXCLUDES[:]
         if exclude is not None:
             self.exclude += shlex.split(exclude)
 
         for dirpath, dirnames, filenames in os.walk(self.basedir):
-            dirpath = dirpath[len(self.basedir) + 1:]
+            dirpath = ndirpath = dirpath[len(self.basedir) + 1:]
+            if os.sep != '/':
+                ndirpath = dirpath.replace(os.sep, '/')
 
             for filename in filenames:
-                filepath = os.path.join(dirpath, filename)
+                filepath = nfilepath = os.path.join(dirpath, filename)
+                if os.sep != '/':
+                    nfilepath = nfilepath.replace(os.sep, '/')
 
                 if self.include:
                     included = False
                     for pattern in self.include:
-                        if fnmatch.fnmatchcase(filepath, pattern) or \
+                        if fnmatch.fnmatchcase(nfilepath, pattern) or \
                            fnmatch.fnmatchcase(filename, pattern):
                             included = True
                             break
@@ -234,7 +239,7 @@
 
                 excluded = False
                 for pattern in self.exclude:
-                    if fnmatch.fnmatchcase(filepath, pattern) or \
+                    if fnmatch.fnmatchcase(nfilepath, pattern) or \
                        fnmatch.fnmatchcase(filename, pattern):
                         excluded = True
                         break
@@ -242,8 +247,8 @@
                     self.files.append(filepath)
 
     def __iter__(self):
-        for file in self.files:
-            yield file
+        for filename in self.files:
+            yield filename
 
-    def __contains__(self, file):
-        return file in self.files
+    def __contains__(self, filename):
+        return filename in self.files
--- a/bitten/build/tests/api.py
+++ b/bitten/build/tests/api.py
@@ -18,15 +18,59 @@
 class FileSetTestCase(unittest.TestCase):
 
     def setUp(self):
-        self.basedir = os.path.realpath(tempfile.mkdtemp())
+        self.basedir = os.path.realpath(tempfile.mkdtemp(suffix='bitten_test'))
 
     def tearDown(self):
         shutil.rmtree(self.basedir)
 
+    # Convenience methods
+
+    def _create_dir(self, *path):
+        cur = self.basedir
+        for part in path:
+            cur = os.path.join(cur, part)
+            os.mkdir(cur)
+        return cur[len(self.basedir) + 1:]
+
+    def _create_file(self, *path):
+        filename = os.path.join(self.basedir, *path)
+        fd = file(filename, 'w')
+        fd.close()
+        return filename[len(self.basedir) + 1:]
+
+    # Test methods
+
     def test_empty(self):
         fileset = FileSet(self.basedir)
         self.assertRaises(StopIteration, iter(fileset).next)
 
+    def test_top_level_files(self):
+        foo_txt = self._create_file('foo.txt')
+        bar_txt = self._create_file('bar.txt')
+        fileset = FileSet(self.basedir)
+        assert foo_txt in fileset and bar_txt in fileset
+
+    def test_files_in_subdir(self):
+        self._create_dir('tests')
+        foo_txt = self._create_file('tests', 'foo.txt')
+        bar_txt = self._create_file('tests', 'bar.txt')
+        fileset = FileSet(self.basedir)
+        assert foo_txt in fileset and bar_txt in fileset
+
+    def test_files_in_subdir_with_include(self):
+        self._create_dir('tests')
+        foo_txt = self._create_file('tests', 'foo.txt')
+        bar_txt = self._create_file('tests', 'bar.txt')
+        fileset = FileSet(self.basedir, include='tests/*.txt')
+        assert foo_txt in fileset and bar_txt in fileset
+
+    def test_files_in_subdir_with_exclude(self):
+        self._create_dir('tests')
+        foo_txt = self._create_file('tests', 'foo.txt')
+        bar_txt = self._create_file('tests', 'bar.txt')
+        fileset = FileSet(self.basedir, include='tests/*.txt', exclude='bar.*')
+        assert foo_txt in fileset and bar_txt not in fileset
+
 
 def suite():
     suite = unittest.TestSuite()
Copyright (C) 2012-2017 Edgewall Software