Exemplo n.º 1
0
    def test_back_saving(self):
        filename = os.path.join(tempfile.gettempdir(),
                                'SectionManagerTestFile')

        # We need to use a bad filename or this will parse coalas .coafile
        # Despite missing settings (coala isn't run) the file is saved
        with self.assertRaises(SystemExit):
            gather_configuration(lambda *args: True,
                                 self.log_printer,
                                 arg_list=[
                                     '-S', 'save=' + escape(filename, '\\'),
                                     '-c=some_bad_filename'
                                 ])

        with open(filename, 'r') as f:
            lines = f.readlines()
        self.assertEqual(['[cli]\n', 'config = some_bad_filename\n'], lines)

        with self.assertRaises(SystemExit):
            gather_configuration(lambda *args: True,
                                 self.log_printer,
                                 arg_list=[
                                     '-S', 'save=true',
                                     'config=' + escape(filename, '\\'),
                                     'test.value=5'
                                 ])

        with open(filename, 'r') as f:
            lines = f.readlines()
        os.remove(filename)
        if os.path.sep == '\\':
            filename = escape(filename, '\\')
        self.assertEqual([
            '[cli]\n', 'config = ' + filename + '\n', '[test]\n', 'value = 5\n'
        ], lines)
Exemplo n.º 2
0
    def test_merge(self):
        tmp = Constants.system_coafile
        Constants.system_coafile = os.path.abspath(
            os.path.join(os.path.dirname(os.path.realpath(__file__)), "section_manager_test_files", "default_coafile")
        )

        config = os.path.abspath(
            os.path.join(os.path.dirname(os.path.realpath(__file__)), "section_manager_test_files", ".coafile")
        )

        # Check merging of default_coafile and .coafile
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True, self.log_printer, arg_list=["-c", re.escape(config)]
        )

        self.assertEqual(str(sections["test"]), "test {value : '2'}")
        self.assertEqual(str(sections["test-2"]), "test-2 {files : '.', bears : 'LineCountBear'}")

        # Check merging of default_coafile, .coafile and cli
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=["-c", re.escape(config), "-S", "test.value=3", "test-2.bears=", "test-5.bears=TestBear2"],
        )

        self.assertEqual(str(sections["test"]), "test {value : '3'}")
        self.assertEqual(str(sections["test-2"]), "test-2 {files : '.', bears : ''}")
        self.assertEqual(str(sections["test-3"]), "test-3 {files : 'MakeFile'}")
        self.assertEqual(str(sections["test-4"]), "test-4 {bears : 'TestBear'}")
        self.assertEqual(str(sections["test-5"]), "test-5 {bears : 'TestBear2'}")

        Constants.system_coafile = tmp
    def test_back_saving(self):
        filename = os.path.join(tempfile.gettempdir(),
                                "SectionManagerTestFile")

        # We need to use a bad filename or this will parse coalas .coafile
        gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=['-S',
                      "save=" + escape(filename, '\\'),
                      "-c=some_bad_filename"])

        with open(filename, "r") as f:
            lines = f.readlines()
        self.assertEqual(["[Default]\n", "config = some_bad_filename\n"], lines)

        gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=['-S',
                      "save=true",
                      "config=" + escape(filename, '\\'),
                      "test.value=5"])

        with open(filename, "r") as f:
            lines = f.readlines()
        os.remove(filename)
        if os.path.sep == '\\':
            filename = escape(filename, '\\')
        self.assertEqual(["[Default]\n",
                          "config = " + filename + "\n",
                          "\n",
                          "[test]\n",
                          "value = 5\n"], lines)
    def test_back_saving(self):
        filename = os.path.join(tempfile.gettempdir(), "SectionManagerTestFile")

        # We need to use a bad filename or this will parse coalas .coafile
        # Despite missing settings (coala isn't run) the file is saved
        with self.assertRaises(SystemExit):
            gather_configuration(
                lambda *args: True,
                self.log_printer,
                arg_list=["-S", "save=" + escape(filename, "\\"), "-c=some_bad_filename"],
            )

        with open(filename, "r") as f:
            lines = f.readlines()
        self.assertEqual(["[cli]\n", "config = some_bad_filename\n"], lines)

        with self.assertRaises(SystemExit):
            gather_configuration(
                lambda *args: True,
                self.log_printer,
                arg_list=["-S", "save=true", "config=" + escape(filename, "\\"), "test.value=5"],
            )

        with open(filename, "r") as f:
            lines = f.readlines()
        os.remove(filename)
        if os.path.sep == "\\":
            filename = escape(filename, "\\")
        self.assertEqual(["[cli]\n", "config = " + filename + "\n", "[test]\n", "value = 5\n"], lines)
Exemplo n.º 5
0
    def test_merge(self):
        Constants.system_coafile = os.path.abspath(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         'section_manager_test_files', 'system_coafile'))

        config = os.path.abspath(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         'section_manager_test_files', '.coafile'))

        # Check merging of system_coafile and .coafile
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True, self.log_printer, arg_list=['-c', config])

        self.assertEqual(str(sections['test']), "test {value : '2'}")
        self.assertEqual(str(sections['test-2']),
                         "test-2 {files : '.', bears : 'LineCountBear'}")

        # Check merging of system_coafile, .coafile and cli
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=[
                '-c', config, '-S', 'test.value=3', 'test-2.bears=',
                'test-5.bears=TestBear2'
            ])

        self.assertEqual(str(sections['test']), "test {value : '3'}")
        self.assertEqual(str(sections['test-2']),
                         "test-2 {files : '.', bears : ''}")
        self.assertEqual(str(sections['test-3']),
                         "test-3 {files : 'MakeFile'}")
        self.assertEqual(str(sections['test-4']),
                         "test-4 {bears : 'TestBear'}")
        self.assertEqual(str(sections['test-5']),
                         "test-5 {bears : 'TestBear2'}")
    def test_back_saving(self):
        filename = os.path.join(tempfile.gettempdir(),
                                "SectionManagerTestFile")

        # We need to use a bad filename or this will parse coalas .coafile
        gather_configuration(lambda *args: True,
                             self.log_printer,
                             arg_list=[
                                 '-S', "save=" + re.escape(filename),
                                 "-c=some_bad_filename"
                             ])

        with open(filename, "r") as f:
            lines = f.readlines()
        self.assertEqual(["[Default]\n", "config = some_bad_filename\n"],
                         lines)

        gather_configuration(lambda *args: True,
                             self.log_printer,
                             arg_list=[
                                 '-S', "save=true",
                                 "config=" + re.escape(filename),
                                 "test.value=5"
                             ])

        with open(filename, "r") as f:
            lines = f.readlines()
        os.remove(filename)
        self.assertEqual([
            "[Default]\n", "config = " + filename + "\n", "\n", "[test]\n",
            "value = 5\n"
        ], lines)
Exemplo n.º 7
0
    def test_autoapply_arg(self):
        sections, _, _, _ = gather_configuration(lambda *args: True, self.log_printer, autoapply=False, arg_list=[])

        self.assertEqual(str(sections["default"].get("autoapply", None)), "False")

        sections, _, _, _ = gather_configuration(lambda *args: True, self.log_printer, autoapply=True, arg_list=[])

        self.assertEqual(str(sections["default"].get("autoapply", None)), "None")
Exemplo n.º 8
0
    def test_nonexistent_file(self):
        filename = "bad.one/test\neven with bad chars in it"
        with self.assertRaises(SystemExit):
            gather_configuration(lambda *args: True, self.log_printer, arg_list=["-S", "config=" + filename])

        tmp = Constants.system_coafile
        Constants.system_coafile = filename

        with self.assertRaises(SystemExit):
            gather_configuration(lambda *args: True, self.log_printer, arg_list=[])

        Constants.system_coafile = tmp
Exemplo n.º 9
0
    def test_nonexistent_file(self):
        filename = "bad.one/test\neven with bad chars in it"
        # Shouldn't throw an exception
        gather_configuration(lambda *args: True,
                             self.log_printer,
                             arg_list=['-S', "config=" + filename])

        tmp = Constants.system_coafile
        Constants.system_coafile = filename
        # Shouldn't throw an exception
        gather_configuration(lambda *args: True, self.log_printer)
        Constants.system_coafile = tmp
