ScriptCollection 4.4.2__py3-none-any.whl → 4.4.6__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.
@@ -36,6 +36,16 @@ class TFCPS_UpdateDependenciesHook(TFCPS_BuildCodeUnitsHook):
36
36
  if os.path.isfile(os.path.join(update_dependencies_script_folder, "UpdateDependencies.py")):
37
37
  self.__sc.log.log("Update dependencies of the repository...")
38
38
  self.__sc.run_program(GeneralUtilities.get_python_executable(), "UpdateDependencies.py", update_dependencies_script_folder)
39
+ self.__ensure_changelog_is_there(repository)
40
+
41
+ @GeneralUtilities.check_arguments
42
+ def __ensure_changelog_is_there(self, repository: str) -> None:
43
+ if not self.__sc.git_repository_has_uncommitted_changes(repository):
44
+ return
45
+ changelog_file: str = os.path.join(repository, "Other", "Resources", "Changelog", f"v{self.__tfcps_tools_general.get_version_of_project(repository)}.md")
46
+ if os.path.isfile(changelog_file):
47
+ return
48
+ self.__tfcps_tools_general.create_changelog_entry(repository, "Updated dependencies.", False, False)
39
49
 
40
50
  @GeneralUtilities.check_arguments
41
51
  def run_after_codeunit_was_built(self, codeunit_build: TFCPS_CodeUnit_BuildCodeUnit) -> None:
@@ -46,6 +56,7 @@ class TFCPS_UpdateDependenciesHook(TFCPS_BuildCodeUnitsHook):
46
56
  return
47
57
  self.__sc.log.log(f"Update dependencies of codeunit {codeunit_name}...")
48
58
  self.__sc.run_program(GeneralUtilities.get_python_executable(), "UpdateDependencies.py", os.path.join(codeunit_folder, "Other"))
59
+ self.__ensure_changelog_is_there(repository)
49
60
  if self.__sc.git_repository_has_uncommitted_changes(repository):
50
61
  #the update changed something, so it has to be verified that the codeunit is still buildable with the updated dependencies.
51
62
  self.__sc.log.log(f"Build codeunit {codeunit_name} again to verify it is still buildable with the updated dependencies...")
@@ -250,7 +261,7 @@ class TFCPS_CodeUnit_BuildCodeUnits:
250
261
  self.sc.format_json_file(workspace_file)
251
262
 
252
263
  #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"])
264
+ 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
265
 
255
266
  @GeneralUtilities.check_arguments
256
267
  def is_working_branch(self)->bool:
@@ -311,20 +322,12 @@ class TFCPS_CodeUnit_BuildCodeUnits:
311
322
 
312
323
  @GeneralUtilities.check_arguments
313
324
  def build_codeunits_in_container(self,base_mount_folder:str) -> tuple[bool, str]:
314
- #base_mount_folder is assumed to be an absolute path set correctly by the caller (see BuildCodeUnitsC in Executables.py, which defaults it to the repository itself).
315
- #it may be the repository itself or any parent-folder of it, which allows the caller to mount not only the repository but the whole surrounding folder-structure into the container.
316
- normalized_base_mount_folder = os.path.normpath(base_mount_folder)
317
- normalized_repository = os.path.normpath(self.repository)
318
- relative_repository_path = os.path.relpath(normalized_repository, normalized_base_mount_folder).replace(os.sep, "/")
319
- GeneralUtilities.assert_condition(relative_repository_path == "." or not relative_repository_path.startswith(".."), f"The repository '{self.repository}' is not located inside the base-mount-folder '{base_mount_folder}'.")
320
- container_base_mount_folder = f"/Workspace/Project/{os.path.basename(normalized_base_mount_folder)}"
321
- container_repository_folder = container_base_mount_folder if relative_repository_path == "." else f"{container_base_mount_folder}/{relative_repository_path}"
322
- image = self.tfcps_tools_general.oci_image_manager.get_registry_address_for_image_with_default_tag(self.repository, "SCBuilder")
325
+ container_base_mount_folder, container_repository_folder = self.__get_folders_inside_container(base_mount_folder)
323
326
 
