From be5139b63530cc89241b668de2cd9634e2ea1748 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Sun, 15 Mar 2015 22:23:08 -0700 Subject: [PATCH] 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 --- pywb/webapp/pywb_init.py | 28 +++++++++++++++++---------- pywb/webapp/views.py | 2 +- tests/test_auto_colls.py | 42 ++++++++++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/pywb/webapp/pywb_init.py b/pywb/webapp/pywb_init.py index 1603e805..42de9e19 100644 --- a/pywb/webapp/pywb_init.py +++ b/pywb/webapp/pywb_init.py @@ -149,9 +149,14 @@ class DirectoryCollsLoader(object): 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): if dir_key in coll: - # already set + coll[dir_key] = self._norm_path(root_dir, coll[dir_key]) return False 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): self.static_routes['static/' + name] = coll_config['static_path'] - # Add templates + # Custom templates dir templates_dir = self.config.get('paths').get('templates_dir') if 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) - if os.path.isfile(full): - coll_config[tname] = full + # Check all templates + template_files = self.config.get('paths')['template_files'] + 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 diff --git a/pywb/webapp/views.py b/pywb/webapp/views.py index 44dcb052..13cff6ce 100644 --- a/pywb/webapp/views.py +++ b/pywb/webapp/views.py @@ -61,7 +61,7 @@ def is_wb_handler(obj): @template_filter() -def jsonify(obj): +def tojson(obj): return json.dumps(obj) diff --git a/tests/test_auto_colls.py b/tests/test_auto_colls.py index bcadf064..5d0700fe 100644 --- a/tests/test_auto_colls.py +++ b/tests/test_auto_colls.py @@ -236,30 +236,44 @@ class TestManagedColls(object): def test_custom_config(self): """ 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') 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: - 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() resp = self.testapp.get('/test/') assert resp.status_int == 200 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): """ 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() - resp = self.testapp.get('/test/') + resp = self.testapp.get('/foo/') assert resp.status_int == 200 assert resp.content_type == 'text/html' assert 'pywb custom search page' not in resp.body @@ -273,12 +287,12 @@ class TestManagedColls(object): main(['list']) sys.stdout = orig_stdout - output = buff.getvalue().splitlines() + output = sorted(buff.getvalue().splitlines()) assert len(output) == 4 - assert 'Collections' in output[0] - assert 'foo' in output[1] - assert 'nested' in output[2] - assert 'test' in output[3] + assert 'Collections:' in output + assert '- foo' in output + assert '- nested' in output + assert '- test' in output def test_err_no_such_coll(self): """ Test error adding warc to non-existant collection @@ -310,6 +324,10 @@ class TestManagedColls(object): """ colls = os.path.join(self.root_dir, 'collections') + # No Statics -- ignorable + shutil.rmtree(os.path.join(colls, 'foo', 'static')) + self._create_app() + # No WARCS warcs_path = os.path.join(colls, 'foo', 'warcs') shutil.rmtree(warcs_path)