ScriptCollection 4.4.2__py3-none-any.whl → 4.4.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. ScriptCollection/Executables.py +1 -1
  2. ScriptCollection/GeneralUtilities.py +13 -1
  3. ScriptCollection/Resources/CultureChooser/CultureChooser.js +0 -0
  4. ScriptCollection/Resources/CultureChooser/index.html +0 -0
  5. ScriptCollection/Resources/Dependencies/JRE/Version.txt +0 -0
  6. ScriptCollection/Resources/Dependencies/OpenAPIGenerator/Version.txt +0 -0
  7. ScriptCollection/Resources/Dependencies/PlantUML/Version.txt +0 -0
  8. ScriptCollection/Resources/Fonts/DejaVuSans-Bold.ttf +0 -0
  9. ScriptCollection/Resources/Fonts/DejaVuSans-BoldOblique.ttf +0 -0
  10. ScriptCollection/Resources/Fonts/DejaVuSans-Oblique.ttf +0 -0
  11. ScriptCollection/Resources/Fonts/DejaVuSans.ttf +0 -0
  12. ScriptCollection/Resources/Fonts/LICENSE.txt +0 -0
  13. ScriptCollection/Resources/MaintenanceSite/MaintenanceSite.html +0 -0
  14. ScriptCollection/SCLog.py +1 -1
  15. ScriptCollection/ScriptCollectionCore.py +117 -11
  16. ScriptCollection/TFCPS/DotNet/TFCPS_CodeUnitSpecific_DotNet.py +1 -1
  17. ScriptCollection/TFCPS/Flutter/TFCPS_CodeUnitSpecific_Flutter.py +1 -1
  18. ScriptCollection/TFCPS/Go/TFCPS_CodeUnitSpecific_Go.py +128 -47
  19. ScriptCollection/TFCPS/Maven/TFCPS_CodeUnitSpecific_Maven.py +139 -7
  20. ScriptCollection/TFCPS/NodeJS/TFCPS_CodeUnitSpecific_NodeJS.py +8 -2
  21. ScriptCollection/TFCPS/Rust/TFCPS_CodeUnitSpecific_Rust.py +169 -7
  22. ScriptCollection/TFCPS/TFCPS_CodeUnit_BuildCodeUnits.py +2 -2
  23. ScriptCollection/TFCPS/TFCPS_Tools_General.py +126 -9
  24. ScriptCollection/TFCPS/TFCPS_VisualRegressionTests.py +446 -0
  25. ScriptCollection/TFCPS/TypeScript/TFCPS_CodeUnitSpecific_TypeScript.py +188 -0
  26. ScriptCollection/TFCPS/TypeScript/__init__.py +0 -0
  27. ScriptCollection/TestKind.py +41 -0
  28. {scriptcollection-4.4.2.dist-info → scriptcollection-4.4.4.dist-info}/METADATA +168 -168
  29. {scriptcollection-4.4.2.dist-info → scriptcollection-4.4.4.dist-info}/RECORD +21 -17
  30. {scriptcollection-4.4.2.dist-info → scriptcollection-4.4.4.dist-info}/WHEEL +0 -0
  31. {scriptcollection-4.4.2.dist-info → scriptcollection-4.4.4.dist-info}/entry_points.txt +0 -0
  32. {scriptcollection-4.4.2.dist-info → scriptcollection-4.4.4.dist-info}/top_level.txt +0 -0
@@ -1,34 +1,166 @@
1
- from ...GeneralUtilities import GeneralUtilities
1
+ import os
2
+ import re
3
+ import shutil
4
+ import requests
5
+ from lxml import etree
6
+ from ...GeneralUtilities import GeneralUtilities, Dependency
2
7
  from ...SCLog import LogLevel
3
8
  from ..TFCPS_CodeUnitSpecific_Base import TFCPS_CodeUnitSpecific_Base, TFCPS_CodeUnitSpecific_Base_CLI
4
9
 
5
10
  class TFCPS_CodeUnitSpecific_Maven_Functions(TFCPS_CodeUnitSpecific_Base):
11
+ """Implements the tasks of a codeunit which contains a maven-project.
12
+
13
+ The maven-project is expected in "<codeunit>/<codeunit>" and it contains its testcases in its own
14
+ "src/test"-folder, because that is the layout maven defines and the layout jacoco measures the
15
+ coverage of without further configuration."""
16
+
17
+ __pom_namespace: str = "http://maven.apache.org/POM/4.0.0"
6
18
 
7
19
  def __init__(self, current_file: str, verbosity: LogLevel, targetenvironmenttype: str, use_cache: bool, is_pre_merge: bool):
8
20
  super().__init__(current_file, verbosity, targetenvironmenttype, use_cache, is_pre_merge)
9
21
 
22
+ @GeneralUtilities.check_arguments
23
+ def get_maven_project_folder(self) -> str:
24
+ """Returns the folder which contains the pom-file of this codeunit."""
25
+ return os.path.join(self.get_codeunit_folder(), self.get_codeunit_name())
26
+
27
+ @GeneralUtilities.check_arguments
28
+ def get_pom_file(self) -> str:
29
+ return os.path.join(self.get_maven_project_folder(), "pom.xml")
30
+
10
31
  @GeneralUtilities.check_arguments
11
32
  def build(self) -> None:
