test/unittest_filters.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Mon, 09 Jun 2008 13:39:44 +0200
changeset 4 6ff5e87d288a
parent 0 7710b138d4eb
permissions -rw-r--r--
change position on __future__ import for compatibility

"""unit tests for filters"""

__revision__ = '$Id: unittest_filters.py,v 1.4 2006-03-26 19:52:22 nico Exp $'

from logilab.common import testlib
from oobrother.filelib import filters
from os.path import join, dirname, abspath
import re
from sets import Set
import sys
from cStringIO import StringIO

class FileNode:
    """mock class, only defines what we really need for filters"""
    def __init__(self, filename = 'foo.txt'):
        self.basename = filename
        self.abspath = join(abspath(dirname(__file__)), 'data', filename)

    def is_directory(self):
        return False
    
class MatchersTC(testlib.TestCase):
    """test suite for existing filters"""

    def setUp(self):
        self.pyfile = FileNode('fo.py')
        self.txtfile = FileNode('foo.txt')
        self.binfile = FileNode('foo.bin')

    def test_abstract_filter(self):
        """checks default behaviour of a filter (<=> returning True)"""
        flt = filters.BaseMatcher()
        self.assertEquals(flt.match(self.pyfile), True)
        self.assertEquals(flt.match(self.txtfile), True)
        self.assertEquals(flt.match(self.binfile), True)
        self.assertEquals(flt.match(None), True)
        
    def test_extension_filter(self):
        """tests extensino filter"""
        flt = filters.ExtensionMatcher('py')
        self.assertEquals(flt.match(self.pyfile), True)
        self.assertEquals(flt.match(self.txtfile), False)
        self.assertEquals(flt.match(self.binfile), False)

    def test_hide_filter(self):
        """tests hide filter"""
        flt = filters.ReversedMatcher(filters.ExtensionMatcher('py'))
        self.assertEquals(flt.match(self.pyfile), False)
        self.assertEquals(flt.match(self.txtfile), True)
        self.assertEquals(flt.match(self.binfile), True)

    def test_abspath_filter(self):
        """tests filters.AbspathMatcher"""
        flt = filters.AbspathMatcher('fo{2}')
        self.assertEquals(flt.match(self.pyfile), False)
        self.assertEquals(flt.match(self.txtfile), True)
        self.assertEquals(flt.match(self.binfile), True)
        
    def test_content_filter(self):
        """tests content filter"""
        flt = filters.ContentMatcher('\d') # file must have digits !
        self.assertRaises(IOError, flt.match, self.pyfile)
        self.assertEquals(flt.match(self.txtfile), True)
        self.assertEquals(flt.match(FileNode('bar.txt')), False)

    def test_content_filter_with_flags(self):
        """tests content filter with flags"""
        flt = filters.ContentMatcher('Contains')
        self.assertEquals(flt.match(self.txtfile), False)
        flt = filters.ContentMatcher('Contains', re.I)
        self.assertEquals(flt.match(self.txtfile), True)

    def test_mimetype_filter(self):
        """tests MimeTypeMatcher's behaviour"""
        flt = filters.MimeTypeMatcher('text/plain')
        self.assertEquals(flt.match(self.pyfile), False)
        self.assertEquals(flt.match(self.txtfile), True)
        flt = filters.MimeTypeMatcher('text/plain', 'gzip')
        self.assertEquals(flt.match(self.pyfile), False)
        self.assertEquals(flt.match(self.txtfile), False)
        self.assertEquals(flt.match(FileNode('toto.txt.gz')), True)

    def test_hidden_files_filter(self):
        """tests HIDDEN_FILE_FILTER"""
        flt = filters.HIDDEN_FILES_FILTER
        self.assertEquals(flt.match(FileNode('.foo')), True)
        self.assertEquals(flt.match(FileNode('bar/.foo')), True)
        self.assertEquals(flt.match(FileNode('foo')), False)
        flt = filters.HIDE_HIDDEN_FILES
        self.assertEquals(flt.match(FileNode('.foo')), False)
        self.assertEquals(flt.match(FileNode('bar/.foo')), False)
        self.assertEquals(flt.match(FileNode('foo')), True)
        

