Skip to content

Commit 2607ffc

Browse files
authored
fix: log a warning and continue on invalid sample metadata yaml (#1230)
* test: failing test for bad metadata * fix: log a warning and continue on invalid sample metadata yaml * chore: fix lint * chore: fix lint
1 parent e33d52a commit 2607ffc

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

synthtool/gcp/samples.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import yaml
1919
from typing import List, Dict
20+
from synthtool.log import logger
2021

2122

2223
def _read_sample_metadata_comment(sample_file: str) -> Dict:
@@ -37,9 +38,13 @@ def _read_sample_metadata_comment(sample_file: str) -> Dict:
3738
# the metadata yaml is stored in a comments, remove the
3839
# prefix so that we can parse the yaml contained.
3940
sample_metadata_string = re.sub(r"((#|//) ?)", "", match.group("metadata"))
40-
sample_metadata = yaml.load(sample_metadata_string, Loader=yaml.SafeLoader)[
41-
"sample-metadata"
42-
]
41+
try:
42+
sample_metadata = yaml.load(
43+
sample_metadata_string, Loader=yaml.SafeLoader
44+
)["sample-metadata"]
45+
except yaml.scanner.ScannerError:
46+
# warn and continue on bad metadata
47+
logger.warning(f"bad metadata detected in {sample_file}")
4348
return sample_metadata
4449

4550

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
//
17+
// sample-metadata:
18+
// title: Metadata Example 1
19+
// description: this is a description.
20+
// bad wrapping here
21+
// usage: node hello-world.js
22+
//
23+
'use strict';
24+
25+
async function viewBucketIamMembers(bucketName) {
26+
// [START storage_view_bucket_iam_members]
27+
// Imports the Google Cloud client library
28+
// [END storage_view_bucket_iam_members]
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
//
17+
// sample-metadata:
18+
// title: Metadata Example 1
19+
// description: this is a description.
20+
// usage: node hello-world.js
21+
//
22+
'use strict';
23+
24+
async function viewBucketIamMembers(bucketName) {
25+
// [START storage_view_bucket_iam_members]
26+
// Imports the Google Cloud client library
27+
// [END storage_view_bucket_iam_members]
28+
}

tests/test_samples.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ def test_load_node_samples():
3333
assert all_samples[0]["usage"] == "node hello-world.js"
3434
assert all_samples[1]["title"] == "Metadata Example 2"
3535
assert all_samples[1]["usage"] == "node goodnight-moon.js"
36+
37+
38+
def test_bad_metadata():
39+
with util.chdir(FIXTURES / "node_templates" / "bad_metadata"):
40+
all_samples = samples.all_samples(["samples/*.js"])
41+
42+
# should have included additional meta-information provided.
43+
assert all_samples[0]["title"] == "Bad_metadata1"
44+
assert "usage" not in all_samples[0]
45+
assert all_samples[1]["title"] == "Metadata Example 1"
46+
assert all_samples[1]["usage"] == "node hello-world.js"

0 commit comments

Comments
 (0)