12
- pass#TODO
33
+ maven_project_folder = self.get_maven_project_folder()
34
+ target_folder = os.path.join(self.get_artifacts_folder(), "BuildResult_Maven")
35
+ GeneralUtilities.ensure_directory_exists(target_folder)
36
+ # The testcases are executed by RunTestcases.py and not by the build, so that a failing testcase is reported
37
+ # by the quality-check and not by the build-step.
38
+ self.__run_maven("-B -DskipTests clean package")
39
+ build_output_folder = os.path.join(maven_project_folder, "target")
40
+ artifacts = [file for file in GeneralUtilities.get_direct_files_of_folder(build_output_folder) if file.endswith(".jar")]
41
+ GeneralUtilities.assert_condition(0 < len(artifacts), f'The maven-build did not produce a jar-file in "{build_output_folder}".')
42
+ for artifact in artifacts:
43
+ shutil.copyfile(artifact, os.path.join(target_folder, os.path.basename(artifact)))
44
+ self.__generate_bom_for_maven_project()
45
+ self.copy_source_files_to_output_directory()
46
+
47
+ @GeneralUtilities.check_arguments
48
+ def __generate_bom_for_maven_project(self) -> None:
49
+ codeunit_version = self.tfcps_Tools_General.get_version_of_codeunit(self.get_codeunit_file())
50
+ bom_folder = os.path.join(self.get_artifacts_folder(), "BOM")
51
+ GeneralUtilities.ensure_directory_exists(bom_folder)
52
+ self.__run_maven("-B org.cyclonedx:cyclonedx-maven-plugin:makeAggregateBom")
53
+ generated_bom_file = os.path.join(self.get_maven_project_folder(), "target", "bom.xml")
54
+ GeneralUtilities.assert_file_exists(generated_bom_file)
55
+ target_bom_file = os.path.join(bom_folder, f"{self.get_codeunit_name()}.{codeunit_version}.bom.xml")
56
+ shutil.copyfile(generated_bom_file, target_bom_file)
57
+ self._protected_sc.format_xml_file(target_bom_file)
13
58
 
14
59
  @GeneralUtilities.check_arguments
15
60
  def linting(self) -> None:
16
- pass#TODO
61
+ # checkstyle is configured in the pom-file of the codeunit, so that the ruleset belongs to the codeunit
62
+ # and not to this implementation.
63
+ self.__run_maven("-B checkstyle:check")
64
+
65
+ @GeneralUtilities.check_arguments
66
+ def do_common_tasks(self, current_codeunit_version: str) -> None:
67
+ self.do_common_tasks_base(current_codeunit_version)
68
+ self.__set_version_in_pom_file(self.get_version_of_project())
69
+
70
+ @GeneralUtilities.check_arguments
71
+ def generate_reference(self) -> None:
72
+ self.generate_reference_using_docfx()
17
73
 
18
74
  @GeneralUtilities.check_arguments
19
75
  def run_testcases(self) -> None:
20
- pass#TODO
76
+ codeunit_name = self.get_codeunit_name()
77
+ # "verify" is used instead of "test" because the report of jacoco is bound to that phase.
78
+ self.__run_maven("-B verify")
79
+ jacoco_file = os.path.join(self.get_maven_project_folder(), "target", "site", "jacoco", "jacoco.xml")
80
+ coverage_file = os.path.join(self.get_artifacts_folder(), "TestCoverage", "TestCoverage.xml")
81
+ GeneralUtilities.ensure_file_does_not_exist(coverage_file)
82
+ self.tfcps_Tools_General.convert_jacoco_report_to_cobertura_report(jacoco_file, coverage_file, codeunit_name, self.get_codeunit_folder(), [f"{codeunit_name}/src/main/java", f"{codeunit_name}/src/test/java"])
83
+ self.run_testcases_common_post_task(self.get_repository_folder(), codeunit_name, True, self.get_target_environment_type())
21
84
 
85
+ @GeneralUtilities.check_arguments
86
+ def __run_maven(self, arguments: str) -> None:
87
+ self._protected_sc.run_program("mvn", arguments, self.get_maven_project_folder(), print_live_output=self.get_verbosity() == LogLevel.Debug)
88
+
89
+ @GeneralUtilities.check_arguments
22
90
  def get_dependencies(self) -> dict[str, set[str]]:
23
- return dict[str, set[str]]()#TODO
91
+ return GeneralUtilities.merge_dependency_lists([self.__get_dependencies_from_pom_file()])
92
+
93
+ @GeneralUtilities.check_arguments
94
+ def __get_dependencies_from_pom_file(self) -> list[Dependency]:
95
+ root: etree._ElementTree = etree.parse(self.get_pom_file())
96
+ namespaces = {"pom": TFCPS_CodeUnitSpecific_Maven_Functions.__pom_namespace}
97
+ result: list[Dependency] = []
98
+ for dependency in root.xpath("//pom:dependencies/pom:dependency", namespaces=namespaces):
99
+ group_id = dependency.xpath("./pom:groupId/text()", namespaces=namespaces)
100
+ artifact_id = dependency.xpath("./pom:artifactId/text()", namespaces=namespaces)
101
+ dependency_version = dependency.xpath("./pom:version/text()", namespaces=namespaces)
102
+ if len(group_id) == 0 or len(artifact_id) == 0:
103
+ continue
104
+ if len(dependency_version) == 0:
105
+ # The version of such a dependency comes from a parent-pom or from a dependency-management-section,
106
+ # so it can not be updated here.
107
+ continue
108
+ result.append(Dependency(f"{group_id[0]}:{artifact_id[0]}", str(dependency_version[0])))
109
+ return result
24
110
 
25
111
  @GeneralUtilities.check_arguments
26
112
  def get_available_versions(self, dependencyname: str) -> list[str]:
