diff options
| author | 2013-03-13 20:58:39 -0300 | |
|---|---|---|
| committer | 2013-03-13 20:58:39 -0300 | |
| commit | 8b14a1e5b2ca574fdd4fd2377567ec98a110d4b6 (patch) | |
| tree | 0895935489e4920d18824f7fb3a0d799649a27c3 /tests/test_integration.py | |
| parent | 878ba1152ebc64a4a2609d23c9e400a6111db642 (diff) | |
Imported Upstream version 5.4.2upstream/5.4.2
Diffstat (limited to 'tests/test_integration.py')
| -rw-r--r-- | tests/test_integration.py | 157 |
1 files changed, 126 insertions, 31 deletions
diff --git a/tests/test_integration.py b/tests/test_integration.py index 947a832..c940a07 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1,13 +1,17 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, print_function +import codecs from contextlib import contextmanager import os import shutil import tempfile import unittest +import lxml.html + from context import nikola +from nikola import main @contextmanager @@ -18,56 +22,147 @@ def cd(path): os.chdir(old_dir) -class IntegrationTest(unittest.TestCase): +class EmptyBuildTest(unittest.TestCase): """Basic integration testcase.""" - def setUp(self): + + dataname = None + + @classmethod + def setUpClass(self): """Setup a demo site.""" self.tmpdir = tempfile.mkdtemp() self.target_dir = os.path.join(self.tmpdir, "target") - self.build_command = nikola.plugins.command_build.CommandBuild() self.init_command = nikola.plugins.command_init.CommandInit() - self.init_command.copy_sample_site(self.target_dir) - self.init_command.create_configuration(self.target_dir) + self.fill_site() self.patch_site() self.build() + @classmethod + def fill_site(self): + """Add any needed initial content.""" + self.init_command.create_empty_site(self.target_dir) + self.init_command.create_configuration(self.target_dir) + + if self.dataname: + src = os.path.join(os.path.dirname(__file__), 'data', + self.dataname) + for root, dirs, files in os.walk(src): + for src_name in files: + rel_dir = os.path.relpath(root, src) + dst_file = os.path.join(self.target_dir, rel_dir, src_name) + src_file = os.path.join(root, src_name) + shutil.copy2(src_file, dst_file) + + @classmethod def patch_site(self): """Make any modifications you need to the site.""" - pass + @classmethod def build(self): """Build the site.""" with cd(self.target_dir): - self.build_command.run() + main.main(["build"]) - def tearDown(self): - """Reove the demo site.""" - shutil.rmtree(self.tmpdir) + @classmethod + def tearDownClass(self): + """Remove the demo site.""" + def test_build(self): + """Ensure the build did something.""" + index_path = os.path.join( + self.target_dir, "output", "archive.html") + self.assertTrue(os.path.isfile(index_path)) -class EmptytBuild(IntegrationTest): - """Basic integration testcase.""" - def setUp(self): - """Setup a demo site.""" - self.tmpdir = tempfile.mkdtemp() - self.target_dir = os.path.join(self.tmpdir, "target") - self.build_command = nikola.plugins.command_build.CommandBuild() - self.init_command = nikola.plugins.command_init.CommandInit() - self.init_command.create_empty_site(self.target_dir) + +class DemoBuildTest(EmptyBuildTest): + """Test that a default build of --demo works.""" + + @classmethod + def fill_site(self): + """Fill the site with demo content.""" + self.init_command.copy_sample_site(self.target_dir) self.init_command.create_configuration(self.target_dir) - self.patch_site() - self.build() - def test_deleted_dodo(self): - """Test that a default build of --demo works.""" - # Ensure the temprary dodo file is deleted (Issue #302) - self.assertFalse(os.path.isfile(self.build_command.dodo.name)) +class TranslatedBuildTest(EmptyBuildTest): + """Test a site with translated content.""" -class DefaultBuild(IntegrationTest): - """Test that a default build of --demo works.""" + dataname = "translated_titles" + + def test_translated_titles(self): + """Check that translated title is picked up.""" + en_file = os.path.join(self.target_dir, "output", "stories", "1.html") + es_file = os.path.join(self.target_dir, "output", "es", "stories", "1.html") + # Files should be created + self.assertTrue(os.path.isfile(en_file)) + self.assertTrue(os.path.isfile(es_file)) + # And now let's check the titles + with codecs.open(en_file, 'r', 'utf8') as inf: + doc = lxml.html.parse(inf) + self.assertEqual(doc.find('//title').text, 'Foo | Demo Site') + with codecs.open(es_file, 'r', 'utf8') as inf: + doc = lxml.html.parse(inf) + self.assertEqual(doc.find('//title').text, 'Bar | Demo Site') + + +class RelativeLinkTest(DemoBuildTest): + """Check that SITE_URL with a path doesn't break links.""" + + @classmethod + def patch_site(self): + """Set the SITE_URL to have a path""" + conf_path = os.path.join(self.target_dir, "conf.py") + with codecs.open(conf_path, "rb", "utf-8") as inf: + data = inf.read() + data = data.replace('SITE_URL = "http://nikola.ralsina.com.ar"', + 'SITE_URL = "http://nikola.ralsina.com.ar/foo/bar/"') + with codecs.open(conf_path, "wb+", "utf8") as outf: + outf.write(data) + + def test_relative_links(self): + """Check that the links in output/index.html are correct""" + test_path = os.path.join(self.target_dir, "output", "index.html") + flag = False + with open(test_path, "rb") as inf: + data = inf.read() + for _, _, url, _ in lxml.html.iterlinks(data): + # Just need to be sure this one is ok + if url.endswith("css"): + self.assertFalse(url.startswith("..")) + flag = True + # But I also need to be sure it is there! + self.assertTrue(flag) + + +class RelativeLinkTest2(DemoBuildTest): + """Check that dropping stories to the root doesn't break links.""" + + @classmethod + def patch_site(self): + """Set the SITE_URL to have a path""" + conf_path = os.path.join(self.target_dir, "conf.py") + with codecs.open(conf_path, "rb", "utf-8") as inf: + data = inf.read() + data = data.replace('("stories/*.txt", "stories", "story.tmpl", False),', + '("stories/*.txt", "", "story.tmpl", False),') + data = data.replace('# INDEX_PATH = ""', + 'INDEX_PATH = "blog"') + with codecs.open(conf_path, "wb+", "utf8") as outf: + outf.write(data) + outf.flush() - def test_deleted_dodo(self): - """Test that a default build of --demo works.""" - # Ensure the temprary dodo file is deleted (Issue #302) - self.assertFalse(os.path.isfile(self.build_command.dodo.name)) + def test_relative_links(self): + """Check that the links in a story are correct""" + conf_path = os.path.join(self.target_dir, "conf.py") + data = open(conf_path).read() + test_path = os.path.join(self.target_dir, "output", "about-nikola.html") + flag = False + with open(test_path, "rb") as inf: + data = inf.read() + for _, _, url, _ in lxml.html.iterlinks(data): + # Just need to be sure this one is ok + if url.endswith("css"): + self.assertFalse(url.startswith("..")) + flag = True + # But I also need to be sure it is there! + self.assertTrue(flag) |
