Ejemplo n.º 1
0
 def setUp(self):
     self.section = Section("")
     self.section.append(Setting('language', 'test'))
     self.section.append(Setting('use_spaces', False))
     self.section.append(Setting('coalang_dir', escape(os.path.join(
         os.path.dirname(__file__), "test_files"), '\\')))
     self.dep_uut = AnnotationBear(self.section, Queue())
Ejemplo n.º 2
0
    def create_arguments(filename, file, config_file,
                         r_keep_comments: bool=True,
                         r_keep_blank_lines: bool=True,
                         r_braces_on_next_line: bool=False,
                         r_use_arrows: bool=False,
                         indent_size:
                         int=SpacingHelper.DEFAULT_TAB_WIDTH,
                         r_max_expression_length: int=0):
        """
        :param r_keep_comments:
            Determines whether comments are kept or not.
        :param r_keep_blank_lines:
            Determines whether blank lines are kept or not.
        :param r_braces_on_next_line:
            Determines whether a brace should be placed on the next line.

            Example:
            If ``True``,
            ```
            if (...) {
            ```
            changes to
            ```
            if (...)
            {
            ```
            If ``False`` the brace is placed on the same line.
        :param r_use_arrows:
            Determines whether the assignment operator ``=`` should be replaced
            by an arrow ``<-`` or not.

            Example: If  ``True``, ``x = 1`` changes to ``x <- 1``.
        :param indent_size:
            Number of spaces per indentation level.
        :param r_max_expression_length:
            Maximum number of characters for an expression.

            Example: If ``20`` then
            ```
            1 + 1 + 1 + 1 + 1 + 1 + 1
            ```
            changes to
            ```
            1 + 1 + 1 + 1 + 1 + 1 +
                1
            ```
        """
        options = {'source="' + escape(filename, '"\\') + '"',
                   'blank=' + _map_to_r_bool(r_keep_blank_lines),
                   'brace.newline=' + _map_to_r_bool(r_braces_on_next_line),
                   'comment=' + _map_to_r_bool(r_keep_comments),
                   'arrow=' + _map_to_r_bool(r_use_arrows),
                   'indent=' + str(indent_size)}
        if r_max_expression_length:
            options.add('width.cutoff=' + str(r_max_expression_length))

        rcode = 'library(formatR);formatR::tidy_source({})'.format(
            ','.join(options))
        return '-e', rcode
Ejemplo n.º 3
0
 def test_external_coalang(self):
     self.section1.append(Setting('coalang_dir', escape(os.path.join(
                         os.path.dirname(__file__), 'test_files'), '\\')))
     self.section1.append(Setting('language', 'test'))
     uut = AnnotationBear(self.section1, Queue())
     text = ['//comment line 1\n', '"""string line 2"""']
     with execute_bear(uut, "F", text) as result:
         self.assertNotEqual(result[0].contents['strings'], ())
         self.assertNotEqual(result[0].contents['comments'], ())
Ejemplo n.º 4
0
 def test_external_coalang(self):
     self.section1.append(
         Setting(
             'coalang_dir',
             escape(os.path.join(os.path.dirname(__file__), 'test_files'),
                    '\\')))
     self.section1.append(Setting('language', 'test'))
     uut = AnnotationBear(self.section1, Queue())
     text = ['//comment line 1\n', '"""string line 2"""']
     with execute_bear(uut, "F", text) as result:
         self.assertNotEqual(result[0].contents['strings'], ())
         self.assertNotEqual(result[0].contents['comments'], ())
Ejemplo n.º 5
0
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit("Add a very long shortlog for a bad project history.")
     os.chdir(no_git_dir)
     # When section doesn't have a project_dir
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], "git:")
     # when section does have a project_dir
     self.section.append(Setting("project_dir", escape(self.gitdir, '\\')))
     self.assertEqual(self.run_uut(),
                      ["Shortlog of HEAD commit is 1 character(s) longer "
                       "than the limit (51 > 50)."])
     self.assertEqual(get_config_directory(self.section),
                      self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)
Ejemplo n.º 6
0
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit("Add a very long shortlog for a bad project history.")
     os.chdir(no_git_dir)
     # When section doesn't have a project_dir
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], "git:")
     # when section does have a project_dir
     self.section.append(Setting("project_dir", escape(self.gitdir, '\\')))
     self.assertEqual(self.run_uut(), [
         "Shortlog of HEAD commit is 1 character(s) longer "
         "than the limit (51 > 50)."
     ])
     self.assertEqual(get_config_directory(self.section), self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)
