Skip to content

Commit

Permalink
Add Gradle plugin and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Simon committed Oct 10, 2024
1 parent 7e1a83d commit 84e98bf
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/cfgnet/config_types/config_type_inferer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,36 @@ class ConfigTypeInferer:

regex_password_option = re.compile(r"password|pwd|pass")
regex_password_value = re.compile(r".+")
regex_port_option = re.compile(r"ports|port|listen|expose|in|out")

regex_port_option = re.compile(r"ports|port|listen|expose")
regex_port_value = re.compile(
r"([1-9][0-9]{0,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])"
)

regex_size_option = re.compile(
r"size|length|max|min|threshold|weight|height|memory|mem|byte|mb"
)
regex_size_value = re.compile(r"(\d)+ ?(B|KB|MB|GB|TB|PB)?")

regex_username_option = re.compile(r"user|usr|username")
regex_username_value = re.compile(r"[a-zA-Z][a-zA-Z0-9_]+")

regex_time_option = re.compile(
r"time|interval|day|month|year|hour|minute|second|millisecond"
)
regex_time_value = re.compile(r"[\d]+ ?(s|min|h|d|ms)*")

regex_filepath_option = re.compile(
r"path|dir|directory|folder|destination|root"
)
# regex_filepath_value = re.compile(r"\/?([^\/]+\/)+[^\/]*")
regex_filepath_value = re.compile(r"^([~.\w\d]*\/[.\w\d]+)+(\.[\w\d]+)*$")

regex_version_number_option = re.compile(r"version|target|source")
regex_version_number_value = re.compile(
r"^(\^|~)?(?:[0-9]{1,3}\.){2}[0-9]{1,3}(-[\w]+)?$"
)

