from __future__ import absolute_import

import os
import pytest

from datetime import datetime
from subprocess import check_call

from changes.testutils import TestCase
from changes.vcs.hg import MercurialVcs


def has_current_hg_version():
    import pkg_resources

    try:
        mercurial = pkg_resources.get_distribution('mercurial')
    except pkg_resources.DistributionNotFound:
        return False

    return mercurial.parsed_version >= pkg_resources.parse_version('2.4')


@pytest.mark.skipif(not has_current_hg_version(),
                    reason='missing or invalid mercurial version')
class MercurialVcsTest(TestCase):
    root = '/tmp/changes-hg-test'
    path = '%s/clone' % (root,)
    remote_path = '%s/remote' % (root,)
    url = 'file://%s' % (remote_path,)

    def setUp(self):
        self.reset()
        self.addCleanup(check_call, 'rm -rf %s' % (self.root,), shell=True)

    def reset(self):
        check_call('rm -rf %s' % (self.root,), shell=True)
        check_call('mkdir -p %s %s' % (self.path, self.remote_path), shell=True)
        check_call('hg init %s' % (self.remote_path,), shell=True)
        with open(os.path.join(self.remote_path, '.hg/hgrc'), 'w') as fp:
            fp.write('[ui]\n')
            fp.write('username=Foo Bar <<EMAIL>>\n')
        check_call('cd %s && touch FOO && hg add FOO && hg commit -m "test\nlol"' % (
            self.remote_path,
        ), shell=True)
        check_call('cd %s && touch BAR && hg add BAR && hg commit -m "biz\nbaz"' % (
            self.remote_path,
        ), shell=True)

    def get_vcs(self):
        return MercurialVcs(
            url=self.url,
            path=self.path
        )

    def test_get_default_revision(self):
        vcs = self.get_vcs()
        assert vcs.get_default_revision() == 'default'

    def test_simple(self):
        vcs = self.get_vcs()
        vcs.clone()
        vcs.update()
        revision = vcs.get_revision('tip')
        assert len(revision.id) == 40
        assert revision.message == 'biz\nbaz'
        assert revision.subject == 'biz'
        assert revision.author == '<NAME> <<EMAIL>>'
        revisions = list(vcs.log())
        assert len(revisions) == 2
        assert revisions[0].subject == 'biz'
        assert revisions[0].message == 'biz\nbaz'
        assert revisions[0].author == '<NAME> <<EMAIL>>'
        assert revisions[0].committer == 'Foo Bar <<EMAIL>>'
        assert revisions[0].parents == [revisions[1].id]
        assert type(revisions[0].author_date) is datetime
        assert revisions[0].author_date == revisions[0].committer_date is not None
        assert revisions[0].branches == ['default']
        assert revisions[1].subject == 'test'
        assert revisions[1].message == 'test\nlol'
        assert revisions[1].author == '<NAME> <<EMAIL>>'
        assert revisions[1].committer == '<NAME> <<EMAIL>>'
        assert revisions[1].parents == []
        assert revisions[1].author_date == revisions[1].committer_date is not None
        assert revisions[1].branches == ['default']
        diff = vcs.export(revisions[0].id)
        assert diff == """diff --git a/BAR b/BAR
new file mode 100644
"""
        revisions = list(vcs.log(offset=0, limit=1))
        assert len(revisions) == 1
        assert revisions[0].subject == 'biz'

        revisions = list(vcs.log(offset=1, limit=1))
        assert len(revisions) == 1
        assert revisions[0].subject == 'test'

    def test_is_child_parent(self):
        vcs = self.get_vcs()
        vcs.clone()
        vcs.update()
        revisions = list(vcs.log())
        assert vcs.is_child_parent(child_in_question=revisions[0].id,
                                   parent_in_question=revisions[1].id)
        assert vcs.is_child_parent(child_in_question=revisions[1].id,
                                   parent_in_question=revisions[0].id) is False

from __future__ import absolute_import