27
- return []#TODO
113
+ group_id, artifact_id = TFCPS_CodeUnitSpecific_Maven_Functions.__split_dependency_name(dependencyname)
114
+ # The maven-central-search-api answers with the released versions of an artifact, newest first.
115
+ response = requests.get("https://search.maven.org/solrsearch/select", params={"q": f'g:"{group_id}" AND a:"{artifact_id}"', "core": "gav", "rows": "200", "wt": "json"}, timeout=60)
116
+ response.raise_for_status()
117
+ result: list[str] = []
118
+ for found_artifact in response.json()["response"]["docs"]:
119
+ found_version = str(found_artifact["v"])
120
+ if re.match(r"^(\d+)\.(\d+)\.(\d+)$", found_version) is not None:
121
+ result.append(found_version)
122
+ elif re.match(r"^(\d+)\.(\d+)$", found_version) is not None:
123
+ result.append(found_version+".0")
124
+ elif re.match(r"^(\d+)$", found_version) is not None:
125
+ result.append(found_version+".0.0")
126
+ return result
28
127
 
29
128
  @GeneralUtilities.check_arguments
30
129
  def set_dependency_version(self, name: str, new_version: str) -> None:
31
- raise ValueError("Operation is not implemented.")
130
+ group_id, artifact_id = TFCPS_CodeUnitSpecific_Maven_Functions.__split_dependency_name(name)
131
+ pom_file = self.get_pom_file()
132
+ root: etree._ElementTree = etree.parse(pom_file)
133
+ namespaces = {"pom": TFCPS_CodeUnitSpecific_Maven_Functions.__pom_namespace}
134
+ for dependency in root.xpath("//pom:dependencies/pom:dependency", namespaces=namespaces):
135
+ group_id_of_dependency = dependency.xpath("./pom:groupId/text()", namespaces=namespaces)
136
+ artifact_id_of_dependency = dependency.xpath("./pom:artifactId/text()", namespaces=namespaces)
137
+ version_element = dependency.xpath("./pom:version", namespaces=namespaces)
138
+ if len(group_id_of_dependency) == 0 or len(artifact_id_of_dependency) == 0 or len(version_element) == 0:
139
+ continue
140
+ if str(group_id_of_dependency[0]) == group_id and str(artifact_id_of_dependency[0]) == artifact_id:
141
+ version_element[0].text = new_version
142
+ root.write(pom_file, pretty_print=True, xml_declaration=True, encoding="UTF-8")
143
+
144
+ @GeneralUtilities.check_arguments
145
+ def __set_version_in_pom_file(self, new_version: str) -> None:
146
+ pom_file = self.get_pom_file()
147
+ root: etree._ElementTree = etree.parse(pom_file)
148
+ namespaces = {"pom": TFCPS_CodeUnitSpecific_Maven_Functions.__pom_namespace}
149
+ # Only the version of the project itself is set here. A version-element inside a dependency is not touched,
150
+ # which is why the version-element is taken from the project-element directly and not searched anywhere.
151
+ version_elements = root.xpath("/pom:project/pom:version", namespaces=namespaces)
152
+ GeneralUtilities.assert_condition(len(version_elements) == 1, f'The pom-file "{pom_file}" must contain exactly one version-element directly in its project-element.')
153
+ version_elements[0].text = new_version
154
+ root.write(pom_file, pretty_print=True, xml_declaration=True, encoding="UTF-8")
155
+
156
+ @staticmethod
157
+ @GeneralUtilities.check_arguments
158
+ def __split_dependency_name(dependencyname: str) -> tuple[str, str]:
159
+ """A maven-dependency is identified by its group-id and its artifact-id, so both of them together are used as
160
+ dependency-name in the format "<group-id>:<artifact-id>"."""
161
+ parts = dependencyname.split(":")
162
+ GeneralUtilities.assert_condition(len(parts) == 2, f'The name of a maven-dependency must have the format "<group-id>:<artifact-id>" but was "{dependencyname}".')
163
+ return (parts[0], parts[1])
32
164
 
33
165
  class TFCPS_CodeUnitSpecific_Maven_CLI:
34
166
 
@@ -18,6 +18,12 @@ class TFCPS_CodeUnitSpecific_NodeJS_Functions(TFCPS_CodeUnitSpecific_Base):
18
18
 
19
19
  @GeneralUtilities.check_arguments
20
20
  def build(self) -> None:
21
+ # The sourcecode-artifact of a previous run is removed before the build, because it is a copy of the
22
+ # codeunit inside the codeunit and the build-tools do not know that it is only an artifact. An
23
+ # angular-library for example is built by ng-packagr, which searches the whole project-folder for
24
+ # "ng-package.json"-files to find the entry-points of the package, finds the copied one and aborts. The
25
+ # copy is written again at the end of this method, so nothing is lost.
26
+ GeneralUtilities.ensure_directory_does_not_exist(os.path.join(self.get_codeunit_folder(), "Other", "Artifacts", "SourceCode"))
21
27
  self._protected_sc.run_with_epew("npm", "run build", self.get_codeunit_folder(),print_live_output=self.get_verbosity()==LogLevel.Debug,encode_argument_in_base64=True)
22
28
  self.standardized_tasks_build_bom_for_node_project()
23
29
  self.copy_source_files_to_output_directory()
@@ -25,13 +31,13 @@ class TFCPS_CodeUnitSpecific_NodeJS_Functions(TFCPS_CodeUnitSpecific_Base):
25
31
  @GeneralUtilities.check_arguments
26
32
  def linting(self) -> None:
27
33
  codeunit_folder = self.get_codeunit_folder()
28
- self._protected_sc.normalize_line_endings_of_files_in_folder(codeunit_folder, ["ts", "js", "css", "scss", "sass"])
34
+ self._protected_sc.normalize_invisible_characters_of_files_in_folder(codeunit_folder, ["ts", "js", "css", "scss", "sass"])
29
35
  # The OpenAPI-generator writes its '.openapi-generator/FILES' metadata-file with the host's native
30
36
  # line-endings (CRLF on Windows, LF on Linux). Normalize it to LF like the other generated text-files
31
37
  # so the committed file does not drift between build-hosts.