324
327
  #build the scbuildcodeunits-arguments based on the current state (analogous to the arguments accepted by the scbuildcodeunits-executable). each token must be a separate argument because run_program_argsasarray passes every list-element verbatim and does not split on spaces.
325
328
  scbuildcodeunits_arguments = ["scbuildcodeunits", "-r", container_repository_folder, "-v", "4"]
326
329
  if not self.__use_cache:
327
- scbuildcodeunits_arguments.append("-c")
330
+ scbuildcodeunits_arguments.append("-n")
328
331
  if self.__is_pre_merge:
329
332
  scbuildcodeunits_arguments.append("-p")
330
333
  if self.__assert_no_new_changes:
@@ -335,12 +338,53 @@ class TFCPS_CodeUnit_BuildCodeUnits:
335
338
  scbuildcodeunits_arguments.append("-f")
336
339
  if GeneralUtilities.string_has_content(self.additionalargumentsfile):
337
340
  scbuildcodeunits_arguments += ["-a", self.__translate_path_into_container(self.additionalargumentsfile, container_repository_folder)]
341
+ return self.__run_scriptcollection_executable_in_container(base_mount_folder, container_base_mount_folder, container_repository_folder, scbuildcodeunits_arguments, "Build codeunits in container")
342
+
343
+ @GeneralUtilities.check_arguments
344
+ def update_dependencies_in_container(self,base_mount_folder:str) -> tuple[bool, str]:
345
+ """Runs the update of the dependencies inside the SCBuilder-image, analogous to build_codeunits_in_container.
346
+
347
+ The whole update runs in the container and not only the codeunit-builds which it triggers: update_dependencies
348
+ hands a hook-object to build_codeunits, and an object can not be passed to a program through
349
+ commandline-arguments, so a containerized inner build would build without updating anything."""
350
+ container_base_mount_folder, container_repository_folder = self.__get_folders_inside_container(base_mount_folder)
351
+
352
+ #only the arguments which scupdatedependencies really accepts are passed. It has no equivalent of the
353
+ #pre-merge-, assert-no-new-changes-, ready-to-merge- and fastlane-flags of scbuildcodeunits, so passing one
354
+ #of them would make the argument-parsing fail inside the container.
355
+ scupdatedependencies_arguments = ["scupdatedependencies", "-r", container_repository_folder, "-v", "4"]
356
+ if not self.__use_cache:
357
+ scupdatedependencies_arguments.append("-n")
358
+ if GeneralUtilities.string_has_content(self.additionalargumentsfile):
359
+ scupdatedependencies_arguments += ["--additionalargumentsfile", self.__translate_path_into_container(self.additionalargumentsfile, container_repository_folder)]
360
+ return self.__run_scriptcollection_executable_in_container(base_mount_folder, container_base_mount_folder, container_repository_folder, scupdatedependencies_arguments, "Update dependencies in container")
361
+
362
+ @GeneralUtilities.check_arguments
363
+ def __get_folders_inside_container(self, base_mount_folder: str) -> tuple[str, str]:
364
+ """Returns the folder which the base-mount-folder is mounted to inside the container and the folder which the
365
+ repository has inside the container."""
366
+ #base_mount_folder is assumed to be an absolute path set correctly by the caller (see BuildCodeUnitsC in Executables.py, which defaults it to the repository itself).
367
+ #it may be the repository itself or any parent-folder of it, which allows the caller to mount not only the repository but the whole surrounding folder-structure into the container.
368
+ normalized_base_mount_folder = os.path.normpath(base_mount_folder)
369
+ normalized_repository = os.path.normpath(self.repository)
370
+ relative_repository_path = os.path.relpath(normalized_repository, normalized_base_mount_folder).replace(os.sep, "/")
371
+ GeneralUtilities.assert_condition(relative_repository_path == "." or not relative_repository_path.startswith(".."), f"The repository '{self.repository}' is not located inside the base-mount-folder '{base_mount_folder}'.")
372
+ container_base_mount_folder = f"/Workspace/Project/{os.path.basename(normalized_base_mount_folder)}"
373
+ container_repository_folder = container_base_mount_folder if relative_repository_path == "." else f"{container_base_mount_folder}/{relative_repository_path}"
374
+ return (container_base_mount_folder, container_repository_folder)
375
+
376
+ @GeneralUtilities.check_arguments
377
+ def __run_scriptcollection_executable_in_container(self, base_mount_folder: str, container_base_mount_folder: str, container_repository_folder: str, executable_arguments: list[str], log_message: str) -> tuple[bool, str]:
378
+ """Runs one of the commandline-tools of ScriptCollection inside the SCBuilder-image. The tool and its
379
+ arguments are given by the caller; everything which is required to make the container able to run it (the
380
+ mounts, the environment-variables and the preparation-hooks) is done here."""
381
+ image = self.tfcps_tools_general.oci_image_manager.get_registry_address_for_image_with_default_tag(self.repository, "SCBuilder")
338
382
 
