#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_vcs
------------

Tests for `cookiecutter.vcs` module.
"""

import logging
import os
import shutil
import sys
import unittest

PY3 = sys.version > '3'
if PY3:
    from unittest.mock import patch
    input_str = 'builtins.input'
else:
    import __builtin__
    from mock import patch
    input_str = '__builtin__.raw_input'
    from cStringIO import StringIO

from cookiecutter import vcs


# Log debug and above to console
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)


class TestVCS(unittest.TestCase):

    def test_git_clone(self):
        repo_dir = vcs.git_clone(
            'https://github.com/audreyr/cookiecutter-pypackage.git'
        )
        self.assertEqual(repo_dir, 'cookiecutter-pypackage')
        self.assertTrue(os.path.isfile('cookiecutter-pypackage/README.rst'))
        if os.path.isdir('cookiecutter-pypackage'):
            shutil.rmtree('cookiecutter-pypackage')


class TestVCSPrompt(unittest.TestCase):

    def setUp(self):
        if os.path.isdir('cookiecutter-pypackage'):
            shutil.rmtree('cookiecutter-pypackage')
        os.mkdir('cookiecutter-pypackage/')

    @patch(input_str, lambda: 'y')
    def test_git_clone_overwrite(self):
        if not PY3:
            sys.stdin = StringIO('y\n\n')
        repo_dir = vcs.git_clone(
            'https://github.com/audreyr/cookiecutter-pypackage.git'
        )
        self.assertEqual(repo_dir, 'cookiecutter-pypackage')
        self.assertTrue(os.path.isfile('cookiecutter-pypackage/README.rst'))

    @patch(input_str, lambda: 'n')
    def test_git_clone_cancel(self):
        if not PY3:
            sys.stdin = StringIO('n\n\n')
        self.assertRaises(
            SystemExit,
            vcs.git_clone,
            'https://github.com/audreyr/cookiecutter-pypackage.git'
        )

    def tearDown(self):
        if os.path.isdir('cookiecutter-pypackage'):
            shutil.rmtree('cookiecutter-pypackage')


if __name__ == '__main__':
    unittest.main()

@@ -148,16 +148,34 @@
  shutil%0A
+import subprocess%0A
 import s
@@ -991,16 +991,575 @@
 kage')%0A%0A
+    def test_git_clone_checkout(self):%0A        repo_dir = vcs.git_clone(%0A            'https://github.com/aptivate/dye.git',%0A            'develop'%0A        )%0A        git_dir = 'dye'%0A        self.assertEqual(repo_dir, git_dir)%0A        self.assertTrue(os.path.isfile(os.path.join('dye', 'README.md')))%0A        symbolic_ref = subprocess.check_output(%5B'git', 'symbolic-ref', 'HEAD'%5D, cwd=git_dir)%0A        branch = symbolic_ref.strip().split('/')%5B-1%5D%0A        self.assertEqual('develop', branch)%0A        if os.path.isdir(git_dir):%0A            shutil.rmtree(git_dir)%0A%0A
 %0Aclass T

Add failing test for git clone with checkout