Exemplo n.º 10
0
    def test_autoapply_arg(self):
        sections, dummy, dummy, dummy = gather_configuration(
            lambda *args: True, self.log_printer, autoapply=False, arg_list=[])

        self.assertEqual(str(sections['default'].get('autoapply', None)),
                         'False')

        sections, dummy, dummy, dummy = gather_configuration(
            lambda *args: True, self.log_printer, autoapply=True, arg_list=[])

        self.assertEqual(str(sections['default'].get('autoapply', None)),
                         'None')
Exemplo n.º 11
0
    def test_default_section_deprecation_warning(self):
        logger = logging.getLogger()

        with self.assertLogs(logger, "WARNING") as cm:
            # This gathers the configuration from the '.coafile' of this repo.
            gather_configuration(lambda *args: True, self.log_printer, arg_list=[])

        self.assertIn("WARNING", cm.output[0])

        with retrieve_stdout() as stdout:
            load_configuration(["--no-config"], self.log_printer)
            self.assertNotIn("WARNING", stdout.getvalue())
Exemplo n.º 12
0
    def test_nonexistent_file(self):
        filename = 'bad.one/test\neven with bad chars in it'
        with self.assertRaises(SystemExit):
            gather_configuration(lambda *args: True,
                                 self.log_printer,
                                 arg_list=['-S', 'config=' + filename])

        Constants.system_coafile = filename

        with self.assertRaises(SystemExit):
            gather_configuration(lambda *args: True,
                                 self.log_printer,
                                 arg_list=[])
Exemplo n.º 13
0
    def test_default_section_deprecation_warning(self):
        logger = logging.getLogger()

        with self.assertLogs(logger, 'WARNING') as cm:
            # This gathers the configuration from the '.coafile' of this repo.
            gather_configuration(lambda *args: True,
                                 self.log_printer,
                                 arg_list=[])

        self.assertIn('WARNING', cm.output[0])

        with retrieve_stdout() as stdout:
            load_configuration(['--no-config'], self.log_printer)
            self.assertNotIn('WARNING', stdout.getvalue())
Exemplo n.º 14
0
    def setUp(self):
        config_path = os.path.abspath(os.path.join(
            os.path.dirname(__file__),
            "section_executor_test_files",
            ".coafile"))
        self.testcode_c_path = os.path.join(os.path.dirname(config_path),
                                            "testcode.c")

        self.result_queue = queue.Queue()
        self.queue = queue.Queue()
        self.log_queue = queue.Queue()
        self.console_printer = ConsolePrinter()
        log_printer = LogPrinter(ConsolePrinter())
        self.log_printer = ProcessingTestLogPrinter(self.log_queue)

        (self.sections,
         self.local_bears,
         self.global_bears,
         targets) = gather_configuration(lambda *args: True,
                                         log_printer,
                                         arg_list=["--config",
                                                   re.escape(config_path)])
        self.assertEqual(len(self.local_bears["default"]), 1)
        self.assertEqual(len(self.global_bears["default"]), 1)
        self.assertEqual(targets, [])
Exemplo n.º 15
0
    def setUp(self):
        config_path = os.path.abspath(os.path.join(
            os.path.dirname(__file__),
            'section_executor_test_files',
            '.coafile'))
        self.testcode_c_path = os.path.join(os.path.dirname(config_path),
                                            'testcode.c')
        self.unreadable_path = os.path.join(os.path.dirname(config_path),
                                            'unreadable')

        self.result_queue = queue.Queue()
        self.queue = queue.Queue()
        self.log_queue = queue.Queue()
        self.console_printer = ConsolePrinter()
        log_printer = LogPrinter(ConsolePrinter())
        self.log_printer = ProcessingTestLogPrinter(self.log_queue)

        (self.sections,
         self.local_bears,
         self.global_bears,
         targets) = gather_configuration(lambda *args: True,
                                         log_printer,
                                         arg_list=['--config',
                                                   config_path])
        self.assertEqual(len(self.local_bears['cli']), 1)
        self.assertEqual(len(self.global_bears['cli']), 1)
        self.assertEqual(targets, [])
