Skip to content

Commit 2afc35f

Browse files
authored
Iterating over documents to force parser throwing any exceptions it encounters (#574)
1 parent b2fa469 commit 2afc35f

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

nca/FileScanners/GenericTreeScanner.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ def convert_documents(documents):
7575
return [to_yaml_objects(document) for document in documents]
7676

7777

78+
def leave_documents_as_is(documents):
79+
"""
80+
Forces the parser to yield all documents and throw parse errors (if any) at this point of time
81+
"""
82+
return [document for document in documents]
83+
84+
7885
class GenericTreeScanner(abc.ABC):
7986
"""
8087
A base class for reading yaml files
@@ -107,7 +114,7 @@ def _yield_yaml_file(self, path, stream):
107114
"""
108115
try:
109116
if self.fast_load:
110-
documents = yaml.load_all(stream, Loader=yaml.CSafeLoader)
117+
documents = leave_documents_as_is(yaml.load_all(stream, Loader=yaml.CSafeLoader))
111118
else:
112119
documents = convert_documents(yaml.compose_all(stream, Loader=yaml.CSafeLoader))
113120
yield YamlFile(documents, path)

tests/bad_yamls/single_bracket.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{

tests/bad_yamls/tab_in_json.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"key": "value"
3+
}

tests/expected_runtime/k8s_tests_expected_runtime.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ test,run_time(seconds)
5555
"k8s_cmdline_tests.yaml, query name: test25-expl",1.08
5656
"k8s_cmdline_tests.yaml, query name: test4-expl",1.08
5757
"k8s_cmdline_tests.yaml, query name: istio-ingress-expl",4.1
58+
"k8s_cmdline_tests.yaml, query name: bad-yamls",0.05
5859
fw_rules_tests/policies/cyclonus-simple-example-scheme.yaml,0.08
5960
fw_rules_tests/policies/label_expr_test_1-scheme.yaml,0.38
6061
fw_rules_tests/policies/poc1-scheme.yaml,0.64

tests/k8s_cmdline_tests.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,9 @@
500500
-d
501501
--expected_output expected_cmdline_output_files/istio-ingress_expl_output.txt
502502
expected: 0
503+
504+
- name: bad-yamls
505+
args: >
506+
--connectivity
507+
-r bad_yamls
508+
expected: 4

0 commit comments

Comments
 (0)