"""Functional tests for command line use."""

import errno
import os
import random
import shutil
import string
import tempfile
import unittest

from fastfood import pack
from fastfood import shell

TEST_TEMPLATEPACK = os.path.abspath(
    os.path.join(os.path.dirname(os.path.realpath(__file__)),
                 os.path.pardir, 'test_templatepack'))
assert os.path.isdir(TEST_TEMPLATEPACK), "Test templatepack not found."


class MockArgs(object):

    def __init__(self, **args):
        self._args = args
        for key, value in args.iteritems():
            setattr(self, key, value)

    def __repr__(self):
        return repr(self._args)


class TestFastfoodCommands(unittest.TestCase):

    def setUp(self):

        self.templatepack_path = TEST_TEMPLATEPACK
        self.pack = pack.TemplatePack(self.templatepack_path)
        # make a separate ref in case something modifies the instance attr
        self.cookbooks_path = self._tmpcbd = tempfile.mkdtemp(
            prefix='%s.' % __file__, suffix='.cookbooks_dir')
        cli_args = {
            'template_pack': self.templatepack_path,
            'cookbooks': self.cookbooks_path,
            'cookbook': None,
            'cookbook_name': None,
            'force': None,
            'options': None,  # stencil options
            'config_file': None,  # for `fastfood build`
        }
        self.args = MockArgs(**cli_args)

    def tearDown(self):
        try:
            shutil.rmtree(self._tmpcbd)
        except OSError as exc:
            if exc.errno != errno.ENOENT:
                raise


class TestFastfoodNewCommand(TestFastfoodCommands):

    def test_fastfood_new(self):

        self.args.cookbook_name = ''.join(
            [random.choice(string.ascii_letters) for _ in xrange(10)])
        written, _ = shell._fastfood_new(self.args)
        # assert that files in pack manifest match the files
        # created by `fastfood new`
        base_path = '%s/' % os.path.join(
            self.cookbooks_path, self.args.cookbook_name)
        actual_files = [k[len(base_path):] for k in written
                        if k.startswith(base_path)]
        base_manifest_files = self.pack.manifest['base']['files']
        self.assertItemsEqual(actual_files, base_manifest_files)


if __name__ == '__main__':
    unittest.main()

@@ -43,21 +43,8 @@
 %22%22%0A%0A
-import errno%0A
 impo
@@ -75,43 +75,13 @@
 rt s
-hutil%0Aimport string%0Aimport tempfile
+tring
 %0Aimp
@@ -119,1448 +119,81 @@
 ort 
-pack%0Afrom fastfood import shell%0A%0ATEST_TEMPLATEPACK = os.path.abspath(%0A    os.path.join(os.path.dirname(os.path.realpath(__file__)),%0A                 os.path.pardir, 'test_templatepack'))%0Aassert os.path.isdir(TEST_TEMPLATEPACK), %22Test templatepack not found.%22%0A%0A%0Aclass MockArgs(object):%0A%0A    def __init__(self, **args):%0A        self._args = args%0A        for key, value in args.iteritems():%0A            setattr(self, key, value)%0A%0A    def __repr__(self):%0A        return repr(self._args)%0A%0A%0Aclass TestFastfoodCommands(unittest.TestCase):%0A%0A    def setUp(self):%0A%0A        self.templatepack_path = TEST_TEMPLATEPACK%0A        self.pack = pack.TemplatePack(self.templatepack_path)%0A        # make a separate ref in case something modifies the instance attr%0A        self.cookbooks_path = self._tmpcbd = tempfile.mkdtemp(%0A            prefix='%25s.' %25 __file__, suffix='.cookbooks_dir')%0A        cli_args = %7B%0A            'template_pack': self.templatepack_path,%0A            'cookbooks': self.cookbooks_path,%0A            'cookbook': None,%0A            'cookbook_name': None,%0A            'force': None,%0A            'options': None,  # stencil options%0A            'config_file': None,  # for %60fastfood build%60%0A        %7D%0A        self.args = MockArgs(**cli_args)%0A%0A    def tearDown(self):%0A        try:%0A            shutil.rmtree(self._tmpcbd)%0A        except OSError as exc:%0A            if exc.errno != errno.ENOENT:%0A                raise%0A%0A%0Aclass TestFastfoodNewCommand(
+shell%0A%0Aimport test_commands%0A%0A%0Aclass TestFastfoodNewCommand(test_commands.
 Test

use common functest module for `fastfood new`