from __future__ import print_function, unicode_literals
import io
import logging
import os
import shutil
import unittest

import mock
import yaml

from chalmers import config, errors
from chalmers.scripts import chalmers_main


class ChalmersCli(object):
    def __init__(self):
        self.script = chalmers_main.__file__
        if self.script.endswith('.pyc') or self.script.endswith('.pyo'):
            self.script = self.script[:-1]
        self.env = os.environ.copy()
        self.root = 'test_config'
        self.env['CHALMERS_ROOT'] = self.root
        config.set_relative_dirs(self.root)

        logging.getLogger('cli-logger').addHandler(logging.NullHandler())


    def __getattr__(self, subcommand):

        def run_subcommand(*args):
            cmd = ['-q', '--no-color', subcommand]
            cmd.extend(args)

            out = io.StringIO()

            log = logging.getLogger('chalmers')
            log.setLevel(logging.ERROR)
            del log.handlers[:]

            with mock.patch('sys.stdout', out), mock.patch('chalmers.scripts.chalmers_main.setup_logging'):
                chalmers_main.main(cmd, False)

            return out.getvalue()

        return run_subcommand


class Test(unittest.TestCase):

    def setUp(self):
        self.cli = ChalmersCli()
        if os.path.isdir(self.cli.root):
            shutil.rmtree(self.cli.root)
        unittest.TestCase.setUp(self)

    def tearDown(self):
        if os.path.isdir(self.cli.root):
            shutil.rmtree(self.cli.root)
        unittest.TestCase.tearDown(self)

    def test_add_show(self):
        self.cli.add('echo', 'hi')
        self.assertEqual(self.cli.show('echo', '--cmd').strip(), 'echo hi')
        self.assertEqual(self.cli.show('echo', '--cmd').strip(), 'echo hi')


    def test_cant_add_twice(self):
        self.cli.add('echo', 'hi')
        with self.assertRaises(errors.ChalmersError):
            self.cli.add('echo', 'hi')

    def test_add_remove(self):
        self.cli.add('echo', 'hi')
        self.cli.remove('echo')

    def test_cant_remove_non_existent(self):

        with self.assertRaises(SystemExit):
            self.cli.remove('echo')

    def test_cant_start_non_existent(self):

        with self.assertRaises(SystemExit):
            self.cli.start('echo')


    def test_list_no_programs(self):

        out = self.cli.list()
        self.assertEqual(out.strip(), "No programs added")

    def test_list(self):

        out = self.cli.add('echo', 'hi')
        out = self.cli.list()
        self.assertEqual(out.split(), ['echo', 'OFF'])

    def test_pause(self):

        out = self.cli.add('echo', 'hi')

        out = self.cli.list()
        self.assertEqual(out.split(), ['echo', 'OFF'])

        out = self.cli.on('echo')

        out = self.cli.list()
        self.assertEqual(out.split(), ['echo', 'STOPPED'])

        out = self.cli.off('echo')

        out = self.cli.list()
        self.assertEqual(out.split(), ['echo', 'OFF'])

    def test_show(self):

        self.cli.add('echo', 'hi')
        out = self.cli.show('echo')

    def test_set(self):

        self.cli.add('echo', 'hi')
        out = self.cli.set('echo', 'x=1')
        self.assertEqual(out.strip(), "Set 'x' to 1 for program echo\ndone")
        out = self.cli.show('echo', '--def')
        with open(out.strip()) as fd:
            data = yaml.load(fd)

        self.assertEqual(data['x'], 1)

    def test_start(self):

        self.cli.add('echo', 'This is the output')
        out = self.cli.start('echo', '-w')
        self.assertIn('This is the output', out)

    def test_log(self):

        self.cli.add('echo', 'This is the output')
        out = self.cli.start('echo', '-w')
        self.assertIn('This is the output', out)

        self.assertEqual(self.cli.log('echo').strip(), 'This is the output')


if __name__ == "__main__":
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

@@ -1613,32 +1613,50 @@
 add('echo', 'hi'
+, '--off', '--off'
 )%0A        self.a
@@ -1855,32 +1855,41 @@
 add('echo', 'hi'
+, '--off'
 )%0A        with s
@@ -1961,24 +1961,33 @@
 'echo', 'hi'
+, '--off'
 )%0A%0A    def t
@@ -2033,32 +2033,41 @@
 add('echo', 'hi'
+, '--off'
 )%0A        self.c
@@ -2523,32 +2523,41 @@
 add('echo', 'hi'
+, '--off'
 )%0A        out = 
@@ -2694,16 +2694,25 @@
 o', 'hi'
+, '--off'
 )%0A%0A     
@@ -3090,32 +3090,41 @@
 add('echo', 'hi'
+, '--off'
 )%0A        out = 
@@ -3204,16 +3204,25 @@
 o', 'hi'
+, '--off'
 )%0A      
@@ -3559,32 +3559,41 @@
 s is the output'
+, '--off'
 )%0A        out = 
@@ -3745,16 +3745,25 @@
  output'
+, '--off'
 )%0A      

fix tests