# -*- coding: utf-8 -*-

import argparse
import os
import tempfile
import unittest

import mock

from cliez import parser

try:
    input = raw_input
    mock_input = '__builtin__.raw_input'
except NameError:
    mock_input = 'builtins.input'
    pass


class InitComponentTestCase(unittest.TestCase):
    def setUp(self):
        from cliez import main
        _ = main
        pass

    def test_ok(self):
        dp = tempfile.TemporaryDirectory()
        parser.parse(argparse.ArgumentParser(),
                     argv=['command', 'create', 'wangwenpei/cliez-kickstart',
                           '--dir', dp.name, '--debug'])

        with mock.patch(mock_input,
                        side_effect=['yes', 'pkg_demo', "\n", "\n"]):
            parser.parse(argparse.ArgumentParser(),
                         argv=['command', 'init', '--dir', dp.name, '--debug'])

        pass

    def test_with_variables(self):
        """
        custom variable
        :return:
        """

        with tempfile.TemporaryDirectory() as dp:
            parser.parse(argparse.ArgumentParser(),
                         argv=['command', 'create',
                               'wangwenpei/cliez-kickstart',
                               '--dir', dp,
                               '--debug'])

            with mock.patch(mock_input,
                            side_effect=['yes', 'pkg_demo', "\n", "\n"]):
                parser.parse(argparse.ArgumentParser(),
                             argv=['command', 'init', '--dir', dp, '--debug',
                                   '-s', 'keywords:a-keyword'])

        pass

    def test_skip_builtin(self):
        """
        custom variable
        :return:
        """

        with tempfile.TemporaryDirectory() as dp:
            parser.parse(argparse.ArgumentParser(),
                         argv=['command', 'create',
                               'wangwenpei/cliez-kickstart',
                               '--dir', dp,
                               '--debug'])
            parser.parse(argparse.ArgumentParser(),
                         argv=['command', 'init', '--dir', dp, '--debug', '-s',
                               'keywords:a-keyword', '--skip-builtin',
                               '--yes'])

        pass

    # def test_in_invalid_root(self):
    #     """
    #     missing argument
    #     :return:
    #     """
    #
    #     dp = tempfile.TemporaryDirectory()
    #     os.chdir(dp.name)
    #
    #     with self.assertRaises(SystemExit):
    #         with mock.patch(mock_input, side_effect=['yes',
    # 'pkg_demo', "\n", "\n"]):
    #             parser.parse(argparse.ArgumentParser(), argv=['command',
    #  'init'])
    #
    #     pass

    def test_in_dange_root(self):
        """
        缺失参数
        :return:
        """

        with self.assertRaises(SystemExit):
            with mock.patch(mock_input,
                            side_effect=['yes', 'pkg_demo', "\n", "\n"]):
                parser.parse(argparse.ArgumentParser(),
                             argv=['command', 'init', '--dir', '/'])

        os.chdir(os.path.expanduser('~'))
        with self.assertRaises(SystemExit):
            with mock.patch(mock_input,
                            side_effect=['yes', 'pkg_demo', "\n", "\n"]):
                parser.parse(argparse.ArgumentParser(),
                             argv=['command', 'init', '--dir', '/'])

        pass

    def test_no_confirm(self):
        """
        :return:
        """

        with tempfile.TemporaryDirectory() as dp:
            with mock.patch(mock_input, side_effect=['pkg_demo', "\n", "\n"]):
                parser.parse(argparse.ArgumentParser(),
                             argv=['command', 'init', '--yes', '--dir', dp])

        pass

    pass

@@ -1,29 +1,4 @@
-# -*- coding: utf-8 -*-%0A%0A
 impo
@@ -95,98 +95,8 @@
 er%0A%0A
-try:%0A    input = raw_input%0A    mock_input = '__builtin__.raw_input'%0Aexcept NameError:%0A    
 mock
@@ -121,25 +121,16 @@
 .input'%0A
-    pass%0A
 %0A%0Aclass 
@@ -2156,542 +2156,37 @@
     
-# def test_in_invalid_root(self):%0A    #     %22%22%22%0A    #     missing argument%0A    #     :return:%0A    #     %22%22%22%0A    #%0A    #     dp = tempfile.TemporaryDirectory()%0A    #     os.chdir(dp.name)%0A    #%0A    #     with self.assertRaises(SystemExit):%0A    #         with mock.patch(mock_input, side_effect=%5B'yes',%0A    # 'pkg_demo', %22%5Cn%22, %22%5Cn%22%5D):%0A    #             parser.parse(argparse.ArgumentParser(), argv=%5B'command',%0A    #  'init'%5D)%0A    #%0A    #     pass%0A%0A    def test_in_dange_root(self):%0A        %22%22%22%0A        %E7%BC%BA%E5%A4%B1%E5%8F%82%E6%95%B0%0A        :return:%0A        %22%22%22%0A
+def test_in_dange_root(self):
 %0A   
@@ -2841,50 +2841,8 @@
 f):%0A
-        %22%22%22%0A        :return:%0A        %22%22%22%0A%0A
     

improve init component testcase