3337
3337
def get_section(self, options, extra_path):
3338
3338
section = config.Section('foo', options)
3339
# We don't care about the length so we use '0'
3340
return config.LocationSection(section, 0, extra_path)
3339
return config.LocationSection(section, extra_path)
3342
3341
def test_simple_option(self):
3343
3342
section = self.get_section({'foo': 'bar'}, '')
3381
3380
[section.id for _, section in store.get_sections()])
3382
3381
matcher = config.LocationMatcher(store, '/foo/bar/quux')
3383
sections = [section for s, section in matcher.get_sections()]
3384
self.assertEquals([3, 2],
3385
[section.length for section in sections])
3382
sections = [section for _, section in matcher.get_sections()]
3386
3383
self.assertEquals(['/foo/bar', '/foo'],
3387
3384
[section.id for section in sections])
3388
3385
self.assertEquals(['quux', 'bar/quux'],
3399
3396
self.assertEquals(['/foo', '/foo/bar'],
3400
3397
[section.id for _, section in store.get_sections()])
3401
3398
matcher = config.LocationMatcher(store, '/foo/bar/baz')
3402
sections = [section for s, section in matcher.get_sections()]
3403
self.assertEquals([3, 2],
3404
[section.length for section in sections])
3399
sections = [section for _, section in matcher.get_sections()]
3405
3400
self.assertEquals(['/foo/bar', '/foo'],
3406
3401
[section.id for section in sections])
3407
3402
self.assertEquals(['baz', 'bar/baz'],
3432
3427
self.assertEquals(expected_location, matcher.location)
3430
class TestStartingPathMatcher(TestStore):
3433
super(TestStartingPathMatcher, self).setUp()
3434
# Any simple store is good enough
3435
self.store = config.IniFileStore()
3437
def assertSectionIDs(self, expected, location, content):
3438
self.store._load_from_string(content)
3439
matcher = config.StartingPathMatcher(self.store, location)
3440
sections = list(matcher.get_sections())
3441
self.assertLength(len(expected), sections)
3442
self.assertEqual(expected, [section.id for _, section in sections])
3445
def test_empty(self):
3446
self.assertSectionIDs([], self.get_url(), '')
3448
def test_url_vs_local_paths(self):
3449
# The matcher location is an url and the section names are local paths
3450
sections = self.assertSectionIDs(['/foo/bar', '/foo'],
3451
'file:///foo/bar/baz', '''\
3456
def test_local_path_vs_url(self):
3457
# The matcher location is a local path and the section names are urls
3458
sections = self.assertSectionIDs(['file:///foo/bar', 'file:///foo'],
3459
'/foo/bar/baz', '''\
3465
def test_no_name_section_included_when_present(self):
3466
# Note that other tests will cover the case where the no-name section
3467
# is empty and as such, not included.
3468
sections = self.assertSectionIDs(['/foo/bar', '/foo', None],
3469
'/foo/bar/baz', '''\
3470
option = defined so the no-name section exists
3474
self.assertEquals(['baz', 'bar/baz', '/foo/bar/baz'],
3475
[s.locals['relpath'] for _, s in sections])
3477
def test_order_reversed(self):
3478
self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', '''\
3483
def test_unrelated_section_excluded(self):
3484
self.assertSectionIDs(['/foo/bar', '/foo'], '/foo/bar/baz', '''\
3490
def test_glob_included(self):
3491
sections = self.assertSectionIDs(['/foo/*/baz', '/foo/b*', '/foo'],
3492
'/foo/bar/baz', '''\
3498
# Note that 'baz' as a relpath for /foo/b* is not fully correct, but
3499
# nothing really is... as far using {relpath} to append it to something
3500
# else, this seems good enough though.
3501
self.assertEquals(['', 'baz', 'bar/baz'],
3502
[s.locals['relpath'] for _, s in sections])
3504
def test_respect_order(self):
3505
self.assertSectionIDs(['/foo', '/foo/b*', '/foo/*/baz'],
3506
'/foo/bar/baz', '''\
3435
3514
class TestNameMatcher(TestStore):
3437
3516
def setUp(self):