32
38
  for openapi_generator_files_file in self._protected_sc.get_not_git_ignored_files_of_folder(codeunit_folder):
33
39
  if os.path.basename(openapi_generator_files_file) == "FILES" and os.path.basename(os.path.dirname(openapi_generator_files_file)) == ".openapi-generator":
34
- self._protected_sc.normalize_line_endings(openapi_generator_files_file)
40
+ self._protected_sc.normalize_invisible_characters(openapi_generator_files_file)
35
41
  for html_file in self._protected_sc.get_not_git_ignored_files_of_folder(codeunit_folder, ".html"):
36
42
  self._protected_sc.format_html_file(html_file, os.path.basename(html_file) == "index.html")
37
43
  self._protected_sc.run_with_epew("npm", "run lint", codeunit_folder,print_live_output=self.get_verbosity()==LogLevel.Debug,encode_argument_in_base64=True)
@@ -1,34 +1,196 @@
1
- from ...GeneralUtilities import GeneralUtilities
1
+ import os
2
+ import re
3
+ import shutil
4
+ import requests
5
+ from lxml import etree
6
+ from ...GeneralUtilities import GeneralUtilities, Dependency
2
7
  from ...SCLog import LogLevel
3
8
  from ..TFCPS_CodeUnitSpecific_Base import TFCPS_CodeUnitSpecific_Base, TFCPS_CodeUnitSpecific_Base_CLI
4
9
 
5
10
  class TFCPS_CodeUnitSpecific_Rust_Functions(TFCPS_CodeUnitSpecific_Base):
11
+ """Implements the tasks of a codeunit which contains a cargo-project.
12
+
13
+ The cargo-project is expected in "<codeunit>/<codeunit>" and it contains its testcases in the same
14
+ project, because that is the layout cargo defines: unit-tests live next to the code they test and
15
+ integration-tests live in the "tests"-folder of the project."""
6
16
 
7
17
  def __init__(self, current_file: str, verbosity: LogLevel, targetenvironmenttype: str, use_cache: bool, is_pre_merge: bool):
8
18
  super().__init__(current_file, verbosity, targetenvironmenttype, use_cache, is_pre_merge)
9
19
 
20
+ @GeneralUtilities.check_arguments
21
+ def get_cargo_project_folder(self) -> str:
22
+ """Returns the folder which contains the Cargo.toml-file of this codeunit."""
23
+ return os.path.join(self.get_codeunit_folder(), self.get_codeunit_name())
24
+
25
+ @GeneralUtilities.check_arguments
26
+ def get_cargo_file(self) -> str:
27
+ return os.path.join(self.get_cargo_project_folder(), "Cargo.toml")
28
+
10
29
  @GeneralUtilities.check_arguments
11
30
  def build(self) -> None:
12
- pass#TODO
31
+ target_folder = os.path.join(self.get_artifacts_folder(), "BuildResult_Rust")
32
+ GeneralUtilities.ensure_directory_exists(target_folder)
33
+ self.__run_cargo("build --release")
34
+ # cargo puts the executable and the library of the release-profile directly into "target/release"; everything
35
+ # below that folder is intermediate build-output and is therefore not part of the artifact.
36
+ build_output_folder = os.path.join(self.get_cargo_project_folder(), "target", "release")
37
+ artifacts = [file for file in GeneralUtilities.get_direct_files_of_folder(build_output_folder) if TFCPS_CodeUnitSpecific_Rust_Functions.__file_is_build_artifact(file)]
38
+ GeneralUtilities.assert_condition(0 < len(artifacts), f'The cargo-build did not produce an artifact in "{build_output_folder}".')
39
+ for artifact in artifacts:
40
+ shutil.copyfile(artifact, os.path.join(target_folder, os.path.basename(artifact)))
41
+ self.__generate_bom_for_cargo_project()
42
+ self.copy_source_files_to_output_directory()
43
+
44
+ @staticmethod
45
+ @GeneralUtilities.check_arguments
46
+ def __file_is_build_artifact(file: str) -> bool:
47
+ # cargo writes further files (for example ".d"-dependency-files and the "*.pdb"-symbols on windows) next to the
48
+ # executable and the library, so the artifact is selected by its extension instead of taking everything.
49
+ extensions_of_artifacts = ("", ".exe", ".dll", ".so", ".dylib", ".a", ".lib", ".rlib")
50
+ return os.path.splitext(file)[1] in extensions_of_artifacts
51
+
52
+ @GeneralUtilities.check_arguments
53
+ def __generate_bom_for_cargo_project(self) -> None:
54
+ codeunit_version = self.tfcps_Tools_General.get_version_of_codeunit(self.get_codeunit_file())
55
+ bom_folder = os.path.join(self.get_artifacts_folder(), "BOM")
56
+ GeneralUtilities.ensure_directory_exists(bom_folder)
57
+ target_bom_file = os.path.join(bom_folder, f"{self.get_codeunit_name()}.{codeunit_version}.bom.xml")
58
+ self.__run_cargo("cyclonedx --format xml")
59
+ # cargo-cyclonedx writes its result into the folder of the cargo-project and derives the filename from the name
60
+ # of the crate, so the generated file is searched instead of being predicted from the name of the codeunit.
61
+ generated_bom_files = [file for file in GeneralUtilities.get_direct_files_of_folder(self.get_cargo_project_folder()) if file.endswith(".xml")]
62
+ GeneralUtilities.assert_condition(len(generated_bom_files) == 1, f'Expected exactly one generated bill-of-materials in "{self.get_cargo_project_folder()}" but found {len(generated_bom_files)}.')
63
+ shutil.move(generated_bom_files[0], target_bom_file)
64
+ self._protected_sc.format_xml_file(target_bom_file)
13
65
 
