Skip to content

Commit 26705b3

Browse files
committed
Add command output key translation for Arduino CLI 1.0.0
The action parses the output of the `arduino-cli core list --format json` command. The name of one of the keys in that output data used by the action was changed in the 1.0.0 release of Arduino CLI, breaking the action when used with that version of Arduino CLI (which is the default behavior). The action code base already has a key name translation system, which was implemented to handle breakage caused by a previous round of key name changes. However, that system was designed under the assumption that the key names would be stable following the comprehensive redesign that caused the previous changes. It was not designed to handle multiple changes to the name of a given key as has now happened. For this reason, the translation system has been redesigned to support any number of key name changes that might occur.
1 parent a5fd631 commit 26705b3

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

compilesketches/compilesketches.py

+35-15
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,13 @@ def __init__(self):
560560
command_data = self.run_arduino_cli_command(command=["core", "list", "--format", "json"])
561561
installed_platform_list = self.cli_core_list_platform_list(json.loads(command_data.stdout))
562562
for installed_platform in installed_platform_list:
563-
if installed_platform[self.cli_json_key("core list", "ID")] == platform[self.dependency_name_key]:
563+
if installed_platform[self.cli_json_key("core list", "id")] == platform[self.dependency_name_key]:
564564
# The platform has been installed via Board Manager, so do an overwrite
565565
platform_installation_path.path = self.board_manager_platforms_path.joinpath(
566566
platform_vendor,
567567
"hardware",
568568
platform_architecture,
569-
installed_platform[self.cli_json_key("core list", "Installed")],
569+
installed_platform[self.cli_json_key("core list", "installed_version")],
570570
)
571571
platform_installation_path.is_overwrite = True
572572

@@ -1457,29 +1457,49 @@ def cli_core_list_platform_list(self, data):
14571457

14581458
return data
14591459

1460-
def cli_json_key(self, command, original_key_name):
1460+
def cli_json_key(self, command, key_name):
14611461
"""Return the appropriate JSON output key name for the Arduino CLI version in use.
14621462
14631463
Keyword arguments:
14641464
command -- Arduino CLI command (e.g., "core list")
1465-
original_key_name -- key name used by the original Arduino CLI JSON interface
1465+
key_name -- key name used by the current Arduino CLI JSON interface
14661466
"""
1467-
final_original_interface_version = "0.17.0" # Interface was changed in the next Arduino CLI release
1468-
1469-
key_translation = {
1467+
key_translations = {
14701468
"core list": {
1471-
"ID": "id",
1472-
"Installed": "installed"
1469+
"id": [
1470+
{"constraints": [">=0.0.0", "<=0.17.0"], "name": "ID"},
1471+
# https://arduino.github.io/arduino-cli/dev/UPGRADING/#arduino-cli-json-output-breaking-changes
1472+
{"constraints": [">0.17.0"], "name": "id"},
1473+
],
1474+
"installed_version": [
1475+
{"constraints": [">=0.0.0", "<=0.17.0"], "name": "Installed"},
1476+
# https://arduino.github.io/arduino-cli/dev/UPGRADING/#arduino-cli-json-output-breaking-changes
1477+
{"constraints": [">0.17.0", "<1.0.0"], "name": "installed"},
1478+
# https://arduino.github.io/arduino-cli/dev/UPGRADING/#cli-core-list-and-core-search-changed-json-output
1479+
{"constraints": [">=1.0.0"], "name": "installed_version"},
1480+
],
14731481
}
14741482
}
14751483

1476-
if (
1477-
not semver.VersionInfo.is_valid(version=self.cli_version)
1478-
or semver.Version.parse(version=self.cli_version).compare(other=final_original_interface_version) > 0
1479-
) and (command in key_translation and original_key_name in key_translation[command]):
1480-
return key_translation[command][original_key_name]
1484+
if not semver.VersionInfo.is_valid(version=self.cli_version):
1485+
# cli_version is "latest", so use the current key name
1486+
return key_name
1487+
1488+
for translation in key_translations[command][key_name]:
1489+
match = True
1490+
for constraint in translation["constraints"]:
1491+
if not semver.Version.parse(version=self.cli_version).match(match_expr=constraint):
1492+
# The Arduino CLI version does not match the translation's version constraints
1493+
match = False
1494+
break
1495+
1496+
if match:
1497+
# The Arduino CLI version matches the translation's version constraints
1498+
return translation["name"]
14811499

1482-
return original_key_name
1500+
raise RuntimeError(
1501+
f"Translation not implemented for `{key_name}` key of `arduino-cli {command}` for version {self.cli_version}"
1502+
) # pragma: no cover
14831503

14841504

14851505
def parse_list_input(list_input):

compilesketches/tests/test_compilesketches.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -2899,20 +2899,18 @@ def test_cli_core_list_platform_list(cli_version, data, assertion):
28992899

29002900

29012901
@pytest.mark.parametrize(
2902-
"cli_version, command, original_key, expected_key",
2902+
"cli_version, command, key_name, expected_key",
29032903
[
2904-
("latest", "core list", "ID", "id"), # Non-semver
2905-
("1.0.0", "core list", "ID", "id"), # >
2906-
("0.17.0", "core list", "ID", "ID"), # ==
2907-
("0.14.0-rc2", "core list", "ID", "ID"), # <
2908-
("1.0.0", "foo", "ID", "ID"), # Command has no translation
2909-
("1.0.0", "core list", "foo", "foo"), # Key has no translation
2904+
("latest", "core list", "installed_version", "installed_version"), # Non-semver
2905+
("0.1.2", "core list", "installed_version", "Installed"),
2906+
("0.17.1", "core list", "installed_version", "installed"),
2907+
("1.2.3", "core list", "installed_version", "installed_version"),
29102908
],
29112909
)
2912-
def test_cli_json_key(cli_version, command, original_key, expected_key):
2910+
def test_cli_json_key(cli_version, command, key_name, expected_key):
29132911
compile_sketches = get_compilesketches_object(cli_version=cli_version)
29142912

2915-
assert compile_sketches.cli_json_key(command, original_key) == expected_key
2913+
assert compile_sketches.cli_json_key(command, key_name) == expected_key
29162914

29172915

29182916
@pytest.mark.parametrize("verbose", ["true", "false"])

0 commit comments

Comments
 (0)