def test_set_from_config_file(self): # regular config file load, no problems config_builder = LintConfigBuilder() config_builder.set_from_config_file(self.get_sample_path("config/gitlintconfig")) config = config_builder.build() # Do some assertions on the config self.assertEqual(config.verbosity, 1) self.assertFalse(config.debug) self.assertFalse(config.ignore_merge_commits) self.assertIsNone(config.extra_path) self.assertEqual(config.ignore, ["title-trailing-whitespace", "B2"]) self.assertEqual(config.get_rule_option('title-max-length', 'line-length'), 20) self.assertEqual(config.get_rule_option('body-max-line-length', 'line-length'), 30)
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
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
def test_set_from_config_file_negative(self): config_builder = LintConfigBuilder() # bad config file load foo_path = self.get_sample_path("foo") with self.assertRaisesRegex(LintConfigError, "Invalid file path: {0}".format(foo_path)): config_builder.set_from_config_file(foo_path) # error during file parsing path = self.get_sample_path("config/no-sections") expected_error_msg = "File contains no section headers." with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.set_from_config_file(path) # non-existing rule path = self.get_sample_path("config/nonexisting-rule") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = "No such rule 'foobar'" config_builder.set_from_config_file(path) with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.build() # non-existing general option path = self.get_sample_path("config/nonexisting-general-option") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = "'foo' is not a valid gitlint option" with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.build() # non-existing option path = self.get_sample_path("config/nonexisting-option") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = "Rule 'title-max-length' has no option 'foobar'" with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.build() # invalid option value path = self.get_sample_path("config/invalid-option-value") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = "'foo' is not a valid value for option 'title-max-length.line-length'. " + \ r"Option 'line-length' must be a positive integer \(current value: 'foo'\)." with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.build()
def test_set_from_config_file_negative(self): config_builder = LintConfigBuilder() # bad config file load foo_path = self.get_sample_path(u"föo") expected_error_msg = u"Invalid file path: {0}".format( re.escape(foo_path)) with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.set_from_config_file(foo_path) # error during file parsing path = self.get_sample_path("config/no-sections") expected_error_msg = u"File contains no section headers." with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.set_from_config_file(path) # non-existing rule path = self.get_sample_path("config/nonexisting-rule") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = u"No such rule 'föobar'" with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.build() # non-existing general option path = self.get_sample_path("config/nonexisting-general-option") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = u"'foo' is not a valid gitlint option" with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.build() # non-existing option path = self.get_sample_path("config/nonexisting-option") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = u"Rule 'title-max-length' has no option 'föobar'" with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.build() # invalid option value path = self.get_sample_path("config/invalid-option-value") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = u"'föo' is not a valid value for option 'title-max-length.line-length'. " + \ ustr(r"Option 'line-length' must be a positive integer \(current value: 'föo'\).") with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.build()
def test_set_from_config_file_negative(self): config_builder = LintConfigBuilder() # bad config file load foo_path = self.get_sample_path("föo") expected_error_msg = f"Invalid file path: {foo_path}" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.set_from_config_file(foo_path) # error during file parsing path = self.get_sample_path("config/no-sections") expected_error_msg = "File contains no section headers." # We only match the start of the message here, since the exact message can vary depending on platform with self.assertRaisesRegex(LintConfigError, expected_error_msg): config_builder.set_from_config_file(path) # non-existing rule path = self.get_sample_path("config/nonexisting-rule") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = "No such rule 'föobar'" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() # non-existing general option path = self.get_sample_path("config/nonexisting-general-option") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = "'foo' is not a valid gitlint option" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() # non-existing option path = self.get_sample_path("config/nonexisting-option") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = "Rule 'title-max-length' has no option 'föobar'" with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build() # invalid option value path = self.get_sample_path("config/invalid-option-value") config_builder = LintConfigBuilder() config_builder.set_from_config_file(path) expected_error_msg = "'föo' is not a valid value for option 'title-max-length.line-length'. " + \ "Option 'line-length' must be a positive integer (current value: 'föo')." with self.assertRaisesMessage(LintConfigError, expected_error_msg): config_builder.build()