Ejemplo n.º 7
0
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit('Add a very long shortlog for a bad project history.')
     os.chdir(no_git_dir)
     # When section doesn't have a project_dir
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], 'git:')
     # when section does have a project_dir
     self.section.append(Setting('project_dir', escape(self.gitdir, '\\')))
     self.assertEqual(self.run_uut(), [
         'Shortlog of the HEAD commit contains 51 '
         'character(s). This is 1 character(s) longer than '
         'the limit (51 > 50).'
     ])
     self.assertEqual(get_config_directory(self.section), self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)
 def test_different_path(self):
     no_git_dir = mkdtemp()
     self.git_commit('Add a very long shortlog for a bad project history.')
     os.chdir(no_git_dir)
     # When section doesn't have a project_dir
     self.assertEqual(self.run_uut(), [])
     git_error = self.msg_queue.get().message
     self.assertEqual(git_error[:4], 'git:')
     # when section does have a project_dir
     self.section.append(Setting('project_dir', escape(self.gitdir, '\\')))
     self.assertEqual(self.run_uut(),
                      ['Shortlog of the HEAD commit contains 51 '
                       'character(s). This is 1 character(s) longer than '
                       'the limit (51 > 50).'])
     self.assertEqual(get_config_directory(self.section),
                      self.gitdir)
     os.chdir(self.gitdir)
     os.rmdir(no_git_dir)
    def test_write_coafile(self):
        from pyprint.ConsolePrinter import ConsolePrinter
        printer = ConsolePrinter()
        input_ = [{TestLocalBear: [{'filename': 'a.py', 'some_setting': 3},
                                   {'filename': 'b.py', 'some_setting': 3},
                                   {'filename': 'c.py', 'some_setting': 4},
                                   {'filename': 'd.py',
                                    'some_other_setting': 'x'}]},
                  None]
        data_struct = generate_data_struct_for_sections(input_)
        test_data_struct = {TestLocalBear: [[{'filename': ['a.py', 'b.py'],
                                              'some_setting': 3},
                                             {'filename': ['c.py'],
                                              'some_setting': 4}],
                                            [{'some_other_setting': 'x',
                                              'filename': ['d.py']}]]}
        self.assertEqual(data_struct, test_data_struct)
        project_files = ['a.py', 'b.py', 'c.py', 'd.py']
        coafile = '.coafile.green'
        full_path = str(Path(__file__).parent.parent.parent)
        full_path_coafile = str(Path(__file__).parent.parent.parent / coafile)
        with patch('os.walk') as mockwalk:
            mockwalk.return_val = mockwalk.return_value = [
                ('', (), ('a.py', 'b.py', 'c.py', 'd.py')), ]
            generate_green_mode_sections(data_struct, full_path,
                                         project_files, ['x'],
                                         printer)
        contents = ""

        with open(full_path_coafile) as f:
            for line in f.readlines():
                contents += line
        full_path_glob = escape(full_path + os.sep + '**', '\\')
        test_contents = dedent("""
            [all]
            ignore = x

            [all.TestLocalBear1]
            ignore += a.py, b.py, c.py, d.py
            bears = TestLocalBear
            some_setting = 3
            files = a.py, b.py, {full_path_glob}

            [all.TestLocalBear2]
            ignore += a.py, b.py, c.py, d.py
            files = c.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 4

            [all.TestLocalBear3]
            ignore += a.py, b.py, c.py, d.py
            files = d.py, {full_path_glob}
            bears = TestLocalBear
            some_other_setting = x""").format(
                full_path_glob=full_path_glob)
        # Since the order of settings within a seciton is volatile.
        print('test_contents')
        for line in test_contents.split('\n'):
            if line == 'ignore = x':
                continue  # Since the path depends on the test directory
                self.assertIn(line, [i.strip('\\').replace('\\\\C', 'C')
                                     for i in contents.split('\n')])

        with patch('os.walk') as mockwalk:
            mockwalk.return_val = mockwalk.return_value = [
                ('', (), ('a.py', 'b.py', 'c.py', 'd.py')), ]
            generate_green_mode_sections(data_struct, full_path,
                                         project_files, [],
                                         printer)
        contents = ""

        with open(full_path_coafile) as f:
            for line in f.readlines():
                contents += line
        # TODO: remove the prefix 'all.' from section names when section
        # all is not present which only happends when the ignore field
        # is empty.
        test_contents = dedent("""
            [all.TestLocalBear1]
            ignore = a.py, b.py, c.py, d.py
            files = a.py, b.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 3

            [all.TestLocalBear2]
            ignore = a.py, b.py, c.py, d.py
            files = c.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 4

            [all.TestLocalBear3]
            ignore = a.py, b.py, c.py, d.py
            files = d.py, {full_path_glob}
            bears = TestLocalBear
            some_other_setting = x""").format(
                full_path_glob=full_path_glob)
        # Since the order of settings within a seciton is volatile.
        for line in test_contents.split('\n'):
            if line == 'ignore = x':
                continue  # Since the path depends on the test directory
            self.assertIn(line, [i.strip('\\').replace('\\\\C', 'C')
                                 for i in contents.split('\n')])

        test_data_struct = {TestLocalBear: [[{'filename': ['a.py', 'b.py'],
                                              'some_setting': 3},
                                             {'filename': ['c.py'],
                                              'some_setting': 4},
                                             {'filename': ['a.py', 'b.py'],
                                              'some_setting': 4}],
                                            [{'some_other_setting': 'x',
                                              'filename': ['d.py']}],
                                            []]}
        with patch('os.walk') as mockwalk:
            mockwalk.return_val = mockwalk.return_value = [
                ('', (), ('a.py', 'b.py', 'c.py', 'd.py')), ]
            generate_green_mode_sections(test_data_struct, full_path,
                                         project_files, [],
                                         printer)
        contents = ""

        with open(full_path_coafile) as f:
            for line in f.readlines():
                contents += line

        # TODO: section name enumerations should not skip integers.
        test_contents = dedent("""
            [all.TestLocalBear1]
            ignore = a.py, b.py, c.py, d.py
            files = a.py, b.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 3

            [all.TestLocalBear2]
            ignore = a.py, b.py, c.py, d.py
            files = c.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 4

            [all.TestLocalBear4]
            ignore = a.py, b.py, c.py, d.py
            files = d.py, {full_path_glob}
            bears = TestLocalBear
            some_other_setting = x""").format(
                full_path_glob=full_path_glob)

        # Since the order of settings within a seciton is volatile.
        for line in test_contents.split('\n'):
            if line == 'ignore = x':
                continue  # Since the path depends on the test directory
            self.assertIn(line, [i.strip('\\').replace('\\\\C', 'C')
                                 for i in contents.split('\n')])
