def test_set_config_from_string_list_negative(self):
        config_builder = LintConfigBuilder()
        config_builder.set_config_from_string_list(['foo.bar=1'])
        # assert error on incorrect rule - this happens at build time
        with self.assertRaisesRegex(LintConfigError, "No such rule 'foo'"):
            config_builder.build()

        # no equal sign
        expected_msg = "'foo.bar' is an invalid configuration option. Use '<rule>.<option>=<value>'"
        with self.assertRaisesRegex(LintConfigError, expected_msg):
            config_builder.set_config_from_string_list(['foo.bar'])

        # missing value
        expected_msg = "'foo.bar=' is an invalid configuration option. Use '<rule>.<option>=<value>'"
        with self.assertRaisesRegex(LintConfigError, expected_msg):
            config_builder.set_config_from_string_list(['foo.bar='])

        # space instead of equal sign
        expected_msg = "'foo.bar 1' is an invalid configuration option. Use '<rule>.<option>=<value>'"
        with self.assertRaisesRegex(LintConfigError, expected_msg):
            config_builder.set_config_from_string_list(['foo.bar 1'])

        # no period between rule and option names
        expected_msg = "'foobar=1' is an invalid configuration option. Use '<rule>.<option>=<value>'"
        with self.assertRaisesRegex(LintConfigError, expected_msg):
            config_builder.set_config_from_string_list(['foobar=1'])
Beispiel #2
0
    def test_set_config_from_string_list_negative(self):
        config_builder = LintConfigBuilder()

        # assert error on incorrect rule - this happens at build time
        config_builder.set_config_from_string_list([u"föo.bar=1"])
        with self.assertRaisesRegex(LintConfigError, u"No such rule 'föo'"):
            config_builder.build()

        # no equal sign
        expected_msg = u"'föo.bar' is an invalid configuration option. Use '<rule>.<option>=<value>'"
        with self.assertRaisesRegex(LintConfigError, expected_msg):
            config_builder.set_config_from_string_list([u"föo.bar"])

        # missing value
        expected_msg = u"'föo.bar=' is an invalid configuration option. Use '<rule>.<option>=<value>'"
        with self.assertRaisesRegex(LintConfigError, expected_msg):
            config_builder.set_config_from_string_list([u"föo.bar="])

        # space instead of equal sign
        expected_msg = u"'föo.bar 1' is an invalid configuration option. Use '<rule>.<option>=<value>'"
        with self.assertRaisesRegex(LintConfigError, expected_msg):
            config_builder.set_config_from_string_list([u"föo.bar 1"])

        # no period between rule and option names
        expected_msg = u"'föobar=1' is an invalid configuration option. Use '<rule>.<option>=<value>'"
        with self.assertRaisesRegex(LintConfigError, expected_msg):
            config_builder.set_config_from_string_list([u'föobar=1'])
Beispiel #3
0
    def test_set_config_from_string_list(self):
        config = LintConfig()
        # assert some defaults
        self.assertEqual(
            config.get_rule_option('title-max-length', 'line-length'), 72)
        self.assertEqual(
            config.get_rule_option('body-max-line-length', 'line-length'), 80)
        self.assertListEqual(
            config.get_rule_option('title-must-not-contain-word', 'words'),
            ["WIP"])
        self.assertEqual(config.verbosity, 3)

        # change and assert changes
        config_builder = LintConfigBuilder()
        config_builder.set_config_from_string_list([
            'general.verbosity=1', 'title-max-length.line-length=60',
            'body-max-line-length.line-length=120',
            u"title-must-not-contain-word.words=håha"
        ])

        config = config_builder.build()
        self.assertEqual(
            config.get_rule_option('title-max-length', 'line-length'), 60)
        self.assertEqual(
            config.get_rule_option('body-max-line-length', 'line-length'), 120)
        self.assertListEqual(
            config.get_rule_option('title-must-not-contain-word', 'words'),
            [u"håha"])
        self.assertEqual(config.verbosity, 1)
    def test_set_config_from_string_list(self):
        config = LintConfig()
        # assert some defaults
        self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 72)
        self.assertEqual(config.get_rule_option('body-max-line-length', 'line-length'), 80)
        self.assertEqual(config.verbosity, 3)

        # change and assert changes
        config_builder = LintConfigBuilder()
        config_builder.set_config_from_string_list(['general.verbosity=1', 'title-max-length.line-length=60',
                                                    'body-max-line-length.line-length=120'])

        config = config_builder.build()
        self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 60)
        self.assertEqual(config.get_rule_option('body-max-line-length', 'line-length'), 120)
        self.assertEqual(config.verbosity, 1)
Beispiel #5
0
def build_config(  # pylint: disable=too-many-arguments
        ctx, target, config_path, c, extra_path, ignore, contrib, ignore_stdin,
        verbose, silent, debug):
    """ Creates a LintConfig object based on a set of commandline parameters. """
    config_builder = LintConfigBuilder()
    try:
        # Config precedence:
        # First, load default config or config from configfile
        if config_path:
            config_builder.set_from_config_file(config_path)
        elif os.path.exists(DEFAULT_CONFIG_FILE):
            config_builder.set_from_config_file(DEFAULT_CONFIG_FILE)

        # Then process any commandline configuration flags
        config_builder.set_config_from_string_list(c)

        # Finally, overwrite with any convenience commandline flags
        if ignore:
            config_builder.set_option('general', 'ignore', ignore)

        if contrib:
            config_builder.set_option('general', 'contrib', contrib)

        if ignore_stdin:
            config_builder.set_option('general', 'ignore-stdin', ignore_stdin)

        if silent:
            config_builder.set_option('general', 'verbosity', 0)
        elif verbose > 0:
            config_builder.set_option('general', 'verbosity', verbose)

        if extra_path:
            config_builder.set_option('general', 'extra-path', extra_path)

        if target:
            config_builder.set_option('general', 'target', target)

        if debug:
            config_builder.set_option('general', 'debug', debug)

        config = config_builder.build()

        return config, config_builder
    except LintConfigError as e:
        click.echo(u"Config Error: {0}".format(ustr(e)))
    ctx.exit(CONFIG_ERROR_CODE)  # return CONFIG_ERROR_CODE on config error
Beispiel #6
0
def build_config(ctx, target, config_path, c, extra_path, ignore, verbose, silent, debug):
    """ Creates a LintConfig object based on a set of commandline parameters. """
    config_builder = LintConfigBuilder()
    try:
        # Config precedence:
        # First, load default config or config from configfile
        if config_path:
            config_builder.set_from_config_file(config_path)
        elif os.path.exists(DEFAULT_CONFIG_FILE):
            config_builder.set_from_config_file(DEFAULT_CONFIG_FILE)

        # Then process any commandline configuration flags
        config_builder.set_config_from_string_list(c)

        # Finally, overwrite with any convenience commandline flags
        if ignore:
            config_builder.set_option('general', 'ignore', ignore)
        if silent:
            config_builder.set_option('general', 'verbosity', 0)
        elif verbose > 0:
            config_builder.set_option('general', 'verbosity', verbose)

        if extra_path:
            config_builder.set_option('general', 'extra-path', extra_path)

        if target:
            config_builder.set_option('general', 'target', target)

        if debug:
            config_builder.set_option('general', 'debug', debug)

        config = config_builder.build()
        if debug:
            click.echo(str(config), nl=True)

        return config, config_builder
    except LintConfigError as e:
        click.echo("Config Error: {0}".format(str(e)))
    ctx.exit(CONFIG_ERROR_CODE)  # return CONFIG_ERROR_CODE on config error