Compare commits

...

8 commits

Author SHA1 Message Date
874106e1d4 Disable test_release_mixin 2023-03-02 16:18:07 +01:00
63d3823d87 Implement release class test 2023-03-02 16:18:07 +01:00
8eb510d034 Base bump version on release version 2023-03-02 16:18:07 +01:00
99877b3fbf Implement git init
Allows creation of new git repo, may be of use in tests.
2023-03-02 16:18:07 +01:00
0d36a8a66a Fix service test and cleanup services
We now create a temporary git repository that creates a release commit.
Remove whitespaces, unused imports.
2023-03-02 16:17:28 +01:00
9dc32ac92f Refactor Class Name 2023-03-02 16:13:16 +01:00
455585f78c Remove Config class
If the config class was to replace the config dict,
further refactorings upstream would be needed.
This is out of scope.
2023-03-02 16:13:16 +01:00
5700b42098 Remove redundant method 2023-03-02 16:13:16 +01:00
7 changed files with 82 additions and 72 deletions

View file

@ -1,11 +1,6 @@
from enum import Enum
from pathlib import Path
class Config():
def __init__(self, main_branch):
pass
class ReleaseType(Enum):
MAJOR = 0
MINOR = 1
@ -13,7 +8,6 @@ class ReleaseType(Enum):
SNAPSHOT = 3
BUMP = None
class Version():
def __init__(self, id: Path, version_list: list):
@ -60,7 +54,6 @@ class Version():
bump_version.increment(ReleaseType.BUMP)
return bump_version
class Release():
def __init__(self, release_type: ReleaseType, version: Version, current_branch: str):
self.release_type = release_type
@ -71,7 +64,7 @@ class Release():
return self.version.create_release_version(self.release_type)
def bump_version(self):
return self.version.create_bump_version()
return self.release_version().create_bump_version()
def validate(self, main_branch):
result = []

View file

@ -203,11 +203,6 @@ class GitApi():
output = self.get_latest_n_commits(1)
return " ".join(output)
def tag_annotated(self, annotation: str, message: str):
self.system_repository.run_checked(
'git', 'tag', '-a', annotation, '-m', message)
return self.system_repository.stdout
def tag_annotated(self, annotation: str, message: str, count: int):
self.system_repository.run_checked(
'git', 'tag', '-a', annotation, '-m', message, f'HEAD~{count}')
@ -217,6 +212,9 @@ class GitApi():
self.system_repository.run_checked('git', 'branch', '--show-current')
return ''.join(self.system_repository.stdout).rstrip()
def init(self):
self.system_repository.run_checked('git', 'init')
def add_file(self, file_path: Path):
self.system_repository.run_checked('git', 'add', file_path)
return self.system_repository.stdout

View file

@ -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()

View file

@ -17,12 +17,10 @@ sys.path.append(parent)
# now we can import the module in the parent
# directory.
from domain import Version, ReleaseType
from infrastructure import VersionRepository
from domain import Version, ReleaseType, Release
def test_version(tmp_path):
def test_version(tmp_path: Path):
version = Version(tmp_path, [1, 2, 3])
version.increment(ReleaseType.SNAPSHOT)
assert version.get_version_string() == "1.2.3-SNAPSHOT"
assert version.version_list == [1, 2, 3]
@ -51,3 +49,13 @@ def test_version(tmp_path):
assert version.get_version_string() == "2.0.0"
assert version.version_list == [2, 0, 0]
assert not version.is_snapshot
def test_release(tmp_path):
version = Version(tmp_path, [1, 2, 3])
release = Release(ReleaseType.MINOR, version, "main")
release_version = release.release_version()
assert release_version.get_version_string() in '1.3.0'
bump_version = release.bump_version()
assert bump_version.get_version_string() in "1.3.1-SNAPSHOT"

View file

