Skip to content
This repository was archived by the owner on Apr 1, 2022. It is now read-only.

Commit f49a119

Browse files
authored
Merge pull request #74 from per1234/sketch-paths-input-yaml-format
libraries/compile-examples: support YAML-format sketch-paths input value
2 parents 8c4f84f + ad41f23 commit f49a119

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

libraries/compile-examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Keys:
102102

103103
### `sketch-paths`
104104

105-
List of paths containing sketches to compile. These paths will be searched recursively. Default `"examples"`.
105+
YAML-format list of paths containing sketches to compile. These paths will be searched recursively. Default `- examples`.
106106

107107
### `verbose`
108108

libraries/compile-examples/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ inputs:
1515
default: ''
1616
sketch-paths:
1717
description: 'List of paths containing sketches to compile.'
18-
default: 'examples'
18+
default: '- examples'
1919
verbose:
2020
description: 'Set to true to show verbose output in the log'
2121
default: false

libraries/compile-examples/compilesketches/compilesketches.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ def __init__(self, cli_version, fqbn_arg, platforms, libraries, sketch_paths, ve
117117
self.libraries = libraries
118118

119119
# Save the space-separated list of paths as a Python list
120-
sketch_paths = parse_list_input(sketch_paths)
121-
absolute_sketch_paths = [absolute_path(path=sketch_path) for sketch_path in sketch_paths]
120+
sketch_paths = get_list_from_multiformat_input(input_value=sketch_paths)
121+
absolute_sketch_paths = [absolute_path(path=sketch_path) for sketch_path in sketch_paths.value]
122122
self.sketch_paths = absolute_sketch_paths
123123

124124
self.verbose = parse_boolean_input(boolean_input=verbose)
@@ -596,21 +596,16 @@ def install_platforms_from_download(self, platform_list):
596596

597597
def install_libraries(self):
598598
"""Install Arduino libraries."""
599-
libraries = yaml.load(stream="", Loader=yaml.SafeLoader)
600-
try:
601-
libraries = yaml.load(stream=self.libraries, Loader=yaml.SafeLoader)
602-
except yaml.parser.ParserError:
603-
# This exception occurs when the space separated list items are individually quoted (e.g., '"Foo" "Bar"')
604-
pass
599+
libraries = get_list_from_multiformat_input(input_value=self.libraries)
605600

606601
library_list = self.Dependencies()
607-
if type(libraries) is list:
602+
if libraries.was_yaml_list:
608603
# libraries input is YAML
609-
library_list = self.sort_dependency_list(libraries)
604+
library_list = self.sort_dependency_list(libraries.value)
610605
else:
611606
# libraries input uses the old space-separated list syntax
612607
library_list.manager = [{self.dependency_name_key: library_name}
613-
for library_name in parse_list_input(self.libraries)]
608+
for library_name in libraries.value]
614609

615610
# The original behavior of the action was to assume the root of the repo is a library to be installed, so
616611
# that behavior is retained when using the old input syntax
@@ -1261,6 +1256,46 @@ def absolute_path(path):
12611256
return path
12621257

12631258

