aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_integration.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_integration.py')
-rw-r--r--tests/test_integration.py221
1 files changed, 171 insertions, 50 deletions
diff --git a/tests/test_integration.py b/tests/test_integration.py
index 802dcc7..fdb2494 100644
--- a/tests/test_integration.py
+++ b/tests/test_integration.py
@@ -1,44 +1,40 @@
# -*- coding: utf-8 -*-
-from __future__ import unicode_literals, print_function
+from __future__ import unicode_literals, print_function, absolute_import
import codecs
-from contextlib import contextmanager
import locale
import os
import shutil
-import subprocess # NOQA
+import subprocess
+import sys
import tempfile
import unittest
import lxml.html
from nose.plugins.skip import SkipTest
-from context import nikola
from nikola import main
+import nikola
+import nikola.plugins.command
+import nikola.plugins.command.init
+from .base import BaseTestCase, cd
-@contextmanager
-def cd(path):
- old_dir = os.getcwd()
- os.chdir(path)
- yield
- os.chdir(old_dir)
-
-class EmptyBuildTest(unittest.TestCase):
+class EmptyBuildTest(BaseTestCase):
"""Basic integration testcase."""
dataname = None
@classmethod
- def setUpClass(self):
+ def setUpClass(cls):
"""Setup a demo site."""
- self.tmpdir = tempfile.mkdtemp()
- self.target_dir = os.path.join(self.tmpdir, "target")
- self.init_command = nikola.plugins.command_init.CommandInit()
- self.fill_site()
- self.patch_site()
- self.build()
+ cls.tmpdir = tempfile.mkdtemp()
+ cls.target_dir = os.path.join(cls.tmpdir, "target")
+ cls.init_command = nikola.plugins.command.init.CommandInit()
+ cls.fill_site()
+ cls.patch_site()
+ cls.build()
@classmethod
def fill_site(self):
@@ -69,7 +65,13 @@ class EmptyBuildTest(unittest.TestCase):
@classmethod
def tearDownClass(self):
"""Remove the demo site."""
- shutil.rmtree(self.tmpdir)
+ # ignore_errors=True for windows by issue #782
+ shutil.rmtree(self.tmpdir, ignore_errors=(sys.platform == 'win32'))
+ # Fixes Issue #438
+ try:
+ del sys.modules['conf']
+ except KeyError:
+ pass
def test_build(self):
"""Ensure the build did something."""
@@ -91,9 +93,82 @@ class DemoBuildTest(EmptyBuildTest):
outf.write(
".. title: foobar\n"
".. slug: foobar\n"
- ".. date: 2013/03/06 19:08:15\n"
+ ".. date: 2013-03-06 19:08:15\n"
+ )
+
+ def test_index_in_sitemap(self):
+ sitemap_path = os.path.join(self.target_dir, "output", "sitemap.xml")
+ sitemap_data = codecs.open(sitemap_path, "r", "utf8").read()
+ self.assertTrue('<loc>http://getnikola.com/index.html</loc>' in sitemap_data)
+
+ def test_avoid_double_slash_in_rss(self):
+ rss_path = os.path.join(self.target_dir, "output", "rss.xml")
+ rss_data = codecs.open(rss_path, "r", "utf8").read()
+ self.assertFalse('http://getnikola.com//' in rss_data)
+
+
+class RepeatedPostsSetting(DemoBuildTest):
+ """Duplicate POSTS, should not read each post twice, which causes conflicts."""
+ @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, "ab", "utf8") as outf:
+ outf.write('\nPOSTS = (("posts/*.txt", "posts", "post.tmpl"),("posts/*.txt", "posts", "post.tmpl"))\n')
+
+
+class FuturePostTest(EmptyBuildTest):
+ """Test a site with future posts."""
+
+ @classmethod
+ def fill_site(self):
+ import datetime
+ from nikola.utils import current_time
+ self.init_command.copy_sample_site(self.target_dir)
+ self.init_command.create_configuration(self.target_dir)
+
+ # Change COMMENT_SYSTEM_ID to not wait for 5 seconds
+ with codecs.open(os.path.join(self.target_dir, 'conf.py'), "ab+", "utf8") as outf:
+ outf.write('\nCOMMENT_SYSTEM_ID = "nikolatest"\n')
+
+ with codecs.open(os.path.join(self.target_dir, 'posts', 'empty1.txt'), "wb+", "utf8") as outf:
+ outf.write(
+ ".. title: foo\n"
+ ".. slug: foo\n"
+ ".. date: %s\n" % (current_time() + datetime.timedelta(-1)).strftime('%Y-%m-%d %H:%M:%S')
)
+ with codecs.open(os.path.join(self.target_dir, 'posts', 'empty2.txt'), "wb+", "utf8") as outf:
+ outf.write(
+ ".. title: bar\n"
+ ".. slug: bar\n"
+ ".. date: %s\n" % (current_time() + datetime.timedelta(1)).strftime('%Y-%m-%d %H:%M:%S')
+ )
+
+ def test_future_post(self):
+ """ Ensure that the future post is not present in the index and sitemap."""
+ index_path = os.path.join(self.target_dir, "output", "index.html")
+ sitemap_path = os.path.join(self.target_dir, "output", "sitemap.xml")
+ foo_path = os.path.join(self.target_dir, "output", "posts", "foo.html")
+ bar_path = os.path.join(self.target_dir, "output", "posts", "bar.html")
+ self.assertTrue(os.path.isfile(index_path))
+ self.assertTrue(os.path.isfile(foo_path))
+ self.assertTrue(os.path.isfile(bar_path))
+ index_data = codecs.open(index_path, "r", "utf8").read()
+ sitemap_data = codecs.open(sitemap_path, "r", "utf8").read()
+ self.assertTrue('foo.html' in index_data)
+ self.assertFalse('bar.html' in index_data)
+ self.assertTrue('foo.html' in sitemap_data)
+ self.assertFalse('bar.html' in sitemap_data)
+
+ # Run deploy command to see if future post is deleted
+ with cd(self.target_dir):
+ main.main(["deploy"])
+
+ self.assertTrue(os.path.isfile(index_path))
+ self.assertTrue(os.path.isfile(foo_path))
+ self.assertFalse(os.path.isfile(bar_path))
+
class TranslatedBuildTest(EmptyBuildTest):
"""Test a site with translated content."""
@@ -132,8 +207,8 @@ class RelativeLinkTest(DemoBuildTest):
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/"')
+ data = data.replace('SITE_URL = "http://getnikola.com/"',
+ 'SITE_URL = "http://getnikola.com/foo/bar/"')
with codecs.open(conf_path, "wb+", "utf8") as outf:
outf.write(data)
@@ -151,36 +226,51 @@ class RelativeLinkTest(DemoBuildTest):
# But I also need to be sure it is there!
self.assertTrue(flag)
+ def test_index_in_sitemap(self):
+ """Test that the correct path is in sitemap, and not the wrong one."""
+ sitemap_path = os.path.join(self.target_dir, "output", "sitemap.xml")
+ sitemap_data = codecs.open(sitemap_path, "r", "utf8").read()
+ self.assertFalse('<loc>http://getnikola.com/</loc>' in sitemap_data)
+ self.assertTrue('<loc>http://getnikola.com/foo/bar/index.html</loc>' in sitemap_data)
+
-#class TestCheck(DemoBuildTest):
- #"""The demo build should pass 'nikola check'"""
+if sys.version_info[0] == 2 and sys.version_info[1] < 7:
+ check_output = subprocess.check_call
+else:
+ check_output = subprocess.check_output
- #def test_check_links(self):
- #with cd(self.target_dir):
- #r = subprocess.call("nikola check -l", shell=True)
- #self.assertEqual(r, 0)
- #def test_check_files(self):
- #with cd(self.target_dir):
- #r = subprocess.call("nikola check -f", shell=True)
- #self.assertEqual(r, 0)
+class TestCheck(DemoBuildTest):
+ """The demo build should pass 'nikola check'"""
+ def test_check_links(self):
+ with cd(self.target_dir):
+ check_output("nikola check -l", shell=True, stderr=subprocess.STDOUT)
-#class TestCheckFailure(DemoBuildTest):
- #"""The demo build should pass 'nikola check'"""
+ def test_check_files(self):
+ with cd(self.target_dir):
+ check_output("nikola check -f", shell=True, stderr=subprocess.STDOUT)
- #def test_check_links_fail(self):
- #with cd(self.target_dir):
- #os.unlink(os.path.join("output", "archive.html"))
- #rv = subprocess.call("nikola check -l", shell=True)
- #self.assertEqual(rv, 1)
- #def test_check_files_fail(self):
- #with cd(self.target_dir):
- #with codecs.open(os.path.join("output", "foobar"), "wb+", "utf8") as outf:
- #outf.write("foo")
- #rv = subprocess.call("nikola check -f", shell=True)
- #self.assertEqual(rv, 1)
+class TestCheckFailure(DemoBuildTest):
+ """The demo build should pass 'nikola check'"""
+
+ def test_check_links_fail(self):
+ with cd(self.target_dir):
+ os.unlink(os.path.join("output", "archive.html"))
+ self.assertRaises(
+ subprocess.CalledProcessError,
+ check_output, "nikola check -f", shell=True
+ )
+
+ def test_check_files_fail(self):
+ with cd(self.target_dir):
+ with codecs.open(os.path.join("output", "foobar"), "wb+", "utf8") as outf:
+ outf.write("foo")
+ self.assertRaises(
+ subprocess.CalledProcessError,
+ check_output, "nikola check -f", shell=True
+ )
class RelativeLinkTest2(DemoBuildTest):
@@ -192,8 +282,10 @@ class RelativeLinkTest2(DemoBuildTest):
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('("stories/*.txt", "stories", "story.tmpl"),',
+ '("stories/*.txt", "", "story.tmpl"),')
+ data = data.replace('("stories/*.rst", "stories", "story.tmpl"),',
+ '("stories/*.rst", "", "story.tmpl"),')
data = data.replace('# INDEX_PATH = ""',
'INDEX_PATH = "blog"')
with codecs.open(conf_path, "wb+", "utf8") as outf:
@@ -202,8 +294,6 @@ class RelativeLinkTest2(DemoBuildTest):
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:
@@ -215,3 +305,34 @@ class RelativeLinkTest2(DemoBuildTest):
flag = True
# But I also need to be sure it is there!
self.assertTrue(flag)
+
+ def test_index_in_sitemap(self):
+ """Test that the correct path is in sitemap, and not the wrong one."""
+ sitemap_path = os.path.join(self.target_dir, "output", "sitemap.xml")
+ sitemap_data = codecs.open(sitemap_path, "r", "utf8").read()
+ self.assertFalse('<loc>http://getnikola.com/</loc>' in sitemap_data)
+ self.assertTrue('<loc>http://getnikola.com/blog/index.html</loc>' in sitemap_data)
+
+
+class MonthlyArchiveTest(DemoBuildTest):
+ """Check that the monthly archives build and are correct."""
+
+ @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('# CREATE_MONTHLY_ARCHIVE = False',
+ 'CREATE_MONTHLY_ARCHIVE = True')
+ with codecs.open(conf_path, "wb+", "utf8") as outf:
+ outf.write(data)
+ outf.flush()
+
+ def test_monthly_archive(self):
+ """See that it builds"""
+ self.assertTrue(os.path.isfile(os.path.join(self.tmpdir, 'target', 'output', '2012', '03', 'index.html')))
+
+
+if __name__ == "__main__":
+ unittest.main()