Skip to content

Commit 155b4c6

Browse files
committed
Add support for definitions containing quotes in compile --build-properties flag
1 parent a9c0a97 commit 155b4c6

File tree

8 files changed

+154
-45
lines changed

8 files changed

+154
-45
lines changed

Diff for: cli/compile/compile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func NewCommand() *cobra.Command {
7373
command.Flags().BoolVarP(&dryRun, "dry-run", "n", false, "Perform the build but do not copy the compile output file.")
7474
command.Flags().StringVar(&buildPath, "build-path", "",
7575
"Path where to save compiled files. If omitted, a directory will be created in the default temporary path of your OS.")
76-
command.Flags().StringSliceVar(&buildProperties, "build-properties", []string{},
77-
"List of custom build properties separated by commas. Or can be used multiple times for multiple properties.")
76+
command.Flags().StringArrayVar(&buildProperties, "build-properties", []string{},
77+
"List of custom build properties separated by spaces. Or can be used multiple times for multiple properties.")
7878
command.Flags().StringVar(&warnings, "warnings", "none",
7979
`Optional, can be "none", "default", "more" and "all". Defaults to "none". Used to tell gcc which warning level to use (-W flag).`)
8080
command.Flags().BoolVarP(&verbose, "verbose", "v", false, "Optional, turns on verbose mode.")

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ require (
3535
github.com/sirupsen/logrus v1.4.2
3636
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c
3737
github.com/spf13/jwalterweatherman v1.0.0
38+
github.com/spf13/pflag v1.0.3
3839
github.com/spf13/viper v1.6.2
3940
github.com/stretchr/testify v1.6.1
4041
go.bug.st/cleanup v1.0.0

Diff for: legacy/builder/types/context.go

-16
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,6 @@ func (ctx *Context) ExtractBuildOptions() *properties.Map {
187187
return opts
188188
}
189189

190-
func (ctx *Context) InjectBuildOptions(opts *properties.Map) {
191-
ctx.HardwareDirs = paths.NewPathList(strings.Split(opts.Get("hardwareFolders"), ",")...)
192-
ctx.BuiltInToolsDirs = paths.NewPathList(strings.Split(opts.Get("builtInToolsFolders"), ",")...)
193-
ctx.BuiltInLibrariesDirs = paths.NewPathList(strings.Split(opts.Get("builtInLibrariesFolders"), ",")...)
194-
ctx.OtherLibrariesDirs = paths.NewPathList(strings.Split(opts.Get("otherLibrariesFolders"), ",")...)
195-
ctx.SketchLocation = opts.GetPath("sketchLocation")
196-
fqbn, err := cores.ParseFQBN(opts.Get("fqbn"))
197-
if err != nil {
198-
i18n.ErrorfWithLogger(ctx.GetLogger(), "Error in FQBN: %s", err)
199-
}
200-
ctx.FQBN = fqbn
201-
ctx.ArduinoAPIVersion = opts.Get("runtime.ide.version")
202-
ctx.CustomBuildProperties = strings.Split(opts.Get("customBuildProperties"), ",")
203-
ctx.OptimizationFlags = opts.Get("compiler.optimization_flags")
204-
}
205-
206190
func (ctx *Context) GetLogger() i18n.Logger {
207191
if ctx.logger == nil {
208192
return &i18n.HumanLogger{}

Diff for: test/conftest.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,14 @@ def detected_boards(run_command):
212212

213213
@pytest.fixture(scope="function")
214214
def copy_sketch(working_dir):
215-
# Copies sketch for testing
216-
sketch_path = Path(__file__).parent / "testdata" / "sketch_simple"
217-
test_sketch_path = Path(working_dir) / "sketch_simple"
218-
shutil.copytree(sketch_path, test_sketch_path)
219-
yield str(test_sketch_path)
215+
def _copy(sketch_name):
216+
# Copies sketch to working directory for testing
217+
sketch_path = Path(__file__).parent / "testdata" / sketch_name
218+
test_sketch_path = Path(working_dir, sketch_name)
219+
shutil.copytree(sketch_path, test_sketch_path)
220+
return str(test_sketch_path)
221+
222+
return _copy
220223

221224

222225
@pytest.fixture(scope="function")

Diff for: test/test_compile.py

+71
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,74 @@ def test_compile_without_precompiled_libraries(run_command, data_dir):
233233
"compile -b arduino:mbed:nano33ble {}/libraries/BSEC_Software_Library/examples/basic/".format(data_dir)
234234
)
235235
assert result.ok
236+
237+
238+
def test_compile_with_build_property_containing_quotes(run_command, data_dir, copy_sketch):
239+
# Init the environment explicitly
240+
assert run_command("core update-index")
241+
242+
# Install Arduino AVR Boards
243+
assert run_command("core install arduino:[email protected]")
244+
245+
sketch_path = copy_sketch("sketch_with_single_define")
246+
fqbn = "arduino:avr:uno"
247+
248+
# Compile using a build property with an equal
249+
res = run_command(
250+
f"compile -b {fqbn} "
251+
+ '--build-properties="build.extra_flags=\\"-DMY_DEFINE=\\"hello world\\"\\"" '
252+
+ f"{sketch_path} --verbose"
253+
)
254+
assert res.ok
255+
assert '-DMY_DEFINE=\\"hello world\\"' in res.stdout
256+
257+
258+
def test_compile_with_multiple_build_properties(run_command, data_dir, copy_sketch):
259+
# Init the environment explicitly
260+
assert run_command("core update-index")
261+
262+
# Install Arduino AVR Boards
263+
assert run_command("core install arduino:[email protected]")
264+
265+
sketch_path = copy_sketch("sketch_with_multiple_defines")
266+
fqbn = "arduino:avr:uno"
267+
268+
# Create a test sketch
269+
assert run_command(f"sketch new {sketch_path}")
270+
271+
# Compile using multiple build properties separated by a space
272+
res = run_command(
273+
f"compile -b {fqbn} "
274+
+ '--build-properties="compiler.cpp.extra_flags=\\"-DPIN=2 -DSSID=\\"This is a String\\"\\"" '
275+
+ f"{sketch_path} --verbose"
276+
)
277+
assert res.ok
278+
assert '-DPIN=2 -DSSID=\\"This is a String\\"' in res.stdout
279+
280+
# Tries compilation using multiple build properties separated by a comma
281+
res = run_command(
282+
f"compile -b {fqbn} "
283+
+ '--build-properties="compiler.cpp.extra_flags=\'-DPIN=2,-DSSID=\\"This is a String\\"\\"\' '
284+
+ f"{sketch_path} --verbose"
285+
)
286+
assert res.failed
287+
288+
res = run_command(
289+
f"compile -b {fqbn} "
290+
+ '--build-properties="compiler.cpp.extra_flags=\\"-DPIN=2\\"" '
291+
+ '--build-properties="compiler.cpp.extra_flags=\\"-DSSID=\\"This is a String\\"\\"" '
292+
+ f"{sketch_path} --verbose"
293+
)
294+
assert res.ok
295+
assert "-DPIN=2" not in res.stdout
296+
assert '-DSSID=\\"This is a String\\"' in res.stdout
297+
298+
res = run_command(
299+
f"compile -b {fqbn} "
300+
+ '--build-properties="compiler.cpp.extra_flags=\\"-DPIN=2\\"" '
301+
+ '--build-properties="build.extra_flags=\\"-DMY_DEFINE=\\"hello world\\"\\"" '
302+
+ f"{sketch_path} --verbose"
303+
)
304+
assert res.ok
305+
assert "-DPIN=2" in res.stdout
306+
assert '-DMY_DEFINE=\\"hello world\\"' in res.stdout

0 commit comments

Comments
 (0)