From 0d36a8a66a8712fdb25cfb808a72798f6ea0b3db Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 2 Mar 2023 14:26:34 +0100 Subject: [PATCH] Fix service test and cleanup services We now create a temporary git repository that creates a release commit. Remove whitespaces, unused imports. --- services.py | 13 +++++-------- test/test_services.py | 39 ++++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/services.py b/services.py index b3b5771..dd55cec 100644 --- a/services.py +++ b/services.py @@ -13,17 +13,16 @@ class PrepareReleaseService(): self.release_repo.version_repository.write_file(version.get_version_string()) self.git_api.add_file(self.release_repo.version_repository.file) - self.git_api.commit(commit_message) + self.git_api.commit(commit_message) def write_and_commit_release(self): - self.__write_and_commit_version(self.release.release_version(), commit_message=f'Release {self.release.release_version().get_version_string()}') + self.__write_and_commit_version(self.release.release_version(), commit_message=f'Release v{self.release.release_version().get_version_string()}') - def write_and_commit_bump(self): - self.__write_and_commit_version(self.release.bump_version(), commit_message=f'Version bump') - + def write_and_commit_bump(self): + self.__write_and_commit_version(self.release.bump_version(), commit_message='Version bump') class TagAndPushReleaseService(): - + def __init__(self, git_api: GitApi): self.git_api = git_api @@ -32,5 +31,3 @@ class TagAndPushReleaseService(): message = 'Release ' + annotation self.git_api.tag_annotated(annotation, message, 1) # self.git_api.push() - - diff --git a/test/test_services.py b/test/test_services.py index 4815e2c..f82ca9b 100644 --- a/test/test_services.py +++ b/test/test_services.py @@ -1,3 +1,4 @@ +import pytest as pt from pathlib import Path import sys import os @@ -5,38 +6,50 @@ import os # getting the name of the directory # where the this file is present. current = os.path.dirname(os.path.realpath(__file__)) - + # Getting the parent directory name # where the current directory is present. parent = os.path.dirname(current) - + # adding the parent directory to # the sys.path. sys.path.append(parent) - + # now we can import the module in the parent # directory. -from services import InitReleaseService +from services import PrepareReleaseService from domain import ReleaseType from infrastructure import VersionRepository, ReleaseRepository, ReleaseTypeRepository from infrastructure_api import GitApi -def test_init_release_service(tmp_path): +def change_test_dir( tmp_path: Path, monkeypatch: pt.MonkeyPatch): + monkeypatch.chdir(tmp_path) + +def test_prepare_release_service(tmp_path: Path, monkeypatch: pt.MonkeyPatch): # init file_name = 'config.json' - with open(f'test/resources/{file_name}', 'r') as gradle_file: - contents = gradle_file.read() - + with open(f'test/resources/{file_name}', 'r') as json_file: + contents = json_file.read() f = tmp_path / file_name f.write_text(contents) + change_test_dir(tmp_path, monkeypatch) # change the context of the script execution to tmp_path + + git_api = GitApi() + git_api.init() + git_api.add_file(file_name) + git_api.commit("MINOR release") repo = ReleaseRepository(VersionRepository(f), ReleaseTypeRepository(GitApi()), 'main') - release_service = InitReleaseService(repo) - version = release_service.get_version().create_release_version(ReleaseType.MINOR) + prepare_release_service = PrepareReleaseService(repo) + prepare_release_service.main_branch = "main" + prepare_release_service.write_and_commit_release() + + latest_commit = git_api.get_latest_commit() - assert "123.124.0" in version.get_version_string() + assert '"Release v123.124.0 "\n' in latest_commit - version = version.create_bump_version() + prepare_release_service.write_and_commit_bump() + latest_commit = git_api.get_latest_commit() - assert "123.124.1-SNAPSHOT" in version.get_version_string() \ No newline at end of file + assert '"Version bump "\n' in latest_commit