def test_it_change_git_module_reference(): git_module = GitModule('apache', 'https://url/to/git/apache', reference_type='branch', reference='test') assert git_module.reference == 'test' git_module.set_reference('newtest') assert git_module.reference == 'newtest'
def test_it_update_both_reference_and_reference_type_if_supplied(): git_module = GitModule('apache', 'https://url/to/git/apache', reference_type='branch', reference='test') assert git_module.reference == 'test' assert git_module.reference_type == 'branch' git_module.set_reference('1.0.0', reference_type='tag') assert git_module.reference == '1.0.0' assert git_module.reference_type == 'tag'
def add_git_module(self, name: str, url: str, reference_type: Optional[str] = None, reference: Optional[str] = None) -> None: """ Add a Puppet git module to the Puppetfile. :type name: string :param name: The name of the Puppet git module to add to the Puppetfile. :type url: string :param url: The git URL of the Puppet git module to add to the Puppetfile. :type reference_type: string :param reference_type: The git reference type (ref, commit, branch, tag) of the Puppet git module to add to the Puppetfile. :type reference: string :param reference: The git reference of the Puppet git module to add to the Puppetfile. """ module = GitModule(name, url, reference_type=reference_type, reference=reference) for git_module in self._git_modules: if name == git_module.name: raise ModuleAlreadyPresentException self._git_modules.append(module) self._update_file_on_github(f'Add git module {name}')
def test_it_convert_a_git_module_with_references_to_string(): git_module = GitModule('apache', 'https://url/to/git/apache', reference_type='branch', reference='test') assert str(git_module) == ("mod 'apache',\n" " :git => 'https://url/to/git/apache',\n" " :branch => 'test'\n")
def test_it_returns_git_module_without_any_reference_type(): git_module = GitModule.from_lines([ "mod 'apache',", " :git => 'https://github.com/puppet/apache'", ]) assert git_module.name == 'apache' assert git_module.git_url == 'https://github.com/puppet/apache' assert git_module.reference_type is None assert git_module.reference is None
def test_with_commit_reference_type_returns_git_module(): git_module = GitModule.from_lines([ "mod 'apache',", " :git => 'https://github.com/puppet/apache'", " :commit => '0dfa12'", ]) assert git_module.name == 'apache' assert git_module.git_url == 'https://github.com/puppet/apache' assert git_module.reference_type == 'commit' assert git_module.reference == '0dfa12'
def test_with_tag_reference_type_returns_git_module(): git_module = GitModule.from_lines([ "mod 'apache',", " :git => 'https://github.com/puppet/apache'", " :tag => '0.1.1'", ]) assert git_module.name == 'apache' assert git_module.git_url == 'https://github.com/puppet/apache' assert git_module.reference_type == 'tag' assert git_module.reference == '0.1.1'
def test_with_branch_reference_type_returns_git_module(): git_module = GitModule.from_lines([ "mod 'apache',", " :git => 'https://github.com/puppet/apache'", " :branch => 'branchname'", ]) assert git_module.name == 'apache' assert git_module.git_url == 'https://github.com/puppet/apache' assert git_module.reference_type == 'branch' assert git_module.reference == 'branchname'
def test_it_returns_the_list_of_modules(): github_repository = MagicMock() content = github_repository.get_file_contents() content.decoded_content.decode.return_value = ('') git_module_custommod = GitModule('custommod', 'https://url/git/custommod') forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10') forge_module_vcsrepo = ForgeModule('puppetlabs/vcsrepo', '0.2.10') puppetfile = Puppetfile( github_repository, 'env', sha='shasha', forge_modules=[forge_module_apache, forge_module_vcsrepo], git_modules=[git_module_custommod]) assert sorted(puppetfile.list_modules()) == sorted( ['custommod', 'puppetlabs/apache', 'puppetlabs/vcsrepo'])
def _parse_puppet_modules( lines: List[str]) -> Tuple[List[ForgeModule], List[GitModule]]: forge_modules = [] git_modules = [] for index, line in enumerate(lines): if line.startswith('mod '): if not line.endswith(','): forge_modules.append(ForgeModule.from_line(line)) else: count = 1 while lines[index + count].endswith(','): count += 1 module_lines = lines[index:(index + count + 1)] git_module = GitModule.from_lines(module_lines) git_modules.append(git_module) return forge_modules, git_modules
def test_it_parse_modules_with_both_simple_and_double_quotes(): github_repository = MagicMock() content = github_repository.get_file_contents() content.decoded_content.decode.return_value = ( "mod \"custommod\",\n" " :git => \"https://url/git/custommod\"\n" "mod 'puppetlabs/apache', '0.1.10'\n" "mod \"puppetlabs/vcsrepo\", \"0.2.10\"") forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10') forge_module_vcsrepo = ForgeModule('puppetlabs/vcsrepo', '0.2.10') git_module_custommod = GitModule('custommod', 'https://url/git/custommod') puppetfile = Puppetfile.from_github_repository(github_repository, 'env') assert forge_module_apache in puppetfile.forge_modules assert forge_module_vcsrepo in puppetfile.forge_modules assert git_module_custommod in puppetfile.git_modules
def test_it_returns_a_puppetfile_with_both_git_and_forge_modules(): github_repository = MagicMock() content = github_repository.get_file_contents() content.decoded_content.decode.return_value = ( "mod 'apache',\n" " :git => 'https://url/git/apache',\n" " :ref => 'ed19f'\n" "mod 'custommod',\n" " :git => 'https://url/git/custommod'\n" "mod 'puppetlabs/apache', '0.1.10'\n" "mod 'puppetlabs/vcsrepo', '0.2.10'") forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10') forge_module_vcsrepo = ForgeModule('puppetlabs/vcsrepo', '0.2.10') git_module_custommod = GitModule('custommod', 'https://url/git/custommod') puppetfile = Puppetfile.from_github_repository(github_repository, 'env') assert forge_module_apache in puppetfile.forge_modules assert forge_module_vcsrepo in puppetfile.forge_modules assert GIT_MODULE_APACHE in puppetfile.git_modules assert git_module_custommod in puppetfile.git_modules
from copy import deepcopy from unittest.mock import MagicMock import pytest from github import GithubException from control_repository.exceptions import (PuppetfileNotFoundException, ModuleAlreadyPresentException, ModuleNotFoundException) from control_repository.modules import ForgeModule, GitModule from control_repository.puppetfile import Puppetfile GIT_MODULE_APACHE = GitModule('apache', 'https://url/git/apache', 'ref', 'ed19f') GIT_MODULE_NGINX = GitModule('nginx', 'https://url/git/nginx') FORGE_MODULE_NGINX = ForgeModule('puppet-nginx', '0.16.0') class TestPuppetfileFromGitubRepository: @staticmethod def test_it_read_and_decode_puppetfile_from_github_repository(): github_repository = MagicMock() Puppetfile.from_github_repository(github_repository, 'env') github_repository.get_file_contents.assert_called_once_with( '/Puppetfile', ref='env') content = github_repository.get_file_contents() content.decoded_content.decode.assert_called_once_with('utf-8') @staticmethod def test_whith_no_puppetfile_in_github_repository(): github_repository = MagicMock()
def test_with_unknow_reference(): with pytest.raises(AttributeError): GitModule('nginx', 'https://url/repo/nginx.git', 'bad_reference', 'version')
def test_if_module_has_reference_type_without_reference(): with pytest.raises(ModuleMalformedException): GitModule('nginx', 'https://url/repo/nginx.git', reference_type='branch')
def test_with_missing_git_url(): with pytest.raises(ModuleParserException): GitModule.from_lines([ "mod 'apache',", " :commit => '0dfa12'", ])
def test_it_convert_a_git_module_without_reference_to_string(): git_module = GitModule('apache', 'https://url/to/git/apache') assert str(git_module) == ("mod 'apache',\n" " :git => 'https://url/to/git/apache'\n")