Ejemplo n.º 10
0
    def test_write_coafile(self):
        from pyprint.ConsolePrinter import ConsolePrinter
        printer = ConsolePrinter()
        input_ = [{
            TestLocalBear: [{
                'filename': 'a.py',
                'some_setting': 3
            }, {
                'filename': 'b.py',
                'some_setting': 3
            }, {
                'filename': 'c.py',
                'some_setting': 4
            }, {
                'filename': 'd.py',
                'some_other_setting': 'x'
            }]
        }, None]
        data_struct = generate_data_struct_for_sections(input_)
        test_data_struct = {
            TestLocalBear: [[{
                'filename': ['a.py', 'b.py'],
                'some_setting': 3
            }, {
                'filename': ['c.py'],
                'some_setting': 4
            }], [{
                'some_other_setting': 'x',
                'filename': ['d.py']
            }]]
        }
        self.assertEqual(data_struct, test_data_struct)
        project_files = ['a.py', 'b.py', 'c.py', 'd.py']
        coafile = '.coafile.green'
        full_path = str(Path(__file__).parent.parent.parent)
        full_path_coafile = str(Path(__file__).parent.parent.parent / coafile)
        with patch('os.walk') as mockwalk:
            mockwalk.return_val = mockwalk.return_value = [
                ('', (), ('a.py', 'b.py', 'c.py', 'd.py')),
            ]
            generate_green_mode_sections(data_struct, full_path, project_files,
                                         ['x'], printer)
        contents = ""

        with open(full_path_coafile) as f:
            for line in f.readlines():
                contents += line
        full_path_glob = escape(full_path + os.sep + '**', '\\')
        test_contents = dedent("""
            [all]
            ignore = x

            [all.TestLocalBear1]
            ignore += a.py, b.py, c.py, d.py
            bears = TestLocalBear
            some_setting = 3
            files = a.py, b.py, {full_path_glob}

            [all.TestLocalBear2]
            ignore += a.py, b.py, c.py, d.py
            files = c.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 4

            [all.TestLocalBear3]
            ignore += a.py, b.py, c.py, d.py
            files = d.py, {full_path_glob}
            bears = TestLocalBear
            some_other_setting = x""").format(full_path_glob=full_path_glob)
        # Since the order of settings within a seciton is volatile.
        print('test_contents')
        for line in test_contents.split('\n'):
            if line == 'ignore = x':
                continue  # Since the path depends on the test directory
                self.assertIn(line, [
                    i.strip('\\').replace('\\\\C', 'C')
                    for i in contents.split('\n')
                ])

        with patch('os.walk') as mockwalk:
            mockwalk.return_val = mockwalk.return_value = [
                ('', (), ('a.py', 'b.py', 'c.py', 'd.py')),
            ]
            generate_green_mode_sections(data_struct, full_path, project_files,
                                         [], printer)
        contents = ""

        with open(full_path_coafile) as f:
            for line in f.readlines():
                contents += line
        # TODO: remove the prefix 'all.' from section names when section
        # all is not present which only happends when the ignore field
        # is empty.
        test_contents = dedent("""
            [all.TestLocalBear1]
            ignore = a.py, b.py, c.py, d.py
            files = a.py, b.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 3

            [all.TestLocalBear2]
            ignore = a.py, b.py, c.py, d.py
            files = c.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 4

            [all.TestLocalBear3]
            ignore = a.py, b.py, c.py, d.py
            files = d.py, {full_path_glob}
            bears = TestLocalBear
            some_other_setting = x""").format(full_path_glob=full_path_glob)
        # Since the order of settings within a seciton is volatile.
        for line in test_contents.split('\n'):
            if line == 'ignore = x':
                continue  # Since the path depends on the test directory
            self.assertIn(line, [
                i.strip('\\').replace('\\\\C', 'C')
                for i in contents.split('\n')
            ])

        test_data_struct = {
            TestLocalBear: [[{
                'filename': ['a.py', 'b.py'],
                'some_setting': 3
            }, {
                'filename': ['c.py'],
                'some_setting': 4
            }, {
                'filename': ['a.py', 'b.py'],
                'some_setting': 4
            }], [{
                'some_other_setting': 'x',
                'filename': ['d.py']
            }], []]
        }
        with patch('os.walk') as mockwalk:
            mockwalk.return_val = mockwalk.return_value = [
                ('', (), ('a.py', 'b.py', 'c.py', 'd.py')),
            ]
            generate_green_mode_sections(test_data_struct, full_path,
                                         project_files, [], printer)
        contents = ""

        with open(full_path_coafile) as f:
            for line in f.readlines():
                contents += line

        # TODO: section name enumerations should not skip integers.
        test_contents = dedent("""
            [all.TestLocalBear1]
            ignore = a.py, b.py, c.py, d.py
            files = a.py, b.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 3

            [all.TestLocalBear2]
            ignore = a.py, b.py, c.py, d.py
            files = c.py, {full_path_glob}
            bears = TestLocalBear
            some_setting = 4

            [all.TestLocalBear4]
            ignore = a.py, b.py, c.py, d.py
            files = d.py, {full_path_glob}
            bears = TestLocalBear
            some_other_setting = x""").format(full_path_glob=full_path_glob)

        # Since the order of settings within a seciton is volatile.
        for line in test_contents.split('\n'):
            if line == 'ignore = x':
                continue  # Since the path depends on the test directory
            self.assertIn(line, [
                i.strip('\\').replace('\\\\C', 'C')
                for i in contents.split('\n')
            ])
