dda-devops-build/test/test_infrastructure.py

165 lines
4.2 KiB
Python
Raw Normal View History

2023-02-24 09:46:43 +00:00
from domain import ReleaseType, Release
from infrastructure import GitRepository, VersionRepository, ReleaseRepository
from infrastructure_api import GitApi
2023-02-20 15:45:36 +00:00
from pathlib import Path
import sys
import os
# getting the name of the directory
# where the this file is present.
current = os.path.dirname(os.path.realpath(__file__))
2023-02-24 09:46:43 +00:00
2023-02-20 15:45:36 +00:00
# Getting the parent directory name
# where the current directory is present.
parent = os.path.dirname(current)
2023-02-24 09:46:43 +00:00
2023-02-20 15:45:36 +00:00
# adding the parent directory to
# the sys.path.
sys.path.append(parent)
2023-02-24 09:46:43 +00:00
2023-02-20 15:45:36 +00:00
# now we can import the module in the parent
# directory.
2023-02-24 09:46:43 +00:00
def TestGitApi(GitApi):
def __init__(self, commit_string):
self.commit_string = commit_string
def get_latest_commit(self):
return self.commit_string
2023-02-09 11:02:31 +00:00
2023-02-24 09:31:04 +00:00
def test_git_repository():
2023-02-09 11:02:31 +00:00
2023-02-24 09:31:04 +00:00
# init
commit_string = "Major bla"
repo = GitRepository.create_from_commit_string(commit_string)
release_type = repo.get_release_type_from_latest_commit()
2023-02-09 11:02:31 +00:00
2023-02-24 09:46:43 +00:00
# test
2023-02-24 09:31:04 +00:00
assert release_type == ReleaseType.MAJOR
2023-02-20 15:37:49 +00:00
2023-02-24 09:31:04 +00:00
# init
commit_string = "MINOR bla"
repo = GitRepository.create_from_commit_string(commit_string)
release_type = repo.get_release_type_from_latest_commit()
2023-02-24 09:46:43 +00:00
# test
2023-02-24 09:31:04 +00:00
assert release_type == ReleaseType.MINOR
2023-02-09 11:02:31 +00:00
2023-02-24 09:31:04 +00:00
# init
commit_string = "PATCH bla"
repo = GitRepository.create_from_commit_string(commit_string)
release_type = repo.get_release_type_from_latest_commit()
2023-02-09 12:20:42 +00:00
2023-02-24 09:31:04 +00:00
# test
assert release_type == ReleaseType.PATCH
# init
commit_string = "SNAPSHOT bla"
repo = GitRepository.create_from_commit_string(commit_string)
release_type = repo.get_release_type_from_latest_commit()
2023-02-24 09:46:43 +00:00
# test
2023-02-24 09:31:04 +00:00
assert release_type == ReleaseType.SNAPSHOT
# init
commit_string = "bla"
repo = GitRepository.create_from_commit_string(commit_string)
release_type = repo.get_release_type_from_latest_commit()
2023-02-24 09:46:43 +00:00
# test
2023-02-24 09:31:04 +00:00
assert release_type == None
2023-02-09 12:20:42 +00:00
2023-02-24 09:46:43 +00:00
2023-02-09 12:33:56 +00:00
def test_gradle(tmp_path):
# init
file_name = 'config.gradle'
2023-02-09 16:09:42 +00:00
with open(f'test/resources/{file_name}', 'r') as gradle_file:
2023-02-09 12:33:56 +00:00
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
# test
2023-02-21 15:14:47 +00:00
repo = VersionRepository(f)
version = repo.get_version()
2023-02-24 09:46:43 +00:00
version = version.create_release_version(ReleaseType.SNAPSHOT)
2023-02-21 15:14:47 +00:00
repo.write_file(version.get_version_string())
2023-02-24 09:46:43 +00:00
2023-02-09 12:33:56 +00:00
# check
assert 'version = "12.4.678-SNAPSHOT"' in f.read_text()
2023-02-24 09:46:43 +00:00
def test_json(tmp_path):
# init
file_name = 'config.json'
with open(f'test/resources/{file_name}', 'r') as gradle_file:
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
2023-02-24 09:46:43 +00:00
# test
2023-02-21 15:14:47 +00:00
repo = VersionRepository(f)
version = repo.get_version()
2023-02-24 09:46:43 +00:00
version = version.create_release_version(ReleaseType.SNAPSHOT)
2023-02-21 15:14:47 +00:00
repo.write_file(version.get_version_string())
2023-02-24 09:46:43 +00:00
# check
assert '"version": "123.123.456-SNAPSHOT"' in f.read_text()
2023-02-24 09:46:43 +00:00
def test_clojure(tmp_path):
# init
file_name = 'config.clj'
with open(f'test/resources/{file_name}', 'r') as gradle_file:
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
# test
2023-02-21 15:14:47 +00:00
repo = VersionRepository(f)
version = repo.get_version()
version = version.create_release_version(ReleaseType.SNAPSHOT)
2023-02-21 15:14:47 +00:00
repo.write_file(version.get_version_string())
2023-02-24 09:46:43 +00:00
# check
assert '1.1.3-SNAPSHOT' in f.read_text()
2023-02-24 09:46:43 +00:00
def test_python(tmp_path):
# init
file_name = 'config.py'
with open(f'test/resources/{file_name}', 'r') as gradle_file:
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
# test
2023-02-21 15:14:47 +00:00
repo = VersionRepository(f)
version = repo.get_version()
2023-02-24 09:46:43 +00:00
version = version.create_release_version(ReleaseType.SNAPSHOT)
2023-02-21 15:14:47 +00:00
repo.write_file(version.get_version_string())
2023-02-24 09:46:43 +00:00
# check
2023-02-24 09:31:04 +00:00
assert '3.1.3-SNAPSHOT' in f.read_text()
2023-02-24 09:46:43 +00:00
2023-02-24 09:31:04 +00:00
def test_release_repository(tmp_path):
# init
file_name = 'config.json'
with open(f'test/resources/{file_name}', 'r') as gradle_file:
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
# test
sut = ReleaseRepository(VersionRepository(f))
current_release = sut.get_current_release()
2023-02-24 09:46:43 +00:00
assert current_release.version is not None