From 6cb63dbbd8c152e9e034b2ed0e1af9f42170b872 Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Fri, 4 Sep 2015 15:30:02 -0700 Subject: [PATCH] Unit testing of meta_ast.py This patch adds a unit test and also Replaces report() with __str__ so it just returns a string rather than printing to stdout. Coverage goes from 44% to 100%. Change-Id: I23af0e09415651a2caaf82f327d3106ff5db65ad --- bandit/bandit.py | 2 +- bandit/core/manager.py | 4 ---- bandit/core/meta_ast.py | 8 +++---- tests/unit/core/test_meta_ast.py | 41 ++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 tests/unit/core/test_meta_ast.py diff --git a/bandit/bandit.py b/bandit/bandit.py index 1c8eca7d..8343e63e 100644 --- a/bandit/bandit.py +++ b/bandit/bandit.py @@ -227,7 +227,7 @@ def main(): # initiate execution of tests within Bandit Manager b_mgr.run_tests() if args.debug: - b_mgr.output_metaast() + print(b_mgr.b_ma) # trigger output of results by Bandit Manager b_mgr.output_results(args.context_lines, args.severity - 1, diff --git a/bandit/core/manager.py b/bandit/core/manager.py index c17c37c1..e491d3b0 100644 --- a/bandit/core/manager.py +++ b/bandit/core/manager.py @@ -130,10 +130,6 @@ class BanditManager(): output_filename=output_filename, output_format=output_format ) - def output_metaast(self): - '''Outputs all the nodes from the Meta AST.''' - self.b_ma.report() - def discover_files(self, targets, recursive=False): '''Add tests directly and from a directory to the test set diff --git a/bandit/core/meta_ast.py b/bandit/core/meta_ast.py index 484b13ca..bb2034a8 100644 --- a/bandit/core/meta_ast.py +++ b/bandit/core/meta_ast.py @@ -40,15 +40,15 @@ class BanditMetaAst(): 'raw': node, 'parent_id': parent_id, 'depth': depth } - def report(self): + def __str__(self): '''Dumps a listing of all of the nodes - Dumps (prints) a listing of all of the nodes for debugging purposes + Dumps a listing of all of the nodes for debugging purposes :return: - ''' tmpstr = "" for k, v in self.nodes.items(): tmpstr += "Node: %s\n" % k tmpstr += "\t%s\n" % str(v) - tmpstr += "Length : %s\n" % len(self.nodes) - print(tmpstr) + tmpstr += "Length: %s\n" % len(self.nodes) + return tmpstr diff --git a/tests/unit/core/test_meta_ast.py b/tests/unit/core/test_meta_ast.py new file mode 100644 index 00000000..452832d7 --- /dev/null +++ b/tests/unit/core/test_meta_ast.py @@ -0,0 +1,41 @@ +# Copyright (c) 2015 VMware, Inc. +# +# 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. + +import six +import testtools + +from bandit.core import meta_ast + + +class BanditMetaAstTests(testtools.TestCase): + + def setUp(self): + super(BanditMetaAstTests, self).setUp() + self.b_meta_ast = meta_ast.BanditMetaAst() + self.node = 'fake_node' + self.parent_id = 'fake_parent_id' + self.depth = 1 + self.b_meta_ast.add_node(self.node, self.parent_id, self.depth) + self.node_id = hex(id(self.node)) + + def test_add_node(self): + expected = {'raw': self.node, + 'parent_id': self.parent_id, + 'depth': self.depth} + self.assertEqual(expected, self.b_meta_ast.nodes[self.node_id]) + + def test_str(self): + node = self.b_meta_ast.nodes[self.node_id] + expected = 'Node: %s\n\t%s\nLength: 1\n' % (self.node_id, node) + self.assertEqual(expected, six.text_type(self.b_meta_ast))