# test_lunagen_cli.py
"""
Test building website from the command line, using the lunagen program.
"""

import os, subprocess

import unittest

class LooksLikeHelper(object):
    srcdir = os.path.abspath(os.path.dirname(__file__))
    checkfiles = os.path.abspath(os.path.dirname(__file__))
    
    def assertFileMatches(self, file1, file2):
        result = subprocess.run(('diff', 
                            os.path.join(self.srcdir, file1), 
                            os.path.join(self.checkfiles, file2)))
        if result.returncode==0:
            return True
        else:
            raise self.failureException(
                "File '%s' differs from '%s'." % (file1, file2))
        
class HTMLTidyHelper(object):
    srcdir = os.path.abspath(os.path.dirname(__file__))
    
    def assertHTMLTidy(self, htmlfile):
        result = subprocess.run(('tidy', '-errors', '-quiet',
                            os.path.join(self.srcdir, htmlfile)))
        if result.returncode==0:
            return True
        else:
            raise self.failureException(
                "File '%s' has HTML Tidy Errors/Warnings."  % htmlfile)

class Test_Lunagen_CLI_Function(unittest.TestCase, LooksLikeHelper, HTMLTidyHelper):
    def setUp(self):
        self.cwd = os.getcwd()
        subprocess.run(('chmod', '+x', '../src/lunagen.py'))
        subprocess.run(('ln', '-s', '../src/lunagen.py', 'lunagen'))
        os.chdir(
            os.path.join(os.path.dirname(__file__), '..', 'testdata')
                )     
        
        self.checkfiles = os.path.join(self.srcdir, '..', 'testdata', 'checkfiles')
        
    def tearDown(self):
        os.chdir(os.path.dirname(__file__))
        subprocess.run(('rm', 'lunagen'))
        
    def test_simple_example(self):
        subprocess.run(('lunagen', 
                            'simple', 'build/simple_site',
                            '--verbose'), 
                       env={'PATH':self.cwd})
        
        index_path =    os.path.join(os.path.dirname(__file__), '..', 'testdata', 
                            'build', 'simple_site', 'index.html')
        
        self.assertTrue(os.path.exists(index_path))
        self.assertHTMLTidy(index_path)
        self.assertFileMatches(index_path, 'simple_index.html')
        
        
    def test_affiliates_example(self):
        subprocess.run(('lunagen', 
                            '-i', 'affiliates', 'build/affiliates_site',
                            '--seed=0', '-v'),
                       env={'PATH':self.cwd})
        
        index_path =    os.path.join(os.path.dirname(__file__), '..', 'testdata', 
                            'build', 'affiliates_site', 'index.html')
        
        self.assertTrue(os.path.exists(index_path))
        self.assertHTMLTidy(index_path)
        self.assertFileMatches(index_path, 'affiliates_index.html')
    
    def test_softwarelist_example(self):
        subprocess.run(('lunagen',
                            'softwarelist', '-o', 'build/software_site',
                            '-v', '--seed=0'),
                       env={'PATH':self.cwd})
        
        index_path =    os.path.join(os.path.dirname(__file__), '..', 'testdata', 
                            'build', 'software_site', 'index.html')
        self.assertTrue(os.path.exists(index_path))
        self.assertHTMLTidy(index_path)
        self.assertFileMatches(index_path, 'software_index.html')
        
    def test_products_example(self):
        subprocess.run(('lunagen',
                            '-i', 'products', '-o', 'build/products_site',
                            '-v'),
                       env={'PATH':self.cwd})
        
        index_path =    os.path.join(os.path.dirname(__file__), '..', 'testdata', 
                            'build', 'products_site', 'index.html')
        
        self.assertTrue(os.path.exists(index_path))
        self.assertHTMLTidy(index_path)
        self.assertFileMatches(index_path, 'products_index.html')
        
    def test_series_example(self):
        subprocess.run(('lunagen',
                        'series', 'build/series_site', '--verbose', '--seed=0'),
                        env={'PATH':self.cwd})
        
        index_path =    os.path.join(os.path.dirname(__file__), '..', 'testdata',
                            'build', 'series_site', 'index_html')
        
        self.assertTrue(os.path.exists(index_path))
        self.assertHTMLTidy(index_path)
        self.assertFileMatches(index_path, 'series_index.html')

