def test_clone_should_abort_if_user_does_not_want_to_reclone(mocker, tmpdir): """In `clone()`, if user doesn't want to reclone, Cookiecutter should exit without cloning anything. """ mocker.patch( 'cookiecutter.vcs.is_vcs_installed', autospec=True, return_value=True ) mocker.patch( 'cookiecutter.vcs.prompt_and_delete_repo', side_effect=SystemExit, autospec=True ) mock_subprocess = mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, ) clone_to_dir = tmpdir.mkdir('clone') # Create repo_dir to trigger prompt_and_delete_repo clone_to_dir.mkdir('cookiecutter-pytest-plugin') repo_url = 'https://github.com/pytest-dev/cookiecutter-pytest-plugin.git' with pytest.raises(SystemExit): vcs.clone(repo_url, clone_to_dir=str(clone_to_dir)) assert not mock_subprocess.called
def test_clone_should_abort_if_user_does_not_want_to_reclone(mocker, tmpdir): """In `clone()`, if user doesn't want to reclone, Cookiecutter should exit without cloning anything. """ mocker.patch('cookiecutter.vcs.is_vcs_installed', autospec=True, return_value=True) mocker.patch('cookiecutter.vcs.prompt_and_delete', side_effect=SystemExit, autospec=True) mock_subprocess = mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, ) clone_to_dir = tmpdir.mkdir('clone') # Create repo_dir to trigger prompt_and_delete clone_to_dir.mkdir('cookiecutter-pytest-plugin') repo_url = 'https://github.com/pytest-dev/cookiecutter-pytest-plugin.git' with pytest.raises(SystemExit): vcs.clone(repo_url, clone_to_dir=str(clone_to_dir)) assert not mock_subprocess.called
def test_clone_handles_repo_typo(mocker, clone_dir, error_message): """In `clone()`, repository not found errors should raise an appropriate exception. """ # side_effect is set to an iterable here (and below), # because of a Python 3.4 unittest.mock regression # http://bugs.python.org/issue23661 mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, side_effect=[subprocess.CalledProcessError( -1, 'cmd', output=error_message )] ) repository_url = 'https://github.com/hackebro/cookiedozer' with pytest.raises(exceptions.RepositoryNotFound) as err: vcs.clone( repository_url, clone_to_dir=clone_dir, no_input=True ) assert str(err.value) == ( 'The repository {} could not be found, have you made a typo?' ).format(repository_url)
def test_clone_handles_branch_typo(mocker, clone_dir, error_message): """In `clone()`, branch not found errors should raise an appropriate exception. """ mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, side_effect=[subprocess.CalledProcessError( -1, 'cmd', output=error_message )] ) repository_url = 'https://github.com/pytest-dev/cookiecutter-pytest-plugin' with pytest.raises(exceptions.RepositoryCloneFailed) as err: vcs.clone( repository_url, clone_to_dir=clone_dir, checkout='unknown_branch', no_input=True ) assert str(err.value) == ( 'The unknown_branch branch of repository ' '{} could not found, have you made a typo?' ).format(repository_url)
def test_vcs_not_installed(monkeypatch): monkeypatch.setattr( 'cookiecutter.vcs.identify_repo', lambda x: (u'stringthatisntashellcommand', u'anotherstring'), ) with pytest.raises(exceptions.VCSNotInstalled): vcs.clone('http://norepotypespecified.com')
def copy_eqpy_code(eqpy_location): clone('https://github.com/emews/EQ-Py.git', clone_to_dir=emews_wd, no_input=True) src = os.path.join(emews_wd, 'EQ-Py/src') util.copy_files(src, eqpy_location, ['eqpy.py', 'EQPy.swift']) shutil.rmtree(os.path.join(emews_wd, 'EQ-Py'), ignore_errors=True)
def test_clone_should_rstrip_trailing_slash_in_repo_url(mocker, clone_dir): """In `clone()`, repo URL's trailing slash should be stripped if one is present. """ mocker.patch( 'cookiecutter.vcs.is_vcs_installed', autospec=True, return_value=True ) mock_subprocess = mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, ) vcs.clone( 'https://github.com/foo/bar/', clone_to_dir=clone_dir, no_input=True ) mock_subprocess.assert_called_once_with( ['git', 'clone', 'https://github.com/foo/bar'], cwd=clone_dir, stderr=subprocess.STDOUT )
def test_hg_clone_cancel(monkeypatch): monkeypatch.setattr( 'cookiecutter.prompt.read_response', lambda x=u'': u'n' ) with pytest.raises(SystemExit): vcs.clone('https://bitbucket.org/pokoli/cookiecutter-trytonmodule')
def test_git_clone_cancel(monkeypatch): monkeypatch.setattr( 'cookiecutter.prompt.read_response', lambda x=u'': u'n' ) with pytest.raises(SystemExit): vcs.clone('https://github.com/audreyr/cookiecutter-pypackage.git')
def test_clone_should_raise_if_vcs_not_installed(mocker, clone_dir): """In `clone()`, a `VCSNotInstalled` exception should be raised if no VCS \ is installed.""" mocker.patch("cookiecutter.vcs.is_vcs_installed", autospec=True, return_value=False) repo_url = "https://github.com/pytest-dev/cookiecutter-pytest-plugin.git" with pytest.raises(exceptions.VCSNotInstalled): vcs.clone(repo_url, clone_to_dir=clone_dir)
def test_clone_unknown_subprocess_error(mocker, clone_dir): """In `clone()`, unknown subprocess errors should be raised.""" mocker.patch( "cookiecutter.vcs.subprocess.check_output", autospec=True, side_effect=[subprocess.CalledProcessError(-1, "cmd", output="Something went wrong")], ) with pytest.raises(subprocess.CalledProcessError): vcs.clone("https://github.com/pytest-dev/cookiecutter-pytest-plugin", clone_to_dir=clone_dir, no_input=True)
def test_clone_should_raise_if_vcs_not_installed(mocker, clone_dir): """In `clone()`, a `VCSNotInstalled` exception should be raised if no VCS is installed. """ mocker.patch("cookiecutter.vcs.is_vcs_installed", autospec=True, return_value=False) repo_url = "https://github.com/pytest-dev/cookiecutter-pytest-plugin.git" with pytest.raises(exceptions.VCSNotInstalled): vcs.clone(repo_url, clone_to_dir=clone_dir)
def copy_eqr_code(eqr_location): clone('https://github.com/emews/EQ-R.git', clone_to_dir=emews_wd, no_input=True) src = os.path.join(emews_wd, 'EQ-R/src') util.copy_files(src, eqr_location, [ 'EQR.swift', 'BlockingQueue.h', 'EQR.cpp', 'EQR.h', 'EQR.i', 'Makefile.in', 'bootstrap', 'configure.ac', 'find-tcl.sh', 'make-package.tcl', 'settings.mk.in' ]) shutil.rmtree(os.path.join(emews_wd, 'EQ-R'), ignore_errors=True)
def test_clone_unknown_subprocess_error(mocker, clone_dir): """In `clone()`, unknown subprocess errors should be raised.""" mocker.patch('cookiecutter.vcs.subprocess.check_output', autospec=True, side_effect=[ subprocess.CalledProcessError( -1, 'cmd', output='Something went wrong') ]) with pytest.raises(subprocess.CalledProcessError): vcs.clone('https://github.com/pytest-dev/cookiecutter-pytest-plugin', clone_to_dir=clone_dir, no_input=True)
def test_hg_clone_overwrite(monkeypatch): monkeypatch.setattr('cookiecutter.vcs.read_user_yes_no', lambda question, default: True) repo_dir = vcs.clone( 'https://bitbucket.org/pokoli/cookiecutter-trytonmodule') assert repo_dir == 'cookiecutter-trytonmodule' assert os.path.isfile('cookiecutter-trytonmodule/README.rst')
def test_git_clone_overwrite_with_no_prompt(self): repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git', no_input=True ) self.assertEqual(repo_dir, 'cookiecutter-pypackage') self.assertTrue(os.path.isfile('cookiecutter-pypackage/README.rst'))
def test_git_clone_overwrite(monkeypatch): monkeypatch.setattr('cookiecutter.prompt.read_response', lambda x=u'': u'y') repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git') assert repo_dir == 'cookiecutter-pypackage' assert os.path.isfile('cookiecutter-pypackage/README.rst')
def test_hg_clone_overwrite(self): if not PY3: sys.stdin = StringIO('y\n\n') repo_dir = vcs.clone( 'https://bitbucket.org/pokoli/cookiecutter-trytonmodule') self.assertEqual(repo_dir, 'cookiecutter-trytonmodule') self.assertTrue(os.path.isfile('cookiecutter-trytonmodule/README.rst'))
def test_git_clone_overwrite(self): if not PY3: sys.stdin = StringIO('y\n\n') repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git') self.assertEqual(repo_dir, 'cookiecutter-pypackage') self.assertTrue(os.path.isfile('cookiecutter-pypackage/README.rst'))
def test_hg_clone_overwrite(monkeypatch): monkeypatch.setattr('cookiecutter.prompt.read_response', lambda x=u'': u'y') repo_dir = vcs.clone( 'https://bitbucket.org/pokoli/cookiecutter-trytonmodule') assert repo_dir == 'cookiecutter-trytonmodule' assert os.path.isfile('cookiecutter-trytonmodule/README.rst')
def test_git_clone_overwrite_with_no_prompt(): repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git', no_input=True ) assert repo_dir == 'cookiecutter-pypackage' assert os.path.isfile('cookiecutter-pypackage/README.rst')
def test_clone_should_invoke_vcs_command(mocker, clone_dir, repo_type, repo_url, repo_name): """When `clone()` is called with a git/hg repo, the corresponding VCS command should be run via `subprocess.check_output()`. This should take place: * In the correct dir * With the correct args. """ mocker.patch('cookiecutter.vcs.is_vcs_installed', autospec=True, return_value=True) mock_subprocess = mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, ) expected_repo_dir = os.path.normpath(os.path.join(clone_dir, repo_name)) branch = 'foobar' repo_dir = vcs.clone(repo_url, checkout=branch, clone_to_dir=clone_dir, no_input=True) assert repo_dir == expected_repo_dir mock_subprocess.assert_any_call([repo_type, 'clone', repo_url], cwd=clone_dir, stderr=subprocess.STDOUT) mock_subprocess.assert_any_call([repo_type, 'checkout', branch], cwd=expected_repo_dir, stderr=subprocess.STDOUT)
def test_git_clone(self): repo_dir = vcs.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'): utils.rmtree('cookiecutter-pypackage')
def test_hg_clone(): repo_dir = vcs.clone( 'https://bitbucket.org/pokoli/cookiecutter-trytonmodule') assert repo_dir == 'cookiecutter-trytonmodule' assert os.path.isfile('cookiecutter-trytonmodule/README.rst') if os.path.isdir('cookiecutter-trytonmodule'): utils.rmtree('cookiecutter-trytonmodule')
def test_git_clone_overwrite(monkeypatch): monkeypatch.setattr('cookiecutter.vcs.read_user_yes_no', lambda question, default: True) repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git') assert repo_dir == 'cookiecutter-pypackage' assert os.path.isfile('cookiecutter-pypackage/README.rst')
def test_hg_clone(self): repo_dir = vcs.clone( 'https://bitbucket.org/pokoli/cookiecutter-trytonmodule') self.assertEqual(repo_dir, 'cookiecutter-trytonmodule') self.assertTrue(os.path.isfile('cookiecutter-trytonmodule/README.rst')) if os.path.isdir('cookiecutter-trytonmodule'): utils.rmtree('cookiecutter-trytonmodule')
def test_clone_unknown_subprocess_error(mocker, clone_dir): """In `clone()`, unknown subprocess errors should be raised.""" mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, side_effect=[subprocess.CalledProcessError( -1, 'cmd', output='Something went wrong'.encode('utf-8') )] ) with pytest.raises(subprocess.CalledProcessError): vcs.clone( 'https://github.com/pytest-dev/cookiecutter-pytest-plugin', clone_to_dir=clone_dir, no_input=True )
def _cookiecutter_configs_have_changed(template, old_version, new_version): """Given an old version and new version, check if the cookiecutter.json files have changed When the cookiecutter.json files change, it means the user will need to be prompted for new context Args: template (str): The git path to the template old_version (str): The git SHA of the old version new_version (str): The git SHA of the new version Returns: bool: True if the cookiecutter.json files have been changed in the old and new versions """ with tempfile.TemporaryDirectory() as clone_dir: repo_dir = cc_vcs.clone(template, old_version, clone_dir) old_config = json.load( open(os.path.join(repo_dir, 'cookiecutter.json'))) subprocess.check_call('git checkout %s' % new_version, cwd=repo_dir, shell=True, stderr=subprocess.PIPE) new_config = json.load( open(os.path.join(repo_dir, 'cookiecutter.json'))) return old_config != new_config
def test_git_clone(self): repo_dir = vcs.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')
def test_hg_clone_overwrite(self): if not PY3: sys.stdin = StringIO('y\n\n') repo_dir = vcs.clone( 'https://bitbucket.org/pokoli/cookiecutter-trytonmodule' ) self.assertEqual(repo_dir, 'cookiecutter-trytonmodule') self.assertTrue(os.path.isfile('cookiecutter-trytonmodule/README.rst'))
def test_git_clone_overwrite(self): if not PY3: sys.stdin = StringIO('y\n\n') repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git' ) self.assertEqual(repo_dir, 'cookiecutter-pypackage') self.assertTrue(os.path.isfile('cookiecutter-pypackage/README.rst'))
def test_hg_clone(self): repo_dir = vcs.clone( 'https://bitbucket.org/pokoli/cookiecutter-trytonmodule' ) self.assertEqual(repo_dir, 'cookiecutter-trytonmodule') self.assertTrue(os.path.isfile('cookiecutter-trytonmodule/README.rst')) if os.path.isdir('cookiecutter-trytonmodule'): shutil.rmtree('cookiecutter-trytonmodule')
def test_git_clone_with_trailing_slash(): repo_dir = vcs.clone('https://github.com/audreyr/cookiecutter-pypackage/') assert repo_dir == 'cookiecutter-pypackage' assert os.path.isfile('cookiecutter-pypackage/README.rst') if os.path.isdir('cookiecutter-pypackage'): utils.rmtree('cookiecutter-pypackage')
def test_hg_clone(): repo_dir = vcs.clone( 'https://bitbucket.org/pokoli/cookiecutter-trytonmodule' ) assert repo_dir == 'cookiecutter-trytonmodule' assert os.path.isfile('cookiecutter-trytonmodule/README.rst') if os.path.isdir('cookiecutter-trytonmodule'): utils.rmtree('cookiecutter-trytonmodule')
def determine_repo_dir(template, abbreviations, clone_to_dir, checkout, no_input, password=None): """ Locate the repository directory from a template reference. Applies repository abbreviations to the template reference. If the template refers to a repository URL, clone it. If the template is a path to a local repository, use it. :param template: A directory containing a project template directory, or a URL to a git repository. :param abbreviations: A dictionary of repository abbreviation definitions. :param clone_to_dir: The directory to clone the repository into. :param checkout: The branch, tag or commit ID to checkout after clone. :param no_input: Prompt the user at command line for manual configuration? :param password: The password to use when extracting the repository. :return: A tuple containing the cookiecutter template directory, and a boolean descriving whether that directory should be cleaned up after the template has been instantiated. :raises: `RepositoryNotFound` if a repository directory could not be found. """ template = expand_abbreviations(template, abbreviations) if is_zip_file(template): unzipped_dir = unzip(zip_uri=template, is_url=is_repo_url(template), clone_to_dir=clone_to_dir, no_input=no_input, password=password) repository_candidates = [unzipped_dir] cleanup = True elif is_repo_url(template): cloned_repo = clone( repo_url=template, checkout=checkout, clone_to_dir=clone_to_dir, no_input=no_input, ) repository_candidates = [cloned_repo] cleanup = False else: repository_candidates = [ template, os.path.join(clone_to_dir, template) ] cleanup = False for repo_candidate in repository_candidates: if repository_has_cookiecutter_config(repo_candidate): return repo_candidate, cleanup raise RepositoryNotFound( 'A valid repository for "{}" could not be found in the following ' 'locations:\n{}'.format(template, '\n'.join(repository_candidates)))
def test_clone_unknown_subprocess_error(mocker, clone_dir): """In `clone()`, unknown subprocess errors should be raised.""" mocker.patch( "cookiecutter.vcs.subprocess.check_output", autospec=True, side_effect=[ subprocess.CalledProcessError( -1, "cmd", output="Something went wrong".encode("utf-8") ) ], ) with pytest.raises(subprocess.CalledProcessError): vcs.clone( "https://github.com/pytest-dev/cookiecutter-pytest-plugin", clone_to_dir=clone_dir, no_input=True, )
def test_git_clone_with_trailing_slash(): repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage/' ) assert repo_dir == 'cookiecutter-pypackage' assert os.path.isfile('cookiecutter-pypackage/README.rst') if os.path.isdir('cookiecutter-pypackage'): utils.rmtree('cookiecutter-pypackage')
def compile_data(template, target, data): context = {"cookiecutter": {"project_name": target, "data": data}} is_repo = repository.is_repo_url(template) if is_repo: logger.info(f"Template is a repo, cloning") template = vcs.clone(template, None, "remote_repos", True) result = generate.generate_files(template, context=context, overwrite_if_exists=True) return result
def test_git_clone_overwrite(monkeypatch): monkeypatch.setattr( 'cookiecutter.prompt.read_response', lambda x=u'': u'y' ) repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git' ) assert repo_dir == 'cookiecutter-pypackage' assert os.path.isfile('cookiecutter-pypackage/README.rst')
def test_hg_clone_overwrite(monkeypatch): monkeypatch.setattr( 'cookiecutter.prompt.read_response', lambda x=u'': u'y' ) repo_dir = vcs.clone( 'https://bitbucket.org/pokoli/cookiecutter-trytonmodule' ) assert repo_dir == 'cookiecutter-trytonmodule' assert os.path.isfile('cookiecutter-trytonmodule/README.rst')
def test_clone_should_silent_exit_if_ok_to_reuse(mocker, tmpdir): """In `clone()`, if user doesn't want to reclone, Cookiecutter should exit \ without cloning anything.""" mocker.patch('cookiecutter.vcs.is_vcs_installed', autospec=True, return_value=True) mocker.patch( 'cookiecutter.vcs.prompt_and_delete', return_value=False, autospec=True ) mock_subprocess = mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, ) clone_to_dir = tmpdir.mkdir('clone') # Create repo_dir to trigger prompt_and_delete clone_to_dir.mkdir('cookiecutter-pytest-plugin') repo_url = 'https://github.com/pytest-dev/cookiecutter-pytest-plugin.git' vcs.clone(repo_url, clone_to_dir=str(clone_to_dir)) assert not mock_subprocess.called
def test_git_clone_custom_dir(self): os.makedirs("tests/custom_dir1/custom_dir2/") repo_dir = vcs.clone( repo_url='https://github.com/audreyr/cookiecutter-pypackage.git', checkout=None, clone_to_dir="tests/custom_dir1/custom_dir2/" ) with utils.work_in("tests/custom_dir1/custom_dir2/"): self.assertEqual(repo_dir, 'tests/custom_dir1/custom_dir2/cookiecutter-pypackage') self.assertTrue(os.path.isfile('cookiecutter-pypackage/README.rst')) if os.path.isdir('cookiecutter-pypackage'): shutil.rmtree('cookiecutter-pypackage') if os.path.isdir('tests/custom_dir1'): shutil.rmtree('tests/custom_dir1')
def test_git_clone_custom_dir(): os.makedirs('tests/custom_dir1/custom_dir2/') repo_dir = vcs.clone( repo_url='https://github.com/audreyr/cookiecutter-pypackage.git', checkout=None, clone_to_dir='tests/custom_dir1/custom_dir2/') with utils.work_in('tests/custom_dir1/custom_dir2/'): test_dir = 'tests/custom_dir1/custom_dir2/cookiecutter-pypackage' assert repo_dir == test_dir.replace('/', os.sep) assert os.path.isfile('cookiecutter-pypackage/README.rst') if os.path.isdir('cookiecutter-pypackage'): utils.rmtree('cookiecutter-pypackage') if os.path.isdir('tests/custom_dir1'): utils.rmtree('tests/custom_dir1')
def cookiecutter(template, checkout=None, no_input=False, extra_context=None): """ Replacement for cookiecutter's own cookiecutter. The difference with cookiecutter's cookiecutter function is that this one doesn't automatically str() all the values passed along to the template. :param template: A directory containing a project template directory, or a URL to a git repository. :param checkout: The branch, tag or commit ID to checkout after clone. :param no_input: Prompt the user at command line for manual configuration? :param extra_context: A dictionary of context that overrides default and user configuration. """ # Get user config from ~/.cookiecutterrc or equivalent # If no config file, sensible defaults from config.DEFAULT_CONFIG are used config_dict = get_user_config() template = expand_abbreviations(template, config_dict) # TODO: find a better way to tell if it's a repo URL if 'git@' in template or 'https://' in template: repo_dir = clone( repo_url=template, checkout=checkout, clone_to_dir=config_dict['cookiecutters_dir'], no_input=no_input ) else: # If it's a local repo, no need to clone or copy to your # cookiecutters_dir repo_dir = template context_file = os.path.join(repo_dir, 'cookiecutter.json') logging.debug('context_file is {0}'.format(context_file)) context = generate_context( context_file=context_file, default_context=config_dict['default_context'], extra_context=extra_context, ) # Create project from local context and project template. generate_files( repo_dir=repo_dir, context=context )
def test_clone_should_invoke_vcs_command(mocker, clone_dir, repo_class, repo_url, repo_name): """When `clone()` is called with a git/hg repo, the corresponding VCS \ command should be run via `subprocess.check_output()`. This should take place: * In the correct dir * With the correct args. :param repo_class: _VCS class that should be used :param repo_url: Repo URL that should be cloned by the VCS :param repo_name: Name of subdirectory where the cloned files will be """ mocker.patch( repo_class.__module__ + '.' + repo_class.__name__ + '.is_installed', autospec=True, return_value=True, ) mock_subprocess = mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, ) expected_repo_dir = os.path.normpath(os.path.join(clone_dir, repo_name)) branch = 'foobar' repo_dir = vcs.clone(repo_url, checkout=branch, clone_to_dir=clone_dir, no_input=True) assert repo_dir == expected_repo_dir if repo_class.cmd == 'svn': mock_subprocess.assert_any_call( ['svn', 'export', repo_url, '-r', branch], cwd=clone_dir, stderr=subprocess.STDOUT, ) else: mock_subprocess.assert_any_call([repo_class.cmd, 'clone', repo_url], cwd=clone_dir, stderr=subprocess.STDOUT) mock_subprocess.assert_any_call( [repo_class.cmd, 'checkout', branch], cwd=expected_repo_dir, stderr=subprocess.STDOUT, )
def test_git_clone_custom_dir(): os.makedirs('tests/custom_dir1/custom_dir2/') repo_dir = vcs.clone( repo_url='https://github.com/audreyr/cookiecutter-pypackage.git', checkout=None, clone_to_dir='tests/custom_dir1/custom_dir2/' ) with utils.work_in('tests/custom_dir1/custom_dir2/'): test_dir = 'tests/custom_dir1/custom_dir2/cookiecutter-pypackage' assert repo_dir == test_dir.replace('/', os.sep) assert os.path.isfile('cookiecutter-pypackage/README.rst') if os.path.isdir('cookiecutter-pypackage'): utils.rmtree('cookiecutter-pypackage') if os.path.isdir('tests/custom_dir1'): utils.rmtree('tests/custom_dir1')
def fetch_ansible_roles(): roles = [] with open('requirements.ansible_roles', 'r') as rfile: my_roles = rfile.read().rstrip().split(',') if len(my_roles) > 0: roles = my_roles roles_dir = 'ansible/roles' make_sure_path_exists(roles_dir) for role in roles: repo_url = '[email protected]:ephes/ansible_{}.git'.format(role) role_dir = os.path.join(roles_dir, role) if not os.path.exists(role_dir): cloned_dir = clone(repo_url, checkout='master', clone_to_dir=roles_dir) shutil.move(cloned_dir, os.path.join(roles_dir, role))
def _create_service_folder(repo_url: str, context: dict, output_dir: str, clone_to_dir: str = '/tmp') -> bool: """Clone the template and create service-folder based on this template.""" filepath = clone(repo_url, clone_to_dir=clone_to_dir, no_input=True) try: generate.generate_files(filepath, context=context, output_dir=output_dir, overwrite_if_exists=False) except OutputDirExistsException: print('Folder already exists!') print('Skipped creation of new service!') return False return True
def test_git_clone_checkout(): repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git', 'console-script') git_dir = 'cookiecutter-pypackage' assert repo_dir == git_dir assert os.path.isfile(os.path.join('cookiecutter-pypackage', 'README.rst')) proc = subprocess.Popen(['git', 'symbolic-ref', 'HEAD'], cwd=git_dir, stdout=subprocess.PIPE) symbolic_ref = proc.communicate()[0] branch = symbolic_ref.decode(ENCODING).strip().split('/')[-1] assert 'console-script' == branch if os.path.isdir(git_dir): utils.rmtree(git_dir)
def test_clone_should_invoke_vcs_command( mocker, clone_dir, repo_type, repo_url, repo_name): """When `clone()` is called with a git/hg repo, the corresponding VCS command should be run via `subprocess.check_output()`. This should take place: * In the correct dir * With the correct args. """ mocker.patch( 'cookiecutter.vcs.is_vcs_installed', autospec=True, return_value=True ) mock_subprocess = mocker.patch( 'cookiecutter.vcs.subprocess.check_output', autospec=True, ) expected_repo_dir = os.path.normpath(os.path.join(clone_dir, repo_name)) branch = 'foobar' repo_dir = vcs.clone( repo_url, checkout=branch, clone_to_dir=clone_dir, no_input=True ) assert repo_dir == expected_repo_dir mock_subprocess.assert_any_call( [repo_type, 'clone', repo_url], cwd=clone_dir, stderr=subprocess.STDOUT ) mock_subprocess.assert_any_call( [repo_type, 'checkout', branch], cwd=expected_repo_dir, stderr=subprocess.STDOUT )
def test_git_clone_checkout(): repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git', 'console-script' ) git_dir = 'cookiecutter-pypackage' assert repo_dir == git_dir assert os.path.isfile(os.path.join('cookiecutter-pypackage', 'README.rst')) proc = subprocess.Popen( ['git', 'symbolic-ref', 'HEAD'], cwd=git_dir, stdout=subprocess.PIPE ) symbolic_ref = proc.communicate()[0] branch = symbolic_ref.decode(ENCODING).strip().split('/')[-1] assert 'console-script' == branch if os.path.isdir(git_dir): utils.rmtree(git_dir)
def test_git_clone_checkout(self): repo_dir = vcs.clone( 'https://github.com/audreyr/cookiecutter-pypackage.git', 'console-script' ) git_dir = 'cookiecutter-pypackage' self.assertEqual(repo_dir, git_dir) self.assertTrue(os.path.isfile(os.path.join('cookiecutter-pypackage', 'README.rst'))) with subprocess.Popen( ['git', 'symbolic-ref', 'HEAD'], cwd=git_dir, stdout=subprocess.PIPE ) as proc: symbolic_ref = proc.communicate()[0] branch = symbolic_ref.decode(encoding).strip().split('/')[-1] self.assertEqual('console-script', branch) if os.path.isdir(git_dir): shutil.rmtree(git_dir)
def test_git_clone_cancel(monkeypatch): monkeypatch.setattr("cookiecutter.vcs.read_user_yes_no", lambda question, default: False) with pytest.raises(SystemExit): vcs.clone("https://github.com/audreyr/cookiecutter-pypackage.git")
def test_hg_clone_cancel(monkeypatch): monkeypatch.setattr("cookiecutter.vcs.read_user_yes_no", lambda question, default: False) with pytest.raises(SystemExit): vcs.clone("https://bitbucket.org/pokoli/cookiecutter-trytonmodule")
def test_hg_clone_overwrite(monkeypatch): monkeypatch.setattr("cookiecutter.vcs.read_user_yes_no", lambda question, default: True) repo_dir = vcs.clone("https://bitbucket.org/pokoli/cookiecutter-trytonmodule") assert repo_dir == "cookiecutter-trytonmodule" assert os.path.isfile("cookiecutter-trytonmodule/README.rst")