import os

import pytest

from hgtools import managers


def _ensure_present(mgr):
    try:
        mgr.version()
    except Exception:
        pytest.skip()


@pytest.fixture
def tmpdir_as_cwd(tmpdir):
    with tmpdir.as_cwd():
        yield tmpdir


@pytest.fixture
def hg_repo(tmpdir_as_cwd):
    mgr = managers.MercurialManager()
    _ensure_present(mgr)
    mgr._invoke('init', '.')
    os.makedirs('bar')
    touch('bar/baz')
    mgr._invoke('addremove')
    mgr._invoke('ci', '-m', 'committed')
    with open('bar/baz', 'w') as baz:
        baz.write('content')
    mgr._invoke('ci', '-m', 'added content')
    return tmpdir_as_cwd


@pytest.fixture
def git_repo(tmpdir_as_cwd):
    mgr = managers.GitManager()
    _ensure_present(mgr)
    mgr._invoke('init')
    mgr._invoke('config', 'user.email', 'hgtools@example.com')
    mgr._invoke('config', 'user.name', 'HGTools')
    os.makedirs('bar')
    touch('bar/baz')
    mgr._invoke('add', '.')
    mgr._invoke('commit', '-m', 'committed')
    with open('bar/baz', 'w') as baz:
        baz.write('content')
    mgr._invoke('commit', '-am', 'added content')
    return tmpdir_as_cwd


def touch(filename):
    with open(filename, 'a'):
        pass

[((23, 10, 23, 37), 'hgtools.managers.MercurialManager', 'managers.MercurialManager', ({}, {}), '()', False, 'from hgtools import managers\n'), ((26, 4, 26, 22), 'os.makedirs', 'os.makedirs', ({(26, 16, 26, 21): '"""bar"""'}, {}), "('bar')", False, 'import os\n'), ((38, 10, 38, 31), 'hgtools.managers.GitManager', 'managers.GitManager', ({}, {}), '()', False, 'from hgtools import managers\n'), ((43, 4, 43, 22), 'os.makedirs', 'os.makedirs', ({(43, 16, 43, 21): '"""bar"""'}, {}), "('bar')", False, 'import os\n'), ((12, 8, 12, 21), 'pytest.skip', 'pytest.skip', ({}, {}), '()', False, 'import pytest\n')]