Esempio n. 1
0
class VultureBearTest(unittest.TestCase):
    def setUp(self):
        self.section = Section('name')
        self.queue = Queue()
        self.file_dict = {}
        self.uut = VultureBear(self.file_dict, self.section, self.queue)

    def get_results(self, *files):
        """
        Runs the bears with the files given.

        :param files: A list of lists containing the file lines. File contents
                      will automatically be dedented and splitted.
        :return:      A list of the results of the uut!
        """
        with ExitStack() as stack:
            for file in files:

                @contextmanager
                def prep_file():
                    with prepare_file(dedent(file).splitlines(True),
                                      None,
                                      tempfile_kwargs={'suffix': '.py'
                                                       }) as lines_filename:
                        lines, filename = lines_filename
                        self.file_dict[filename] = file
                        yield

                stack.enter_context(prep_file())

            return list(self.uut.run())

    def test_used_variable(self):
        good_file = """
        x = 2
        print(x)
        """
        self.assertEqual(len(self.get_results(good_file)), 0)

    def test_unused_variable(self):
        bad_file = """
        b = 10
        a = 12
        print(a)
        """
        self.assertEqual(len(self.get_results(bad_file)), 1)

    def test_unused_parameter(self):
        bad_file = """
        def test(a):
            return 1

        test(1)
        """
        self.assertEqual(len(self.get_results(bad_file)), 1)

    def test_unused_function(self):
        bad_file = """
        def test(a):
            return a
        """
        self.assertEqual(len(self.get_results(bad_file)), 1)

    def test_import_confidence(self):
        bad_file = """
        from os import *
        import subprocess
        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].confidence, 95)

    def test_class_confidence(self):
        bad_file = """
        class Name:

            def __init__(self, name):
                self.name = name
                self.sur_name = 'Something'

        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].confidence, 70)

    def test_func_confidence(self):
        bad_file = """
        def hello(name):
            print('Hello World')
        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].confidence, 70)

    def test_prop_confidence(self):
        bad_file = """
        class Bar(object):
            @property
            def prop(self):
                pass

        c = Bar()
        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].confidence, 70)

    def test_var_confidence(self):
        bad_file = """
        name = 'Foo Bar'
        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].confidence, 70)
Esempio n. 2
0
 def setUp(self):
     self.section = Section('name')
     self.queue = Queue()
     self.file_dict = {}
     self.uut = VultureBear(self.file_dict, self.section, self.queue)
 def setUp(self):
     self.section = Section('name')
     self.queue = Queue()
     self.file_dict = {}
     self.uut = VultureBear(self.file_dict, self.section, self.queue)
Esempio n. 4
0
class VultureBearTest(unittest.TestCase):

    def setUp(self):
        self.section = Section('name')
        self.queue = Queue()
        self.file_dict = {}
        self.uut = VultureBear(self.file_dict, self.section, self.queue)

    def get_results(self, *files):
        """
        Runs the bears with the files given.

        :param files: A list of lists containing the file lines. File contents
                      will automatically be dedented and splitted.
        :return:      A list of the results of the uut!
        """
        with ExitStack() as stack:
            for file in files:
                @contextmanager
                def prep_file():
                    with prepare_file(
                        dedent(file).splitlines(True),
                        None,
                        tempfile_kwargs={'suffix': '.py'}
                    ) as lines_filename:
                        lines, filename = lines_filename
                        self.file_dict[filename] = file
                        yield

                stack.enter_context(prep_file())

            return list(self.uut.run())

    def test_used_variable(self):
        good_file = """
        x = 2
        print(x)
        """
        self.assertEqual(len(self.get_results(good_file)), 0)

    def test_unused_variable(self):
        bad_file = """
        b = 10
        a = 12
        print(a)
        """
        self.assertEqual(len(self.get_results(bad_file)), 1)

    def test_unused_parameter(self):
        bad_file = """
        def test(a):
            return 1

        test(1)
        """
        self.assertEqual(len(self.get_results(bad_file)), 1)

    def test_unused_function(self):
        bad_file = """
        def test(a):
            return a
        """
        self.assertEqual(len(self.get_results(bad_file)), 1)
class VultureBearTest(unittest.TestCase):

    def setUp(self):
        self.section = Section('name')
        self.queue = Queue()
        self.file_dict = {}
        self.uut = VultureBear(self.file_dict, self.section, self.queue)

    def get_results(self, *files):
        """
        Runs the bears with the files given.

        :param files: A list of lists containing the file lines. File contents
                      will automatically be dedented and splitted.
        :return:      A list of the results of the uut!
        """
        with ExitStack() as stack:
            for file in files:
                @contextmanager
                def prep_file():
                    with prepare_file(
                        dedent(file).splitlines(True),
                        None,
                        tempfile_kwargs={'suffix': '.py'}
                    ) as lines_filename:
                        lines, filename = lines_filename
                        self.file_dict[filename] = file
                        yield

                stack.enter_context(prep_file())

            return list(self.uut.run())

    def verify_results(self, test_file, expected):
        detected = dict((item.message, (item.affected_code[0].start.line,
                                        item.affected_code[0].end.line,
                                        item.confidence))
                        for item in self.get_results(load_testfile(test_file)))
        self.assertEqual(detected, expected)

    def test_used_variable(self):
        self.verify_results('used_variable.py', {})

    def test_unused_variable(self):
        self.verify_results('unused_variable.py', {
            "unused variable 'b'": (1, 1, 60)
        })

    def test_unused_arg(self):
        self.verify_results('unused_arg.py', {
            "unused variable 'a'": (1, 1, 100)
        })

    def test_import(self):
        self.verify_results('unused_import.py', {
            "unused import 'subprocess'": (2, 2, 90)
        })

    def test_class(self):
        self.verify_results('unused_class.py', {
            "unused class 'Name'": (1, 5, 60),
            "unused attribute 'name'": (4, 4, 60),
            "unused attribute 'surname'": (5, 5, 60)
        })

    def test_function(self):
        self.verify_results('unused_function.py', {
            "unused function 'hello'": (1, 2, 60)
        })

    def test_property(self):
        self.verify_results('unused_property.py', {
            "unused property 'prop'": (3, 5, 60)
        })

    def test_unsatisfiable_while(self):
        self.verify_results('unsatisfiable_while.py', {
            "unsatisfiable 'while' condition": (5, 7, 100)
        })

    def test_unsatisfiable_if(self):
        self.verify_results('unsatisfiable_if.py', {
            "unsatisfiable 'if' condition": (1, 2, 100)
        })

    def test_unreachable_else(self):
        self.verify_results('unreachable_else.py', {
            "unreachable 'else' block": (3, 6, 100)
        })
Esempio n. 6
0
class VultureBearTest(unittest.TestCase):

    def setUp(self):
        self.section = Section('name')
        self.queue = Queue()
        self.file_dict = {}
        self.uut = VultureBear(self.file_dict, self.section, self.queue)

    def get_results(self, *files):
        """
        Runs the bears with the files given.

        :param files: A list of lists containing the file lines. File contents
                      will automatically be dedented and splitted.
        :return:      A list of the results of the uut!
        """
        with ExitStack() as stack:
            for file in files:
                @contextmanager
                def prep_file():
                    with prepare_file(
                        dedent(file).splitlines(True),
                        None,
                        tempfile_kwargs={'suffix': '.py'}
                    ) as lines_filename:
                        lines, filename = lines_filename
                        self.file_dict[filename] = file
                        yield

                stack.enter_context(prep_file())

            return list(self.uut.run())

    def test_used_variable(self):
        good_file = """
        x = 2
        print(x)
        """
        self.assertEqual(len(self.get_results(good_file)), 0)

    def test_unused_variable(self):
        bad_file = """
        b = 10
        a = 12
        print(a)
        """
        self.assertEqual(len(self.get_results(bad_file)), 1)

    def test_unused_parameter(self):
        bad_file = """
        def test(a):
            return 1

        test(1)
        """
        self.assertEqual(len(self.get_results(bad_file)), 1)

    def test_unused_function(self):
        bad_file = """
        def test(a):
            return a
        """
        self.assertEqual(len(self.get_results(bad_file)), 1)

    def test_import_confidence(self):
        bad_file = """
        from os import *
        import subprocess
        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].confidence, 95)

    def test_class_confidence(self):
        bad_file = """
        class Name:

            def __init__(self, name):
                self.name = name
                self.sur_name = 'Something'

        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].confidence, 70)

    def test_func_confidence(self):
        bad_file = """
        def hello(name):
            print('Hello World')
        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].confidence, 70)

    def test_prop_confidence(self):
        bad_file = """
        class Bar(object):
            @property
            def prop(self):
                pass

        c = Bar()
        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].confidence, 70)

    def test_var_confidence(self):
        bad_file = """
        name = 'Foo Bar'
        """
        result = self.get_results(bad_file)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].confidence, 70)
Esempio n. 7
0
class VultureBearTest(unittest.TestCase):
    def setUp(self):
        self.section = Section('name')
        self.queue = Queue()
        self.file_dict = {}
        self.uut = VultureBear(self.file_dict, self.section, self.queue)

    def get_results(self, *files):
        """
        Runs the bears with the files given.

        :param files: A list of lists containing the file lines. File contents
                      will automatically be dedented and splitted.
        :return:      A list of the results of the uut!
        """
        with ExitStack() as stack:
            for file in files:

                @contextmanager
                def prep_file():
                    with prepare_file(dedent(file).splitlines(True),
                                      None,
                                      tempfile_kwargs={'suffix': '.py'
                                                       }) as lines_filename:
                        lines, filename = lines_filename
                        self.file_dict[filename] = file
                        yield

                stack.enter_context(prep_file())

            return list(self.uut.run())

    def verify_results(self, test_file, expected):
        detected = dict(
            (item.message, (item.affected_code[0].start.line,
                            item.affected_code[0].end.line, item.confidence))
            for item in self.get_results(load_testfile(test_file)))
        self.assertEqual(detected, expected)

    def test_used_variable(self):
        self.verify_results('used_variable.py', {})

    def test_unused_variable(self):
        self.verify_results('unused_variable.py',
                            {"unused variable 'b'": (1, 1, 60)})

    def test_unused_arg(self):
        self.verify_results('unused_arg.py',
                            {"unused variable 'a'": (1, 1, 100)})

    def test_import(self):
        self.verify_results('unused_import.py',
                            {"unused import 'subprocess'": (2, 2, 90)})

    def test_class(self):
        self.verify_results(
            'unused_class.py', {
                "unused class 'Name'": (1, 5, 60),
                "unused attribute 'name'": (4, 4, 60),
                "unused attribute 'surname'": (5, 5, 60)
            })

    def test_function(self):
        self.verify_results('unused_function.py',
                            {"unused function 'hello'": (1, 2, 60)})

    def test_property(self):
        self.verify_results('unused_property.py',
                            {"unused property 'prop'": (3, 5, 60)})

    def test_unsatisfiable_while(self):
        self.verify_results('unsatisfiable_while.py',
                            {"unsatisfiable 'while' condition": (5, 7, 100)})

    def test_unsatisfiable_if(self):
        self.verify_results('unsatisfiable_if.py',
                            {"unsatisfiable 'if' condition": (1, 2, 100)})

    def test_unreachable_else(self):
        self.verify_results('unreachable_else.py',
                            {"unreachable 'else' block": (3, 6, 100)})