14
66
  @GeneralUtilities.check_arguments
15
67
  def linting(self) -> None:
16
- pass#TODO
68
+ # "--all-targets" includes the testcases in the check, and "-D warnings" makes clippy exit with a non-zero
69
+ # exit-code for a warning as well, because a warning is a linting-issue too.
70
+ self.__run_cargo("clippy --all-targets --all-features -- -D warnings")
71
+
72
+ @GeneralUtilities.check_arguments
73
+ def do_common_tasks(self, current_codeunit_version: str) -> None:
74
+ self.do_common_tasks_base(current_codeunit_version)
75
+ self.__set_version_in_cargo_file(self.get_version_of_project())
76
+
77
+ @GeneralUtilities.check_arguments
78
+ def generate_reference(self) -> None:
79
+ self.generate_reference_using_docfx()
17
80
 
18
81
  @GeneralUtilities.check_arguments
19
82
  def run_testcases(self) -> None:
20
- pass#TODO
83
+ codeunit_name = self.get_codeunit_name()
84
+ coverage_file = os.path.join(self.get_artifacts_folder(), "TestCoverage", "TestCoverage.xml")
85
+ GeneralUtilities.ensure_directory_exists(os.path.dirname(coverage_file))
86
+ GeneralUtilities.ensure_file_does_not_exist(coverage_file)
87
+ # cargo-llvm-cov runs the testcases and writes the coverage-report in the cobertura-format directly, so no
88
+ # conversion of the report-format is required here.
89
+ self.__run_cargo(f"llvm-cov --all-features --cobertura --output-path {coverage_file}")
90
+ GeneralUtilities.assert_file_exists(coverage_file)
91
+ self.__normalize_coverage_file(coverage_file)
92
+ self.run_testcases_common_post_task(self.get_repository_folder(), codeunit_name, True, self.get_target_environment_type())
21
93
 
94
+ @GeneralUtilities.check_arguments
95
+ def __normalize_coverage_file(self, coverage_file: str) -> None:
96
+ """Makes the coverage-report which cargo-llvm-cov wrote match the expectations of the further processing:
97
+ it must contain exactly one package which is named like the codeunit, and the filename of a class must be
98
+ relative to the codeunit-folder (cargo-llvm-cov writes it relative to the cargo-project-folder)."""
99
+ codeunit_name = self.get_codeunit_name()
100
+ tree = etree.parse(coverage_file)
101
+ root = tree.getroot()
102
+ for package in root.findall(".//package"):
103
+ package.set("name", codeunit_name)
104
+ for class_element in root.findall(".//class"):
105
+ filename = class_element.get("filename")
106
+ if filename is not None:
107
+ filename_with_slashes = filename.replace("\\", "/")
108
+ class_element.set("filename", f"{codeunit_name}/{filename_with_slashes}")
109
+ class_element.set("name", os.path.basename(filename_with_slashes))
110
+ tree.write(coverage_file, pretty_print=True, xml_declaration=True, encoding="UTF-8")
111
+ self.tfcps_Tools_General.merge_packages(coverage_file, codeunit_name)
112
+
113
+ @GeneralUtilities.check_arguments
114
+ def __run_cargo(self, arguments: str) -> None:
115
+ self._protected_sc.run_program("cargo", arguments, self.get_cargo_project_folder(), print_live_output=self.get_verbosity() == LogLevel.Debug)
116
+
117
+ @GeneralUtilities.check_arguments
22
118
  def get_dependencies(self) -> dict[str, set[str]]:
23
- return dict[str, set[str]]()#TODO
119
+ return GeneralUtilities.merge_dependency_lists([self.__get_dependencies_from_cargo_file()])
120
+
121
+ @GeneralUtilities.check_arguments
122
+ def __get_dependencies_from_cargo_file(self) -> list[Dependency]:
123
+ result: list[Dependency] = []
124
+ for section, name, dependency_version in self.__enumerate_dependency_entries():
125
+ GeneralUtilities.assert_condition(GeneralUtilities.string_has_content(section), "Internal error while reading the dependencies.")
126
+ result.append(Dependency(name, dependency_version))
127
+ return result
128
+
129
+ @GeneralUtilities.check_arguments
130
+ def __enumerate_dependency_entries(self) -> list[tuple[str, str, str]]:
131
+ """Returns the dependencies of the cargo-file as tuples of the section they are declared in, their name and
132
+ their version. Only a dependency which declares a plain version-string is returned, because a dependency which
133
+ is declared as a table (for example a path- or git-dependency) has no version which could be updated."""
134
+ dependency_sections = ("dependencies", "dev-dependencies", "build-dependencies")
135
+ current_section: str = None
136
+ result: list[tuple[str, str, str]] = []
137
+ for line in GeneralUtilities.read_lines_from_file(self.get_cargo_file()):
138
+ stripped_line = line.strip()
139
+ if stripped_line.startswith("[") and stripped_line.endswith("]"):
140
+ current_section = stripped_line[1:-1].strip()
141
+ continue
142
+ if current_section not in dependency_sections or not GeneralUtilities.string_has_content(stripped_line) or stripped_line.startswith("#"):
143
+ continue
144
+ match = re.match(r'^([A-Za-z0-9_\-]+)\s*=\s*"([^"]+)"$', stripped_line)
145
+ if match is not None:
146
+ result.append((current_section, match.group(1), match.group(2)))
147
+ return result
24
148
 
25
149
  @GeneralUtilities.check_arguments
26
150
  def get_available_versions(self, dependencyname: str) -> list[str]:
