allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [4/5] allura git commit: [#8033] tests for create_sitemap_files.py
Date Fri, 11 Dec 2015 19:33:01 GMT
[#8033] tests for create_sitemap_files.py

* remove unused template files
* better config options for output domain & url dir
* could not actually test for c.app problem since it'd be very compilicated to run
  script as a paster command and also have sample data set up and also have `c` set
  as the right type of object
* it validates XML and stuff though :)


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/d274bc6f
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/d274bc6f
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/d274bc6f

Branch: refs/heads/db/8033
Commit: d274bc6fef913d1b8447df238fad01f726e5d3e6
Parents: c9ce7e3
Author: Dave Brondsema <dave@brondsema.net>
Authored: Fri Dec 11 14:30:19 2015 -0500
Committer: Dave Brondsema <dave@brondsema.net>
Committed: Fri Dec 11 14:32:50 2015 -0500

----------------------------------------------------------------------
 Allura/allura/scripts/create_sitemap_files.py   |  9 ++-
 Allura/allura/templates/sitemap.xml             | 28 ----------
 Allura/allura/templates/sitemap_index.xml       | 28 ----------
 .../tests/scripts/test_create_sitemap_files.py  | 58 ++++++++++++++++++++
 4 files changed, 64 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/d274bc6f/Allura/allura/scripts/create_sitemap_files.py
----------------------------------------------------------------------
diff --git a/Allura/allura/scripts/create_sitemap_files.py b/Allura/allura/scripts/create_sitemap_files.py
index e4d98a9..bd34320 100644
--- a/Allura/allura/scripts/create_sitemap_files.py
+++ b/Allura/allura/scripts/create_sitemap_files.py
@@ -37,6 +37,7 @@ import pylons
 import webob
 from pylons import tmpl_context as c
 from ming.orm import ThreadLocalORMSession
+from tg import config
 
 from allura import model as M
 from allura.lib import security, utils
@@ -44,7 +45,6 @@ from allura.scripts import ScriptTask
 
 
 MAX_SITEMAP_URLS = 50000
-BASE_URL = 'http://sourceforge.net'
 
 INDEX_TEMPLATE = """\
 <?xml version="1.0" encoding="utf-8"?>
@@ -112,7 +112,7 @@ class CreateSitemapFiles(ScriptTask):
                 c.project = p
                 try:
                     for s in p.sitemap(excluded_tools=['git', 'hg', 'svn']):
-                        url = BASE_URL + s.url if s.url[0] == '/' else s.url
+                        url = config['base_url'] + s.url if s.url[0] == '/' else s.url
                         locs.append({'url': url,
                                      'date': p.last_updated.strftime("%Y-%m-%d")})
 
@@ -135,7 +135,7 @@ class CreateSitemapFiles(ScriptTask):
             sitemap_index_vars = dict(
                 now=now,
                 sitemaps=[
-                    '%s/allura_sitemap/sitemap-%d.xml' % (BASE_URL, n)
+                    '%s%s/sitemap-%d.xml' % (config['base_url'], options.url_dir, n)
                     for n in range(file_count)])
             sitemap_index_content = Template(
                 INDEX_TEMPLATE).render(sitemap_index_vars)
@@ -164,6 +164,9 @@ class CreateSitemapFiles(ScriptTask):
         parser.add_argument('-n', '--neighborhood', dest='neighborhood',
                             help="URL prefix of excluded neighborhood(s)",
                             default=None, nargs='*')
+        parser.add_argument('--url-dir', dest='url_dir',
+                            default='/allura_sitemap',
+                            help='URL directory in which the files will be served from')
         return parser
 
 

http://git-wip-us.apache.org/repos/asf/allura/blob/d274bc6f/Allura/allura/templates/sitemap.xml
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/sitemap.xml b/Allura/allura/templates/sitemap.xml
deleted file mode 100644
index d139349..0000000
--- a/Allura/allura/templates/sitemap.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-       Licensed to the Apache Software Foundation (ASF) under one
-       or more contributor license agreements.  See the NOTICE file
-       distributed with this work for additional information
-       regarding copyright ownership.  The ASF licenses this file
-       to you under the Apache License, Version 2.0 (the
-       "License"); you may not use this file except in compliance
-       with the License.  You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-       Unless required by applicable law or agreed to in writing,
-       software distributed under the License is distributed on an
-       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-       KIND, either express or implied.  See the License for the
-       specific language governing permissions and limitations
-       under the License.
--->
-<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
-    {% for loc in locs %}
-    <url>
-        <loc>{{ loc }}</loc>
-        <lastmod>{{ now }}</lastmod>
-        <changefreq>daily</changefreq>
-    </url>
-    {% endfor %}
-</urlset>

http://git-wip-us.apache.org/repos/asf/allura/blob/d274bc6f/Allura/allura/templates/sitemap_index.xml
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/sitemap_index.xml b/Allura/allura/templates/sitemap_index.xml
deleted file mode 100644
index 9b1e244..0000000
--- a/Allura/allura/templates/sitemap_index.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-       Licensed to the Apache Software Foundation (ASF) under one
-       or more contributor license agreements.  See the NOTICE file
-       distributed with this work for additional information
-       regarding copyright ownership.  The ASF licenses this file
-       to you under the Apache License, Version 2.0 (the
-       "License"); you may not use this file except in compliance
-       with the License.  You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-       Unless required by applicable law or agreed to in writing,
-       software distributed under the License is distributed on an
-       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-       KIND, either express or implied.  See the License for the
-       specific language governing permissions and limitations
-       under the License.
--->
-<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
-  {% for sitemap in sitemaps %}
-   <sitemap>
-      <loc>{{ sitemap }}</loc>
-      <lastmod>{{ now }}</lastmod>
-      <changefreq>daily</changefreq>
-   </sitemap>
-   {% endfor %}
-</sitemapindex>

http://git-wip-us.apache.org/repos/asf/allura/blob/d274bc6f/Allura/allura/tests/scripts/test_create_sitemap_files.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/scripts/test_create_sitemap_files.py b/Allura/allura/tests/scripts/test_create_sitemap_files.py
new file mode 100644
index 0000000..240d7ba
--- /dev/null
+++ b/Allura/allura/tests/scripts/test_create_sitemap_files.py
@@ -0,0 +1,58 @@
+#       Licensed to the Apache Software Foundation (ASF) under one
+#       or more contributor license agreements.  See the NOTICE file
+#       distributed with this work for additional information
+#       regarding copyright ownership.  The ASF licenses this file
+#       to you under the Apache License, Version 2.0 (the
+#       "License"); you may not use this file except in compliance
+#       with the License.  You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#       Unless required by applicable law or agreed to in writing,
+#       software distributed under the License is distributed on an
+#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#       KIND, either express or implied.  See the License for the
+#       specific language governing permissions and limitations
+#       under the License.
+
+import os
+from shutil import rmtree
+import xml.etree.ElementTree as ET
+
+from pylons import tmpl_context as c
+from nose.tools import assert_in
+from testfixtures import TempDirectory
+
+from alluratest.controller import setup_basic_test
+from allura import model as M
+from allura.lib import helpers as h
+from allura.scripts.create_sitemap_files import CreateSitemapFiles
+
+
+class TestCreateSitemapFiles(object):
+
+    def setUp(self):
+        setup_basic_test()
+
+    def run_script(self, options):
+        cls = CreateSitemapFiles
+        opts = cls.parser().parse_args(options)
+        with h.push_config(c, user=M.User.anonymous()):  # tasks & scripts have c.user
set
+            cls.execute(opts)
+
+    def test_create(self):
+        with TempDirectory() as tmpdir:
+            rmtree(tmpdir.path)  # needs to be non-existent for the script
+            self.run_script(['-o', tmpdir.path])
+
+            tmpdir.check('sitemap-0.xml', 'sitemap.xml')
+
+            xml_index = ET.parse(os.path.join(tmpdir.path, 'sitemap.xml'))
+            ns = {'ns0': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
+            locs = [loc.text for loc in xml_index.findall('ns0:sitemap/ns0:loc', ns)]
+            assert_in('http://localhost:8080/allura_sitemap/sitemap-0.xml', locs)
+
+            xml_0 = ET.parse(os.path.join(tmpdir.path, 'sitemap-0.xml'))
+            urls = [loc.text for loc in xml_0.findall('ns0:url/ns0:loc', ns)]
+            assert_in('http://localhost:8080/p/wiki/', urls)
+            assert_in('http://localhost:8080/p/test/sub1/', urls)


Mime
View raw message