Example #1
0
    def test_get_file_dict(self):
        file_dict = get_file_dict([self.testcode_c_path], self.log_printer)
        self.assertEqual(len(file_dict), 1)
        self.assertEqual(type(file_dict[self.testcode_c_path]),
                         tuple,
                         msg="files in file_dict should not be editable")

        # Non existent file
        file_dict = get_file_dict(["non_existent_file"], self.log_printer)
        self.assertEqual(file_dict, {})
        self.assertIn(("Failed to read file 'non_existent_file' because of "
                       "an unknown error."),
                      self.log_printer.log_queue.get().message)
Example #2
0
    def test_get_file_dict(self):
        file_dict = get_file_dict([self.testcode_c_path], self.log_printer)
        self.assertEqual(len(file_dict), 1)
        self.assertEqual(type(file_dict[self.testcode_c_path]),
                         tuple,
                         msg="files in file_dict should not be editable")

        # Non existent file
        file_dict = get_file_dict(["non_existent_file"], self.log_printer)
        self.assertEqual(file_dict, {})
        self.assertIn(("Failed to read file 'non_existent_file' because of "
                       "an unknown error."),
                      self.log_printer.log_queue.get().message)
Example #3
0
 def test_get_file_dict_allow_raw_file(self):
     log_printer = ListLogPrinter()
     file_dict = get_file_dict([self.unreadable_path], log_printer,
                               True)
     self.assertNotEqual(file_dict, {})
     self.assertEqual(file_dict[self.unreadable_path], None)
     self.assertEqual(len(log_printer.logs), 0)
Example #4
0
 def test_get_file_dict(self):
     file_dict = get_file_dict([self.testcode_c_path], self.log_printer)
     self.assertEqual(len(file_dict), 1)
     self.assertEqual(type(file_dict[self.testcode_c_path]), tuple, msg="files in file_dict should not be editable")
     self.assertEqual(
         "Files that will be checked:\n" + self.testcode_c_path, self.log_printer.log_queue.get().message
     )
Example #5
0
 def test_get_file_dict_non_existent_file(self):
     file_dict = get_file_dict(["non_existent_file"], self.log_printer)
     self.assertEqual(file_dict, {})
     self.assertIn(
         ("Failed to read file 'non_existent_file' because of " "an unknown error."),
         self.log_printer.log_queue.get().message,
     )
Example #6
0
 def test_get_file_dict(self):
     file_dict = get_file_dict([self.testcode_c_path], self.log_printer)
     self.assertEqual(len(file_dict), 1)
     self.assertEqual(type(file_dict[self.testcode_c_path]),
                      tuple,
                      msg='files in file_dict should not be editable')
     self.assertEqual('Files that will be checked:\n' + self.testcode_c_path,
                      self.log_printer.log_queue.get().message)
Example #7
0
 def test_get_file_dict(self):
     with LogCapture() as capture:
         file_dict = get_file_dict([self.testcode_c_path], self.log_printer)
     self.assertEqual(len(file_dict), 1)
     self.assertEqual(type(file_dict[self.testcode_c_path]),
                      tuple,
                      msg='files in file_dict should not be editable')
     capture.check(('root', 'DEBUG',
                    'Files that will be checked:\n' + self.testcode_c_path))
Example #8
0
 def test_get_file_dict_non_existent_file(self):
     with LogCapture() as capture:
         file_dict = get_file_dict(['non_existent_file'], self.log_printer)
     self.assertEqual(file_dict, {})
     capture.check(
         ('root', 'WARNING',
          StringComparison(r".*Failed to read file 'non_existent_file' "
                           r'because of an unknown error.*')),
         ('root', 'INFO', StringComparison(r'.*Exception was:.*')))
Example #9
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, [])
Example #10
0
 def test_get_file_dict_forbid_raw_file(self):
     log_printer = ListLogPrinter()
     with LogCapture() as capture:
         file_dict = get_file_dict([self.unreadable_path], log_printer,
                                   False)
     self.assertEqual(file_dict, {})
     capture.check(('root', 'WARNING',
                    "Failed to read file '{}'. It seems to contain "
                    'non-unicode characters. Leaving it out.'.format(
                        self.unreadable_path)))
Example #11
0
 def test_get_file_dict_allow_raw_file(self):
     log_printer = ListLogPrinter()
     with LogCapture() as capture:
         file_dict = get_file_dict([self.unreadable_path], log_printer,
                                   True)
     self.assertNotEqual(file_dict, {})
     self.assertEqual(file_dict[self.unreadable_path], None)
     capture.check(('root', 'DEBUG',
                    StringComparison(r'(?s).*Files that will be '
                                     r'checked(?s).*')))
Example #12
0
 def test_get_file_dict_non_existent_file(self):
     with LogCapture() as capture:
         file_dict = get_file_dict(['non_existent_file'], self.log_printer)
     self.assertEqual(file_dict, {})
     capture.check(
         ('root', 'WARNING',
          StringComparison(r".*Failed to read file 'non_existent_file' "
                           r'because of an unknown error.*')),
         ('root', 'INFO', StringComparison(r'.*Exception was:.*'))
     )
