Skip to content

Commit e5d5d98

Browse files
authored
Fix binaries not being exported when using settings or env var (#1056)
1 parent 1d49b6a commit e5d5d98

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

cli/compile/compile.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ func NewCommand() *cobra.Command {
8787
command.Flags().BoolVar(&optimizeForDebug, "optimize-for-debug", false, "Optional, optimize compile output for debugging, rather than for release.")
8888
command.Flags().StringVarP(&programmer, "programmer", "P", "", "Optional, use the specified programmer to upload.")
8989
command.Flags().BoolVar(&clean, "clean", false, "Optional, cleanup the build folder and do not use any cached build.")
90-
command.Flags().BoolVarP(&exportBinaries, "export-binaries", "e", false, "If set built binaries will be exported to the sketch folder.")
90+
// We must use the following syntax for this flag since it's also bound to settings, we could use the other one too
91+
// but it wouldn't make sense since we still must explicitly set the exportBinaries variable by reading from settings.
92+
// This must be done because the value is set when the binding is accessed from viper. Accessing from cobra would only
93+
// read the value if the flag is set explicitly by the user.
94+
command.Flags().BoolP("export-binaries", "e", false, "If set built binaries will be exported to the sketch folder.")
9195

9296
configuration.Settings.BindPFlag("sketch.always_export_binaries", command.Flags().Lookup("export-binaries"))
9397

@@ -107,6 +111,10 @@ func run(cmd *cobra.Command, args []string) {
107111
}
108112

109113
sketchPath := initSketchPath(path)
114+
// We must read this from settings since the value is set when the binding is accessed from viper,
115+
// accessing it from cobra would only read it if the flag is explicitly set by the user and ignore
116+
// the config file and the env vars.
117+
exportBinaries = configuration.Settings.GetBool("sketch.always_export_binaries")
110118

111119
_, err = compile.Compile(context.Background(), &rpc.CompileReq{
112120
Instance: inst,

test/test_compile.py

+71
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,74 @@ def test_compile_with_custom_build_path(run_command, data_dir):
340340
assert not (build_dir / f"{sketch_name}.ino.hex").exists()
341341
assert not (build_dir / f"{sketch_name}.ino.with_bootloader.bin").exists()
342342
assert not (build_dir / f"{sketch_name}.ino.with_bootloader.hex").exists()
343+
344+
345+
def test_compile_with_export_binaries_env_var(run_command, data_dir, downloads_dir):
346+
# Init the environment explicitly
347+
run_command("core update-index")
348+
349+
# Download latest AVR
350+
run_command("core install arduino:avr")
351+
352+
sketch_name = "CompileWithExportBinariesEnvVar"
353+
sketch_path = Path(data_dir, sketch_name)
354+
fqbn = "arduino:avr:uno"
355+
356+
# Create a test sketch
357+
assert run_command("sketch new {}".format(sketch_path))
358+
359+
env = {
360+
"ARDUINO_DATA_DIR": data_dir,
361+
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
362+
"ARDUINO_SKETCHBOOK_DIR": data_dir,
363+
"ARDUINO_SKETCH_ALWAYS_EXPORT_BINARIES": "true",
364+
}
365+
# Test compilation with export binaries env var set
366+
result = run_command(f"compile -b {fqbn} {sketch_path}", custom_env=env)
367+
assert result.ok
368+
assert Path(sketch_path, "build").exists()
369+
assert Path(sketch_path, "build").is_dir()
370+
371+
# Verifies binaries are exported when export binaries env var is set
372+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.eep").exists()
373+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.elf").exists()
374+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.hex").exists()
375+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.with_bootloader.bin").exists()
376+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.with_bootloader.hex").exists()
377+
378+
379+
def test_compile_with_export_binaries_config(run_command, data_dir, downloads_dir):
380+
# Init the environment explicitly
381+
run_command("core update-index")
382+
383+
# Download latest AVR
384+
run_command("core install arduino:avr")
385+
386+
sketch_name = "CompileWithExportBinariesConfig"
387+
sketch_path = Path(data_dir, sketch_name)
388+
fqbn = "arduino:avr:uno"
389+
390+
# Create a test sketch
391+
assert run_command("sketch new {}".format(sketch_path))
392+
393+
# Create settings with export binaries set to true
394+
env = {
395+
"ARDUINO_DATA_DIR": data_dir,
396+
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
397+
"ARDUINO_SKETCHBOOK_DIR": data_dir,
398+
"ARDUINO_SKETCH_ALWAYS_EXPORT_BINARIES": "true",
399+
}
400+
assert run_command("config init --dest-dir .", custom_env=env)
401+
402+
# Test compilation with export binaries env var set
403+
result = run_command(f"compile -b {fqbn} {sketch_path}")
404+
assert result.ok
405+
assert Path(sketch_path, "build").exists()
406+
assert Path(sketch_path, "build").is_dir()
407+
408+
# Verifies binaries are exported when export binaries env var is set
409+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.eep").exists()
410+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.elf").exists()
411+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.hex").exists()
412+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.with_bootloader.bin").exists()
413+
assert (sketch_path / "build" / fqbn.replace(":", ".") / f"{sketch_name}.ino.with_bootloader.hex").exists()

0 commit comments

Comments
 (0)