339
383
  update_scriptcollection=True
340
384
  update_argument:str=""
341
385
  if update_scriptcollection:
342
386
  update_argument="pip3 install scriptcollection --upgrade && "
343
- scbuildcodeunits_arguments=["bash","-c", f"{update_argument}scshowversion && "+" ".join(scbuildcodeunits_arguments)]
387
+ executable_arguments=["bash","-c", f"{update_argument}scshowversion && "+" ".join(executable_arguments)]
344
388
 
345
389
  #run the optional user-specific script which prepares this host for a container-build (for example to log in to the registry the image is pulled from).
346
390
  #it runs before the environment-variables are resolved, so it can also create the files their values are read from.
@@ -358,7 +402,7 @@ class TFCPS_CodeUnit_BuildCodeUnits:
358
402
  mount_arguments += ["-v", f"{custom_scripts_folder_for_inside_the_container}:{self.tfcps_tools_general.get_folder_of_custom_scripts_in_container()}"]
359
403
 
360
404
  #pass the environment-variables which are declared as required in <repository>/.ScriptCollection/ProductInformation.xml into the container
361
- #so they do not have to be specified explicitly on every scbuildcodeunits-call. Their values are resolved from the user-specific
405
+ #so they do not have to be specified explicitly on every call. Their values are resolved from the user-specific
362
406
  #configuration-file (see TFCPS_Tools_General.get_environment_variables_configuration_file), which only exists on the host.
363
407
  #only the names are passed as arguments; the values are given to the docker-client through its own environment, because arguments
364
408
  #are written to the log and are visible in the process-list of this host, which must not happen for a value which is typically a secret.
@@ -367,7 +411,7 @@ class TFCPS_CodeUnit_BuildCodeUnits:
367
411
  for env_variable_name in required_environment_variables:
368
412
  env_arguments += ["-e", env_variable_name]
369
413
 
370
- #run scbuildcodeunits inside the SCBuilder-image. base_mount_folder is mounted into the container (covering the repository and, for submodules, its real gitdir) and the docker-socket is forwarded because codeunit-builds often start containers (for example local test-services).
414
+ #run the tool inside the SCBuilder-image. base_mount_folder is mounted into the container (covering the repository and, for submodules, its real gitdir) and the docker-socket is forwarded because codeunit-builds often start containers (for example local test-services).
371
415
  docker_arguments = [
372
416
  "run", "--rm",
373
417
  "-v", f"{base_mount_folder}:{container_base_mount_folder}",
@@ -375,8 +419,8 @@ class TFCPS_CodeUnit_BuildCodeUnits:
375
419
  "-w", container_repository_folder,
376
420
  ] + mount_arguments + env_arguments + [
377
421
  image,
378
- ] + scbuildcodeunits_arguments
379
- self.sc.log.log(f"Build codeunits in container using image \"{image}\"...")
422
+ ] + executable_arguments
423
+ self.sc.log.log(f"{log_message} using image \"{image}\"...")
380
424
  image_address, image_tag = ScriptCollectionCore.split_image_address_and_tag(image)