Exemplo n.º 16
0
    def test_find_user_config(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        c_file = os.path.join(current_dir, 'section_manager_test_files',
                              'project', 'test.c')

        retval = find_user_config(c_file, 1)
        self.assertEqual('', retval)

        retval = find_user_config(c_file, 2)
        self.assertEqual(
            os.path.join(current_dir, 'section_manager_test_files',
                         '.coafile'), retval)

        child_dir = os.path.join(current_dir, 'section_manager_test_files',
                                 'child_dir')
        retval = find_user_config(child_dir, 2)
        self.assertEqual(
            os.path.join(current_dir, 'section_manager_test_files',
                         'child_dir', '.coafile'), retval)

        with change_directory(child_dir):
            sections, _, _, _ = gather_configuration(
                lambda *args: True,
                self.log_printer,
                arg_list=['--find-config'])
            self.assertEqual(bool(sections['cli']['find_config']), True)
Exemplo n.º 17
0
    def test_section_inheritance(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        test_dir = os.path.join(current_dir, 'section_manager_test_files')
        logger = logging.getLogger()

        with change_directory(test_dir), \
                self.assertLogs(logger, 'WARNING') as cm:
            sections, _, _, _ = gather_configuration(
                lambda *args: True,
                self.log_printer,
                arg_list=['-c', 'inherit_coafile'])
            self.assertEqual(sections['all.python'].defaults, sections['all'])
            self.assertEqual(sections['all.c']['key'], sections['cli']['key'])
            self.assertEqual(sections['java.test'].defaults, sections['cli'])
            self.assertEqual(int(sections['all.python']['max_line_length']),
                             80)
            self.assertEqual(sections['all.python.codestyle'].defaults,
                             sections['all.python'])
            self.assertEqual(sections['all.java.codestyle'].defaults,
                             sections['all'])
            self.assertEqual(str(sections['all']['ignore']), './vendor')
            sections['cli']['ignore'] = './user'
            self.assertEqual(str(sections['all']['ignore']),
                             './user, ./vendor')
            sections['cli']['ignore'] = './client'
            self.assertEqual(str(sections['all']['ignore']),
                             './client, ./vendor')
        self.assertRegex(cm.output[0],
                         '\'cli\' is an internally reserved section name.')
Exemplo n.º 18
0
    def Analyze(self):
        """
        This method analyzes the document and sends back the result

        :return: The output is a list with an element for each section.
                 It contains:
                 - The name of the section
                 - Boolean which is true if all bears in the section executed
                   successfully
                 - List of results where each result is a list which contains:
                   (str)origin, (str)message, (str)file, (str)line_nr,
                   (str)severity
        """
        retval = []
        if self.path == "" or self.config_file == "":
            return retval

        args = ["--config=" + self.config_file]

        log_printer = ListLogPrinter()
        exitcode = 0
        try:
            yielded_results = False
            (sections,
             local_bears,
             global_bears,
             targets) = gather_configuration(fail_acquire_settings,
                                             log_printer,
                                             arg_list=args)

            for section_name in sections:
                section = sections[section_name]

                if not section.is_enabled(targets):
                    continue

                if any([fnmatch(self.path, file_pattern)
                        for file_pattern in path_list(section["files"])]):

                    section["files"].value = self.path
                    section_result = execute_section(
                        section=section,
                        global_bear_list=global_bears[section_name],
                        local_bear_list=local_bears[section_name],
                        print_results=lambda *args: True,
                        log_printer=log_printer,
                        file_diff_dict={})
                    yielded_results = yielded_results or section_result[0]

                    retval.append(
                        DbusDocument.results_to_dbus_struct(section_result,
                                                            section_name))

            if yielded_results:
                exitcode = 1
        except BaseException as exception:  # pylint: disable=broad-except
            exitcode = exitcode or get_exitcode(exception, log_printer)

        logs = [log.to_string_dict() for log in log_printer.logs]
        return (exitcode, logs, retval)
    def test_section_inheritance(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        test_dir = os.path.join(current_dir, 'section_manager_test_files')
        logger = logging.getLogger()

        with change_directory(test_dir), \
                self.assertLogs(logger, 'WARNING') as cm:
            sections, _, _, _ = gather_configuration(
                lambda *args: True,
                self.log_printer,
                arg_list=['-c', 'inherit_coafile'])
            self.assertEqual(sections['all.python'].defaults, sections['all'])
            self.assertEqual(sections['all.c']['key'],
                             sections['cli']['key'])
            self.assertEqual(sections['java.test'].defaults,
                             sections['cli'])
            self.assertEqual(int(sections['all.python']['max_line_length']),
                             80)
            self.assertEqual(sections['all.python.codestyle'].defaults,
                             sections['all.python'])
            self.assertEqual(sections['all.java.codestyle'].defaults,
                             sections['all'])
            self.assertEqual(str(sections['all']['ignore']),
                             './vendor')
            sections['cli']['ignore'] = './user'
            self.assertEqual(str(sections['all']['ignore']),
                             './user, ./vendor')
            sections['cli']['ignore'] = './client'
            self.assertEqual(str(sections['all']['ignore']),
                             './client, ./vendor')
        self.assertRegex(cm.output[0],
                         '\'cli\' is an internally reserved section name.')
    def test_targets(self):
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=["default", "test1", "test2"])

        self.assertEqual(targets, ["default", "test1", "test2"])
Exemplo n.º 21
0
    def test_find_user_config(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        c_file = os.path.join(current_dir, "section_manager_test_files",
                              "project", "test.c")

        retval = find_user_config(c_file, 1)
        self.assertEqual("", retval)

        retval = find_user_config(c_file, 2)
        self.assertEqual(
            os.path.join(current_dir, "section_manager_test_files",
                         ".coafile"), retval)

        child_dir = os.path.join(current_dir, "section_manager_test_files",
                                 "child_dir")
        retval = find_user_config(child_dir, 2)
        self.assertEqual(
            os.path.join(current_dir, "section_manager_test_files",
                         "child_dir", ".coafile"), retval)

        old_cwd = os.getcwd()
        try:
            os.chdir(child_dir)
            sections, _, _, _ = gather_configuration(
                lambda *args: True,
                self.log_printer,
                arg_list=["--find-config"])
            self.assertEqual(bool(sections["default"]['find_config']), True)
        finally:
            os.chdir(old_cwd)
    def test_find_user_config(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        c_file = os.path.join(current_dir,
                              "section_manager_test_files",
                              "project",
                              "test.c")

        retval = find_user_config(c_file, 1)
        self.assertEqual("", retval)

        retval = find_user_config(c_file, 2)
        self.assertEqual(os.path.join(current_dir,
                                      "section_manager_test_files",
                                      ".coafile"), retval)

        child_dir = os.path.join(current_dir,
                                 "section_manager_test_files",
                                 "child_dir")
        retval = find_user_config(child_dir, 2)
        self.assertEqual(os.path.join(current_dir,
                                      "section_manager_test_files",
                                      "child_dir",
                                      ".coafile"), retval)

        with change_directory(child_dir):
            sections, _, _, _ = gather_configuration(
                lambda *args: True,
                self.log_printer,
                arg_list=["--find-config"])
            self.assertEqual(bool(sections["default"]['find_config']), True)
    def test_targets(self):
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=["default", "test1", "test2"])

        self.assertEqual(targets, ["default", "test1", "test2"])
Exemplo n.º 24
0
    def test_targets(self):
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=['cli', 'test1', 'test2'])

        self.assertEqual(targets, ['cli', 'test1', 'test2'])
Exemplo n.º 25
0
def main():
    log_printer = LogPrinter(ConsolePrinter())
    exitcode = 0
    try:
        yielded_results = False
        (sections,
         local_bears,
         global_bears,
         targets) = gather_configuration(lambda *args: True, log_printer)

        for section_name in sections:
            section = sections[section_name]
            if not section.is_enabled(targets):
                continue

            results = execute_section(
                section=section,
                global_bear_list=global_bears[section_name],
                local_bear_list=local_bears[section_name],
                print_results=lambda *args: True,
                log_printer=log_printer,
                file_diff_dict={})
            yielded_results = yielded_results or results[0]

        if yielded_results:
            exitcode = 1
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    return exitcode
    def test_find_user_config(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        c_file = os.path.join(current_dir,
                              'section_manager_test_files',
                              'project',
                              'test.c')

        retval = find_user_config(c_file, 1)
        self.assertEqual('', retval)

        retval = find_user_config(c_file, 2)
        self.assertEqual(os.path.join(current_dir,
                                      'section_manager_test_files',
                                      '.coafile'), retval)

        child_dir = os.path.join(current_dir,
                                 'section_manager_test_files',
                                 'child_dir')
        retval = find_user_config(child_dir, 2)
        self.assertEqual(os.path.join(current_dir,
                                      'section_manager_test_files',
                                      'child_dir',
                                      '.coafile'), retval)

        with change_directory(child_dir):
            sections, _, _, _ = gather_configuration(
                lambda *args: True,
                self.log_printer,
                arg_list=['--find-config'])
            self.assertEqual(bool(sections['cli']['find_config']), True)
    def test_targets(self):
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=['cli', 'test1', 'test2'])

        self.assertEqual(targets, ['cli', 'test1', 'test2'])
Exemplo n.º 28
0
    def setUp(self):
        config_path = os.path.abspath(os.path.join(
            os.path.dirname(__file__),
            'section_executor_test_files',
            '.coafile'))
        self.testcode_c_path = os.path.join(os.path.dirname(config_path),
                                            'testcode.c')
        self.unreadable_path = os.path.join(os.path.dirname(config_path),
                                            'unreadable')

        self.result_queue = queue.Queue()
        self.queue = queue.Queue()
        self.log_queue = queue.Queue()
        self.console_printer = ConsolePrinter()
        log_printer = LogPrinter(ConsolePrinter())
        self.log_printer = ProcessingTestLogPrinter(self.log_queue)

        (self.sections,
         self.local_bears,
         self.global_bears,
         targets) = gather_configuration(lambda *args: True,
                                         log_printer,
                                         arg_list=['--config',
                                                   re.escape(config_path)])
        self.assertEqual(len(self.local_bears['cli']), 1)
        self.assertEqual(len(self.global_bears['cli']), 1)
        self.assertEqual(targets, [])
Exemplo n.º 29
0
    def Analyze(self):
        """
        This method analyzes the document and sends back the result

        :return: The output is a list with an element for each section.
                 It contains:

                 -  The name of the section
                 -  Boolean which is true if all bears in the section executed
                    successfully
                 -  List of results where each result is a string dictionary
                    which contains: id, origin, message, file, line_nr, severity
        """
        retval = []
        if self.path == "" or self.config_file == "":
            return retval

        args = ["--config=" + self.config_file]

        log_printer = ListLogPrinter()
        exitcode = 0
        try:
            yielded_results = False
            (sections, local_bears, global_bears,
             targets) = gather_configuration(fail_acquire_settings,
                                             log_printer,
                                             arg_list=args)

            for section_name in sections:
                section = sections[section_name]

                if not section.is_enabled(targets):
                    continue

                if any([
                        fnmatch(self.path, file_pattern)
                        for file_pattern in path_list(section["files"])
                ]):

                    section["files"].value = self.path
                    section_result = execute_section(
                        section=section,
                        global_bear_list=global_bears[section_name],
                        local_bear_list=local_bears[section_name],
                        print_results=lambda *args: True,
                        log_printer=log_printer)
                    yielded_results = yielded_results or section_result[0]

                    retval.append(
                        DbusDocument.results_to_dbus_struct(
                            section_result, section_name))

            if yielded_results:
                exitcode = 1
        except BaseException as exception:  # pylint: disable=broad-except
            exitcode = exitcode or get_exitcode(exception, log_printer)

        logs = [log.to_string_dict() for log in log_printer.logs]
        return (exitcode, logs, retval)
    def test_merge(self):
        tmp = Constants.system_coafile
        Constants.system_coafile = os.path.abspath(os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            "section_manager_test_files",
            "default_coafile"))

        config = os.path.abspath(os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            "section_manager_test_files",
            ".coafile"))

        # Check merging of default_coafile and .coafile
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=["-c", re.escape(config)])

        self.assertEqual(str(sections["test"]),
                         "test {value : '2'}")
        self.assertEqual(str(sections["test-2"]),
                         "test-2 {files : '.', bears : 'LineCountBear'}")

        # Check merging of default_coafile, .coafile and cli
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=["-c",
                      re.escape(config),
                      "-S",
                      "test.value=3",
                      "test-2.bears=",
                      "test-5.bears=TestBear2"])

        self.assertEqual(str(sections["test"]), "test {value : '3'}")
        self.assertEqual(str(sections["test-2"]),
                         "test-2 {files : '.', bears : ''}")
        self.assertEqual(str(sections["test-3"]),
                         "test-3 {files : 'MakeFile'}")
        self.assertEqual(str(sections["test-4"]),
                         "test-4 {bears : 'TestBear'}")
        self.assertEqual(str(sections["test-5"]),
                         "test-5 {bears : 'TestBear2'}")

        Constants.system_coafile = tmp
    def test_merge(self):
        tmp = Constants.system_coafile
        Constants.system_coafile = os.path.abspath(os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            'section_manager_test_files',
            'system_coafile'))

        config = os.path.abspath(os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            'section_manager_test_files',
            '.coafile'))

        # Check merging of system_coafile and .coafile
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=['-c', config])

        self.assertEqual(str(sections['test']),
                         "test {value : '2'}")
        self.assertEqual(str(sections['test-2']),
                         "test-2 {files : '.', bears : 'LineCountBear'}")

        # Check merging of system_coafile, .coafile and cli
        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=['-c',
                      config,
                      '-S',
                      'test.value=3',
                      'test-2.bears=',
                      'test-5.bears=TestBear2'])

        self.assertEqual(str(sections['test']), "test {value : '3'}")
        self.assertEqual(str(sections['test-2']),
                         "test-2 {files : '.', bears : ''}")
        self.assertEqual(str(sections['test-3']),
                         "test-3 {files : 'MakeFile'}")
        self.assertEqual(str(sections['test-4']),
                         "test-4 {bears : 'TestBear'}")
        self.assertEqual(str(sections['test-5']),
                         "test-5 {bears : 'TestBear2'}")

        Constants.system_coafile = tmp