Example #13
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, [])
Example #14
0
 def test_get_file_dict_forbid_raw_file(self):
     log_printer = ListLogPrinter()
     with LogCapture() as capture:
         file_dict = get_file_dict([self.unreadable_path], log_printer,
                                   False)
     self.assertEqual(file_dict, {})
     capture.check(
         ('root', 'WARNING', "Failed to read file '{}'. It seems to contain "
          'non-unicode characters. Leaving it out.'
             .format(self.unreadable_path))
     )
Example #15
0
 def test_get_file_dict_allow_raw_file(self):
     log_printer = ListLogPrinter()
     with LogCapture() as capture:
         file_dict = get_file_dict([self.unreadable_path], log_printer,
                                   True)
     self.assertNotEqual(file_dict, {})
     self.assertEqual(file_dict[self.unreadable_path], None)
     capture.check(
         ('root', 'DEBUG', StringComparison(r'(?s).*Files that will be '
                                            r'checked(?s).*'))
     )
Example #16
0
 def test_get_file_dict(self):
     with LogCapture() as capture:
         file_dict = get_file_dict([self.testcode_c_path], self.log_printer)
     self.assertEqual(len(file_dict), 1)
     self.assertEqual(type(file_dict[self.testcode_c_path]),
                      tuple,
                      msg='files in file_dict should not be editable')
     capture.check(
         ('root', 'DEBUG', 'Files that will be checked:\n' +
          self.testcode_c_path)
     )
Example #17
0
    def test_get_file_dict_forbid_raw_file(self):
        log_printer = ListLogPrinter()
        file_dict = get_file_dict([self.unreadable_path], log_printer, False)
        self.assertEqual(file_dict, {})
        self.assertEqual(len(log_printer.logs), 1)

        log_message = log_printer.logs[0]
        self.assertEqual(("Failed to read file '{}'. It seems to contain "
                          'non-unicode characters. Leaving it '
                          'out.'.format(self.unreadable_path)),
                         log_message.message)
        self.assertEqual(log_message.log_level, LOG_LEVEL.WARNING)
Example #18
0
    def get_file_dict(self, filename_list, allow_raw_files=False):
        """
        Returns a file dictionary mapping from filename to lines of
        file. Uses coalib.processes.Processing.get_file_dict().

        :param filename_list:   List of filenames as strings to build
                                the file dict.
        :param allow_raw_files: Indicates if the file could also be
                                read as a binary.
        :return:                Reads the content of each file into
                                dictionary with filenames as keys.
        """
        return get_file_dict(filename_list, allow_raw_files=allow_raw_files)
Example #19
0
    def get_file_dict(self, filename_list, allow_raw_files=False):
        """
        Returns a file dictionary mapping from filename to lines of
        file. Uses coalib.processes.Processing.get_file_dict().

        :param filename_list:   List of filenames as strings to build
                                the file dict.
        :param allow_raw_files: Indicates if the file could also be
                                read as a binary.
        :return:                Reads the content of each file into
                                dictionary with filenames as keys.
        """
        return get_file_dict(filename_list,
                             allow_raw_files=allow_raw_files)
Example #20
0
 def test_get_file_dict(self):
     with prepare_file(['line\n'], None) as (_, file):
         self.assertEqual(self.cache.get_file_dict([file]),
                          get_file_dict([file]))
def run_quickstartbear(contents, project_dir):
    """
    Runs the QuickstartBear which pareses the file_dict
    to get the exact value of some settings which can attain
    any infinite amount of values.
    :param contents:
        The python object written to 'PROJECT_DATA' which
        contains the directory structure and values of some
        settings which can attain an infinite set of values.
    :param project_dir:
        The project directory from which to get the files for the
        QuickstartBear to run.
    :return:
        - An updated contents value after guessing values of certain
          settings.
        - Collection of SourceRange objects indicating the parts of
          code to ignore.
        - The complete file dict contains file names as keys and file
          contents as values to those keys.
        - The complete file name list from the project directory and sub
          directories.
    """
    section = Section('green_mode')
    quickstartbear_obj = QuickstartBear(section, None)

    complete_filename_list = generate_complete_filename_list(
        contents['dir_structure'], project_dir)
    complete_file_dict = get_file_dict(complete_filename_list,
                                       allow_raw_files=True)
    ignore_ranges = list(yield_ignore_ranges(complete_file_dict))
    find_max = ['max_lines_per_file', 'max_line_length']
    find_min = ['min_lines_per_file']
    for key in complete_file_dict:
        return_val = quickstartbear_obj.execute(
            filename=key, file=complete_file_dict[key])
        return_val = return_val[0]
        # eg. return_val = {'setting_name': value, ...}
        if return_val is not None:
            for setting in find_max:
                contents = find_max_min_of_setting(
                    setting, return_val[setting], contents,
                    operator.gt)

            for setting in find_min:
                contents = find_max_min_of_setting(
                    setting, return_val[setting], contents,
                    operator.lt)

    bear_settings = get_yaml_contents(str(
        Path(__file__).parent / 'bear_settings.yaml'))['type2']
    full_dict = {}

    for small_dict in bear_settings.values():
        full_dict.update(small_dict)

    resort_to_default_settings = deepcopy(find_max)
    resort_to_default_settings += find_min

    for setting_name in resort_to_default_settings:
        default_val = full_dict[setting_name]
        insert_index = -1
        for index, dict_ in enumerate(contents[settings_key]):
            if setting_name in dict_:
                current_val = dict_[setting_name]
                insert_index = index
                break
        if 'current_val' in locals() and current_val < default_val:
            contents[settings_key][insert_index][setting_name] = default_val

    return (contents, ignore_ranges, complete_file_dict,
            complete_filename_list)
