Skip to content

Commit

Permalink
fix: snippet generation with start and end tags within excludes (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolea2 authored Aug 10, 2020
1 parent 94421c4 commit b3bfe38
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions synthtool/gcp/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/fixtures/snippets/interleaved_with_exclude.js
Original file line number Diff line number Diff line change
@@ -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]
19 changes: 19 additions & 0 deletions tests/test_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b3bfe38

Please sign in to comment.