Exemplo n.º 32
0
    def test_merge_defaults(self):
        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = gather_configuration(
                lambda *args: True,
                self.log_printer,
                arg_list=["-S", "value=1", "test.value=2", "-c", escape(temporary, "\\")],
            )

        self.assertEqual(sections["default"], sections["test"].defaults)
Exemplo n.º 33
0
    def setUp(self):
        config_path = os.path.abspath(os.path.join(
            os.path.dirname(__file__),
            'section_executor_test_files',
            '.coafile'))
        self.testcode_c_path = os.path.join(os.path.dirname(config_path),
                                            'testcode.c')
        self.unreadable_path = os.path.join(os.path.dirname(config_path),
                                            'unreadable')

        factory_test_path = os.path.abspath(os.path.join(
            os.path.dirname(__file__),
            'file_factory_test_files'))
        self.factory_test_file = os.path.join(factory_test_path,
                                              'factory_test.txt')
        self.a_bear_test_path = os.path.join(factory_test_path,
                                             'a_bear_test.txt')
        self.b_bear_test_path = os.path.join(factory_test_path,
                                             'b_bear_test.txt')
        self.c_bear_test_path = os.path.join(factory_test_path,
                                             'c_bear_test.txt')
        self.d_bear_test_path = os.path.join(factory_test_path,
                                             'd_bear_test.txt')
        self.e_bear_test_path = os.path.join(factory_test_path,
                                             'e_bear_test.txt')
        self.n_bear_test_path = os.path.join(factory_test_path,
                                             'n_bear_test.txt')
        self.n_bear_test_path_2 = os.path.join(factory_test_path,
                                               'n_bear_test2.txt')
        self.x_bear_test_path = os.path.join(factory_test_path,
                                             'x_bear_test.txt')

        filename_list = [self.factory_test_file, self.a_bear_test_path,
                         self.b_bear_test_path, self.c_bear_test_path,
                         self.d_bear_test_path, self.e_bear_test_path,
                         self.n_bear_test_path, self.n_bear_test_path_2,
                         self.x_bear_test_path]

        self.file_dict = get_file_dict(filename_list)

        self.result_queue = queue.Queue()
        self.queue = queue.Queue()
        self.log_queue = queue.Queue()
        self.console_printer = ConsolePrinter()
        log_printer = LogPrinter(ConsolePrinter())
        self.log_printer = ProcessingTestLogPrinter(self.log_queue)

        (self.sections,
         self.local_bears,
         self.global_bears,
         targets) = gather_configuration(lambda *args: True,
                                         log_printer,
                                         arg_list=['--config',
                                                   config_path])
        self.assertEqual(len(self.local_bears['cli']), 1)
        self.assertEqual(len(self.global_bears['cli']), 1)
        self.assertEqual(targets, [])
    def test_autoapply_arg(self):
        sections, _, _, _ = gather_configuration(
            lambda *args: True,
            self.log_printer,
            autoapply=False,
            arg_list=[])

        self.assertEqual(str(sections['default'].get('autoapply', None)),
                         'False')

        sections, _, _, _ = gather_configuration(
            lambda *args: True,
            self.log_printer,
            autoapply=True,
            arg_list=[])

        self.assertEqual(str(sections['default'].get('autoapply', None)),
                         'None')
Exemplo n.º 35
0
    def test_user_coafile_parsing(self):
        Constants.user_coafile = os.path.abspath(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         'section_manager_test_files', 'system_coafile'))

        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True, self.log_printer, arg_list=[])

        self.assertEqual(str(sections['test']),
                         "test {value : '1', testval : '5'}")
Exemplo n.º 36
0
 def test_merge_defaults(self):
     (sections,
      local_bears,
      global_bears,
      targets) = gather_configuration(
         lambda *args: True,
         self.log_printer,
         arg_list=["-S", "value=1", "test.value=2", "-c", "bad_file_name"])
     self.assertEqual(sections["default"],
                      sections["test"].defaults)
    def test_merge_defaults(self):
        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(lambda *args: True,
                                     self.log_printer,
                                     arg_list=[
                                         "-S", "value=1", "test.value=2", "-c",
                                         escape(temporary, "\\")
                                     ]))

        self.assertEqual(sections["default"], sections["test"].defaults)
Exemplo n.º 38
0
    def test_merge_defaults(self):
        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(lambda *args: True,
                                     self.log_printer,
                                     arg_list=[
                                         '-S', 'value=1', 'test.value=2', '-c',
                                         escape(temporary, '\\')
                                     ] + self.min_args))

        self.assertEqual(sections['cli'], sections['test'].defaults)