class FilterChainTC(testlib.TestCase):
    """test suite for filter chains"""
    def setUp(self):
        self.pyfile = FileNode('foo.py')
        self.txtfile = FileNode('foo.txt')
        self.txtfile2 = FileNode('bar.txt')
        self.pycfile = FileNode('foo.pyc')
        self.binfile = FileNode('foo.bin')
        self.hiddenfile = FileNode('.foo')
        # file must have digits !
        self.fchain = filters.FilterDef(fname_restrictions = ['txt$'],
                                        content_restrictions = ['\d'])
        self.stdout_backup = sys.stdout
        sys.stdout = StringIO()

    def tearDown(self):
        sys.stdout = self.stdout_backup
        
    def test_basic_chain(self):
        """tests basic properties of the chain"""
        self.assertEquals(self.fchain.file_is_dropped(self.txtfile), False)
        self.assertEquals(self.fchain.file_is_dropped(self.txtfile2), True)
        self.assertEquals(self.fchain.file_is_dropped(self.pyfile), True)

    def test_filter_from_ignored_extensions(self):
        """tests the 'from_ignored_extensions' constructor"""
        flt = filters.FilterDef.from_ignored_extensions(['bak', '~', 'pyc'])
        # self.assertEquals(len(flt), 4)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.bak')), True)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.txt~')), True)
        self.assertEquals(flt.file_is_dropped(self.pycfile), True)
        self.assertEquals(flt.file_is_dropped(self.txtfile), False)
        # hidden files are ignored
        self.assertEquals(flt.file_is_dropped(self.hiddenfile), True)

    def test_filter_from_ignored_extensions_hidden(self):
        """tests 'from_ingored_extensions', but still showing hidden files"""
        flt = filters.FilterDef.from_ignored_extensions(['bak', '~', 'pyc'], True)
        # self.assertEquals(len(flt), 3)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.bak')), True)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.txt~')), True)
        self.assertEquals(flt.file_is_dropped(self.pycfile), True)
        self.assertEquals(flt.file_is_dropped(self.txtfile), False)
        # hidden files are not ignored
        self.assertEquals(flt.file_is_dropped(self.hiddenfile), False)

    def test_filter_from_showed_extensions(self):
        """tests 'from_showed_extensions'"""
        flt = filters.FilterDef.from_showed_extensions(['txt', 'py'])
        # self.assertEquals(len(flt), 3)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.bak')), True)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.txt~')), True)
        self.assertEquals(flt.file_is_dropped(self.pycfile), True)
        self.assertEquals(flt.file_is_dropped(self.pyfile), False)
        self.assertEquals(flt.file_is_dropped(self.txtfile), False)
        # hidden files are filtered
        self.assertEquals(flt.file_is_dropped(self.hiddenfile), True)
        
    def test_filter_from_showed_extensions_hidden(self):
        """tests 'from_showed_extensions' but still showing hidden files"""
        flt = filters.FilterDef.from_showed_extensions(['txt', 'py'], True)
        # self.assertEquals(len(flt), 2)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.bak')), True)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.txt~')), True)
        self.assertEquals(flt.file_is_dropped(self.pycfile), True)
        self.assertEquals(flt.file_is_dropped(self.pyfile), False)
        self.assertEquals(flt.file_is_dropped(self.txtfile), False)
        # hidden files are not filtered
        self.assertEquals(flt.file_is_dropped(FileNode('.foo.py')), False)
        self.assertEquals(flt.file_is_dropped(FileNode('.foo.txt')), False)
        self.assertEquals(flt.file_is_dropped(self.hiddenfile), True)
        
    def test_set_show_hidden_files(self):
        """tests that we can change the 'show_hidden_file' option"""
        pyfile = FileNode('.foo.py')
        flt = filters.FilterDef.from_showed_extensions(['py'])
        # hidden files are filtered
        self.assertEquals(flt.file_is_dropped(pyfile), True)
        flt.show_hidden_files = True
        # hidden files are not filtered
        self.assertEquals(flt.file_is_dropped(pyfile), False)

    def test_ignore_extension(self):
        """tests that we can add an extension to ignore after creation"""
        flt = filters.FilterDef.from_ignored_extensions(['bak'])
        self.assertEquals(Set(flt.ignored_extensions()), Set(['bak']))
        self.assertEquals(flt.file_is_dropped(FileNode('foo.bak')), True)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.txt~')), False)
        flt.ignore_extension('~')
        self.assertEquals(Set(flt.ignored_extensions()), Set(['bak', '~']))
        self.assertEquals(flt.file_is_dropped(FileNode('foo.txt~')), True)

    def test_unignore_extension(self):
        """tests that we can remove an extension to ignore after creation"""
        flt = filters.FilterDef.from_ignored_extensions(['bak', '~'])
        self.assertEquals(Set(flt.ignored_extensions()), Set(['bak', '~']))
        self.assertEquals(flt.file_is_dropped(FileNode('foo.bak')), True)
        self.assertEquals(flt.file_is_dropped(FileNode('foo.txt~')), True)
        flt.unignore_extension('~')
        self.assertEquals(Set(flt.ignored_extensions()), Set(['bak']))
        self.assertEquals(flt.file_is_dropped(FileNode('foo.txt~')), False)
        # test that we can remove already unignored extension
        flt.unignore_extension('~')
        self.assertEquals(Set(flt.ignored_extensions()), Set(['bak']))

    def test_add_filename_restrictions(self):
        """tests that we can add filename restrictions after creation"""
        fchain = filters.FilterDef(fname_restrictions = ['txt$'])
        self.assertEquals(Set(fchain.filename_restrictions()), Set(['txt$']))
        self.assertEquals(fchain.file_is_dropped(self.txtfile), False)
        self.assertEquals(fchain.file_is_dropped(self.pyfile), True)
        fchain.add_filename_restriction('.py$')
        self.assertEquals(Set(fchain.filename_restrictions()), Set(['txt$', '.py$']))
        self.assertEquals(fchain.file_is_dropped(self.txtfile), False)
        self.assertEquals(fchain.file_is_dropped(self.pyfile), False)
        
    def test_remove_filename_restrictions(self):
        """tests that we can remove filename restrictions after creation"""
        fchain = filters.FilterDef(fname_restrictions = ['txt$', 'py$'])
        self.assertEquals(Set(fchain.filename_restrictions()), Set(['txt$', 'py$']))
        self.assertEquals(fchain.file_is_dropped(self.txtfile), False)
        self.assertEquals(fchain.file_is_dropped(self.pyfile), False)
        fchain.remove_filename_restriction('py$')
        self.assertEquals(Set(fchain.filename_restrictions()), Set(['txt$']))
        self.assertEquals(fchain.file_is_dropped(self.txtfile), False)
        self.assertEquals(fchain.file_is_dropped(self.pyfile), True)
        # test that we can re-remove a restriction
        fchain.remove_filename_restriction('py$')
        self.assertEquals(Set(fchain.filename_restrictions()), Set(['txt$']))

    def test_remove_all_filename_restrictions(self):
        """tests that we can remove all filename restrictions"""
        fchain = filters.FilterDef(fname_restrictions = ['txt$', 'py$'])
        self.assertEquals(Set(fchain.filename_restrictions()), Set(['txt$', 'py$']))
        self.assertEquals(fchain.file_is_dropped(self.txtfile), False)
        self.assertEquals(fchain.file_is_dropped(self.pyfile), False)
        self.assertEquals(fchain.file_is_dropped(self.pycfile), True)
        self.assertEquals(fchain.file_is_dropped(self.binfile), True)
        fchain.remove_all_filename_restrictions()
        self.assertEquals(fchain.filename_restrictions(), [])
        self.assertEquals(fchain.file_is_dropped(self.txtfile), False)
        self.assertEquals(fchain.file_is_dropped(self.pyfile), False)
        self.assertEquals(fchain.file_is_dropped(self.pycfile), False)
        self.assertEquals(fchain.file_is_dropped(self.binfile), False)
        
    def test_add_content_restriction(self):
        """tests that we can add content restrictions after creation"""
        fchain = filters.FilterDef()
        self.assertEquals(fchain.content_restrictions(), [])
        self.assertEquals(fchain.file_is_dropped(self.txtfile2), False)
        fchain.add_content_restriction('\d')
        self.assertEquals(fchain.content_restrictions(), ['\d'])
        self.assertEquals(fchain.file_is_dropped(self.txtfile2), True)

    def test_remove_content_restriction(self):
        """tests that we can remove content restrictions after creation"""
        fchain = filters.FilterDef(content_restrictions = ['\d'])
        self.assertEquals(fchain.content_restrictions(), ['\d'])
        self.assertEquals(fchain.file_is_dropped(self.txtfile2), True)
        fchain.remove_content_restriction('\d')
        self.assertEquals(fchain.content_restrictions(), [])
        self.assertEquals(fchain.file_is_dropped(self.txtfile2), False)
        # test that we can re-remove a restriction
        fchain.remove_content_restriction('\d')
        self.assertEquals(fchain.content_restrictions(), [])
        
    def test_remove_all_content_restrictions(self):
        """tests that we can remove all content restrictions"""
        fchain = filters.FilterDef(content_restrictions = ['\d'])
        self.assertEquals(fchain.file_is_dropped(self.txtfile2), True)
        self.assertEquals(fchain.file_is_dropped(self.txtfile), False)
        fchain.remove_all_content_restrictions()
        self.assertEquals(fchain.content_restrictions(), [])
        self.assertEquals(fchain.file_is_dropped(self.txtfile2), False)
        self.assertEquals(fchain.file_is_dropped(self.txtfile), False)
        


if __name__ == '__main__':
    testlib.unittest_main()