Implement tests for file handling

This commit is contained in:
bom 2023-02-09 13:33:56 +01:00
parent ca3db5efe9
commit 86c66dc7f7
3 changed files with 34 additions and 9 deletions

View file

@ -6,11 +6,11 @@ class FileHandler(ABC):
@classmethod
def from_file_path(cls, file_path):
config_file_type = file_path.split('.')[-1]
config_file_type = file_path.suffix
match config_file_type:
case 'json':
case '.json':
file_handler = JsonFileHandler()
case 'gradle':
case '.gradle':
file_handler = GradleFileHandler()
case _:
raise Exception(f'The file type "{config_file_type}" is not implemented')

View file

@ -1,4 +1,5 @@
from devops_test import Version, ReleaseLevel
from pathlib import Path
def test_version():
version = Version([1, 2, 3], False)
@ -25,12 +26,36 @@ def test_version():
assert version.get() == "2.0.0"
def test_gradle():
# validate_values()
version = Version.from_file('build.gradle')
def test_gradle(tmp_path):
# init
file_name = 'config.gradle'
with open(f'test/{file_name}', 'r') as gradle_file:
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
# test
version = Version.from_file(f)
version.increment(ReleaseLevel.SNAPSHOT)
version.to_file()
print(version.get())
# check
assert 'version = "12.4.678-SNAPSHOT"' in f.read_text()
def test_json(tmp_path):
# init
file_name = 'config.json'
with open(f'test/{file_name}', 'r') as gradle_file:
contents = gradle_file.read()
f = tmp_path / file_name
f.write_text(contents)
# test
version = Version.from_file(f)
version.increment(ReleaseLevel.SNAPSHOT)
version.to_file()
# check
assert '"version": "123.123.456-SNAPSHOT"' in f.read_text()