Exemplo n.º 39
0
    def setUp(self):
        config_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__),
                         'section_executor_test_files', '.coafile'))
        self.testcode_c_path = os.path.join(os.path.dirname(config_path),
                                            'testcode.c')
        self.unreadable_path = os.path.join(os.path.dirname(config_path),
                                            'unreadable')

        factory_test_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), 'file_factory_test_files'))
        self.factory_test_file = os.path.join(factory_test_path,
                                              'factory_test.txt')
        self.a_bear_test_path = os.path.join(factory_test_path,
                                             'a_bear_test.txt')
        self.b_bear_test_path = os.path.join(factory_test_path,
                                             'b_bear_test.txt')
        self.c_bear_test_path = os.path.join(factory_test_path,
                                             'c_bear_test.txt')
        self.d_bear_test_path = os.path.join(factory_test_path,
                                             'd_bear_test.txt')
        self.e_bear_test_path = os.path.join(factory_test_path,
                                             'e_bear_test.txt')
        self.n_bear_test_path = os.path.join(factory_test_path,
                                             'n_bear_test.txt')
        self.n_bear_test_path_2 = os.path.join(factory_test_path,
                                               'n_bear_test2.txt')
        self.x_bear_test_path = os.path.join(factory_test_path,
                                             'x_bear_test.txt')

        filename_list = [
            self.factory_test_file, self.a_bear_test_path,
            self.b_bear_test_path, self.c_bear_test_path,
            self.d_bear_test_path, self.e_bear_test_path,
            self.n_bear_test_path, self.n_bear_test_path_2,
            self.x_bear_test_path
        ]

        self.file_dict = get_file_dict(filename_list)

        self.result_queue = queue.Queue()
        self.queue = queue.Queue()
        self.log_queue = queue.Queue()
        self.console_printer = ConsolePrinter()
        log_printer = LogPrinter(ConsolePrinter())
        self.log_printer = ProcessingTestLogPrinter(self.log_queue)

        (self.sections, self.local_bears, self.global_bears,
         targets) = gather_configuration(lambda *args: True,
                                         log_printer,
                                         arg_list=['--config', config_path])
        self.assertEqual(len(self.local_bears['cli']), 1)
        self.assertEqual(len(self.global_bears['cli']), 1)
        self.assertEqual(targets, [])
    def test_merge_defaults(self):
        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(lambda *args: True,
                                     self.log_printer,
                                     arg_list=['-S', 'value=1', 'test.value=2',
                                               '-c', escape(temporary, '\\')] +
                                     self.min_args))

        self.assertEqual(sections['cli'],
                         sections['test'].defaults)
Exemplo n.º 41
0
    def test_gather_configuration(self):
        # We need to use a bad filename or this will parse coalas .coafile
        (sections,
         local_bears,
         global_bears,
         targets) = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=['-S', "test=5", "-c", "some_bad_filename"])

        self.assertEqual(str(sections["default"]),
                         "Default {config : 'some_bad_filename', test : '5'}")

        (sections,
         local_bears,
         global_bears,
         targets) = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=['-S test=5', '-c bad_filename', '-b LineCountBear'])
        self.assertEqual(len(local_bears["default"]), 1)
Exemplo n.º 42
0
    def test_gather_configuration(self):
        args = (lambda *args: True, self.log_printer)

        # Using incomplete settings (e.g. an invalid coafile) will error
        with self.assertRaises(SystemExit):
            gather_configuration(*args, arg_list=["-c abcdefghi/invalid/.coafile"])

        # Using a bad filename explicitly exits coala.
        with self.assertRaises(SystemExit):
            gather_configuration(*args, arg_list=["-S", "test=5", "-c", "some_bad_filename"])

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = gather_configuration(
                *args, arg_list=["-S", "test=5", "-c", escape(temporary, "\\"), "-s"] + self.min_args
            )

        self.assertEqual(
            str(sections["cli"]),
            "cli {bears : 'JavaTestBear', config : "
            + repr(temporary)
            + ", files : '*.java', save : 'True', test : '5'}",
        )

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = gather_configuration(
                *args, arg_list=["-S test=5", "-f *.java", "-c " + escape(temporary, "\\"), "-b LineCountBear -s"]
            )

        self.assertEqual(len(local_bears["cli"]), 0)
    def test_gather_configuration(self):
        args = (lambda *args: True, self.log_printer)

        # Passing the default coafile name only triggers a warning.
        gather_configuration(*args, arg_list=["-c abcdefghi/invalid/.coafile"])

        # Using a bad filename explicitly exits coala.
        with self.assertRaises(SystemExit):
            gather_configuration(
                *args, arg_list=["-S", "test=5", "-c", "some_bad_filename"])

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(*args,
                                     arg_list=[
                                         "-S", "test=5", "-c",
                                         escape(temporary, "\\"), "-s"
                                     ]))

        self.assertEqual(
            str(sections["default"]),
            "Default {config : " + repr(temporary) + ", save : "
            "'True', test : '5'}")

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(*args,
                                     arg_list=[
                                         "-S test=5",
                                         "-c " + escape(temporary, "\\"),
                                         "-b LineCountBear -s"
                                     ]))

        self.assertEqual(len(local_bears["default"]), 0)
    def test_gather_configuration(self):
        args = (lambda *args: True, self.log_printer)

        # Passing the default coafile name only triggers a warning.
        gather_configuration(*args, arg_list=["-c abcdefghi/invalid/.coafile"])

        # Using a bad filename explicitly exits coala.
        with self.assertRaises(SystemExit):
            gather_configuration(
                *args,
                arg_list=["-S", "test=5", "-c", "some_bad_filename"])

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(
                    *args,
                    arg_list=["-S",
                              "test=5",
                              "-c",
                              escape(temporary, "\\"),
                              "-s"]))

        self.assertEqual(str(sections["default"]),
                         "Default {config : " +
                         repr(temporary) + ", save : 'True', test : '5'}")

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(*args,
                                     arg_list=["-S test=5",
                                               "-c " + escape(temporary, "\\"),
                                               "-b LineCountBear -s"]))

        self.assertEqual(len(local_bears["default"]), 0)
Exemplo n.º 45
0
    def test_gather_configuration(self):
        args = (lambda *args: True, self.log_printer)

        # Using incomplete settings (e.g. an invalid coafile) will error
        with self.assertRaises(SystemExit):
            gather_configuration(*args,
                                 arg_list=['-c abcdefghi/invalid/.coafile'])

        # Using a bad filename explicitly exits coala.
        with self.assertRaises(SystemExit):
            gather_configuration(
                *args,
                arg_list=['-S', 'test=5', '-c', 'some_bad_filename'])

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(
                    *args,
                    arg_list=['-S', 'test=5', '-c', escape(temporary, '\\'),
                              '-s'] + self.min_args))

        self.assertEqual(
            str(sections['default']),
            "Default {bears : 'JavaTestBear', config : " + repr(temporary) +
            ", files : '*.java', save : 'True', test : '5'}")

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(*args,
                                     arg_list=['-S test=5',
                                               '-f *.java',
                                               '-c ' + escape(temporary, '\\'),
                                               '-b LineCountBear -s']))

        self.assertEqual(len(local_bears['default']), 0)
Exemplo n.º 46
0
    def test_gather_configuration(self):
        args = (lambda *args: True, self.log_printer)

        # Using incomplete settings (e.g. an invalid coafile) will error
        with self.assertRaises(SystemExit):
            gather_configuration(*args,
                                 arg_list=['-c abcdefghi/invalid/.coafile'])

        # Using a bad filename explicitly exits coala.
        with self.assertRaises(SystemExit):
            gather_configuration(
                *args, arg_list=['-S', 'test=5', '-c', 'some_bad_filename'])

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(*args,
                                     arg_list=[
                                         '-S', 'test=5', '-c',
                                         escape(temporary, '\\'), '-s'
                                     ] + self.min_args))

        self.assertEqual(
            str(sections['cli']), "cli {bears : 'JavaTestBear', config : " +
            repr(temporary) + ", files : '*.java', save : 'True', test : '5'}")

        with make_temp() as temporary:
            sections, local_bears, global_bears, targets = (
                gather_configuration(*args,
                                     arg_list=[
                                         '-S test=5', '-f *.java',
                                         '-c ' + escape(temporary, '\\'),
                                         '-b LineCountBear -s'
                                     ]))

        self.assertEqual(len(local_bears['cli']), 0)
    def test_user_coafile_parsing(self):
        Constants.user_coafile = os.path.abspath(os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            'section_manager_test_files',
            'system_coafile'))

        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=[])

        self.assertEqual(str(sections['test']),
                         "test {value : '1', testval : '5'}")
