import unittest
from tempfile import mkdtemp
from os.path import join
from shutil import rmtree
from json import dumps, loads
import copy

from .settings import SPEC_DATA_DIR

from slyd.gitstorage.repoman import Repoman


def j(json):
    return dumps(json, sort_keys=True, indent=4)


class RepomanTest(unittest.TestCase):

    def setUp(self):
        self.temp_repos_dir = mkdtemp(dir=SPEC_DATA_DIR,
                                      prefix='test-run-')
        Repoman.setup(
            storage_backend='dulwich.fsrepo.FsRepo',
            location=self.temp_repos_dir
        )

    def tearDown(self):
        rmtree(self.temp_repos_dir)

    def get_full_name(self, repo_name):
        return join(self.temp_repos_dir, repo_name)

    def test_create(self):
        Repoman.create_repo(self.get_full_name('my_repo'))
        self.assertTrue(Repoman.repo_exists(self.get_full_name('my_repo')))

    def test_save_file(self):
        repoman = Repoman.create_repo(self.get_full_name('my_repo'))