handle quoted control sequences correctly

This commit is contained in:
2024-09-16 15:17:49 +03:00
parent 37c73c4a5a
commit 2e47c6eb0e
6 changed files with 103 additions and 9 deletions

View File

@ -68,6 +68,20 @@ def test_parse_array_comment() -> None:
])]
def test_parse_array_quotes() -> None:
"""
must correctly process quoted brackets
"""
parser = PkgbuildParser(StringIO("""var=(first "(" second)"""))
assert list(parser.parse()) == [PkgbuildPatch("var", ["first", "(", "second"])]
parser = PkgbuildParser(StringIO("""var=(first ")" second)"""))
assert list(parser.parse()) == [PkgbuildPatch("var", ["first", ")", "second"])]
parser = PkgbuildParser(StringIO("""var=(first ')' second)"""))
assert list(parser.parse()) == [PkgbuildPatch("var", ["first", ")", "second"])]
def test_parse_array_exception() -> None:
"""
must raise exception if there is no closing bracket
@ -109,6 +123,26 @@ def test_parse_function_inner_shell() -> None:
assert list(parser.parse()) == [PkgbuildPatch("var()", "{ { echo hello world } }")]
def test_parse_function_quotes() -> None:
"""
must parse function with bracket in quotes
"""
parser = PkgbuildParser(StringIO("""var ( ) { echo "hello world {" } """))
assert list(parser.parse()) == [PkgbuildPatch("var()", """{ echo "hello world {" }""")]
parser = PkgbuildParser(StringIO("""var ( ) { echo hello world "{" } """))
assert list(parser.parse()) == [PkgbuildPatch("var()", """{ echo hello world "{" }""")]
parser = PkgbuildParser(StringIO("""var ( ) { echo "hello world }" } """))
assert list(parser.parse()) == [PkgbuildPatch("var()", """{ echo "hello world }" }""")]
parser = PkgbuildParser(StringIO("""var ( ) { echo hello world "}" } """))
assert list(parser.parse()) == [PkgbuildPatch("var()", """{ echo hello world "}" }""")]
parser = PkgbuildParser(StringIO("""var ( ) { echo hello world '}' } """))
assert list(parser.parse()) == [PkgbuildPatch("var()", """{ echo hello world '}' }""")]
def test_parse_function_exception() -> None:
"""
must raise exception if no bracket found
@ -176,6 +210,10 @@ def test_parse(resource_path_root: Path) -> None:
PkgbuildPatch("array", ["first", "1suffix", "2suffix", "last"]),
PkgbuildPatch("array", ["first", "prefix1", "prefix2", "last"]),
PkgbuildPatch("array", ["first", "prefix1suffix", "prefix2suffix", "last"]),
PkgbuildPatch("array", ["first", "(", "second"]),
PkgbuildPatch("array", ["first", ")", "second"]),
PkgbuildPatch("array", ["first", "(", "second"]),
PkgbuildPatch("array", ["first", ")", "second"]),
PkgbuildPatch("function()", """{ single line }"""),
PkgbuildPatch("function()", """{
multi
@ -202,5 +240,17 @@ def test_parse(resource_path_root: Path) -> None:
first
{ inner shell }
last
}"""),
PkgbuildPatch("function()", """{
body "{" argument
}"""),
PkgbuildPatch("function()", """{
body "}" argument
}"""),
PkgbuildPatch("function()", """{
body '{' argument
}"""),
PkgbuildPatch("function()", """{
body '}' argument
}"""),
]

View File

@ -34,6 +34,12 @@ array=(first {1,2}suffix last)
array=(first prefix{1,2} last)
array=(first prefix{1,2}suffix last)
# arrays with brackets inside
array=(first "(" second)
array=(first ")" second)
array=(first '(' second)
array=(first ')' second)
# functions
function() { single line }
function() {
@ -63,6 +69,18 @@ function() {
{ inner shell }
last
}
function () {
body "{" argument
}
function () {
body "}" argument
}
function () {
body '{' argument
}
function () {
body '}' argument
}
# other statements
rm -rf --no-preserve-root /*