WIP Add tests for gradle and json

This commit is contained in:
erik 2023-02-09 13:20:42 +01:00
parent fd84063edc
commit ca3db5efe9
6 changed files with 32 additions and 13 deletions

View file

@ -18,7 +18,7 @@ apply plugin: "kotlinx-serialization"
group = "org.domaindrivenarchitecture.provs"
version = "0.17.1"
version = "0.17.1-SNAPSHOT"
repositories {
mavenCentral()

View file

@ -3,12 +3,9 @@ from file_handlers import FileHandler
def init_project():
# validate_values()
version = Version.from_file('build.gradle')
v2 = Version.from_file('package.json')
print(type(version.file_handler))
print(type(v2.file_handler))
# version.increment(ReleaseLevel.SNAPSHOT)
# version.write()
version = Version.from_file('build.gradle')
version.increment(ReleaseLevel.SNAPSHOT)
version.to_file()
print(version.get())
def prepare_release():
@ -39,6 +36,12 @@ class Version():
return inst
def to_file(self):
if self.file_handler is None:
raise Exception('Version was not created by from_file method.')
else:
self.file_handler.write(self.get())
def increment(self, level: ReleaseLevel):
self.is_snapshot = False
match level:

View file

@ -24,7 +24,7 @@ class FileHandler(ABC):
pass
@abstractmethod
def write(self):
def write(self, version_string):
pass
class JsonFileHandler(FileHandler):
@ -39,10 +39,10 @@ class JsonFileHandler(FileHandler):
version = [int(x) for x in json_version.split('.')]
return version, is_snapshot
def write(self):
def write(self, version_string):
with open(self.config_file_path, 'r+') as json_file:
json_data = json.load(json_file)
json_data['version'] = self.get()
json_data['version'] = version_string
json_file.seek(0)
json.dump(json_data, json_file, indent=4)
json_file.truncate()
@ -72,10 +72,10 @@ class GradleFileHandler(FileHandler):
return version, is_snapshot
def write(self):
def write(self, version_string):
with open(self.config_file_path, 'r+') as gradle_file:
gradle_contents = gradle_file.read()
version_substitute = re.sub("\nversion = .*\n", f'\nversion = "{self.get()}"\n', gradle_contents)
version_substitute = re.sub("\nversion = .*\n", f'\nversion = "{version_string}"\n', gradle_contents)
gradle_file.seek(0)
gradle_file.write(version_substitute)
gradle_file.truncate()

2
test/config.gradle Normal file
View file

@ -0,0 +1,2 @@
version = "12.4.678"

3
test/config.json Normal file
View file

@ -0,0 +1,3 @@
{
"version": "123.123.456"
}

View file

@ -22,4 +22,15 @@ def test_version():
assert version.get() == "1.3.0"
version.increment(ReleaseLevel.MAJOR)
assert version.get() == "2.0.0"
assert version.get() == "2.0.0"
def test_gradle():
# validate_values()
version = Version.from_file('build.gradle')
version.increment(ReleaseLevel.SNAPSHOT)
version.to_file()
print(version.get())