Skip to content

Commit 96ea15d

Browse files
committed
Add support for definitions containing quotes in compile --build-properties flag
1 parent 17d24eb commit 96ea15d

File tree

8 files changed

+210
-45
lines changed

8 files changed

+210
-45
lines changed

Diff for: cli/compile/compile.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func NewCommand() *cobra.Command {
7272
command.Flags().StringVarP(&exportDir, "output-dir", "", "", "Save build artifacts in this directory.")
7373
command.Flags().StringVar(&buildPath, "build-path", "",
7474
"Path where to save compiled files. If omitted, a directory will be created in the default temporary path of your OS.")
75-
command.Flags().StringSliceVar(&buildProperties, "build-properties", []string{},
76-
"List of custom build properties separated by commas. Or can be used multiple times for multiple properties.")
75+
command.Flags().StringArrayVar(&buildProperties, "build-properties", []string{},
76+
"List of custom build properties separated by spaces. Or can be used multiple times for multiple properties.")
7777
command.Flags().StringVar(&warnings, "warnings", "none",
7878
`Optional, can be "none", "default", "more" and "all". Defaults to "none". Used to tell gcc which warning level to use (-W flag).`)
7979
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
@@ -36,6 +36,7 @@ require (
3636
github.com/sirupsen/logrus v1.4.2
3737
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c
3838
github.com/spf13/jwalterweatherman v1.0.0
39+
github.com/spf13/pflag v1.0.3
3940
github.com/spf13/viper v1.6.2
4041
github.com/stretchr/testify v1.6.1
4142
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

+127
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,133 @@ def test_compile_without_precompiled_libraries(run_command, data_dir):
233233
assert result.ok
234234

235235

236+
def test_compile_with_build_properties_flag(run_command, data_dir, copy_sketch):
237+
# Init the environment explicitly
238+
assert run_command("core update-index")
239+
240+
# Install Arduino AVR Boards
241+
assert run_command("core install arduino:[email protected]")
242+
243+
sketch_path = copy_sketch("sketch_with_single_string_define")
244+
fqbn = "arduino:avr:uno"
245+
246+
# Compile using a build property with quotes
247+
res = run_command(
248+
f"compile -b {fqbn} "
249+
+ '--build-properties="build.extra_flags=\\"-DMY_DEFINE=\\"hello world\\"\\"" '
250+
+ f"{sketch_path} --verbose --clean"
251+
)
252+
assert res.failed
253+
assert "Flag --build-properties has been deprecated, please use --build-property instead." not in res.stderr
254+
255+
# Try again with quotes
256+
res = run_command(
257+
f"compile -b {fqbn} "
258+
+ '--build-properties="build.extra_flags=-DMY_DEFINE=\\"hello\\"" '
259+
+ f"{sketch_path} --verbose --clean"
260+
)
261+
assert res.failed
262+
assert "Flag --build-properties has been deprecated, please use --build-property instead." not in res.stderr
263+
264+
# Try without quotes
265+
sketch_path = copy_sketch("sketch_with_single_int_define")
266+
res = run_command(
267+
f"compile -b {fqbn} "
268+
+ '--build-properties="build.extra_flags=-DMY_DEFINE=1" '
269+
+ f"{sketch_path} --verbose --clean"
270+
)
271+
assert res.ok
272+
assert "Flag --build-properties has been deprecated, please use --build-property instead." in res.stderr
273+
assert "-DMY_DEFINE=1" in res.stdout
274+
275+
sketch_path = copy_sketch("sketch_with_multiple_int_defines")
276+
res = run_command(
277+
f"compile -b {fqbn} "
278+
+ '--build-properties="build.extra_flags=-DFIRST_PIN=1,compiler.cpp.extra_flags=-DSECOND_PIN=2" '
279+
+ f"{sketch_path} --verbose --clean"
280+
)
281+
assert res.ok
282+
assert "Flag --build-properties has been deprecated, please use --build-property instead." in res.stderr
283+
assert "-DFIRST_PIN=1" in res.stdout
284+
assert "-DSECOND_PIN=2" in res.stdout
285+
286+
287+
def test_compile_with_build_property_containing_quotes(run_command, data_dir, copy_sketch):
288+
# Init the environment explicitly
289+
assert run_command("core update-index")
290+
291+
# Install Arduino AVR Boards
292+
assert run_command("core install arduino:[email protected]")
293+
294+
sketch_path = copy_sketch("sketch_with_single_string_define")
295+
fqbn = "arduino:avr:uno"
296+
297+
# Compile using a build property with quotes
298+
res = run_command(
299+
f"compile -b {fqbn} "
300+
+ '--build-property="build.extra_flags=\\"-DMY_DEFINE=\\"hello world\\"\\"" '
301+
+ f"{sketch_path} --verbose"
302+
)
303+
assert res.ok
304+
assert '-DMY_DEFINE=\\"hello world\\"' in res.stdout
305+
306+
307+
def test_compile_with_multiple_build_property_flags(run_command, data_dir, copy_sketch, working_dir):
308+
# Init the environment explicitly
309+
assert run_command("core update-index")
310+
311+
# Install Arduino AVR Boards
312+
assert run_command("core install arduino:[email protected]")
313+
314+
sketch_path = copy_sketch("sketch_with_multiple_defines")
315+
fqbn = "arduino:avr:uno"
316+
317+
# Compile using multiple build properties separated by a space
318+
res = run_command(
319+
f"compile -b {fqbn} "
320+
+ '--build-property="compiler.cpp.extra_flags=\\"-DPIN=2 -DSSID=\\"This is a String\\"\\"" '
321+
+ f"{sketch_path} --verbose --clean"
322+
)
323+
assert res.failed
324+
325+
# Compile using multiple build properties separated by a space and properly quoted
326+
res = run_command(
327+
f"compile -b {fqbn} "
328+
+ '--build-property="compiler.cpp.extra_flags=-DPIN=2 \\"-DSSID=\\"This is a String\\"\\"" '
329+
+ f"{sketch_path} --verbose --clean"
330+
)
331+
assert res.ok
332+
assert '-DPIN=2 "-DSSID=\\"This is a String\\""' in res.stdout
333+
334+
# Tries compilation using multiple build properties separated by a comma
335+
res = run_command(
336+
f"compile -b {fqbn} "
337+
+ '--build-property="compiler.cpp.extra_flags=\\"-DPIN=2,-DSSID=\\"This is a String\\"\\"\\" '
338+
+ f"{sketch_path} --verbose --clean"
339+
)
340+
assert res.failed
341+
342+
res = run_command(
343+
f"compile -b {fqbn} "
344+
+ '--build-property="compiler.cpp.extra_flags=\\"-DPIN=2\\"" '
345+
+ '--build-property="compiler.cpp.extra_flags=\\"-DSSID=\\"This is a String\\"\\"" '
346+
+ f"{sketch_path} --verbose --clean"
347+
)
348+
assert res.failed
349+
assert "-DPIN=2" not in res.stdout
350+
assert '-DSSID=\\"This is a String\\"' in res.stdout
351+
352+
res = run_command(
353+
f"compile -b {fqbn} "
354+
+ '--build-property="compiler.cpp.extra_flags=\\"-DPIN=2\\"" '
355+
+ '--build-property="build.extra_flags=\\"-DSSID=\\"hello world\\"\\"" '
356+
+ f"{sketch_path} --verbose --clean"
357+
)
358+
assert res.ok
359+
assert "-DPIN=2" in res.stdout
360+
assert '-DSSID=\\"hello world\\"' in res.stdout
361+
362+
236363
def test_compile_with_output_dir_flag(run_command, data_dir):
237364
# Init the environment explicitly
238365
run_command("core update-index")

0 commit comments

Comments
 (0)