Example #22
0
 def test_get_file_dict_allow_raw_file(self):
     file_dict = get_file_dict([self.unreadable_path], self.log_printer,
                               True)
     self.assertNotEqual(file_dict, {})
     self.assertEqual(file_dict[self.unreadable_path], None)
Example #23
0
 def test_get_file_dict(self):
     with prepare_file(['line\n'], None) as (_, file):
         self.assertEqual(self.cache.get_file_dict([file]),
                          get_file_dict([file]))
Example #24
0
 def test_get_file_dict(self):
     file_dict = get_file_dict([self.testcode_c_path], self.log_printer)
     self.assertEqual(len(file_dict), 1)
     self.assertEqual(type(file_dict[self.testcode_c_path]),
                      tuple,
                      msg="files in file_dict should not be editable")
Example #25
0
 def test_get_file_dict_non_existent_file(self):
     file_dict = get_file_dict(["non_existent_file"], self.log_printer)
     self.assertEqual(file_dict, {})
     self.assertIn(("Failed to read file 'non_existent_file' because of "
                    "an unknown error."),
                   self.log_printer.log_queue.get().message)
def run_quickstartbear(contents, project_dir):
    """
    Runs the QuickstartBear which pareses the file_dict
    to get the exact value of some settings which can attain
    any infinite amount of values.
    :param contents:
        The python object written to 'PROJECT_DATA' which
        contains the directory structure and values of some
        settings which can attain an infinite set of values.
    :param project_dir:
        The project directory from which to get the files for the
        QuickstartBear to run.
    :return:
        - An updated contents value after guessing values of certain
          settings.
        - Collection of SourceRange objects indicating the parts of
          code to ignore.
        - The complete file dict contains file names as keys and file
          contents as values to those keys.
        - The complete file name list from the project directory and sub
          directories.
    """
    section = Section('green_mode')
    quickstartbear_obj = QuickstartBear(section, None)

    complete_filename_list = generate_complete_filename_list(
        contents['dir_structure'], project_dir)
    complete_file_dict = get_file_dict(complete_filename_list,
                                       allow_raw_files=True)
    ignore_ranges = list(yield_ignore_ranges(complete_file_dict))
    find_max = ['max_lines_per_file', 'max_line_length']
    find_min = ['min_lines_per_file']
    for key in complete_file_dict:
        return_val = quickstartbear_obj.execute(
            filename=key, file=complete_file_dict[key])
        return_val = return_val[0]
        # eg. return_val = {'setting_name': value, ...}
        if return_val is not None:
            for setting in find_max:
                contents = find_max_min_of_setting(
                    setting, return_val[setting], contents,
                    operator.gt)

            for setting in find_min:
                contents = find_max_min_of_setting(
                    setting, return_val[setting], contents,
                    operator.lt)

    bear_settings = get_yaml_contents(str(
        Path(__file__).parent / 'bear_settings.yaml'))['type2']
    full_dict = {}

    for small_dict in bear_settings.values():
        full_dict.update(small_dict)

    resort_to_default_settings = deepcopy(find_max)
    resort_to_default_settings += find_min

    for setting_name in resort_to_default_settings:
        default_val = full_dict[setting_name]
        insert_index = -1
        for index, dict_ in enumerate(contents[settings_key]):
            if setting_name in dict_:
                current_val = dict_[setting_name]
                insert_index = index
                break
        if 'current_val' in locals() and current_val < default_val:
            contents[settings_key][insert_index][setting_name] = default_val

    return (contents, ignore_ranges, complete_file_dict,
            complete_filename_list)
Example #27
0
 def test_get_file_dict(self):
     file_dict = get_file_dict([self.testcode_c_path], self.log_printer)
     self.assertEqual(len(file_dict), 1)
     self.assertEqual(type(file_dict[self.testcode_c_path]),
                      tuple,
                      msg="files in file_dict should not be editable")
Example #28
0
 def test_get_file_dict_allow_raw_file(self):
     file_dict = get_file_dict([self.unreadable_path], self.log_printer,
                               True)
     self.assertNotEqual(file_dict, {})
     self.assertEqual(file_dict[self.unreadable_path], None)