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 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_cannot_add_an_existing_module(): github_repository = MagicMock() content = github_repository.get_file_contents() content.decoded_content.decode.return_value = ('') forge_module_apache = ForgeModule('puppetlabs/apache') puppetfile = Puppetfile(github_repository, 'env', sha='shasha', forge_modules=[forge_module_apache]) with pytest.raises(ModuleAlreadyPresentException): puppetfile.add_forge_module('puppetlabs/apache')
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
def test_it_remove_a_forge_module_from_the_puppetfile(): github_repository = MagicMock() content = github_repository.get_file_contents() content.decoded_content.decode.return_value = ('') forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10') puppetfile = Puppetfile(github_repository, 'env', sha='shasha', forge_modules=[forge_module_apache]) assert forge_module_apache in puppetfile.forge_modules puppetfile.remove_forge_module('puppetlabs/apache') assert puppetfile.forge_modules == [] github_repository.update_file.assert_called_once_with( "Puppetfile", "Puppetfile - Remove forge module puppetlabs/apache", "", "shasha")
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_add_a_module_to_the_puppetfile(): github_repository = MagicMock() content = github_repository.get_file_contents() content.decoded_content.decode.return_value = ('') puppetfile = Puppetfile(github_repository, 'env', sha='shasha', forge_modules=[FORGE_MODULE_NGINX]) assert puppetfile.forge_modules == [FORGE_MODULE_NGINX] puppetfile.add_forge_module('puppetlabs/apache', '0.1.10') forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10') assert forge_module_apache in puppetfile.forge_modules github_repository.update_file.assert_called_once_with( "Puppetfile", "Puppetfile - Add forge module puppetlabs/apache", f"{str(FORGE_MODULE_NGINX)}mod 'puppetlabs/apache', '0.1.10'\n", "shasha")
def test_it_update_a_module_in_the_puppetfile(): github_repository = MagicMock() content = github_repository.get_file_contents() content.decoded_content.decode.return_value = ('') forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.1') puppetfile = Puppetfile(github_repository, 'env', sha='shasha', forge_modules=[forge_module_apache]) assert puppetfile.forge_modules[0].version == '0.1.1' puppetfile.update_forge_module('puppetlabs/apache', version='0.1.2') assert puppetfile.forge_modules[0].version == '0.1.2' github_repository.update_file.assert_called_once_with( "Puppetfile", ("Puppetfile - Update forge module puppetlabs/apache " "from 0.1.1 to 0.1.2"), "mod 'puppetlabs/apache', '0.1.2'\n", "shasha")
def add_forge_module(self, name: str, version: Optional[str] = None) -> None: """ Add a Puppet forge module to the Puppetfile. :type name: string :param name: The name of the Puppet forge module to add to the Puppetfile. :type version: string :param version: The version of the Puppet forge module to add to the Puppetfile. """ module = ForgeModule(name, version=version) for forge_module in self._forge_modules: if name == forge_module.name: raise ModuleAlreadyPresentException self._forge_modules.append(module) self._update_file_on_github(f'Add forge module {name}')
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_a_bad_module(): with pytest.raises(ModuleParserException): ForgeModule.from_line("mod 'puppetlabs/apache', '0.1', 'bad'")
def test_it_returns_forge_module_from_line_with_version_latest(): forge_module = ForgeModule.from_line( "mod 'puppetlabs/apache', :latest") assert forge_module.name == 'puppetlabs/apache' assert forge_module.version == ':latest'
def test_it_returns_forge_module_from_line_without_version(): forge_module = ForgeModule.from_line("mod 'puppetlabs/apache'") assert forge_module.name == 'puppetlabs/apache' assert forge_module.version is None
def test_it_change_module_version(): forge_module = ForgeModule('puppetlabs/apache', version='0.1.10') assert forge_module.version == '0.1.10' forge_module.set_version('0.1.11') assert forge_module.version == '0.1.11'
def test_it_convert_a_module_without_version_to_string(): forge_module = ForgeModule('puppetlabs/apache') assert str(forge_module) == "mod 'puppetlabs/apache'\n"