Repository: ckan/ckan

Issue:
Snippet HTML comment should only be inserted when debugging is enabled
As of CKAN 2.5.1, the `{% snippet %}` Jinja-directive always places HTML-comments around the snippet content:

``` html
<!-- Snippet snippets/tag_list.html start -->
  <ul class="tag-list">
      <li>
        <a class="tag" href="/data/dataset?tags=geodaten">geodaten</a>    
      <li>
        <a class="tag" href="/data/dataset?tags=markt">markt</a>    
  </ul>
<!-- Snippet snippets/tag_list.html end -->
```

In my opinion, this should only be done if debug mode is enabled.

Snippet HTML comment should only be inserted when debugging is enabled
As of CKAN 2.5.1, the `{% snippet %}` Jinja-directive always places HTML-comments around the snippet content:

``` html
<!-- Snippet snippets/tag_list.html start -->
  <ul class="tag-list">
      <li>
        <a class="tag" href="/data/dataset?tags=geodaten">geodaten</a>    
      <li>
        <a class="tag" href="/data/dataset?tags=markt">markt</a>    
  </ul>
<!-- Snippet snippets/tag_list.html end -->
```

In my opinion, this should only be done if debug mode is enabled.



Discussion:

Resolving patch:
diff --git a/ckan/lib/base.py b/ckan/lib/base.py
--- a/ckan/lib/base.py
+++ b/ckan/lib/base.py
@@ -75,8 +75,9 @@ def render_snippet(template_name, **kw):
     cache_force = kw.pop('cache_force', None)
     output = render(template_name, extra_vars=kw, cache_force=cache_force,
                     renderer='snippet')
-    output = '\n<!-- Snippet %s start -->\n%s\n<!-- Snippet %s end -->\n' % (
-        template_name, output, template_name)
+    if config.get('debug'):
+        output = ('\n<!-- Snippet %s start -->\n%s\n<!-- Snippet %s end -->\n'
+                  % (template_name, output, template_name))
     return literal(output)
 
 


Test patch:
diff --git a/ckan/tests/controllers/test_admin.py b/ckan/tests/controllers/test_admin.py
--- a/ckan/tests/controllers/test_admin.py
+++ b/ckan/tests/controllers/test_admin.py
@@ -223,6 +223,7 @@ def test_custom_css(self):
         style_tag = reset_intro_response_html.select('head style')
         assert_equal(len(style_tag), 0)
 
+    @helpers.change_config('debug', True)
     def test_homepage_style(self):
         '''Select a homepage style'''
         app = self._get_test_app()
@@ -255,6 +256,7 @@ def test_homepage_style(self):
 class TestTrashView(helpers.FunctionalTestBase):
     '''View tests for permanently deleting datasets with Admin Trash.'''
 
+    @helpers.change_config('debug', True)
     def test_trash_view_anon_user(self):
         '''An anon user shouldn't be able to access trash view.'''
         app = self._get_test_app()
diff --git a/ckan/tests/lib/test_base.py b/ckan/tests/lib/test_base.py
--- a/ckan/tests/lib/test_base.py
+++ b/ckan/tests/lib/test_base.py
@@ -3,6 +3,21 @@
 import ckan.tests.helpers as helpers
 
 
+class TestRenderSnippet(helpers.FunctionalTestBase):
+    """
+    Test ``ckan.lib.base.render_snippet``.
+    """
+    @helpers.change_config('debug', True)
+    def test_comment_present_if_debug_true(self):
+        response = self._get_test_app().get('/')
+        assert '<!-- Snippet ' in response
+
+    @helpers.change_config('debug', False)
+    def test_comment_absent_if_debug_false(self):
+        response = self._get_test_app().get('/')
+        assert '<!-- Snippet ' not in response
+
+
 class TestCORS(helpers.FunctionalTestBase):
 
     def test_options(self):