Ejemplo n.º 11
0
 def create_arguments(filename, file, config_file):
     lintcode = ('import Lint.lintfile; display(lintfile("' +
                 escape(filename, '"\\') + '"))')
     return '-e', lintcode
Ejemplo n.º 12
0
 def create_arguments(filename, file, config_file):
     lintcode = ('import Lint.lintfile; display(lintfile("' +
                 escape(filename, '"\\') + '"))')
     return '-e', lintcode
Ejemplo n.º 13
0
def generate_green_mode_sections(data,
                                 project_dir,
                                 project_files,
                                 ignore_globs,
                                 printer=None,
                                 suffix=''):
    """
    Generates the section objects for the green_mode.
    :param data:
        This is the data structure generated from the method
        generate_data_struct_for_sections().
    :param project_dir:
        The path of the project directory.
    :param project_files:
        List of paths to only the files inside the project directory.
    :param ignore_globs:
        The globs of files to ignore.
    :param printer:
        The ConsolePrinter object.
    :param suffix:
        A suffix that can be added to the `.coafile.green`.
    """
    all_sections = {
        'all': [],
    }
    lang_files = split_by_language(project_files)
    extset = get_extensions(project_files)
    ignored_files = generate_ignore_field(project_dir, lang_files.keys(),
                                          extset, ignore_globs)
    if ignored_files:
        section_all = Section('all')
        section_all['ignore'] = ignored_files
        all_sections['all'].append(section_all)

    for bear in data:
        num = 0
        bear_sections = []
        for setting_combs in data[bear]:
            if not setting_combs:
                continue
            for dict_ in setting_combs:
                num = num + 1
                section = Section('all.' + bear.__name__ + str(num), None)
                for key_ in dict_:
                    if key_ == 'filename':
                        file_list_sec = dict_[key_]
                        file_list_sec, ignore_list = aggregate_files(
                            file_list_sec, project_dir)
                        dict_[key_] = file_list_sec
                        section['ignore'] = ', '.join(
                            escape(x, '\\') for x in ignore_list)
                        section['files'] = ', '.join(
                            escape(x, '\\') for x in dict_[key_])
                    else:
                        section[key_] = str(dict_[key_])
                    section['bears'] = bear.__name__
                bear_sections.append(section)
        # Remove sections for the same bear who have common files i.e. more
        # than one setting was found to be green.
        file_list = []
        new_bear_sections = []
        for index, section in enumerate(bear_sections):
            if not section.contents['files'] in file_list:
                file_list.append(section.contents['files'])
                new_bear_sections.append(section)
        all_sections[bear.__name__] = new_bear_sections

    coafile = os.path.join(project_dir, '.coafile.green' + suffix)
    writer = ConfWriter(coafile)
    write_sections(writer, all_sections)
    writer.close()
    printer.print("'" + coafile + "' successfully generated.", color='green')
