# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function

from contextlib import contextmanager
import os
import tempfile
import unittest

from context import nikola
from nikola import main


@contextmanager
def cd(path):
    old_dir = os.getcwd()
    os.chdir(path)
    yield
    os.chdir(old_dir)


class EmptyBuildTest(unittest.TestCase):
    """Basic integration testcase."""
    def setUp(self):
        """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()

    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)

    def patch_site(self):
        """Make any modifications you need to the site."""

    def build(self):
        """Build the site."""
        with cd(self.target_dir):
            main.main([])

    def tearDown(self):
        """Reove the demo site."""
        #shutil.rmtree(self.tmpdir)

    def test_build(self):
        self.assertTrue(True)


class DemoBuildTest(EmptyBuildTest):
    """Test that a default build of --demo works."""

    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)

@@ -122,16 +122,30 @@
 port os%0A
+import shutil%0A
 import t
@@ -169,16 +169,34 @@
 ittest%0A%0A
+import lxml.html%0A%0A
 from con
@@ -1119,16 +1119,23 @@
 n.main(%5B
+%22build%22
 %5D)%0A%0A    
@@ -1167,16 +1167,17 @@
    %22%22%22Re
+m
 ove the 
@@ -1198,17 +1198,16 @@
         
-#
 shutil.r
@@ -1264,28 +1264,186 @@
     
-self.assertTrue(True
+%22%22%22Ensure the build did something.%22%22%22%0A        index_path = os.path.join(%0A            self.target_dir, %22output%22, %22archive.html%22)%0A        self.assertTrue(os.path.isfile(index_path)
 )%0A%0A%0A
@@ -1709,28 +1709,1161 @@
 figuration(self.target_dir)%0A
+%0Aclass RelativeLinkTest(DemoBuildTest):%0A    %22%22%22Check that SITE_URL with a path doesn't break links.%22%22%22%0A%0A    def patch_site(self):%0A        %22%22%22Set the SITE_URL to have a path%22%22%22%0A        conf_path = os.path.join(self.target_dir, %22conf.py%22)%0A        with open(conf_path, %22rb%22) as inf:%0A            data = inf.read()%0A            data.replace('SITE_URL = %22http://nikola.ralsina.com.ar%22',%0A                         'SITE_URL = %22http://nikola.ralsina.com.ar/foo/bar/%22')%0A        with open(conf_path, %22wb+%22) as outf:%0A            outf.write(data)%0A%0A    def test_relative_links(self):%0A        %22%22%22Check that the links in output/index.html are correct%22%22%22%0A        test_path = os.path.join(self.target_dir, %22output%22, %22index.html%22)%0A        with open(test_path, %22rb%22) as inf:%0A            data = inf.read()%0A            for _, _, url, _ in lxml.html.iterlinks(data):%0A                # Just need to be sure this one is ok%0A                if %22assets/css/all-nocdn.css%22 in url:%0A                    self.assertEqual(url, 'assets/css/all-nocdn.css')%0A                    flag = True%0A        # But I also need to be sure it is there!%0A        self.assertTrue(flag)%0A

fix integration tests, added one for Issue 304