Skip to content

Commit 40f6d20

Browse files
committed
Handle Arduino CLI 1.x core list data format
The action parses the output of the `arduino-cli core list --format json` command. There was a breaking change to the data format of that output was changed in the 1.0.0 release of Arduino CLI. The action code is updated to handle the new output, while also retaining backwards compatibility with the pre-1.x data format in order to support workflows that pin the Arduino CLI version to >1.0.0 via the action's "cli-version" input.
1 parent b35a113 commit 40f6d20

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

compilesketches/compilesketches.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ def __init__(self):
558558
self.run_arduino_cli_command(command=["core", "update-index"])
559559
# Use Arduino CLI to get the list of installed platforms
560560
command_data = self.run_arduino_cli_command(command=["core", "list", "--format", "json"])
561-
installed_platform_list = json.loads(command_data.stdout)
561+
installed_platform_list = self.cli_core_list_platform_list(json.loads(command_data.stdout))
562562
for installed_platform in installed_platform_list:
563563
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
@@ -1437,6 +1437,26 @@ def create_sketches_report_file(self, sketches_report):
14371437
) as report_file:
14381438
json.dump(obj=sketches_report, fp=report_file, indent=2)
14391439

1440+
def cli_core_list_platform_list(self, data):
1441+
"""Extract the list of platform data from the `arduino-cli core list` command output according to the Arduino
1442+
CLI version in use.
1443+
1444+
Keyword arguments:
1445+
data -- Arduino CLI command output data
1446+
"""
1447+
# Interface was changed at this Arduino CLI release:
1448+
# https://arduino.github.io/arduino-cli/dev/UPGRADING/#cli-changed-json-output-for-some-lib-core-config-board-and-sketch-commands
1449+
first_new_interface_version = "1.0.0"
1450+
1451+
if (
1452+
not semver.VersionInfo.is_valid(version=self.cli_version)
1453+
or semver.Version.parse(version=self.cli_version).compare(other=first_new_interface_version) >= 0
1454+
):
1455+
# cli_version is either "latest" (which will now always be >=1.0.0) or an explicit version >=1.0.0
1456+
return data["platforms"]
1457+
1458+
return data
1459+
14401460
def cli_json_key(self, command, original_key_name):
14411461
"""Return the appropriate JSON output key name for the Arduino CLI version in use.
14421462

compilesketches/tests/test_compilesketches.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,21 @@ def test_create_sketches_report_file(monkeypatch, tmp_path):
28832883
assert json.load(sketch_report_file) == sketches_report
28842884

28852885

2886+
@pytest.mark.parametrize(
2887+
"cli_version, data, assertion",
2888+
[
2889+
("latest", {"platforms": [unittest.mock.sentinel.list_item]}, [unittest.mock.sentinel.list_item]), # Non-semver
2890+
("2.0.0", {"platforms": [unittest.mock.sentinel.list_item]}, [unittest.mock.sentinel.list_item]), # >
2891+
("1.0.0", {"platforms": [unittest.mock.sentinel.list_item]}, [unittest.mock.sentinel.list_item]), # ==
2892+
("0.1.2", [unittest.mock.sentinel.list_item], [unittest.mock.sentinel.list_item]), # <
2893+
],
2894+
)
2895+
def test_cli_core_list_platform_list(cli_version, data, assertion):
2896+
compile_sketches = get_compilesketches_object(cli_version=cli_version)
2897+
2898+
assert compile_sketches.cli_core_list_platform_list(data) == assertion
2899+
2900+
28862901
@pytest.mark.parametrize(
28872902
"cli_version, command, original_key, expected_key",
28882903
[

0 commit comments

Comments
 (0)