import os
import pytest

from datetime import datetime
from subprocess import check_call

from changes.testutils import TestCase
from changes.vcs.hg import MercurialVcs


def has_current_hg_version():
    import pkg_resources

    try:
        mercurial = pkg_resources.get_distribution('mercurial')
    except pkg_resources.DistributionNotFound:
        return False

    return mercurial.parsed_version >= pkg_resources.parse_version('2.4')


@pytest.mark.skipif(not has_current_hg_version(),
                    reason='missing or invalid mercurial version')
class MercurialVcsTest(TestCase):
    root = '/tmp/changes-hg-test'
    path = '%s/clone' % (root,)
    remote_path = '%s/remote' % (root,)
    url = 'file://%s' % (remote_path,)

    def setUp(self):
        self.reset()
        self.addCleanup(check_call, 'rm -rf %s' % (self.root,), shell=True)

    def reset(self):
        check_call('rm -rf %s' % (self.root,), shell=True)
        check_call('mkdir -p %s %s' % (self.path, self.remote_path), shell=True)
        check_call('hg init %s' % (self.remote_path,), shell=True)
        with open(os.path.join(self.remote_path, '.hg/hgrc'), 'w') as fp:
            fp.write('[ui]\n')
            fp.write('username=Foo Bar <<EMAIL>>\n')
        check_call('cd %s && touch FOO && hg add FOO && hg commit -m "test\nlol"' % (
            self.remote_path,
        ), shell=True)
        check_call('cd %s && touch BAR && hg add BAR && hg commit -m "biz\nbaz"' % (
            self.remote_path,
        ), shell=True)

    def get_vcs(self):
        return MercurialVcs(
            url=self.url,
            path=self.path
        )

    def test_get_default_revision(self):
        vcs = self.get_vcs()
        assert vcs.get_default_revision() == 'default'

    def test_simple(self):
        vcs = self.get_vcs()
        vcs.clone()
        vcs.update()
        revision = vcs.get_revision('tip')
        assert len(revision.id) == 40
        assert revision.message == 'biz\nbaz'
        assert revision.subject == 'biz'
        assert revision.author == '<NAME> <<EMAIL>>'
        revisions = list(vcs.log())
        assert len(revisions) == 2
        assert revisions[0].subject == 'biz'
        assert revisions[0].message == 'biz\nbaz'
        assert revisions[0].author == '<NAME> <<EMAIL>>'
        assert revisions[0].committer == 'Foo Bar <<EMAIL>>'
        assert revisions[0].parents == [revisions[1].id]
        assert type(revisions[0].author_date) is datetime
        assert revisions[0].author_date == revisions[0].committer_date is not None
        assert revisions[0].branches == ['default']
        assert revisions[1].subject == 'test'
        assert revisions[1].message == 'test\nlol'
        assert revisions[1].author == '<NAME> <<EMAIL>>'
        assert revisions[1].committer == '<NAME> <<EMAIL>>'
        assert revisions[1].parents == []
        assert revisions[1].author_date == revisions[1].committer_date is not None
        assert revisions[1].branches == ['default']
        diff = vcs.export(revisions[0].id)
        assert diff == """diff --git a/BAR b/BAR
new file mode 100644
"""
        revisions = list(vcs.log(offset=0, limit=1))
        assert len(revisions) == 1
        assert revisions[0].subject == 'biz'

        revisions = list(vcs.log(offset=1, limit=1))
        assert len(revisions) == 1
        assert revisions[0].subject == 'test'

    def test_is_child_parent(self):
        vcs = self.get_vcs()
        vcs.clone()
        vcs.update()
        revisions = list(vcs.log())
        assert vcs.is_child_parent(child_in_question=revisions[0].id,
                                   parent_in_question=revisions[1].id)
        assert vcs.is_child_parent(child_in_question=revisions[1].id,
                                   parent_in_question=revisions[0].id) is False

diff --git a/BAR b/BAR new file mode 100644