27
- return []#TODO
151
+ # The crates.io-api answers with all published versions of a crate.
152
+ response = requests.get(f"https://crates.io/api/v1/crates/{dependencyname}/versions", headers={"User-Agent": "ScriptCollection"}, timeout=60)
153
+ response.raise_for_status()
154
+ result: list[str] = []
155
+ for found_version in response.json()["versions"]:
156
+ if found_version.get("yanked", False):
157
+ continue
158
+ version_string = str(found_version["num"])
159
+ if re.match(r"^(\d+)\.(\d+)\.(\d+)$", version_string) is not None:
160
+ result.append(version_string)
161
+ return result
28
162
 
29
163
  @GeneralUtilities.check_arguments
30
164
  def set_dependency_version(self, name: str, new_version: str) -> None:
31
- raise ValueError("Operation is not implemented.")
165
+ cargo_file = self.get_cargo_file()
166
+ new_lines: list[str] = []
167
+ for line in GeneralUtilities.read_lines_from_file(cargo_file):
168
+ match = re.match(r'^(\s*)('+re.escape(name)+r')(\s*=\s*)"([^"]+)"(\s*)$', line)
169
+ if match is None:
170
+ new_lines.append(line)
171
+ else:
172
+ new_lines.append(f'{match.group(1)}{match.group(2)}{match.group(3)}"{new_version}"')
173
+ GeneralUtilities.write_lines_to_file(cargo_file, new_lines)
174
+
175
+ @GeneralUtilities.check_arguments
176
+ def __set_version_in_cargo_file(self, new_version: str) -> None:
177
+ cargo_file = self.get_cargo_file()
178
+ new_lines: list[str] = []
179
+ is_in_package_section: bool = False
180
+ version_was_set: bool = False
181
+ for line in GeneralUtilities.read_lines_from_file(cargo_file):
182
+ stripped_line = line.strip()
183
+ if stripped_line.startswith("[") and stripped_line.endswith("]"):
184
+ is_in_package_section = stripped_line[1:-1].strip() == "package"
185
+ # Only the version in the package-section is the version of the project itself; a version in a
186
+ # dependency-section belongs to a dependency and must not be changed here.
187
+ if is_in_package_section and re.match(r'^version\s*=\s*"[^"]+"$', stripped_line) is not None:
188
+ new_lines.append(f'version = "{new_version}"')
189
+ version_was_set = True
190
+ else:
191
+ new_lines.append(line)
192
+ GeneralUtilities.assert_condition(version_was_set, f'The cargo-file "{cargo_file}" does not contain a version in its package-section.')
193
+ GeneralUtilities.write_lines_to_file(cargo_file, new_lines)
32
194
 
33
195
  class TFCPS_CodeUnitSpecific_Rust_CLI:
34
196
 
@@ -250,7 +250,7 @@ class TFCPS_CodeUnit_BuildCodeUnits:
250
250
  self.sc.format_json_file(workspace_file)
251
251
 
252
252
  #TODO add option to define exceptions (means: files which should not be normalized).
253
- self.sc.normalize_line_endings_of_files_in_folder(self.repository, ["txt", "md", "py", "json", "xml", "svg", "csv", "yml", "yaml", "toml","gitignore", "gitattributes", "code-workspace"])
253
+ self.sc.normalize_invisible_characters_of_files_in_folder(self.repository, ["txt", "md", "py", "json", "xml", "svg", "csv", "yml", "yaml", "toml","gitignore", "gitattributes", "code-workspace"])
254
254
 
255
255
  @GeneralUtilities.check_arguments
256
256
  def is_working_branch(self)->bool:
@@ -461,7 +461,7 @@ class TFCPS_CodeUnit_BuildCodeUnits:
461
461
  loc=splitted[2]
462
462
  csv_lines.append(f"{v},{t},{loc}")
463
463
  GeneralUtilities.write_lines_to_file(loc_data_file,csv_lines)
