def _get_recipe_file_contents(self, runner, kitchen, recipe_name, recipe_file_key, file_name, temp_dir=None):
     delete_temp_dir = False
     if temp_dir is None:
         td = tempfile.mkdtemp(prefix='unit-tests-grfc', dir=TestCommandLine._TEMPFILE_LOCATION)
         delete_temp_dir = True
         DKKitchenDisk.write_kitchen(kitchen, td)
         kitchen_directory = os.path.join(td, kitchen)
     else:
         td = temp_dir
         kitchen_directory = os.getcwd()
     cwd = os.getcwd()
     os.chdir(kitchen_directory)
     result = runner.invoke(dk, ['recipe-get', recipe_name])
     os.chdir(cwd)
     rv = result.output
     self.assertTrue(recipe_name in rv)
     abspath = os.path.join(td, os.path.join(kitchen, recipe_file_key, file_name))
     if os.path.isfile(abspath):
         with open(abspath, 'r') as rfile:
             rfile.seek(0)
             the_file = rfile.read()
         rc = the_file
     else:
         rc = None
     if delete_temp_dir is True:
         shutil.rmtree(td, ignore_errors=True)
     return rc
 def _get_recipe_file_contents(self,
                               runner,
                               kitchen,
                               recipe_name,
                               recipe_file_key,
                               file_name,
                               temp_dir=None):
     delete_temp_dir = False
     if temp_dir is None:
         td = tempfile.mkdtemp(prefix='unit-tests-grfc',
                               dir=TestCommandLine._TEMPFILE_LOCATION)
         delete_temp_dir = True
         DKKitchenDisk.write_kitchen(kitchen, td)
         kitchen_directory = os.path.join(td, kitchen)
     else:
         td = temp_dir
         kitchen_directory = os.getcwd()
     cwd = os.getcwd()
     os.chdir(kitchen_directory)
     result = runner.invoke(dk, ['recipe-get', recipe_name])
     os.chdir(cwd)
     rv = result.output
     self.assertTrue(recipe_name in rv)
     abspath = os.path.join(
         td, os.path.join(kitchen, recipe_file_key, file_name))
     if os.path.isfile(abspath):
         with open(abspath, 'r') as rfile:
             rfile.seek(0)
             the_file = rfile.read()
         rc = the_file
     else:
         rc = None
     if delete_temp_dir is True:
         shutil.rmtree(td, ignore_errors=True)
     return rc
    def test_update_file(self):
        # setup
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'CLI-test_update_file'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        recipe_file_key = recipe_name
        file_name = 'description.json'
        message = 'test update CLI-test_update_file'
        api_file_key = file_name
        update_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        runner = CliRunner()  # for the CLI level
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        temp_dir = tempfile.mkdtemp(prefix='unit-tests',
                                    dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(parent_kitchen, temp_dir)
        parent_kitchen_dir = os.path.join(temp_dir, parent_kitchen)
        os.chdir(parent_kitchen_dir)
        original_file = self._get_recipe_file_contents(runner, parent_kitchen,
                                                       recipe_name,
                                                       recipe_file_key,
                                                       file_name)
        result = runner.invoke(
            dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        test_kitchen_dir = os.path.join(temp_dir, test_kitchen)
        os.chdir(test_kitchen_dir)
        new_kitchen_file = self._get_recipe_file_contents(
            runner, test_kitchen, recipe_name, recipe_file_key, file_name,
            temp_dir)
        self.assertEqual(original_file, new_kitchen_file)
        new_kitchen_file_dict = self._get_the_dict(new_kitchen_file)
        new_kitchen_file_abspath = os.path.join(
            test_kitchen_dir, os.path.join(recipe_file_key, file_name))
        new_kitchen_file_dict[test_kitchen] = update_str
        new_kitchen_file2 = self._get_the_json_str(new_kitchen_file_dict)
        with open(new_kitchen_file_abspath, 'w') as rfile:
            rfile.seek(0)
            rfile.truncate()
            rfile.write(new_kitchen_file2)
        # test
        orig_dir = os.getcwd()
        working_dir = os.path.join(test_kitchen_dir, recipe_name)
        os.chdir(working_dir)
        result = runner.invoke(dk, [
            'file-update', '--recipe', recipe_name, '--message', message,
            api_file_key
        ])
        os.chdir(orig_dir)
        self.assertTrue('ERROR' not in result.output)
        new_kitchen_file3 = self._get_recipe_file_contents(
            runner, test_kitchen, recipe_name, recipe_file_key, file_name)
        self.assertEqual(new_kitchen_file2, new_kitchen_file3)

        # cleanup
        runner.invoke(dk, ['kitchen-delete', '--kitchen', test_kitchen])
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_delete_file_top(self):
        # setup
        temp_dir = None
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'CLI-test_delete_file_top'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        recipe_file_key = recipe_name
        file_name = 'description.json'
        message = ' test Delete CLI-test_delete_file_top'
        runner = CliRunner()
        cwd = os.getcwd()
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        try:
            temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=TestCommandLine._TEMPFILE_LOCATION)
        except Exception as e:
            self.assertTrue(False, 'Problem creating temp folder %s' % e)
        os.chdir(temp_dir)
        result = runner.invoke(dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kitchen_dir = os.path.join(temp_dir, test_kitchen)
        os.chdir(kitchen_dir)
        result = runner.invoke(dk, ['file-delete',
                                    '--recipe', recipe_name,
                                    '--message', message,
                                    file_name
                                    ])
        self.assertTrue('ERROR' not in result.output)
        self.assertTrue(self._get_recipe_file_contents(runner, test_kitchen, recipe_name,
                                                       recipe_file_key, file_name, temp_dir) is None, "Found the file")
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        os.chdir(cwd)
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_create_file(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'test_create_file-Runner'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        file_name = 'added.sql'
        filedir = 'resources'
        recipe_file_key = os.path.join(recipe_name, filedir)
        api_file_key = os.path.join(filedir, file_name)
        file_contents = '--\n-- sql for you\n--\n\nselect 1024\n\n'
        message = 'test update test_create_file-API'
        runner = CliRunner()

        # create test kitchen
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        result = runner.invoke(
            dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir = tempfile.mkdtemp(prefix='unit-test_create_file',
                                    dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kd = os.path.join(temp_dir, test_kitchen)
        orig_dir = os.getcwd()
        os.chdir(kd)
        self._get_recipe(runner, recipe_name)

        # create new file on disk
        try:
            os.chdir(recipe_name)
            f = open(api_file_key, 'w')
            f.write(file_contents)
            f.close()
        except ValueError as e:
            print(('could not write file %s.' % e))
            self.assertTrue(False)

        # add file from disk THE TEST
        result = runner.invoke(dk, [
            'file-add', '--recipe', recipe_name, '--message', message,
            api_file_key
        ])
        self.assertTrue('ERROR' not in result.output)

        # make sure file is in kitchen (get file)
        file_contents2 = self._get_recipe_file_contents(
            runner, test_kitchen, recipe_name, recipe_file_key, file_name)
        self.assertEqual(file_contents, file_contents2, 'Create check')

        # cleanup
        os.chdir(orig_dir)
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_update_file(self):
        # setup
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'CLI-test_update_file'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        recipe_file_key = recipe_name
        file_name = 'description.json'
        message = 'test update CLI-test_update_file'
        api_file_key = file_name
        update_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        runner = CliRunner()  # for the CLI level
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(parent_kitchen, temp_dir)
        parent_kitchen_dir = os.path.join(temp_dir, parent_kitchen)
        os.chdir(parent_kitchen_dir)
        original_file = self._get_recipe_file_contents(runner, parent_kitchen, recipe_name, recipe_file_key, file_name)
        result = runner.invoke(dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        test_kitchen_dir = os.path.join(temp_dir, test_kitchen)
        os.chdir(test_kitchen_dir)
        new_kitchen_file = self._get_recipe_file_contents(runner, test_kitchen, recipe_name,
                                                          recipe_file_key, file_name, temp_dir)
        self.assertEqual(original_file, new_kitchen_file)
        new_kitchen_file_dict = self._get_the_dict(new_kitchen_file)
        new_kitchen_file_abspath = os.path.join(test_kitchen_dir, os.path.join(recipe_file_key, file_name))
        new_kitchen_file_dict[test_kitchen] = update_str
        new_kitchen_file2 = self._get_the_json_str(new_kitchen_file_dict)
        with open(new_kitchen_file_abspath, 'w') as rfile:
            rfile.seek(0)
            rfile.truncate()
            rfile.write(new_kitchen_file2)
        # test
        orig_dir = os.getcwd()
        working_dir = os.path.join(test_kitchen_dir, recipe_name)
        os.chdir(working_dir)
        result = runner.invoke(dk, ['file-update',
                                    '--recipe', recipe_name,
                                    '--message', message,
                                    api_file_key])
        os.chdir(orig_dir)
        self.assertTrue('ERROR' not in result.output)
        new_kitchen_file3 = self._get_recipe_file_contents(runner, test_kitchen, recipe_name,
                                                           recipe_file_key, file_name)
        self.assertEqual(new_kitchen_file2, new_kitchen_file3)

        # cleanup
        runner.invoke(dk, ['kitchen-delete', '--kitchen', test_kitchen])
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_kitchen_which(self):

        kn = 'bobo'
        temp_dir = tempfile.mkdtemp(prefix='unit-tests', dir=TestCommandLine._TEMPFILE_LOCATION)
        os.chdir(temp_dir)
        DKKitchenDisk.write_kitchen(kn, temp_dir)
        os.chdir(os.path.join(temp_dir, kn))

        runner = CliRunner()
        result = runner.invoke(dk, ['kitchen-which'])
        self.assertTrue(0 == result.exit_code)
        self.assertIn('bobo', result.output)
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_kitchen_which(self):

        kn = 'bobo'
        temp_dir = tempfile.mkdtemp(prefix='unit-tests',
                                    dir=TestCommandLine._TEMPFILE_LOCATION)
        os.chdir(temp_dir)
        DKKitchenDisk.write_kitchen(kn, temp_dir)
        os.chdir(os.path.join(temp_dir, kn))

        runner = CliRunner()
        result = runner.invoke(dk, ['kitchen-which'])
        self.assertTrue(0 == result.exit_code)
        self.assertIn('bobo', result.output)
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_delete_file_deeper_multi(self):
        # setup
        temp_dir = None
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'CLI-test_delete_file_deeper_multi'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        recipe_file_key = 'resources/very_cool.sql'
        file_name = 'very_cool.sql'
        file2 = 'description.json'
        message = ' test Delete CLI-test_delete_file_deeper_multi'
        runner = CliRunner()
        cwd = os.getcwd()
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        try:
            temp_dir = tempfile.mkdtemp(prefix='unit-tests',
                                        dir=TestCommandLine._TEMPFILE_LOCATION)
        except Exception as e:
            self.assertTrue(False, 'Problem creating temp folder %s' % e)
        os.chdir(temp_dir)
        result = runner.invoke(
            dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kitchen_dir = os.path.join(temp_dir, test_kitchen)
        os.chdir(kitchen_dir)

        result = runner.invoke(dk, [
            'file-delete', '--recipe', recipe_name, '--message', message,
            recipe_file_key, file2
        ])
        self.assertTrue('ERROR' not in result.output)
        self.assertTrue(
            self._get_recipe_file_contents(
                runner, test_kitchen, recipe_name,
                os.path.join(recipe_name, recipe_file_key), file_name,
                temp_dir) is None)
        runner.invoke(dk, ['kitchen-delete', '--kitchen', test_kitchen])
        os.chdir(cwd)
        shutil.rmtree(temp_dir, ignore_errors=True)
    def test_create_file(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'test_create_file-Runner'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        file_name = 'added.sql'
        filedir = 'resources'
        recipe_file_key = os.path.join(recipe_name, filedir)
        api_file_key = os.path.join(filedir, file_name)
        file_contents = '--\n-- sql for you\n--\n\nselect 1024\n\n'
        message = 'test update test_create_file-API'
        runner = CliRunner()

        # create test kitchen
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        result = runner.invoke(
            dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir = tempfile.mkdtemp(prefix='unit-test_create_file',
                                    dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kd = os.path.join(temp_dir, test_kitchen)
        orig_dir = os.getcwd()
        os.chdir(kd)
        self._get_recipe(runner, recipe_name)

        # create new file on disk
        try:
            os.chdir(recipe_name)
            f = open(api_file_key, 'w')
            f.write(file_contents)
            f.close()
        except ValueError, e:
            print('could not write file %s.' % e)
            self.assertTrue(False)
    def test_create_file(self):
        parent_kitchen = 'CLI-Top'
        test_kitchen = 'test_create_file-Runner'
        test_kitchen = self._add_my_guid(test_kitchen)
        recipe_name = 'simple'
        file_name = 'added.sql'
        filedir = 'resources'
        recipe_file_key = os.path.join(recipe_name, filedir)
        api_file_key = os.path.join(filedir, file_name)
        file_contents = '--\n-- sql for you\n--\n\nselect 1024\n\n'
        message = 'test update test_create_file-API'
        runner = CliRunner()

        # create test kitchen
        runner.invoke(dk, ['kitchen-delete', test_kitchen])
        result = runner.invoke(dk, ['kitchen-create', '--parent', parent_kitchen, test_kitchen])
        self.assertTrue(0 == result.exit_code)

        # make and cd to kitchen dir and get the recipe to disk
        temp_dir = tempfile.mkdtemp(prefix='unit-test_create_file', dir=TestCommandLine._TEMPFILE_LOCATION)

        DKKitchenDisk.write_kitchen(test_kitchen, temp_dir)
        kd = os.path.join(temp_dir, test_kitchen)
        orig_dir = os.getcwd()
        os.chdir(kd)
        self._get_recipe(runner, recipe_name)

        # create new file on disk
        try:
            os.chdir(recipe_name)
            f = open(api_file_key, 'w')
            f.write(file_contents)
            f.close()
        except ValueError, e:
            print('could not write file %s.' % e)
            self.assertTrue(False)