1259+
def get_list_from_multiformat_input(input_value):
1260+
"""For backwards compatibility with the legacy API, some inputs support a space-separated list format in addition to
1261+
the modern YAML format. This function converts either input format into a list and returns an object with the
1262+
attributes:
1263+
value -- the list that was parsed from the input
1264+
was_yaml_list -- whether the input was in YAML format (True, False)
1265+
1266+
Keyword arguments:
1267+
input_value -- the raw input
1268+
"""
1269+
1270+
class InputList:
1271+
def __init__(self):
1272+
self.value = []
1273+
self.was_yaml_list = False
1274+
1275+
input_list = InputList()
1276+
1277+
try:
1278+
processed_input_value = yaml.load(stream=input_value, Loader=yaml.SafeLoader)
1279+
except yaml.parser.ParserError:
1280+
# The input value was not valid YAML
1281+
# This exception occurs when the space separated list items are individually quoted (e.g., '"Foo" "Bar"')
1282+
# Note: some old format list values are also valid YAML by chance (e.g., a normal string), so old format input
1283+
# won't always cause this exception
1284+
processed_input_value = input_value
1285+
pass
1286+
1287+
if type(processed_input_value) is list:
1288+
# The input value was valid YAML and in list format
1289+
input_list.value = processed_input_value
1290+
input_list.was_yaml_list = True
1291+
else:
1292+
# The input value was either valid YAML, but not a list, or invalid YAML
1293+
input_list.value = parse_list_input(list_input=input_value)
1294+
input_list.was_yaml_list = False
1295+
1296+
return input_list
1297+
1298+
12641299
def path_is_sketch(path):
12651300
"""Return whether the specified path is an Arduino sketch.
12661301

libraries/compile-examples/compilesketches/tests/test_compilesketches.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,30 +1101,30 @@ def test_find_sketches(capsys):
11011101

11021102
# Test sketch path is a sketch file
11031103
compile_sketches = get_compilesketches_object(
1104-
sketch_paths="\"" + str(test_data_path.joinpath("HasSketches", "Sketch1", "Sketch1.ino")) + "\""
1104+
sketch_paths="\'" + str(test_data_path.joinpath("HasSketches", "Sketch1", "Sketch1.ino")) + "\'"
11051105
)
11061106
assert compile_sketches.find_sketches() == [
11071107
test_data_path.joinpath("HasSketches", "Sketch1")
11081108
]
11091109

11101110
# Test sketch path is a non-sketch file
11111111
non_sketch_path = str(test_data_path.joinpath("NoSketches", "NotSketch", "NotSketch.foo"))
1112-
compile_sketches = get_compilesketches_object(sketch_paths="\"" + non_sketch_path + "\"")
1112+
compile_sketches = get_compilesketches_object(sketch_paths="\'" + non_sketch_path + "\'")
11131113
with pytest.raises(expected_exception=SystemExit, match="1"):
11141114
compile_sketches.find_sketches()
11151115
assert capsys.readouterr().out.strip() == ("::error::Sketch path: " + non_sketch_path + " is not a sketch")
11161116

11171117
# Test sketch path is a sketch folder
11181118
compile_sketches = get_compilesketches_object(
1119-
sketch_paths="\"" + str(test_data_path.joinpath("HasSketches", "Sketch1")) + "\""
1119+
sketch_paths="\'" + str(test_data_path.joinpath("HasSketches", "Sketch1")) + "\'"
11201120
)
11211121
assert compile_sketches.find_sketches() == [
11221122
test_data_path.joinpath("HasSketches", "Sketch1")
11231123
]
11241124

11251125
# Test sketch path does contain sketches
11261126
compile_sketches = get_compilesketches_object(
1127-
sketch_paths="\"" + str(test_data_path.joinpath("HasSketches")) + "\"")
1127+
sketch_paths="\'" + str(test_data_path.joinpath("HasSketches")) + "\'")
11281128
assert compile_sketches.find_sketches() == [
11291129
test_data_path.joinpath("HasSketches", "Sketch1"),
11301130
test_data_path.joinpath("HasSketches", "Sketch2")
@@ -1133,13 +1133,27 @@ def test_find_sketches(capsys):
11331133
# Test sketch path doesn't contain any sketches
11341134
no_sketches_path = str(test_data_path.joinpath("NoSketches"))
11351135
compile_sketches = get_compilesketches_object(
1136-
sketch_paths="\"" + no_sketches_path + "\"")
1136+
sketch_paths="\'" + no_sketches_path + "\'")
11371137
with pytest.raises(expected_exception=SystemExit, match="1"):
11381138
compile_sketches.find_sketches()
11391139
assert capsys.readouterr().out.strip() == ("::error::No sketches were found in "
11401140
+ no_sketches_path)
11411141

11421142

1143+
@pytest.mark.parametrize(
1144+
"input_value, expected_list, expected_was_yaml_list",
1145+
[("", [], False),
1146+
("foo", ["foo"], False),
1147+
("\'\"foo bar\" baz\'", ["foo bar", "baz"], False),
1148+
("foo: bar", ["foo:", "bar"], False),
1149+
("-", [None], True),
1150+
("- foo: asdf\n bar: qwer\n- baz: zxcv", [{"foo": "asdf", "bar": "qwer"}, {"baz": "zxcv"}], True)])
1151+
def test_get_list_from_multiformat_input(input_value, expected_list, expected_was_yaml_list):
1152+
input_list = compilesketches.get_list_from_multiformat_input(input_value=input_value)
1153+
assert input_list.value == expected_list
1154+
assert input_list.was_yaml_list == expected_was_yaml_list
1155+
1156+
11431157
def test_path_is_sketch():
11441158
# Sketch file
11451159
assert compilesketches.path_is_sketch(path=test_data_path.joinpath("HasSketches", "Sketch1", "Sketch1.ino")) is True

0 commit comments

Comments
 (0)