464
- self.sc.normalize_line_endings(loc_data_file) # ensure the generated LoC-diagram-csv always uses LF line-endings
464
+ self.sc.normalize_invisible_characters(loc_data_file) # ensure the generated LoC-diagram-csv always uses LF line-endings
465
465
  diagram_json = {
466
466
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
467
467
  "description": "Lines of Code over time",
@@ -1078,6 +1078,20 @@ class TFCPS_Tools_General:
1078
1078
  return title+".svg"
1079
1079
  return Path(plantuml_file).stem+".svg"
1080
1080
 
1081
+ @staticmethod
1082
+ @GeneralUtilities.check_arguments
1083
+ def __get_plantuml_alias_for_codeunit(codeunitname: str) -> str:
1084
+ """Returns the identifier by which the codeunit is referenced inside the generated plantuml-diagram.
1085
+
1086
+ A codeunit-name is allowed to contain characters which plantuml does not accept in an identifier (for
1087
+ example the hyphens of a name written in kebab-case, which is usual for an npm-package). Such a name is
1088
+ therefore only used as the displayed label while the references use the returned alias. Names which are
1089
+ valid identifiers are returned unchanged, so the generated diagram of a codeunit which does not need an
1090
+ alias stays exactly as it was."""
1091
+ if re.fullmatch("[A-Za-z0-9_]+", codeunitname) is not None:
1092
+ return codeunitname
1093
+ return "codeunit_"+re.sub("[^A-Za-z0-9_]", "_", codeunitname)
1094
+
1081
1095
  @GeneralUtilities.check_arguments
1082
1096
  def generate_codeunits_overview_diagram(self, repository_folder: str) -> None:
1083
1097
  self.__sc.log.log("Generate Codeunits-overview-diagram...")
@@ -1098,19 +1112,23 @@ class TFCPS_Tools_General:
1098
1112
 
1099
1113
  description = self.get_codeunit_description(codeunit_file)
1100
1114
 
1115
+ alias = TFCPS_Tools_General.__get_plantuml_alias_for_codeunit(codeunitname)
1101
1116
  lines.append(GeneralUtilities.empty_string)
1102
- lines.append(f"[{codeunitname}]")
1103
- lines.append(f"note as {codeunitname}Note")
1117
+ if alias == codeunitname:
1118
+ lines.append(f"[{codeunitname}]")
1119
+ else:
1120
+ lines.append(f"[{codeunitname}] as {alias}")
1121
+ lines.append(f"note as {alias}Note")
1104
1122
  lines.append(f" {description}")
1105
1123
  lines.append(f"end note")
1106
- lines.append(f"{codeunitname} .. {codeunitname}Note")
1124
+ lines.append(f"{alias} .. {alias}Note")
1107
1125
 
1108
1126
  lines.append(GeneralUtilities.empty_string)
1109
1127
  for codeunitname in codeunits:
1110
1128
  codeunit_file: str = os.path.join(repository_folder, codeunitname, f"{codeunitname}.codeunit.xml")
1111
1129
  dependent_codeunits = self.get_dependent_code_units(codeunit_file)
1112
1130
  for dependent_codeunit in dependent_codeunits:
1113
- lines.append(f"{codeunitname} --> {dependent_codeunit}")
1131
+ lines.append(f"{TFCPS_Tools_General.__get_plantuml_alias_for_codeunit(codeunitname)} --> {TFCPS_Tools_General.__get_plantuml_alias_for_codeunit(dependent_codeunit)}")
1114
1132
 
1115
1133
  lines.append(GeneralUtilities.empty_string)
1116
1134
  lines.append("@enduml")
@@ -1504,9 +1522,106 @@ class TFCPS_Tools_General:
1504
1522
  GeneralUtilities.copy_content_of_folder(source_folder, target_folder)
1505
1523
 
1506
1524
 
1525
+ @GeneralUtilities.check_arguments
1526
+ def convert_jacoco_report_to_cobertura_report(self, jacoco_file: str, cobertura_file: str, package_name: str, codeunit_folder: str, source_folders_relative_to_codeunit: list[str]) -> None:
1527
+ """Converts a coverage-report in the jacoco-format into a coverage-report in the cobertura-format.
1528
+ This is required because the tools of the jvm-ecosystem write jacoco-reports while the further processing of a
1529
+ coverage-report (the check of the coverage-threshold, the html-report and the badges) expects the cobertura-format.
1530
+ The resulting report contains exactly one package (named package_name) and the filename of each class is relative
1531
+ to the codeunit-folder, which is what the further processing expects. source_folders_relative_to_codeunit contains
1532
+ the source-folders in which the source-files of the report are searched (for example the main- and the test-source-folder);
1533
+ the first folder which actually contains the file wins, and a file which does not exist in any of them is skipped."""
1534
+ GeneralUtilities.assert_file_exists(jacoco_file)
1535
+ jacoco_root = etree.parse(jacoco_file).getroot()
1536
+ coverage_element = etree.Element("coverage")
1537
+ sources_element = etree.SubElement(coverage_element, "sources")
1538
+ etree.SubElement(sources_element, "source").text = codeunit_folder.replace("\\", "/")
1539
+ packages_element = etree.SubElement(coverage_element, "packages")
1540
+ package_element = etree.SubElement(packages_element, "package", name=package_name)
1541
+ classes_element = etree.SubElement(package_element, "classes")
1542
+ for jacoco_package in jacoco_root.findall("./package"):
1543
+ package_path = jacoco_package.get("name") # jacoco writes the package as path, for example "com/example"
1544
+ for jacoco_sourcefile in jacoco_package.findall("./sourcefile"):
1545
+ sourcefile_name = jacoco_sourcefile.get("name")
1546
+ relative_file: str = None
1547
+ for source_folder in source_folders_relative_to_codeunit:
1548
+ candidate = f"{source_folder}/{package_path}/{sourcefile_name}"
1549
+ if os.path.isfile(os.path.join(codeunit_folder, candidate)):
1550
+ relative_file = candidate
1551
+ break
1552
+ if relative_file is None:
1553
+ # A sourcefile which does not exist in any of the given source-folders is generated code or belongs to
1554
+ # another codeunit. Keeping it would let the further processing fail when it verifies that every file
1555
+ # of the report exists, so it is left out here where the reason for it is known.
1556
+ self.__sc.log.log(f"Sourcefile \"{package_path}/{sourcefile_name}\" of the jacoco-report was not found in the codeunit and is therefore not part of the cobertura-report.", LogLevel.Debug)
1557
+ continue
1558
+ class_element = etree.SubElement(classes_element, "class", name=sourcefile_name, filename=relative_file)
1559
+ etree.SubElement(class_element, "methods")
1560
+ lines_element = etree.SubElement(class_element, "lines")
1561
+ for jacoco_line in jacoco_sourcefile.findall("./line"):
1562
+ covered_instructions = int(jacoco_line.get("ci", "0"))
1563
+ covered_branches = int(jacoco_line.get("cb", "0"))
1564
+ missed_branches = int(jacoco_line.get("mb", "0"))
1565
+ amount_of_branches = covered_branches+missed_branches
1566
+ line_element = etree.SubElement(lines_element, "line", number=jacoco_line.get("nr"), hits=str(covered_instructions))
1567
+ if 0 < amount_of_branches:
1568
+ line_element.set("branch", "true")
1569
+ line_element.set("condition-coverage", f"{round(covered_branches/amount_of_branches*100)}% ({covered_branches}/{amount_of_branches})")
1570
+ else:
1571
+ line_element.set("branch", "false")
1572
+ GeneralUtilities.ensure_directory_exists(os.path.dirname(cobertura_file))
1573
+ etree.ElementTree(coverage_element).write(cobertura_file, pretty_print=True, xml_declaration=True, encoding="UTF-8")
1574
+ self.calculate_entire_line_rate(cobertura_file)
1575
+
1576
+ @GeneralUtilities.check_arguments
1577
+ def convert_go_coverprofile_to_cobertura_report(self, coverprofile_file: str, cobertura_file: str, package_name: str, codeunit_folder: str, go_module_name: str, source_folder_relative_to_codeunit: str) -> None:
1578
+ """Converts a coverage-report in the coverprofile-format which "go test -coverprofile" writes into a
1579
+ coverage-report in the cobertura-format.
1580
+ The conversion is done here instead of by a third-party-tool because such a tool would have to be installed
1581
+ during the build (which makes the build depend on the state of a foreign repository at that moment) and because
1582
+ the tools for it read their input from stdin, which is not available on every platform in the same way.
1583
+ A line of a coverprofile-file looks like "<go-module>/<file>:<startline>.<startcolumn>,<endline>.<endcolumn> <amount-of-statements> <amount-of-executions>"
1584
+ and describes a block of statements, so every line of such a block gets the amount of executions of its block.
1585
+ If a line is covered by several blocks then the highest amount of executions wins, because the line was executed
1586
+ as soon as one of the blocks containing it was executed."""
1587
+ GeneralUtilities.assert_file_exists(coverprofile_file)
1588
+ hits_per_file: dict[str, dict[int, int]] = {}
1589
+ for line in GeneralUtilities.read_lines_from_file(coverprofile_file):
1590
+ stripped_line = line.strip()
1591
+ if not GeneralUtilities.string_has_content(stripped_line) or stripped_line.startswith("mode:"):
1592
+ continue
1593
+ match = re.match(r"^(.+):(\d+)\.\d+,(\d+)\.\d+ \d+ (\d+)$", stripped_line)
1594
+ GeneralUtilities.assert_condition(match is not None, f'Unparsable line in the coverprofile-file: "{stripped_line}"')
1595
+ file_in_module = match.group(1)
1596
+ if file_in_module.startswith(f"{go_module_name}/"):
1597
+ file_in_module = file_in_module[len(go_module_name)+1:]
1598
+ relative_file = f"{source_folder_relative_to_codeunit}/{file_in_module}"
1599
+ if not os.path.isfile(os.path.join(codeunit_folder, relative_file)):
1600
+ self.__sc.log.log(f'File "{relative_file}" of the coverprofile-file was not found in the codeunit and is therefore not part of the cobertura-report.', LogLevel.Debug)
1601
+ continue
1602
+ hits_of_file = hits_per_file.setdefault(relative_file, {})
1603
+ amount_of_executions = int(match.group(4))
1604
+ for line_number in range(int(match.group(2)), int(match.group(3))+1):
1605
+ hits_of_file[line_number] = max(hits_of_file.get(line_number, 0), amount_of_executions)
1606
+ coverage_element = etree.Element("coverage")
1607
+ sources_element = etree.SubElement(coverage_element, "sources")
1608
+ etree.SubElement(sources_element, "source").text = codeunit_folder.replace("\\", "/")
1609
+ packages_element = etree.SubElement(coverage_element, "packages")
1610
+ package_element = etree.SubElement(packages_element, "package", name=package_name)
1611
+ classes_element = etree.SubElement(package_element, "classes")
1612
+ for relative_file, hits_of_file in sorted(hits_per_file.items()):
1613
+ class_element = etree.SubElement(classes_element, "class", name=os.path.basename(relative_file), filename=relative_file)
1614
+ etree.SubElement(class_element, "methods")
1615
+ lines_element = etree.SubElement(class_element, "lines")
1616
+ for line_number, hits in sorted(hits_of_file.items()):
1617
+ etree.SubElement(lines_element, "line", number=str(line_number), hits=str(hits), branch="false")
1618
+ GeneralUtilities.ensure_directory_exists(os.path.dirname(cobertura_file))
1619
+ etree.ElementTree(coverage_element).write(cobertura_file, pretty_print=True, xml_declaration=True, encoding="UTF-8")
1620
+ self.calculate_entire_line_rate(cobertura_file)
1621
+
1507
1622
  @GeneralUtilities.check_arguments
1508
1623
  def merge_packages(self,coverage_file:str,package_name:str) -> None:
1509
- tree = etree.parse(coverage_file)
1624
+ tree = etree.parse(coverage_file)
1510
1625
  root = tree.getroot()
1511
1626
  packages = root.findall("./packages/package")
1512
1627
  all_classes = []
@@ -2035,10 +2150,12 @@ class TFCPS_Tools_General:
2035
2150
  normalized_line_splitted = ' '.join(line.split()).split(" ")
2036
2151
  package = normalized_line_splitted[0]
2037
2152
  latest_version = normalized_line_splitted[3]
2038
- if package in package_json_content["dependencies"]:
2039
- package_json_content["dependencies"][package] = latest_version
2040
- if package in package_json_content["devDependencies"]:
2041
- package_json_content["devDependencies"][package] = latest_version
2153
+ # A package.json does not have to declare both sections (a project which only delivers
2154
+ # runtime-dependencies has no "devDependencies" at all), so the sections are looked up
2155
+ # instead of being accessed directly.
2156
+ for dependency_section in ["dependencies", "devDependencies"]:
2157
+ if dependency_section in package_json_content and package in package_json_content[dependency_section]:
2158
+ package_json_content[dependency_section][package] = latest_version
2042
2159
  with open(package_json_file, "w", encoding="utf-8") as package_json_file_object:
2043
2160
  json.dump(package_json_content, package_json_file_object, indent=4)
2044
2161
  GeneralUtilities.write_text_to_file(package_json_file, GeneralUtilities.read_text_from_file(package_json_file).replace("\r", ""))