Example #1
0
def extract_aspects_from_section(section):
    """
    Extract aspects settings from a section into an AspectList.

    Note that the section is assumed to already have valid and complete aspects
    related setting. This checking could be done by
    :meth:`coalib.settings.ConfigurationGathering.validate_aspect_config`.

    :param section: Section object.
    :return:        AspectList containing aspectclass instance with
                    user-defined tastes.
    """
    ASPECT_TASTE_DELIMITER = ':'
    aspects = section.get('aspects')
    language = section.language

    aspect_instances = AspectList(exclude=section.get('excludes'))

    for aspect in AspectList(aspects):
        # Search all related tastes in section.
        tastes = {
            name.split(ASPECT_TASTE_DELIMITER)[-1]: value
            for name, value in section.contents.items()
            if name.lower().startswith(aspect.__name__.lower())
        }
        aspect_instances.append(aspect(language, **tastes))

    return aspect_instances
Example #2
0
def extract_aspects_from_section(section):
    """
    Extract aspects settings from a section into an AspectList.

    Note that the section is assumed to already have valid and complete aspects
    related setting. This checking could be done by
    :meth:`coalib.settings.ConfigurationGathering.validate_aspect_config`.

    :param section: Section object.
    :return:        AspectList containing aspectclass instance with
                    user-defined tastes.
    """
    ASPECT_TASTE_DELIMITER = ':'
    aspects = section.get('aspects')
    language = section.language

    aspect_instances = AspectList(exclude=section.get('excludes'))

    for aspect in AspectList(aspects):
        # Search all related tastes in section.
        tastes = {name.split(ASPECT_TASTE_DELIMITER)[-1]: value
                  for name, value in section.contents.items()
                  if name.lower().startswith(aspect.__name__.lower())}
        aspect_instances.append(aspect(language, **tastes))

    return aspect_instances
Example #3
0
def extract_aspects_from_section(section):
    """
    Extracts aspects and their related settings from a section and create an
    AspectList from it.

    :param section: Section object.
    :return:        AspectList containing aspectclass instance with
                    user-defined tastes.
    """
    aspects = section.get('aspects')
    language = section.get('language')

    # Skip aspects initialization if not configured in section
    if not len(aspects):
        return None

    if not len(language):
        raise AttributeError('Language was not found in configuration file. '
                             'Usage of aspect-based configuration must '
                             'include language information.')

    aspect_instances = AspectList(exclude=section.get('excludes'))

    for aspect in AspectList(aspects):
        # Search all related tastes in section.
        tastes = {
            name.split('.')[-1]: value
            for name, value in section.contents.items()
            if name.lower().startswith(aspect.__name__.lower())
        }
        aspect_instances.append(aspect(language, **tastes))

    return aspect_instances
Example #4
0
 def test_mapping(self):
     self.section.aspects = AspectList([
         get_aspect('UnreachableCode')('py'),
         get_aspect('Clone')('py', min_clone_tokens=30),
     ])
     result = self.bear.execute()
     self.assertEqual([True, 30], result)
Example #5
0
 def test_partial_mapping(self):
     self.section.aspects = AspectList([
         get_aspect('UnreachableCode')('py'),
     ])
     result = self.bear.execute()
     expected = self.EXPECTED.copy()
     expected['remove_unreachable_code'] = True
     self.assertEqual(expected, dict(result))
Example #6
0
 def test_setting_priority(self):
     self.section.aspects = AspectList([
         get_aspect('Indentation')('py', indent_type='tab'),
     ])
     self.section.append(Setting('use_spaces', True))
     result = self.bear.execute()
     expected = self.EXPECTED.copy()
     self.assertEqual(expected, dict(result))
Example #7
0
 def test_mapping(self):
     self.section.aspects = AspectList([
         get_aspect('Indentation')('py', indent_type='tab'),
     ])
     result = self.bear.execute()
     expected = self.EXPECTED.copy()
     expected['use_spaces'] = False
     self.assertEqual(expected, dict(result))
Example #8
0
 def test_setting_priority(self):
     self.section.aspects = AspectList([
         get_aspect('UnreachableCode')('py'),
         get_aspect('Clone')('py', min_clone_tokens=30),
     ])
     self.section.append(Setting('remove_unreachable_code', 'False'))
     self.section.append(Setting('minimum_clone_tokens', 40))
     result = self.bear.execute()
     self.assertEqual([False, 40], result)