Exemplo n.º 48
0
    def test_find_user_config(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        c_file = os.path.join(current_dir,
                              "section_manager_test_files",
                              "project",
                              "test.c")

        retval = find_user_config(c_file, 1)
        self.assertEqual("", retval)

        retval = find_user_config(c_file, 2)
        self.assertEqual(os.path.join(current_dir,
                                      "section_manager_test_files",
                                      ".coafile"), retval)

        # We need to use a bad filename or this will parse coalas .coafile
        (sections,
         dummy,
         dummy,
         dummy) = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=["--find-config", "-c", "some_bad_filename"])

        self.assertEqual(str(sections["default"]),
                         "Default {config : 'some_bad_filename', "
                         "find_config : 'True'}")

        (sections,
         dummy,
         dummy,
         dummy) = gather_configuration(
            lambda *args: True,
            self.log_printer,
            arg_list=["--find-config"])

        self.assertRegex(str(sections["default"]),
                         ".*find_config : 'True'.*, config : '.*'")
Exemplo n.º 49
0
def main():
    log_printer = ListLogPrinter()
    exitcode = 0
    results = {}
    try:
        yielded_results = False
        section_results = []

        (sections,
         local_bears,
         global_bears,
         targets) = gather_configuration(fail_acquire_settings, log_printer)

        for section_name in sections:
            section = sections[section_name]
            if not section.is_enabled(targets):
                continue

            section_result = execute_section(
                section=section,
                global_bear_list=global_bears[section_name],
                local_bear_list=local_bears[section_name],
                print_results=lambda *args: True,
                log_printer=log_printer,
                file_diff_dict={})
            yielded_results = yielded_results or section_result[0]

            results_for_section = []
            for i in [1, 2]:
                for key, value in section_result[i].items():
                    for result in value:
                        if isinstance(result, HiddenResult):
                            continue
                        results_for_section.append(result)
            results[section_name] = results_for_section

        if yielded_results:
            exitcode = 1
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    retval = {"logs": log_printer.logs, "results": results}
    retval = json.dumps(retval,
                        cls=JSONEncoder,
                        sort_keys=True,
                        indent=2,
                        separators=(',', ': '))
    print(retval)

    return exitcode
Exemplo n.º 50
0
 def test_user_coafile_parsing(self):
     tmp = Constants.user_coafile
     Constants.user_coafile = os.path.abspath(os.path.join(
         os.path.dirname(os.path.realpath(__file__)),
         "section_manager_test_files",
         "default_coafile"))
     (sections,
      local_bears,
      global_bears,
      targets) = gather_configuration(lambda *args: True,
                                      self.log_printer)
     self.assertEqual(str(sections["test"]),
                      "test {value : '1', testval : '5'}")
     Constants.user_coafile = tmp
    def test_user_coafile_parsing(self):
        tmp = Constants.user_coafile

        Constants.user_coafile = os.path.abspath(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         "section_manager_test_files", "default_coafile"))

        sections, local_bears, global_bears, targets = gather_configuration(
            lambda *args: True, self.log_printer)

        self.assertEqual(str(sections["test"]),
                         "test {value : '1', testval : '5'}")

        Constants.user_coafile = tmp
Exemplo n.º 52
0
    def test_back_saving(self):
        filename = os.path.join(tempfile.gettempdir(),
                                "SectionManagerTestFile")

        # We need to use a bad filename or this will parse coalas .coafile
        # Despite missing settings (coala isn't run) the file is saved
        with self.assertRaises(SystemExit):
            gather_configuration(lambda *args: True,
                                 self.log_printer,
                                 arg_list=[
                                     '-S', "save=" + escape(filename, '\\'),
                                     "-c=some_bad_filename"
                                 ])

        with open(filename, "r") as f:
            lines = f.readlines()
        self.assertEqual(["[Default]\n", "config = some_bad_filename\n"],
                         lines)

        with self.assertRaises(SystemExit):
            gather_configuration(lambda *args: True,
                                 self.log_printer,
                                 arg_list=[
                                     '-S', "save=true",
                                     "config=" + escape(filename, '\\'),
                                     "test.value=5"
                                 ])

        with open(filename, "r") as f:
            lines = f.readlines()
        os.remove(filename)
        if os.path.sep == '\\':
            filename = escape(filename, '\\')
        self.assertEqual([
            "[Default]\n", "config = " + filename + "\n", "[test]\n",
            "value = 5\n"
        ], lines)
    def run_coala(path):
        """
        Run coala on the file at the given path. The config file is got using
        the `find-config` option of coala.

        :param path: The path of the file to analyze.
        :return:     The result dictionary from coala.
        """
        results = {}
        log_printer = LogPrinter(ConsolePrinter())
        cwd = os.getcwd()
        try:
            os.chdir(os.path.dirname(path))
            args = [
                "--find-config", "--limit-files", path, '-S', 'autoapply=false'
            ]
            sections, local_bears, global_bears, targets = (
                # Use `lambda *args: True` so that `gather_configuration` does
                # nothing when it needs to request settings from user.
                gather_configuration(lambda *args: True,
                                     log_printer,
                                     arg_list=args))

            for section_name in sections:
                section = sections[section_name]
                if not section.is_enabled(targets):
                    continue

                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=lambda *args: True,
                    log_printer=log_printer)

                results_for_section = []
                for i in 1, 2:
                    for value in section_result[i].values():
                        for result in value:
                            if isinstance(result, HiddenResult):
                                continue
                            results_for_section.append(result)
                results[section_name] = results_for_section
        except BaseException as exception:
            log_printer.log_exception(str(exception), exception)
        finally:
            os.chdir(cwd)

        return results
    def test_find_user_config(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        c_file = os.path.join(current_dir, "section_manager_test_files",
                              "project", "test.c")

        retval = find_user_config(c_file, 1)
        self.assertEqual("", retval)

        retval = find_user_config(c_file, 2)
        self.assertEqual(
            os.path.join(current_dir, "section_manager_test_files",
                         ".coafile"), retval)

        sections, dummy, dummy, dummy = gather_configuration(
            lambda *args: True, self.log_printer, arg_list=["--find-config"])

        self.assertRegex(str(sections["default"]),
                         ".*find_config : 'True'.*, config : '.*'")
Exemplo n.º 55
0
    def setUp(self):
        config_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__),
                         "section_executor_test_files", ".coafile"))
        self.testcode_c_path = os.path.join(os.path.dirname(config_path),
                                            "testcode.c")

        self.result_queue = queue.Queue()
        self.queue = queue.Queue()
        self.log_queue = queue.Queue()
        log_printer = LogPrinter(ConsolePrinter())
        self.log_printer = ProcessingTestLogPrinter(self.log_queue)

        (self.sections, self.local_bears,
         self.global_bears, targets) = gather_configuration(
             lambda *args: True, log_printer,
             ["--config", re.escape(config_path)])
        self.assertEqual(len(self.local_bears["default"]), 1)
        self.assertEqual(len(self.global_bears["default"]), 1)
        self.assertEqual(targets, [])