381
425
  self.sc.docker_pull(image_address, image_tag)
382
426
  # the exitcode is evaluated by the caller (returned as part of the result-tuple), so the program-runner must not raise on a non-zero exitcode here.
@@ -461,7 +505,7 @@ class TFCPS_CodeUnit_BuildCodeUnits:
461
505
  loc=splitted[2]
462
506
  csv_lines.append(f"{v},{t},{loc}")
463
507
  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
508
+ self.sc.normalize_invisible_characters(loc_data_file) # ensure the generated LoC-diagram-csv always uses LF line-endings
465
509
  diagram_json = {
466
510
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
467
511
  "description": "Lines of Code over time",
@@ -660,10 +704,6 @@ class TFCPS_CodeUnit_BuildCodeUnits:
660
704
  self.update_year_in_license_file()
661
705
  self.sc.assert_is_git_repository(repository)
662
706
  self.sc.assert_no_uncommitted_changes(repository)
663
- #the update is done while the codeunits are built regularly (see TFCPS_UpdateDependenciesHook): a codeunit is built before its
664
- #dependencies are updated (some programming-languages need that) and again afterwards if the update changed something. Using the
665
- #regular build means the update also gets all preparation-steps of a build - especially the package-sources which are required to
666
- #resolve dependencies which are not available on a public package-source.
667
707
  self.build_codeunits(TFCPS_UpdateDependenciesHook(self.sc, self.tfcps_tools_general))
668
708
  if self.sc.git_repository_has_uncommitted_changes(repository):
669
709
  changelog_folder = os.path.join(repository, "Other", "Resources", "Changelog")
@@ -672,6 +712,7 @@ class TFCPS_CodeUnit_BuildCodeUnits:
672
712
  if not os.path.isfile(changelog_file):
673
713
  self.__ensure_changelog_file_is_added(repository, project_version)
674
714
  self.build_codeunits()#check the codeunits are buildable at all with all updates together
715
+ self.sc.log.log("Build all codeunits again to ensure they are buildable with all updates together...")
675
716
  self.sc.git_commit(repository, "Updated dependencies", stage_all_changes=True)
676
717
 
677
718
  @GeneralUtilities.check_arguments
@@ -411,7 +411,7 @@ class TFCPS_Tools_General:
411
411
  @GeneralUtilities.check_arguments
412
412
  def set_default_constants(self, codeunit_folder: str) -> None:
413
413
  self.assert_is_codeunit_folder(codeunit_folder)
414
- self.set_constant_for_curenttimestamp(codeunit_folder)
414
+ self.set_constant_for_currenttimestamp(codeunit_folder)
415
415
  self.set_constant_for_commitid(codeunit_folder)
416
416
  self.set_constant_for_commitdate(codeunit_folder)
417
417
  self.set_constant_for_codeunitname(codeunit_folder)
@@ -420,7 +420,7 @@ class TFCPS_Tools_General:
420
420
  self.set_constant_for_description(codeunit_folder)
421
421
 
422
422
  @GeneralUtilities.check_arguments
423
- def set_constant_for_curenttimestamp(self, codeunit_folder: str) -> None:
423
+ def set_constant_for_currenttimestamp(self, codeunit_folder: str) -> None:
424
424
  self.assert_is_codeunit_folder(codeunit_folder)
425
425
  timestamp = GeneralUtilities.datetime_to_string_for_logfile_entry(GeneralUtilities.get_now().astimezone(timezone.utc),False)
426
426
  self.set_constant(codeunit_folder, "CurrentTimestamp", timestamp)
@@ -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", ""))