diff --git a/tools/setup.py b/tools/setup.py index 1f065769a7..1a0b491c01 100755 --- a/tools/setup.py +++ b/tools/setup.py @@ -54,6 +54,18 @@ gn_args_header = [ ] +def gn_string(s): + # In gn, strings are enclosed in double-quotes and use backslash as the + # escape character. The only escape sequences supported are: + # \" (for literal quote) + # \$ (for literal dollars sign) + # \\ (for literal backslash) + # Any other use of a backslash is treated as a literal backslash. + s = re.sub(r'("|\$|\\(?=["$\\]))', r'\\\1', s) + s = '"' + s + '"' + return s + + def gn_args_are_generated(lines): for line in lines: if re.match("^\s*#.*REMOVE THIS LINE", line): diff --git a/tools/setup_test.py b/tools/setup_test.py index 670bf33dba..05f094c60d 100644 --- a/tools/setup_test.py +++ b/tools/setup_test.py @@ -1,11 +1,18 @@ # Copyright 2018 the Deno authors. All rights reserved. MIT license. import os -from setup import read_gn_args, write_gn_args +from setup import gn_string, read_gn_args, write_gn_args from shutil import rmtree from tempfile import mktemp +def gn_string_test(): + assert '"abc"' == gn_string('abc') + assert '"foo\\$bar\\"baz"' == gn_string('foo$bar"baz') + assert '"do\\not\\escape"' == gn_string('do\\not\\escape') + assert '"so\\\\\\very\\\\\\"fun\\"' == gn_string('so\\\\very\\"fun\\') + + def read_gn_args_test(): # Args file doesn't exist. (args, hand_edited) = read_gn_args("/baddir/hopefully/nonexistent/args.gn") @@ -53,6 +60,7 @@ def write_gn_args_test(): def setup_test(): + gn_string_test() read_gn_args_test() write_gn_args_test()