Exemplo n.º 56
0
def run_coala(console_printer=None,
              log_printer=None,
              print_results=do_nothing,
              acquire_settings=fail_acquire_settings,
              print_section_beginning=do_nothing,
              nothing_done=do_nothing,
              autoapply=True,
              force_show_patch=False,
              arg_parser=None,
              arg_list=None):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param console_printer:         Object to print messages on the console.
    :param log_printer:             A LogPrinter object to use for logging.
    :param print_results:           A callback that takes a LogPrinter, a
                                    section, a list of results to be printed,
                                    the file dict and the mutable file diff
                                    dict.
    :param acquire_settings:        The method to use for requesting settings.
                                    It will get a parameter which is a
                                    dictionary with the settings name as key
                                    and a list containing a description in [0]
                                    and the names of the bears who need this
                                    setting in all following indexes.
    :param print_section_beginning: A callback that will be called with a
                                    section name string whenever analysis of a
                                    new section is started.
    :param nothing_done:            A callback that will be called with only a
                                    log printer that shall indicate that
                                    nothing was done.
    :param autoapply:               Set this to false to not autoapply any
                                    actions. If you set this to `False`,
                                    `force_show_patch` will be ignored.
    :param force_show_patch:        If set to True, a patch will be always
                                    shown. (Using ApplyPatchAction.)
    :param arg_parser:              Instance of ArgParser that is used to parse
                                    non-setting arguments.
    :param arg_list:                The CLI argument list.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """

    log_printer = (LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG)
                   if log_printer is None else log_printer)

    exitcode = 0
    results = {}
    file_dicts = {}
    try:
        yielded_results = yielded_unfixed_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = gather_configuration(
            acquire_settings,
            log_printer,
            arg_parser=arg_parser,
            arg_list=arg_list)

        log_printer.debug('Platform {} -- Python {}, coalib {}'.format(
            platform.system(), platform.python_version(), VERSION))

        settings_hash = get_settings_hash(sections, targets)
        flush_cache = bool(sections['cli'].get('flush_cache', False)
                           or settings_changed(log_printer, settings_hash))

        cache = None
        if not sections['cli'].get('disable_caching', False):
            cache = FileCache(log_printer, os.getcwd(), flush_cache)

        for section_name, section in sections.items():
            if not section.is_enabled(targets):
                continue

            if not autoapply:
                section['default_actions'] = ''
            elif force_show_patch:
                section['default_actions'] = '*: ShowPatchAction'
                section['show_result_on_top'] = 'yeah'

            print_section_beginning(section)
            section_result = execute_section(
                section=section,
                global_bear_list=global_bears[section_name],
                local_bear_list=local_bears[section_name],
                print_results=print_results,
                cache=cache,
                log_printer=log_printer,
                console_printer=console_printer)
            yielded, yielded_unfixed, results[section_name] = (
                simplify_section_result(section_result))

            yielded_results = yielded_results or yielded
            yielded_unfixed_results = (yielded_unfixed_results
                                       or yielded_unfixed)
            did_nothing = False

            file_dicts[section_name] = section_result[3]

        update_settings_db(log_printer, settings_hash)
        if cache:
            cache.write()

        if CounterHandler.get_num_calls_for_level('ERROR') > 0:
            exitcode = 1
        elif did_nothing:
            nothing_done(log_printer)
            exitcode = 2
        elif yielded_unfixed_results:
            exitcode = 1
        elif yielded_results:
            exitcode = 5
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    return results, exitcode, file_dicts
Exemplo n.º 57
0
def run_coala(console_printer=None,
              log_printer=None,
              print_results=do_nothing,
              acquire_settings=fail_acquire_settings,
              print_section_beginning=do_nothing,
              nothing_done=do_nothing,
              autoapply=True,
              force_show_patch=False,
              arg_parser=None,
              arg_list=None,
              args=None,
              debug=False):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param console_printer:         Object to print messages on the console.
    :param log_printer:             A LogPrinter object to use for logging.
    :param print_results:           A callback that takes a LogPrinter, a
                                    section, a list of results to be printed,
                                    the file dict and the mutable file diff
                                    dict.
    :param acquire_settings:        The method to use for requesting settings.
                                    It will get a parameter which is a
                                    dictionary with the settings name as key
                                    and a list containing a description in [0]
                                    and the names of the bears who need this
                                    setting in all following indexes.
    :param print_section_beginning: A callback that will be called with a
                                    section name string whenever analysis of a
                                    new section is started.
    :param nothing_done:            A callback that will be called with only a
                                    log printer that shall indicate that
                                    nothing was done.
    :param autoapply:               Set this to false to not autoapply any
                                    actions. If you set this to `False`,
                                    `force_show_patch` will be ignored.
    :param force_show_patch:        If set to True, a patch will be always
                                    shown. (Using ApplyPatchAction.)
    :param arg_parser:              Instance of ArgParser that is used to parse
                                    non-setting arguments.
    :param arg_list:                The CLI argument list.
    :param args:                    Alternative pre-parsed CLI arguments.
    :param debug:                   Run in debug mode, bypassing
                                    multiprocessing, and not catching any
                                    exceptions.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    all_actions_possible = provide_all_actions()
    apply_single = None
    if getattr(args, 'single_action', None) is not None:
        while True:
            for i, action in enumerate(all_actions_possible, 1):
                console_printer.print(
                    format_lines('{}'.format(action), symbol='['))

            line = format_lines(STR_ENTER_NUMBER, symbol='[')

            choice = input(line)

            if choice.isalpha():
                choice = choice.upper()
                choice = '(' + choice + ')'
                if choice == '(N)':
                    apply_single = 'Do (N)othing'
                    break
                for i, action in enumerate(all_actions_possible, 1):
                    if choice in action:
                        apply_single = action
                        break
                if apply_single:
                    break
                console_printer.print(
                    format_lines('Please enter a valid letter.', symbol='['))

        args.apply_patch = False

    log_printer = (LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG)
                   if log_printer is None else log_printer)

    exitcode = 0
    sections = {}
    results = {}
    file_dicts = {}
    try:
        yielded_results = yielded_unfixed_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = gather_configuration(
            acquire_settings,
            log_printer,
            arg_parser=arg_parser,
            arg_list=arg_list,
            args=args)

        log_printer.debug('Platform {} -- Python {}, coalib {}'.format(
            platform.system(), platform.python_version(), VERSION))

        settings_hash = get_settings_hash(sections, targets)
        flush_cache = bool(sections['cli'].get('flush_cache', False)
                           or settings_changed(log_printer, settings_hash))

        cache = None
        if not sections['cli'].get('disable_caching', False):
            cache = FileCache(log_printer, os.getcwd(), flush_cache)

        for section_name, section in sections.items():
            if not section.is_enabled(targets):
                continue

            if not autoapply:
                section['default_actions'] = ''
            elif force_show_patch:
                section['default_actions'] = '*: ShowPatchAction'
                section['show_result_on_top'] = 'yeah'

            print_section_beginning(section)
            section_result = execute_section(
                section=section,
                global_bear_list=global_bears[section_name],
                local_bear_list=local_bears[section_name],
                print_results=print_results,
                cache=cache,
                log_printer=log_printer,
                console_printer=console_printer,
                debug=debug or args and args.debug,
                apply_single=(apply_single
                              if apply_single is not None else False))
            yielded, yielded_unfixed, results[section_name] = (
                simplify_section_result(section_result))

            yielded_results = yielded_results or yielded
            yielded_unfixed_results = (yielded_unfixed_results
                                       or yielded_unfixed)
            did_nothing = False

            file_dicts[section_name] = section_result[3]

        update_settings_db(log_printer, settings_hash)
        if cache:
            cache.write()

        if CounterHandler.get_num_calls_for_level('ERROR') > 0:
            exitcode = 1
        elif did_nothing:
            nothing_done(log_printer)
            exitcode = 2
        elif yielded_unfixed_results:
            exitcode = 1
        elif yielded_results:
            exitcode = 5
    except BaseException as exception:  # pylint: disable=broad-except
        if not isinstance(exception, SystemExit):
            if args and args.debug or (sections and sections.get(
                    'cli', {}).get('debug', False)):
                import ipdb
                with ipdb.launch_ipdb_on_exception():
                    raise

            if debug:
                raise

        exitcode = exitcode or get_exitcode(exception, log_printer)

    return results, exitcode, file_dicts
Exemplo n.º 58
0
def run_coala(log_printer=None,
              print_results=do_nothing,
              acquire_settings=fail_acquire_settings,
              print_section_beginning=do_nothing,
              nothing_done=do_nothing,
              show_bears=do_nothing,
              autoapply=True):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param log_printer:             A LogPrinter object to use for logging.
    :param print_results:           A callback that takes a LogPrinter, a
                                    section, a list of results to be printed,
                                    the file dict and the mutable file diff
                                    dict.
    :param acquire_settings:        The method to use for requesting settings.
                                    It will get a parameter which is a
                                    dictionary with the settings name as key
                                    and a list containing a description in [0]
                                    and the names of the bears who need this
                                    setting in all following indexes.
    :param print_section_beginning: A callback that will be called with a
                                    section name string whenever analysis of a
                                    new section is started.
    :param nothing_done:            A callback that will be called with only a
                                    log printer that shall indicate that
                                    nothing was done.
    :param show_bears:              A callback that will be called with first
                                    a list of local bears, second a list of
                                    global bears to output them. A third bool
                                    parameter may be used to indicate if a
                                    compressed output (True) or a normal output
                                    (False) is desired, the former being used
                                    for showing all available bears to the
                                    user.
    :param autoapply:               Set to False to autoapply nothing by
                                    default; this is overridable via any
                                    configuration file/CLI.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    exitcode = 0
    results = None
    try:
        yielded_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = (gather_configuration(
            acquire_settings, log_printer))

        tag = str(sections['default'].get('tag', None))
        dtag = str(sections['default'].get('dtag', None))

        if not autoapply and 'autoapply' not in sections['default']:
            sections['default']['autoapply'] = "False"

        show_all_bears = bool(sections['default'].get('show_all_bears', False))
        show_bears_ = bool(sections["default"].get("show_bears", "False"))
        if show_all_bears:
            show_bears_ = True
            for section in sections:
                bear_dirs = sections[section].bear_dirs()
                local_bears[section] = collect_bears(bear_dirs, ["**"],
                                                     [BEAR_KIND.LOCAL],
                                                     log_printer)
                global_bears[section] = collect_bears(bear_dirs, ["**"],
                                                      [BEAR_KIND.GLOBAL],
                                                      log_printer)

        if dtag != "None":
            delete_tagged_results(
                dtag, os.path.abspath(str(sections["default"].get("config"))))

        if show_bears_:
            show_bears(local_bears, global_bears, show_all_bears)
            did_nothing = False
        else:
            results = {}
            for section_name in sections:
                section = sections[section_name]
                if not section.is_enabled(targets):
                    continue

                print_section_beginning(section)
                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=print_results,
                    log_printer=log_printer)
                yielded_results = yielded_results or section_result[0]

                results_for_section = []
                for value in chain(section_result[1].values(),
                                   section_result[2].values()):
                    if value is None:
                        continue

                    for result in value:
                        results_for_section.append(result)

                results[section_name] = results_for_section
                did_nothing = False

            if tag != "None":
                tag_results(
                    tag,
                    os.path.abspath(str(sections["default"].get("config"))),
                    results)

        if did_nothing:
            nothing_done(log_printer)

        if yielded_results:
            exitcode = 1
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    return results, exitcode
Exemplo n.º 59
0
def run_coala(log_printer=None,
              print_results=do_nothing,
              acquire_settings=fail_acquire_settings,
              print_section_beginning=do_nothing,
              nothing_done=do_nothing,
              autoapply=True,
              arg_parser=None,
              arg_list=None):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param log_printer:             A LogPrinter object to use for logging.
    :param print_results:           A callback that takes a LogPrinter, a
                                    section, a list of results to be printed,
                                    the file dict and the mutable file diff
                                    dict.
    :param acquire_settings:        The method to use for requesting settings.
                                    It will get a parameter which is a
                                    dictionary with the settings name as key
                                    and a list containing a description in [0]
                                    and the names of the bears who need this
                                    setting in all following indexes.
    :param print_section_beginning: A callback that will be called with a
                                    section name string whenever analysis of a
                                    new section is started.
    :param nothing_done:            A callback that will be called with only a
                                    log printer that shall indicate that
                                    nothing was done.
    :param autoapply:               Set to False to autoapply nothing by
                                    default; this is overridable via any
                                    configuration file/CLI.
    :param arg_list:                The CLI argument list.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    configure_logging()

    log_printer = (LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG)
                   if log_printer is None else log_printer)

    exitcode = 0
    results = {}
    file_dicts = {}
    try:
        yielded_results = yielded_unfixed_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = gather_configuration(
            acquire_settings,
            log_printer,
            autoapply=autoapply,
            arg_parser=arg_parser,
            arg_list=arg_list)

        log_printer.debug("Platform {} -- Python {}, pip {}, coalib {}".format(
            platform.system(), platform.python_version(), pip.__version__,
            VERSION))

        config_file = os.path.abspath(str(sections["default"].get("config")))

        settings_hash = get_settings_hash(sections, targets)
        flush_cache = bool(sections["default"].get("flush_cache", False)
                           or settings_changed(log_printer, settings_hash))

        disable_caching = bool(sections["default"].get("disable_caching",
                                                       False))
        cache = None
        if not sections["default"].get("disable_caching", False):
            cache = FileCache(log_printer, os.getcwd(), flush_cache)

        for section_name, section in sections.items():
            if not section.is_enabled(targets):
                continue

            print_section_beginning(section)
            section_result = execute_section(
                section=section,
                global_bear_list=global_bears[section_name],
                local_bear_list=local_bears[section_name],
                print_results=print_results,
                cache=cache,
                log_printer=log_printer)
            yielded, yielded_unfixed, results[section_name] = (
                simplify_section_result(section_result))

            yielded_results = yielded_results or yielded
            yielded_unfixed_results = (yielded_unfixed_results
                                       or yielded_unfixed)
            did_nothing = False

            file_dicts[section_name] = section_result[3]

        update_settings_db(log_printer, settings_hash)
        if cache:
            cache.write()

        if did_nothing:
            nothing_done(log_printer)
        elif yielded_unfixed_results:
            exitcode = 1
        elif yielded_results:
            exitcode = 5
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    return results, exitcode, file_dicts
Exemplo n.º 60
0
def run_coala(log_printer=None,
              print_results=do_nothing,
              acquire_settings=fail_acquire_settings,
              print_section_beginning=do_nothing,
              nothing_done=do_nothing,
              show_bears=do_nothing,
              autoapply=True):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param log_printer:             A LogPrinter object to use for logging.
    :param print_results:           A callback that takes a LogPrinter, a
                                    section, a list of results to be printed,
                                    the file dict and the mutable file diff
                                    dict.
    :param acquire_settings:        The method to use for requesting settings.
                                    It will get a parameter which is a
                                    dictionary with the settings name as key
                                    and a list containing a description in [0]
                                    and the names of the bears who need this
                                    setting in all following indexes.
    :param print_section_beginning: A callback that will be called with a
                                    section name string whenever analysis of a
                                    new section is started.
    :param nothing_done:            A callback that will be called with only a
                                    log printer that shall indicate that
                                    nothing was done.
    :param show_bears:              A callback that will be called with first
                                    a list of local bears, second a list of
                                    global bears to output them. A third bool
                                    parameter may be used to indicate if a
                                    compressed output (True) or a normal output
                                    (False) is desired, the former being used
                                    for showing all available bears to the
                                    user.
    :param autoapply:               Set to False to autoapply nothing by
                                    default; this is overridable via any
                                    configuration file/CLI.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    exitcode = 0
    results = None
    try:
        yielded_results = yielded_unfixed_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = gather_configuration(
            acquire_settings, log_printer, autoapply=autoapply)

        tag = str(sections['default'].get('tag', None))
        dtag = str(sections['default'].get('dtag', None))
        config_file = os.path.abspath(str(sections["default"].get("config")))
        show_all_bears = bool(sections['default'].get('show_all_bears', False))
        show_bears_ = bool(sections["default"].get("show_bears", "False"))

        # Deleting all .orig files, so the latest files are up to date!
        coala_delete_orig.main(log_printer, sections["default"])

        delete_tagged_results(dtag, config_file, log_printer)

        if show_bears_ or show_all_bears:
            if show_all_bears:
                (local_bears, global_bears) = collect_all_bears_from_sections(
                    sections, log_printer)
            show_bears(local_bears, global_bears, show_all_bears)
            did_nothing = False
        else:
            results = {}
            for section_name, section in sections.items():
                if not section.is_enabled(targets):
                    continue

                print_section_beginning(section)
                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=print_results,
                    log_printer=log_printer)
                yielded, yielded_unfixed, results[section_name] = (
                    simplify_section_result(section_result))

                yielded_results = yielded_results or yielded
                yielded_unfixed_results = (yielded_unfixed_results
                                           or yielded_unfixed)
                did_nothing = False

            tag_results(tag, config_file, results, log_printer)

        if did_nothing:
            nothing_done(log_printer)
        elif yielded_unfixed_results:
            exitcode = 1
        elif yielded_results:
            exitcode = 5
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    return results, exitcode