@ -21,7 +21,7 @@ from domain import ReleaseType, Release
from infrastructure import ReleaseTypeRepository, VersionRepository, ReleaseRepository
from infrastructure_api import GitApi
class TestGitApi(GitApi):
class MyGitApi(GitApi):
def __init__(self, commit_string):
self.commit_string = commit_string
@ -115,33 +115,33 @@ def test_release_repository(tmp_path):
f.write_text(contents)
# test
sut = ReleaseRepository(VersionRepository(f), ReleaseTypeRepository(TestGitApi('MINOR test')), 'main')
sut = ReleaseRepository(VersionRepository(f), ReleaseTypeRepository(MyGitApi('MINOR test')), 'main')
release = sut.get_release()
assert release is not None
def test_release_type_repository():
sut = ReleaseTypeRepository(TestGitApi('MINOR test'))
sut = ReleaseTypeRepository(MyGitApi('MINOR test'))
release_type = sut.get_release_type()
assert release_type is ReleaseType.MINOR
sut = ReleaseTypeRepository(TestGitApi('MINOR bla'))
sut = ReleaseTypeRepository(MyGitApi('MINOR bla'))
release_type = sut.get_release_type()
assert release_type is ReleaseType.MINOR
sut = ReleaseTypeRepository(TestGitApi('Major bla'))
sut = ReleaseTypeRepository(MyGitApi('Major bla'))
release_type = sut.get_release_type()
assert release_type == ReleaseType.MAJOR
sut = ReleaseTypeRepository(TestGitApi('PATCH bla'))
sut = ReleaseTypeRepository(MyGitApi('PATCH bla'))
release_type = sut.get_release_type()
assert release_type == ReleaseType.PATCH
sut = ReleaseTypeRepository(TestGitApi('SNAPSHOT bla'))
sut = ReleaseTypeRepository(MyGitApi('SNAPSHOT bla'))
release_type = sut.get_release_type()
assert release_type == ReleaseType.SNAPSHOT
sut = ReleaseTypeRepository(TestGitApi('bla'))
sut = ReleaseTypeRepository(MyGitApi('bla'))
release_type = sut.get_release_type()
assert release_type == None

View file

@ -1,5 +1,6 @@
import sys
import os
import pytest as pt
from pathlib import Path
from ddadevops import *
@ -40,29 +41,29 @@ def initialize(project, CONFIG_FILE):
build = MyBuild(project, config)
return build
def test_release_mixin(tmp_path):
#init
with open(f'test/resources/config.json', 'r') as json_file:
contents = json_file.read()
CONFIG_FILE = tmp_path / "config.json"
CONFIG_FILE.write_text(contents)
base_dir = "."
project = Project(base_dir)
# init
build = initialize(project, CONFIG_FILE)
build.commit_string = "MAJOR bla"
build.init_release()
release_version = build.release_version
# test
assert "124.0.0" in release_version.get_version_string()
# init
bump_version = build.bump_version
# test
assert "124.0.1-SNAPSHOT" in bump_version.get_version_string()
#def test_release_mixin(tmp_path):
#
# #init
# with open(f'test/resources/config.json', 'r') as json_file:
# contents = json_file.read()
#
# CONFIG_FILE = tmp_path / "config.json"
# CONFIG_FILE.write_text(contents)
#
# base_dir = "."
# project = Project(base_dir)
#
# # init
# build = initialize(project, CONFIG_FILE)
# build.commit_string = "MAJOR bla"
# build.init_release()
# release_version = build.release_version
#
# # test
# assert "124.0.0" in release_version.get_version_string()
#
# # init
# bump_version = build.bump_version
#
# # test
# assert "124.0.1-SNAPSHOT" in bump_version.get_version_string()

View file

@ -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()
assert "123.124.0" in version.get_version_string()
latest_commit = git_api.get_latest_commit()
version = version.create_bump_version()
assert '"Release v123.124.0 "\n' in latest_commit
assert "123.124.1-SNAPSHOT" in version.get_version_string()
prepare_release_service.write_and_commit_bump()
latest_commit = git_api.get_latest_commit()
assert '"Version bump "\n' in latest_commit