2016-05-29 19:02:39 +00:00
|
|
|
# Copyright 2016 Hardcoded Software (http://www.hardcoded.net)
|
|
|
|
#
|
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
2015-01-03 21:33:16 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
2009-06-07 14:26:46 +00:00
|
|
|
|
2010-08-11 14:39:06 +00:00
|
|
|
import io
|
2010-08-15 12:42:55 +00:00
|
|
|
from xml.etree import ElementTree as ET
|
2009-06-07 14:26:46 +00:00
|
|
|
|
2012-03-14 16:47:21 +00:00
|
|
|
from pytest import raises
|
2011-01-11 10:59:53 +00:00
|
|
|
from hscommon.testutil import eq_
|
2009-06-07 14:26:46 +00:00
|
|
|
|
2016-05-29 19:02:39 +00:00
|
|
|
from ..ignore import IgnoreList
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_empty():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(0, len(il))
|
|
|
|
assert not il.AreIgnored('foo', 'bar')
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_simple():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
assert il.AreIgnored('foo', 'bar')
|
|
|
|
assert il.AreIgnored('bar', 'foo')
|
|
|
|
assert not il.AreIgnored('foo', 'bleh')
|
|
|
|
assert not il.AreIgnored('bleh', 'bar')
|
|
|
|
eq_(1, len(il))
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_multiple():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
il.Ignore('foo', 'bleh')
|
|
|
|
il.Ignore('bleh', 'bar')
|
|
|
|
il.Ignore('aybabtu', 'bleh')
|
|
|
|
assert il.AreIgnored('foo', 'bar')
|
|
|
|
assert il.AreIgnored('bar', 'foo')
|
|
|
|
assert il.AreIgnored('foo', 'bleh')
|
|
|
|
assert il.AreIgnored('bleh', 'bar')
|
|
|
|
assert not il.AreIgnored('aybabtu', 'bar')
|
|
|
|
eq_(4, len(il))
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_clear():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
2009-06-07 14:26:46 +00:00
|
|
|
il.Clear()
|
2016-05-29 19:02:39 +00:00
|
|
|
assert not il.AreIgnored('foo', 'bar')
|
|
|
|
assert not il.AreIgnored('bar', 'foo')
|
|
|
|
eq_(0, len(il))
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_add_same_twice():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
il.Ignore('bar', 'foo')
|
|
|
|
eq_(1, len(il))
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_save_to_xml():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
il.Ignore('foo', 'bleh')
|
|
|
|
il.Ignore('bleh', 'bar')
|
2010-08-11 14:39:06 +00:00
|
|
|
f = io.BytesIO()
|
2009-06-07 14:26:46 +00:00
|
|
|
il.save_to_xml(f)
|
|
|
|
f.seek(0)
|
2010-08-15 12:42:55 +00:00
|
|
|
doc = ET.parse(f)
|
2010-03-01 11:21:43 +00:00
|
|
|
root = doc.getroot()
|
|
|
|
eq_(root.tag, 'ignore_list')
|
|
|
|
eq_(len(root), 2)
|
|
|
|
eq_(len([c for c in root if c.tag == 'file']), 2)
|
|
|
|
f1, f2 = root[:]
|
|
|
|
subchildren = [c for c in f1 if c.tag == 'file'] + [c for c in f2 if c.tag == 'file']
|
|
|
|
eq_(len(subchildren), 3)
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_SaveThenLoad():
|
|
|
|
il = IgnoreList()
|
2010-03-01 11:21:43 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
il.Ignore('foo', 'bleh')
|
|
|
|
il.Ignore('bleh', 'bar')
|
2010-08-11 14:39:06 +00:00
|
|
|
il.Ignore('\u00e9', 'bar')
|
|
|
|
f = io.BytesIO()
|
2009-06-07 14:26:46 +00:00
|
|
|
il.save_to_xml(f)
|
|
|
|
f.seek(0)
|
|
|
|
il = IgnoreList()
|
|
|
|
il.load_from_xml(f)
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(4, len(il))
|
|
|
|
assert il.AreIgnored('\u00e9', 'bar')
|
|
|
|
|
2009-06-07 14:26:46 +00:00
|
|
|
def test_LoadXML_with_empty_file_tags():
|
2010-08-11 14:39:06 +00:00
|
|
|
f = io.BytesIO()
|
|
|
|
f.write(b'<?xml version="1.0" encoding="utf-8"?><ignore_list><file><file/></file></ignore_list>')
|
2009-06-07 14:26:46 +00:00
|
|
|
f.seek(0)
|
|
|
|
il = IgnoreList()
|
|
|
|
il.load_from_xml(f)
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(0, len(il))
|
|
|
|
|
2009-06-07 14:26:46 +00:00
|
|
|
def test_AreIgnore_works_when_a_child_is_a_key_somewhere_else():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
il.Ignore('bar', 'baz')
|
|
|
|
assert il.AreIgnored('bar', 'foo')
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_no_dupes_when_a_child_is_a_key_somewhere_else():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
il.Ignore('bar', 'baz')
|
|
|
|
il.Ignore('bar', 'foo')
|
|
|
|
eq_(2, len(il))
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_iterate():
|
|
|
|
#It must be possible to iterate through ignore list
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
expected = [('foo', 'bar'), ('bar', 'baz'), ('foo', 'baz')]
|
2009-06-07 14:26:46 +00:00
|
|
|
for i in expected:
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore(i[0], i[1])
|
2009-06-07 14:26:46 +00:00
|
|
|
for i in il:
|
|
|
|
expected.remove(i) #No exception should be raised
|
|
|
|
assert not expected #expected should be empty
|
|
|
|
|
|
|
|
def test_filter():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
il.Ignore('bar', 'baz')
|
|
|
|
il.Ignore('foo', 'baz')
|
|
|
|
il.Filter(lambda f, s: f == 'bar')
|
|
|
|
eq_(1, len(il))
|
|
|
|
assert not il.AreIgnored('foo', 'bar')
|
|
|
|
assert il.AreIgnored('bar', 'baz')
|
2009-06-07 14:26:46 +00:00
|
|
|
|
2010-03-01 11:21:43 +00:00
|
|
|
def test_save_with_non_ascii_items():
|
2009-06-07 14:26:46 +00:00
|
|
|
il = IgnoreList()
|
2010-08-11 14:39:06 +00:00
|
|
|
il.Ignore('\xac', '\xbf')
|
|
|
|
f = io.BytesIO()
|
2009-06-07 14:26:46 +00:00
|
|
|
try:
|
|
|
|
il.save_to_xml(f)
|
|
|
|
except Exception as e:
|
2010-08-11 14:39:06 +00:00
|
|
|
raise AssertionError(str(e))
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_len():
|
|
|
|
il = IgnoreList()
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(0, len(il))
|
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
eq_(1, len(il))
|
2009-06-07 14:26:46 +00:00
|
|
|
|
|
|
|
def test_nonzero():
|
|
|
|
il = IgnoreList()
|
|
|
|
assert not il
|
2016-05-29 19:02:39 +00:00
|
|
|
il.Ignore('foo', 'bar')
|
2009-06-07 14:26:46 +00:00
|
|
|
assert il
|
2012-03-14 16:47:21 +00:00
|
|
|
|
|
|
|
def test_remove():
|
|
|
|
il = IgnoreList()
|
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
il.Ignore('foo', 'baz')
|
|
|
|
il.remove('bar', 'foo')
|
|
|
|
eq_(len(il), 1)
|
|
|
|
assert not il.AreIgnored('foo', 'bar')
|
|
|
|
|
|
|
|
def test_remove_non_existant():
|
|
|
|
il = IgnoreList()
|
|
|
|
il.Ignore('foo', 'bar')
|
|
|
|
il.Ignore('foo', 'baz')
|
|
|
|
with raises(ValueError):
|
|
|
|
il.remove('foo', 'bleh')
|