mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-24 06:59:52 +01:00
fix tests for coll listing, #78
config override: when loading from coll-specific config.yaml, resolve relative paths to that collection, not to root #55
This commit is contained in:
parent
30454abb6b
commit
be5139b635
@ -149,9 +149,14 @@ class DirectoryCollsLoader(object):
|
|||||||
|
|
||||||
return colls
|
return colls
|
||||||
|
|
||||||
|
def _norm_path(self, root_dir, path):
|
||||||
|
result = os.path.normpath(os.path.join(root_dir, path))
|
||||||
|
print(result)
|
||||||
|
return result
|
||||||
|
|
||||||
def _add_dir_if_exists(self, coll, root_dir, dir_key, required=False):
|
def _add_dir_if_exists(self, coll, root_dir, dir_key, required=False):
|
||||||
if dir_key in coll:
|
if dir_key in coll:
|
||||||
# already set
|
coll[dir_key] = self._norm_path(root_dir, coll[dir_key])
|
||||||
return False
|
return False
|
||||||
|
|
||||||
thedir = self.config.get('paths')[dir_key]
|
thedir = self.config.get('paths')[dir_key]
|
||||||
@ -189,19 +194,22 @@ class DirectoryCollsLoader(object):
|
|||||||
if self._add_dir_if_exists(coll_config, root_dir, 'static_path', False):
|
if self._add_dir_if_exists(coll_config, root_dir, 'static_path', False):
|
||||||
self.static_routes['static/' + name] = coll_config['static_path']
|
self.static_routes['static/' + name] = coll_config['static_path']
|
||||||
|
|
||||||
# Add templates
|
# Custom templates dir
|
||||||
templates_dir = self.config.get('paths').get('templates_dir')
|
templates_dir = self.config.get('paths').get('templates_dir')
|
||||||
if templates_dir:
|
if templates_dir:
|
||||||
template_dir = os.path.join(root_dir, templates_dir)
|
template_dir = os.path.join(root_dir, templates_dir)
|
||||||
if template_dir:
|
|
||||||
for tname, tfile in self.config.get('paths')['template_files'].iteritems():
|
|
||||||
if tname in coll_config:
|
|
||||||
# Already set
|
|
||||||
continue
|
|
||||||
|
|
||||||
full = os.path.join(template_dir, tfile)
|
# Check all templates
|
||||||
if os.path.isfile(full):
|
template_files = self.config.get('paths')['template_files']
|
||||||
coll_config[tname] = full
|
for tname, tfile in template_files.iteritems():
|
||||||
|
if tname in coll_config:
|
||||||
|
# Already set
|
||||||
|
coll_config[tname] = self._norm_path(root_dir, coll_config[tname])
|
||||||
|
# If templates override dir
|
||||||
|
elif templates_dir:
|
||||||
|
full = os.path.join(template_dir, tfile)
|
||||||
|
if os.path.isfile(full):
|
||||||
|
coll_config[tname] = full
|
||||||
|
|
||||||
return coll_config
|
return coll_config
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ def is_wb_handler(obj):
|
|||||||
|
|
||||||
|
|
||||||
@template_filter()
|
@template_filter()
|
||||||
def jsonify(obj):
|
def tojson(obj):
|
||||||
return json.dumps(obj)
|
return json.dumps(obj)
|
||||||
|
|
||||||
|
|
||||||
|
@ -236,30 +236,44 @@ class TestManagedColls(object):
|
|||||||
|
|
||||||
def test_custom_config(self):
|
def test_custom_config(self):
|
||||||
""" Test custom created config.yaml which overrides auto settings
|
""" Test custom created config.yaml which overrides auto settings
|
||||||
Template relative to root dir, not collection-specific so far
|
Template is relative to collection-specific dir
|
||||||
|
Add custom metadata and test its presence in custom search page
|
||||||
"""
|
"""
|
||||||
config_path = os.path.join(self.root_dir, 'collections', 'test', 'config.yaml')
|
config_path = os.path.join(self.root_dir, 'collections', 'test', 'config.yaml')
|
||||||
with open(config_path, 'w+b') as fh:
|
with open(config_path, 'w+b') as fh:
|
||||||
fh.write('search_html: ./custom_search.html\n')
|
fh.write('search_html: ./templates/custom_search.html\n')
|
||||||
|
fh.write('index_paths: ./cdx2/\n')
|
||||||
|
|
||||||
|
custom_search = os.path.join(self.root_dir, 'collections', 'test',
|
||||||
|
'templates', 'custom_search.html')
|
||||||
|
|
||||||
|
# add metadata
|
||||||
|
main(['metadata', 'test', '--set', 'some=value'])
|
||||||
|
|
||||||
custom_search = os.path.join(self.root_dir, 'custom_search.html')
|
|
||||||
with open(custom_search, 'w+b') as fh:
|
with open(custom_search, 'w+b') as fh:
|
||||||
fh.write('config.yaml overriden search page')
|
fh.write('config.yaml overriden search page: ')
|
||||||
|
fh.write('{{ wbrequest.user_metadata | tojson }}\n')
|
||||||
|
|
||||||
|
os.rename(os.path.join(self.root_dir, 'collections', 'test', 'cdx'),
|
||||||
|
os.path.join(self.root_dir, 'collections', 'test', 'cdx2'))
|
||||||
|
|
||||||
self._create_app()
|
self._create_app()
|
||||||
resp = self.testapp.get('/test/')
|
resp = self.testapp.get('/test/')
|
||||||
assert resp.status_int == 200
|
assert resp.status_int == 200
|
||||||
assert resp.content_type == 'text/html'
|
assert resp.content_type == 'text/html'
|
||||||
assert 'config.yaml overriden search page' in resp.body
|
assert 'config.yaml overriden search page: {"some": "value"}' in resp.body
|
||||||
|
|
||||||
|
resp = self.testapp.get('/test/20140103030321/http://example.com?example=1')
|
||||||
|
assert resp.status_int == 200
|
||||||
|
|
||||||
def test_no_templates(self):
|
def test_no_templates(self):
|
||||||
""" Test removing templates dir, using default template again
|
""" Test removing templates dir, using default template again
|
||||||
"""
|
"""
|
||||||
shutil.rmtree(os.path.join(self.root_dir, 'collections', 'test', 'templates'))
|
shutil.rmtree(os.path.join(self.root_dir, 'collections', 'foo', 'templates'))
|
||||||
|
|
||||||
self._create_app()
|
self._create_app()
|
||||||
|
|
||||||
resp = self.testapp.get('/test/')
|
resp = self.testapp.get('/foo/')
|
||||||
assert resp.status_int == 200
|
assert resp.status_int == 200
|
||||||
assert resp.content_type == 'text/html'
|
assert resp.content_type == 'text/html'
|
||||||
assert 'pywb custom search page' not in resp.body
|
assert 'pywb custom search page' not in resp.body
|
||||||
@ -273,12 +287,12 @@ class TestManagedColls(object):
|
|||||||
main(['list'])
|
main(['list'])
|
||||||
sys.stdout = orig_stdout
|
sys.stdout = orig_stdout
|
||||||
|
|
||||||
output = buff.getvalue().splitlines()
|
output = sorted(buff.getvalue().splitlines())
|
||||||
assert len(output) == 4
|
assert len(output) == 4
|
||||||
assert 'Collections' in output[0]
|
assert 'Collections:' in output
|
||||||
assert 'foo' in output[1]
|
assert '- foo' in output
|
||||||
assert 'nested' in output[2]
|
assert '- nested' in output
|
||||||
assert 'test' in output[3]
|
assert '- test' in output
|
||||||
|
|
||||||
def test_err_no_such_coll(self):
|
def test_err_no_such_coll(self):
|
||||||
""" Test error adding warc to non-existant collection
|
""" Test error adding warc to non-existant collection
|
||||||
@ -310,6 +324,10 @@ class TestManagedColls(object):
|
|||||||
"""
|
"""
|
||||||
colls = os.path.join(self.root_dir, 'collections')
|
colls = os.path.join(self.root_dir, 'collections')
|
||||||
|
|
||||||
|
# No Statics -- ignorable
|
||||||
|
shutil.rmtree(os.path.join(colls, 'foo', 'static'))
|
||||||
|
self._create_app()
|
||||||
|
|
||||||
# No WARCS
|
# No WARCS
|
||||||
warcs_path = os.path.join(colls, 'foo', 'warcs')
|
warcs_path = os.path.join(colls, 'foo', 'warcs')
|
||||||
shutil.rmtree(warcs_path)
|
shutil.rmtree(warcs_path)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user