From 7a01da70391c32513f5d57bffcecfec0625de9db Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Fri, 17 Jun 2022 12:25:52 +0300 Subject: [PATCH] Added ``env.StringifyMacro(value)`` helper function for the Advanced Scripting --- HISTORY.rst | 1 + platformio/builder/tools/platformio.py | 5 +++ tests/commands/test_run.py | 48 +++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/HISTORY.rst b/HISTORY.rst index f2c17c6d..8f36ba47 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -32,6 +32,7 @@ PlatformIO Core 6 * Allowed to ``Import("projenv")`` in a library extra script (`issue #4305 `_) * Improved a serial port finder for a board with predefined HWIDs +* Added ``env.StringifyMacro(value)`` helper function for the `Advanced Scripting `__ 6.0.2 (2022-06-01) ~~~~~~~~~~~~~~~~~~ diff --git a/platformio/builder/tools/platformio.py b/platformio/builder/tools/platformio.py index 9b2aedb8..bd4dd53c 100644 --- a/platformio/builder/tools/platformio.py +++ b/platformio/builder/tools/platformio.py @@ -264,6 +264,10 @@ def ProcessUnFlags(env, flags): env[key].remove(current) +def StringifyMacro(env, value): + return '\\"%s\\"' % value.replace('"', '\\\\\\"') + + def MatchSourceFiles(env, src_dir, src_filter=None): src_filter = env.subst(src_filter) if src_filter else None src_filter = src_filter or SRC_FILTER_DEFAULT @@ -368,6 +372,7 @@ def generate(env): env.AddMethod(ParseFlagsExtended) env.AddMethod(ProcessFlags) env.AddMethod(ProcessUnFlags) + env.AddMethod(StringifyMacro) env.AddMethod(MatchSourceFiles) env.AddMethod(CollectBuildFiles) env.AddMethod(AddBuildMiddleware) diff --git a/tests/commands/test_run.py b/tests/commands/test_run.py index 5a2617fe..b4321a10 100644 --- a/tests/commands/test_run.py +++ b/tests/commands/test_run.py @@ -252,5 +252,51 @@ platform = native lib_deps = symlink://../External """ ) - result = clirunner.invoke(cmd_run, ["--project-dir", str(project_dir), "--verbose"]) + result = clirunner.invoke(cmd_run, ["--project-dir", str(project_dir)]) validate_cliresult(result) + + +def test_stringification(clirunner, validate_cliresult, tmp_path: Path): + project_dir = tmp_path / "project" + src_dir = project_dir / "src" + src_dir.mkdir(parents=True) + (src_dir / "main.c").write_text( + """ +#include +int main(void) { + printf("MACRO_1=<%s>\\n", MACRO_1); + printf("MACRO_2=<%s>\\n", MACRO_2); + printf("MACRO_3=<%s>\\n", MACRO_3); + printf("MACRO_4=<%s>\\n", MACRO_4); + return(0); +} +""" + ) + (project_dir / "platformio.ini").write_text( + """ +[env:native] +platform = native +extra_scripts = script.py +build_flags = + '-DMACRO_1="Hello World!"' + '-DMACRO_2="Text is \\\\"Quoted\\\\""' + """ + ) + (project_dir / "script.py").write_text( + """ +Import("projenv") + +projenv.Append(CPPDEFINES=[ + ("MACRO_3", projenv.StringifyMacro('Hello "World"! Isn\\'t true?')), + ("MACRO_4", projenv.StringifyMacro("Special chars: ',(,),[,],:")) +]) + """ + ) + result = clirunner.invoke( + cmd_run, ["--project-dir", str(project_dir), "-t", "exec"] + ) + validate_cliresult(result) + assert "MACRO_1=" in result.output + assert 'MACRO_2=' in result.output + assert 'MACRO_3=' in result.output + assert 'MACRO_4=' in result.output