Skip to content

Commit

Permalink
Iterating over documents to force parser throwing any exceptions it e…
Browse files Browse the repository at this point in the history
…ncounters (#574)
  • Loading branch information
zivnevo authored Jul 26, 2023
1 parent b2fa469 commit 2afc35f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion nca/FileScanners/GenericTreeScanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ def convert_documents(documents):
return [to_yaml_objects(document) for document in documents]


def leave_documents_as_is(documents):
"""
Forces the parser to yield all documents and throw parse errors (if any) at this point of time
"""
return [document for document in documents]


class GenericTreeScanner(abc.ABC):
"""
A base class for reading yaml files
Expand Down Expand Up @@ -107,7 +114,7 @@ def _yield_yaml_file(self, path, stream):
"""
try:
if self.fast_load:
documents = yaml.load_all(stream, Loader=yaml.CSafeLoader)
documents = leave_documents_as_is(yaml.load_all(stream, Loader=yaml.CSafeLoader))
else:
documents = convert_documents(yaml.compose_all(stream, Loader=yaml.CSafeLoader))
yield YamlFile(documents, path)
Expand Down
1 change: 1 addition & 0 deletions tests/bad_yamls/single_bracket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{
3 changes: 3 additions & 0 deletions tests/bad_yamls/tab_in_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": "value"
}
1 change: 1 addition & 0 deletions tests/expected_runtime/k8s_tests_expected_runtime.csv
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ test,run_time(seconds)
"k8s_cmdline_tests.yaml, query name: test25-expl",1.08
"k8s_cmdline_tests.yaml, query name: test4-expl",1.08
"k8s_cmdline_tests.yaml, query name: istio-ingress-expl",4.1
"k8s_cmdline_tests.yaml, query name: bad-yamls",0.05
fw_rules_tests/policies/cyclonus-simple-example-scheme.yaml,0.08
fw_rules_tests/policies/label_expr_test_1-scheme.yaml,0.38
fw_rules_tests/policies/poc1-scheme.yaml,0.64
Expand Down
6 changes: 6 additions & 0 deletions tests/k8s_cmdline_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,9 @@
-d
--expected_output expected_cmdline_output_files/istio-ingress_expl_output.txt
expected: 0

- name: bad-yamls
args: >
--connectivity
-r bad_yamls
expected: 4

0 comments on commit 2afc35f

Please sign in to comment.