# -*- coding: utf-8 -*-
import os
from tempfile import mkstemp, mkdtemp

import pytest
from dulwich import porcelain

from bamp.exc import VCSException
from bamp.vcs.git import is_tree_clean, get_repo, create_commit


class RepoStatusMock(object):
    def __init__(self, staged, unstaged):
        self.unstaged = unstaged
        self.staged = staged


def status_mockreturn(mock_status):
    return RepoStatusMock(**mock_status)


def test_is_tree_clean_no_staged_no_unstaged(monkeypatch):
    monkeypatch.setattr(porcelain, 'status', status_mockreturn)
    assert is_tree_clean({'staged': {}, 'unstaged': []})


def test_is_tree_clean_no_staged_with_unstaged(monkeypatch):
    monkeypatch.setattr(porcelain, 'status', status_mockreturn)
    assert not is_tree_clean({'staged': {}, 'unstaged': ['file']})


def test_is_tree_clean_with_staged_no_unstaged(monkeypatch):
    monkeypatch.setattr(porcelain, 'status', status_mockreturn)
    assert not is_tree_clean({'staged': {'file': 'path'}, 'unstaged': []})


def test_is_tree_clean_with_staged_with_unstaged(monkeypatch):
    monkeypatch.setattr(porcelain, 'status', status_mockreturn)
    assert not is_tree_clean({'staged': {'file': 'path'},
                              'unstaged': ['moar files']})


def test_get_repo_raise_exception():
    _, path = mkstemp()
    with pytest.raises(VCSException):
        get_repo(path)

    # cleanup
    os.remove(path)


def test_create_commit_with_unicode_message():
    path = mkdtemp()
    repo = porcelain.init(path)
    commit_sha1 = create_commit(repo, '', u'bździągwą')
    assert commit_sha1


def test_create_commit_with_message():
    path = mkdtemp()
    repo = porcelain.init(path)
    commit_sha1 = create_commit(repo, '', 'a')
    assert commit_sha1

@@ -1413,51 +1413,36 @@
 )%0A%0A%0A
-def test_create_commit_with_unicode_message
+@pytest.fixture%0Adef git_repo
 ():%0A
@@ -1486,32 +1486,178 @@
 lain.init(path)%0A
+    config = repo.get_config()%0A    config.set('user', 'name', 'Mr. Git')%0A    return repo%0A%0A%0Adef test_create_commit_with_unicode_message(git_repo):%0A
     commit_sha1 
@@ -1664,32 +1664,36 @@
 = create_commit(
+git_
 repo, '', u'b%C5%BAdz
@@ -1765,63 +1765,18 @@
 age(
-):%0A    path = mkdtemp()%0A    repo = porcelain.init(path)
+git_repo):
 %0A   
@@ -1804,16 +1804,20 @@
 _commit(
+git_
 repo, ''

Move repo init to fixture function