import unittest
import os
import shutil
import stat

from mpm import MPMMetadata, mpm_init, mpm_purge, mpm_install, mpm_uninstall, mpm_update, mpm_load, mpm_freeze, mpm_convert, mpm_show
from mpm_helpers import clone_and_checkout_helper, clone_helper, checkout_helper, yaml_to_path_helper, path_to_yaml_helper, onerror_helper, remove_from_gitignore_helper, add_to_gitignore_helper, is_local_commit_helper, with_open_or_create_tinydb_helper, with_open_or_create_file_helper, create_directory_helper
from mpm_yaml_storage import YAMLStorage
from tinydb import TinyDB, Query
from git import Repo, GitCommandError

class HelperObject(object):
    pass

class TestHelpers(unittest.TestCase):
    def test_is_local_commit_helper_not_local(self):
        path = os.path.join('test', 'broker')
        url = 'https://github.com/msembinelli/broker.git'
        ref = '2dc33423188a7e06fa6e9725a0a74059b009ff6a'
        clone_helper(url, path)
        repo = Repo(path)
        self.assertFalse(is_local_commit_helper(repo, '2dc3342318'))
        repo.close()
        shutil.rmtree(path, onerror=onerror_helper)
        os.rmdir('test')

    def test_is_local_commit_helper_is_local(self):
        path = os.path.join('test', 'broker')
        url = 'https://github.com/msembinelli/broker.git'
        ref = '2dc33423188a7e06fa6e9725a0a74059b009ff6a'
        clone_helper(url, path)
        repo = Repo(path)
        branch = repo.create_head('test_branch', '2dc33423188a7e06fa6e9725a0a74059b009ff6a')
        self.assertTrue(is_local_commit_helper(repo, 'test_branch'))
        repo.git.checkout('test_branch')
        new_path = os.path.join(path, 'test_module')
        os.mkdir(new_path)