Ejemplo n.º 14
0
def generate_green_mode_sections(data, project_dir, project_files,
                                 ignore_globs, printer=None, suffix=''):
    """
    Generates the section objects for the green_mode.
    :param data:
        This is the data structure generated from the method
        generate_data_struct_for_sections().
    :param project_dir:
        The path of the project directory.
    :param project_files:
        List of paths to only the files inside the project directory.
    :param ignore_globs:
        The globs of files to ignore.
    :param printer:
        The ConsolePrinter object.
    :param suffix:
        A suffix that can be added to the `.coafile.green`.
    """
    all_sections = {'all': [], }
    lang_files = split_by_language(project_files)
    extset = get_extensions(project_files)
    ignored_files = generate_ignore_field(project_dir, lang_files.keys(),
                                          extset, ignore_globs)
    if ignored_files:
        section_all = Section('all')
        section_all['ignore'] = ignored_files
        all_sections['all'].append(section_all)

    for bear in data:
        num = 0
        bear_sections = []
        for setting_combs in data[bear]:
            if not setting_combs:
                continue
            for dict_ in setting_combs:
                num = num + 1
                section = Section('all.' + bear.__name__ + str(num), None)
                for key_ in dict_:
                    if key_ == 'filename':
                        file_list_sec = dict_[key_]
                        file_list_sec, ignore_list = aggregate_files(
                            file_list_sec, project_dir)
                        dict_[key_] = file_list_sec
                        section['ignore'] = ', '.join(
                            escape(x, '\\') for x in ignore_list)
                        section['files'] = ', '.join(
                            escape(x, '\\') for x in dict_[key_])
                    else:
                        section[key_] = str(dict_[key_])
                    section['bears'] = bear.__name__
                bear_sections.append(section)
        # Remove sections for the same bear who have common files i.e. more
        # than one setting was found to be green.
        file_list = []
        new_bear_sections = []
        for index, section in enumerate(bear_sections):
            if not section.contents['files'] in file_list:
                file_list.append(section.contents['files'])
                new_bear_sections.append(section)
        all_sections[bear.__name__] = new_bear_sections

    coafile = os.path.join(project_dir, '.coafile.green' + suffix)
    writer = ConfWriter(coafile)
    write_sections(writer, all_sections)
    writer.close()
    printer.print("'" + coafile + "' successfully generated.", color='green')