regex_ip_address_option = re.compile(r"address|ip")
regex_ip_address_value = re.compile(r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$")

Expand Down Expand Up @@ -99,12 +106,12 @@ def get_config_type( # noqa: C901
return ConfigType.BOOLEAN

if bool(
re.match(ConfigTypeInferer.regex_port_option, option_name)
re.search(ConfigTypeInferer.regex_port_option, option_name)
) and bool(re.fullmatch(ConfigTypeInferer.regex_port_value, value)):
return ConfigType.PORT

if bool(
re.match(ConfigTypeInferer.regex_username_option, option_name)
re.search(ConfigTypeInferer.regex_username_option, option_name)
) and bool(
re.fullmatch(ConfigTypeInferer.regex_username_value, value)
):
Expand All @@ -121,7 +128,7 @@ def get_config_type( # noqa: C901
return ConfigType.TIME

if bool(
re.match(ConfigTypeInferer.regex_password_option, option_name)
re.search(ConfigTypeInferer.regex_password_option, option_name)
) and bool(
re.fullmatch(ConfigTypeInferer.regex_password_value, value)
):
Expand All @@ -133,7 +140,7 @@ def get_config_type( # noqa: C901
return ConfigType.PATH

if bool(
re.match(
re.search(
ConfigTypeInferer.regex_version_number_option, option_name
)
) or bool(
Expand All @@ -142,7 +149,7 @@ def get_config_type( # noqa: C901
return ConfigType.VERSION_NUMBER

if bool(
re.match(ConfigTypeInferer.regex_ip_address_option, option_name)
re.search(ConfigTypeInferer.regex_ip_address_option, option_name)
) or bool(
re.fullmatch(ConfigTypeInferer.regex_ip_address_value, value)
):
Expand Down Expand Up @@ -174,6 +181,7 @@ def get_config_type( # noqa: C901
return ConfigType.COUNT

if bool(re.search(ConfigTypeInferer.regex_name, option_name)):
print(f"Option {option_name} is name.")
return ConfigType.NAME

if bool(re.search(ConfigTypeInferer.regex_pattern, option_name)):
Expand Down
48 changes: 48 additions & 0 deletions src/cfgnet/plugins/concept/gradle_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This file is part of the CfgNet module.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
import os
from cfgnet.config_types.config_types import ConfigType
from cfgnet.plugins.file_type.configparser_plugin import ConfigParserPlugin


class GradlePlugin(ConfigParserPlugin):
def __init__(self):
super().__init__("gradle")

def is_responsible(self, abs_file_path):
file_name = os.path.basename(abs_file_path)
return file_name == "gradle.properties"

def get_config_type(self, option_name: str) -> ConfigType:

if option_name.endswith(("home", "projectcachedir")):
return ConfigType.PATH

if option_name.endswith(("max")):
return ConfigType.NUMBER

if option_name.endswith(("console", "level", "mode")):
return ConfigType.MODE

if option_name.endswith("idletimeout"):
return ConfigType.TIME

# if option_name.endswith("user"):
# return ConfigType.USERNAME

# if option_name.endswith("password"):
# return ConfigType.PASSWORD

return ConfigType.UNKNOWN
2 changes: 2 additions & 0 deletions src/cfgnet/plugins/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from cfgnet.plugins.concept.circleci_plugin import CircleCiPlugin
from cfgnet.plugins.concept.cargo_plugin import CargoPlugin
from cfgnet.plugins.concept.github_actions_plugin import GitHubActionPlugin
from cfgnet.plugins.concept.gradle_plugin import GradlePlugin


class PluginManager:
Expand Down Expand Up @@ -89,6 +90,7 @@ class PluginManager:
CircleCiPlugin(),
CargoPlugin(),
GitHubActionPlugin(),
GradlePlugin(),
]

file_type_plugins: List[Plugin] = [
Expand Down
2 changes: 1 addition & 1 deletion tests/cfgnet/config_types/test_config_type_inferer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_get_config_type():
assert ConfigTypeInferer.get_config_type("count_leafs", "5") == ConfigType.COUNT
assert ConfigTypeInferer.get_config_type("domain_name", "https://192.168.34.164:8080") == ConfigType.DOMAIN_NAME
assert ConfigTypeInferer.get_config_type("server_name", "MainServer15") == ConfigType.NAME
assert ConfigTypeInferer.get_config_type("num_resources", "123123123") == ConfigType.NUMBER
assert ConfigTypeInferer.get_config_type("num_cores", "123123123") == ConfigType.NUMBER
assert ConfigTypeInferer.get_config_type("version_number", "1.12.12") == ConfigType.VERSION_NUMBER
assert ConfigTypeInferer.get_config_type("test", "true") == ConfigType.BOOLEAN
assert ConfigTypeInferer.get_config_type("io.file.buffer.size", "131072") == ConfigType.SIZE
Expand Down
59 changes: 59 additions & 0 deletions tests/cfgnet/plugins/concept/test_gradle_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This file is part of the CfgNet module.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.

import os

import pytest

from cfgnet.plugins.concept.gradle_plugin import GradlePlugin
from cfgnet.config_types.config_types import ConfigType
from tests.utility.id_creator import make_id


@pytest.fixture(name="get_plugin")
def get_plugin_():
plugin = GradlePlugin()
return plugin


def test_is_responsible(get_plugin):
plugin = get_plugin

assert plugin.is_responsible("tests/files/gradle.properties")
assert not plugin.is_responsible("tests/files/test.properties")


def test_config_types(get_plugin):
plugin = get_plugin
gradle_file = os.path.abspath("tests/files/gradle.properties")
artifact = plugin.parse_file(gradle_file, "gradle.properties")
nodes = artifact.get_nodes()

for node in nodes:
print(node, node.config_type)

name_node = next(filter(lambda x: x.id == make_id("gradle.properties", "appname", "MyApp"), nodes))
boolean_node = next(filter(lambda x: x.id == make_id("gradle.properties", "org.gradle.daemon", "true"), nodes))
time_node = next(filter(lambda x: x.id == make_id("gradle.properties", "org.gradle.daemon.idletimeout", "1000"), nodes))
version_node = next(filter(lambda x: x.id == make_id("gradle.properties", "projectversion", "1.0.0"), nodes))
user_node = next(filter(lambda x: x.id == make_id("gradle.properties", "systemprop.gradle.wrapperuser", "myuser"), nodes))
password_node = next(filter(lambda x: x.id == make_id("gradle.properties", "systemprop.gradle.wrapperpassword", "12345"), nodes))

assert name_node.config_type == ConfigType.NAME
assert boolean_node.config_type == ConfigType.BOOLEAN
assert time_node.config_type == ConfigType.TIME
assert version_node.config_type == ConfigType.VERSION_NUMBER
assert user_node.config_type == ConfigType.USERNAME
assert password_node.config_type == ConfigType.PASSWORD
4 changes: 3 additions & 1 deletion tests/cfgnet/plugins/test_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def test_get_all_plugins():
all_plugins = PluginManager.get_plugins()

assert len(all_plugins) == 29
assert len(all_plugins) == 30


def test_get_responsible_plugin():
Expand Down Expand Up @@ -54,6 +54,7 @@ def test_get_responsible_plugin():
circleci_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/.circleci/config.yml")
cargo_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/Cargo.toml")
github_action_plugin = PluginManager.get_responsible_plugin(plugins, ".github/workflows/test.yml")
gradle_plugin = PluginManager.get_responsible_plugin(plugins, "path/to/gradle.properties")

assert docker_plugin.concept_name == "docker"
assert maven_plugin.concept_name == "maven"
Expand Down Expand Up @@ -84,3 +85,4 @@ def test_get_responsible_plugin():
assert circleci_plugin.concept_name == "circleci"
assert cargo_plugin.concept_name == "cargo"
assert github_action_plugin.concept_name == "github-action"
assert gradle_plugin.concept_name == "gradle"
19 changes: 19 additions & 0 deletions tests/files/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Project-wide Gradle settings
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.daemon.idletimeout=1000

# JVM memory settings
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -Dfile.encoding=UTF-8

# Custom properties
projectVersion=1.0.0
isRelease=false
appName=MyApp

systemProp.pts.enabled=true
systemProp.log4j2.disableJmx=true
systemProp.file.encoding = UTF-8
systemProp.gradle.wrapperUser=myuser
systemProp.gradle.wrapperPassword=12345

0 comments on commit 84e98bf

Please sign in to comment.