Example #9
0
    def test_aspect_bear(self):
        with bear_test_module():
            aspects = AspectList([get_aspect('unusedvariable')('py')])
            local_bears, global_bears = collect_bears_by_aspects(
                aspects, [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL])

        self.assertEqual(len(global_bears), 0)
        self.assertEqual(len(local_bears), 1)
        self.assertIs(local_bears[0].name, 'AspectTestBear')
Example #10
0
 def test_mapping(self):
     self.section.aspects = AspectList([
         get_aspect('UnreachableCode')('py'),
         get_aspect('Clone')('py', min_clone_tokens=30),
     ])
     result = self.bear.execute()
     expected = self.EXPECTED.copy()
     expected['remove_unreachable_code'] = True
     expected['minimum_clone_tokens'] = 30
     self.assertEqual(expected, dict(result))
Example #11
0
 def test_setting_priority(self):
     self.section.aspects = AspectList([
         get_aspect('UnreachableCode')('py'),
         get_aspect('Clone')('py', min_clone_tokens=30),
     ])
     self.section.append(Setting('remove_unreachable_code', 'False'))
     self.section.append(Setting('minimum_clone_tokens', 40))
     result = self.bear.execute()
     expected = self.EXPECTED.copy()
     expected['minimum_clone_tokens'] = 40
     self.assertEqual(expected, dict(result))
Example #12
0
    def test_collect_bears_unfulfilled_aspect(self):
        aspects = AspectList([
            get_aspect('unusedvariable')('py'),
        ])

        logger = logging.getLogger()
        with bear_test_module(), self.assertLogs(logger, 'WARNING') as log:
            local_bears, global_bears = collect_bears_by_aspects(
                aspects, [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL])
        self.assertRegex(
            log.output[0], 'coala cannot find bear that could analyze the '
            r'following aspects: \['
            r"'Root\.Redundancy\.UnusedVariable\.UnusedParameter'"
            r'\]')

        self.assertEqual(global_bears, [])
        self.assertEqual(str(local_bears),
                         "[<class 'AspectTestBear.AspectTestBear'>]")
Example #13
0
 def test_no_mapping(self):
     self.section.aspects = AspectList([])
     result = self.bear.execute()
     self.assertEqual([False, 10], result)
 def setUp(self):
     self.section = Section('')
     self.section.aspects = AspectList([
         get_aspect('LineLength')('Python', max_line_length=30),
     ])
     self.uut = PycodestyleBear(self.section, Queue())
LineLengthBearValidLanguageTest = verify_local_bear(
    LineLengthBear,
    valid_files=(test_file,),
    invalid_files=(invalid_general_file,),
    settings={'language': 'C'},
)


LineLengthBearAspectTest = verify_local_bear(
    LineLengthBear,
    valid_files=(test_file,),
    invalid_files=('testa',
                   'test line'),
    aspects=AspectList([
        get_aspect('LineLength')('Unknown', max_line_length=4),
        ]),
)


SettingsOverAspectsPriorityTest = verify_local_bear(
    LineLengthBear,
    valid_files=(test_file,),
    invalid_files=('testa',
                   'test line'),
    aspects=AspectList([
        get_aspect('LineLength')('Unknown', max_line_length=10),
        ]),
    settings={'max_line_length': '4'},
)
Example #16
0
 def test_no_mapping(self):
     self.section.aspects = AspectList([])
     result = self.bear.execute()
     expected = self.EXPECTED.copy()
     self.assertEqual(expected, dict(result))
Example #17
0
 def test_partial_mapping(self):
     self.section.aspects = AspectList([
         get_aspect('UnreachableCode')('py'),
     ])
     result = self.bear.execute()
     self.assertEqual([True, 10], result)
    valid_files=(long_line, ),
    invalid_files=(),
    settings={'max_line_length': 200})

PycodestyleBearInfiniteLineLengthTest = verify_local_bear(
    PycodestyleBear,
    valid_files=(file_with_very_long_line, ),
    invalid_files=(),
    settings={'max_line_length': 0})

PycodestyleBearAspectsTest = verify_local_bear(
    PycodestyleBear,
    valid_files=(small_line, ),
    invalid_files=(long_line, ),
    aspects=AspectList([
        get_aspect('LineLength')('Python', max_line_length=30),
    ]),
)

PycodestyleBearSettingsOverAspectsTest = verify_local_bear(
    PycodestyleBear,
    valid_files=(small_line, ),
    invalid_files=(long_line, ),
    aspects=AspectList([
        get_aspect('LineLength')('Python', max_line_length=2),
    ]),
    settings={'max_line_length': 30},
)


class PycodestyleBearTest(unittest.TestCase):