diff --git a/synthtool/gcp/snippets.py b/synthtool/gcp/snippets.py index 72c00e3d6..c697a8ecf 100644 --- a/synthtool/gcp/snippets.py +++ b/synthtool/gcp/snippets.py @@ -87,10 +87,10 @@ def all_snippets_from_file(sample_file: str) -> Dict[str, str]: close_match = re.match(pattern=CLOSE_SNIPPET_REGEX, string=line) open_exclude_match = re.match(pattern=OPEN_EXCLUDE_REGEX, string=line) close_exclude_match = re.match(pattern=CLOSE_EXCLUDE_REGEX, string=line) - if open_match: + if open_match and not excluding: open_snippets.add(open_match[1]) snippet_lines[open_match[1]] = [] - elif close_match: + elif close_match and not excluding: open_snippets.discard(close_match[1]) elif open_exclude_match: excluding = True diff --git a/tests/fixtures/snippets/interleaved_with_exclude.js b/tests/fixtures/snippets/interleaved_with_exclude.js new file mode 100644 index 000000000..21b3c39d3 --- /dev/null +++ b/tests/fixtures/snippets/interleaved_with_exclude.js @@ -0,0 +1,25 @@ +/** + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START snippet_1] +var line1 = 1; +// [START_EXCLUDE] +// [START snippet_2] +var line2 = 2; +// [END snippet_2] +// [END_EXCLUDE] +var line3 = 3; +// [END snippet_1] diff --git a/tests/test_snippets.py b/tests/test_snippets.py index 60c28671f..5870db510 100644 --- a/tests/test_snippets.py +++ b/tests/test_snippets.py @@ -85,6 +85,25 @@ def test_interleaving_snippets(): os.chdir(cwd) +def test_interleaving_snippets_with_exclude(): + cwd = os.getcwd() + os.chdir(FIXTURES) + + all_snippets = snippets.all_snippets_from_file( + "snippets/interleaved_with_exclude.js" + ) + assert len(all_snippets) == 1 + + assert ( + all_snippets["snippet_1"] + == """var line1 = 1; +var line3 = 3; +""" + ) + + os.chdir(cwd) + + def test_nested_snippets(): cwd = os.getcwd() os.chdir(FIXTURES)