Skip to content

Commit

Permalink
Fix extraction of dependencies and plugins in pom.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
simisimon committed Dec 5, 2024
1 parent ce5c316 commit af9e9ee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 58 deletions.
72 changes: 35 additions & 37 deletions src/cfgnet/plugins/concept/maven_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,51 +104,48 @@ def is_responsible(self, abs_file_path: str) -> bool:
def parse_tree(self, subtree_root: _Element, parent_node: Node):
name = subtree_root.tag

# remove prefix in curly brackets
name = self._remove_prefix(name=name)

if name:
if name in ["dependencies", "plugins"]:
config_type = self.get_config_type(name)
option = OptionNode(name, subtree_root.sourceline, config_type)
parent_node.add_child(option)
config_type = self.get_config_type(name)
option = OptionNode(name, subtree_root.sourceline, config_type)
parent_node.add_child(option)

self._parse_artifacts(subtree_root, option)
qualified_option = None
if name in ["dependency", "plugin"]:
qualified_name = self._get_fully_qualified_name(subtree_root)
config_type = self.get_config_type(qualified_name)

else:
config_type = self.get_config_type(name)
option = OptionNode(name, subtree_root.sourceline, config_type)
parent_node.add_child(option)
qualified_option = OptionNode(
name=qualified_name,
location=subtree_root.sourceline,
config_type=config_type,
)

option.add_child(qualified_option)

text = subtree_root.text
text = subtree_root.text
if text:
text = text.strip()
if text:
text = text.strip()
if text:
value_node = ValueNode(name=text)
option.add_child(value_node)
value_node = ValueNode(name=text)
option.add_child(value_node)

for child in subtree_root:
if child.tag is not ET.Comment:
for child in subtree_root:
if child.tag is not ET.Comment:
if qualified_option:
self.parse_tree(child, qualified_option)
else:
self.parse_tree(child, option)

# remove option nodes without children
# remove option nodes without children
if not option.children:
parent_node.children.remove(option)

def _parse_artifacts(self, subtree: _Element, current_node: OptionNode):
"""Parse Maven artifacts."""
for child in subtree:
if child.tag in ["dependency", "plugin"]:
qualified_name = self._get_fully_qualified_name(child)
config_type = self.get_config_type(qualified_name)
option_node = OptionNode(
name=qualified_name,
location=child.sourceline,
config_type=config_type,
)
current_node.add_child(option_node)

for element in child:
self.parse_tree(
subtree_root=element, parent_node=option_node
)
def _remove_prefix(self, name: str) -> str:
"""Remove the prefix in curly brackets."""
return name.split("}")[-1]

# pylint: disable=invalid-name
def _get_fully_qualified_name(self, subtree: _Element) -> str:
Expand All @@ -157,11 +154,12 @@ def _get_fully_qualified_name(self, subtree: _Element) -> str:
groupID = None
version = None
for child in subtree:
if child.tag == "artifactId":
tag = self._remove_prefix(name=child.tag)
if tag == "artifactId":
artifactID = child.text
if child.tag == "groupId":
if tag == "groupId":
groupID = child.text
if child.tag == "version":
if tag == "version":
version = child.text

if version:
Expand Down
48 changes: 27 additions & 21 deletions tests/cfgnet/plugins/concept/test_maven_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,24 @@ def test_parse_maven_file(get_plugin):

value_nodes = artifact.get_nodes()
ids = {node.id for node in value_nodes}

assert artifact is not None
assert len(value_nodes) == 31

assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate:3.2.5.ga", "version", "3.2.5.ga") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate:3.2.5.ga", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate:3.2.5.ga", "artifactId", "hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:ejb3-persistence:1.0.1.GA", "version", "1.0.1.GA") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:ejb3-persistence:1.0.1.GA", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:ejb3-persistence:1.0.1.GA", "artifactId", "ejb3-persistence") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate-core", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "org.hibernate:hibernate-core", "artifactId", "hibernate-core") in ids
assert len(value_nodes) == 35

assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency", "org.hibernate:hibernate:3.2.5.ga", "version", "3.2.5.ga") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency", "org.hibernate:hibernate:3.2.5.ga", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency", "org.hibernate:hibernate:3.2.5.ga", "artifactId", "hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency", "org.hibernate:ejb3-persistence:1.0.1.GA", "version", "1.0.1.GA") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency", "org.hibernate:ejb3-persistence:1.0.1.GA", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency", "org.hibernate:ejb3-persistence:1.0.1.GA", "artifactId", "ejb3-persistence") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency", "org.hibernate:hibernate-core", "groupId", "org.hibernate") in ids
assert make_id("pom.xml", "project", "dependencyManagement", "dependencies", "dependency", "org.hibernate:hibernate-core", "artifactId", "hibernate-core") in ids

assert make_id("pom.xml", "project", "dependencies", "dependency", "test-dependency:dependencyA", "groupId", "test-dependency") in ids
assert make_id("pom.xml", "project", "dependencies", "dependency", "test-dependency:dependencyA", "artifactId", "dependencyA") in ids
assert make_id("pom.xml", "project", "plugins", "plugin", "test-plugin:pluginA", "groupId", "test-plugin") in ids
assert make_id("pom.xml", "project", "plugins", "plugin", "test-plugin:pluginA", "artifactId", "pluginA") in ids

assert make_id("pom.xml", "project", "modules", "module", "config") in ids
assert make_id("pom.xml", "project", "modelVersion", "4.0.0") in ids
assert make_id("pom.xml", "project", "packaging", "jar") in ids
Expand All @@ -71,16 +77,16 @@ def test_parse_maven_file(get_plugin):
assert make_id("pom.xml", "project", "distributionManagement", "downloadUrl", "http://test/my-project") in ids
assert make_id("pom.xml", "project", "issueManagement", "system", "Bugzilla") in ids
assert make_id("pom.xml", "project", "issueManagement", "url", "http://test/bugzilla/") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "version", "1.4") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "groupId", "org.apache.maven.plugins") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "artifactId", "maven-shade-plugin") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "executions", "execution", "phase", "package") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "configuration", "finalName", "test") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-shade-plugin:1.4", "executions", "execution", "goals", "goal", "shade") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-compiler-plugin", "groupId", "org.apache.maven.plugins") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-compiler-plugin", "artifactId", "maven-compiler-plugin") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-compiler-plugin", "configuration", "source", "1.6") in ids
assert make_id("pom.xml", "project", "build", "plugins", "org.apache.maven.plugins:maven-compiler-plugin", "configuration", "target", "1.6") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-shade-plugin:1.4", "version", "1.4") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-shade-plugin:1.4", "groupId", "org.apache.maven.plugins") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-shade-plugin:1.4", "artifactId", "maven-shade-plugin") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-shade-plugin:1.4", "executions", "execution", "phase", "package") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-shade-plugin:1.4", "configuration", "finalName", "test") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-shade-plugin:1.4", "executions", "execution", "goals", "goal", "shade") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-compiler-plugin", "groupId", "org.apache.maven.plugins") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-compiler-plugin", "artifactId", "maven-compiler-plugin") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-compiler-plugin", "configuration", "source", "1.6") in ids
assert make_id("pom.xml", "project", "build", "plugins", "plugin", "org.apache.maven.plugins:maven-compiler-plugin", "configuration", "target", "1.6") in ids


def test_config_types(get_plugin):
Expand Down
15 changes: 15 additions & 0 deletions tests/files/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@
<module>config</module>
</modules>

<dependencies>
<!-- Spring Boot Web Starter for creating web applications -->
<dependency>
<groupId>test-dependency</groupId>
<artifactId>dependencyA</artifactId>
</dependency>
</dependencies>

<plugins>
<plugin>
<groupId>test-plugin</groupId>
<artifactId>pluginA</artifactId>
</plugin>
</plugins>

<build>
<plugins>
<plugin>
Expand Down

0 comments on commit af9e9ee

Please sign in to comment.