Skip to content

Commit

Permalink
Improved debug mode
Browse files Browse the repository at this point in the history
Debug mode (--debug) will now print all the active rules and their
config values as well as the general config.

Also:
 - get_expected(...) method in BaseTestCase can now be used to easily
   fetch expected output files (useful for comparing big strings).
 - Tweaks to run_tests.sh output and fixes to summarized test result output
 - pylint should now also run in Travis (fingers crossed!)
  • Loading branch information
jroovers-cisco committed Dec 2, 2016
1 parent 10802a0 commit 9235aeb
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ python:
install:
- "pip install -r requirements.txt"
- "pip install -r test-requirements.txt"
script: "./run_tests.sh && ./run_tests.sh --pep8 && ./run_tests.sh --git"
script: "./run_tests.sh && ./run_tests.sh --pep8 && ./run_tests.sh --lint && ./run_tests.sh --git"
after_success:
- coveralls
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog #

## v0.8.0 (In Development) ##

- debug output improvements: Gitlint will now print a lot more information when using ```--debug```

## v0.7.1 (2016-06-18) ##
Bugfixes:

Expand Down
9 changes: 5 additions & 4 deletions gitlint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ def get_config(ctx, target, config_path, c, extra_path, ignore, verbose, silent,
# First, load default config or config from configfile
lint_config = load_config_from_path(ctx, config_path)
# default to default configuration when no config file was loaded
if lint_config:
if debug:
click.echo("Using config from {0}".format(lint_config.config_path))
else:
if not lint_config:
lint_config = LintConfig()

# Then process any commandline configuration flags
Expand All @@ -67,6 +64,10 @@ def get_config(ctx, target, config_path, c, extra_path, ignore, verbose, silent,

# Set target
lint_config.target = target

if lint_config.debug:
click.echo(str(lint_config), nl=True)

return lint_config
except LintConfigError as e:
click.echo("Config Error: {0}".format(str(e)))
Expand Down
13 changes: 13 additions & 0 deletions gitlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,19 @@ def __eq__(self, other):
self.debug == other.debug and \
self.config_path == other.config_path # noqa

def __str__(self):
return_str = "[GENERAL]\n"
return_str += "config path: {0}\n".format(self.config_path)
return_str += "extra path: {0}\n".format(self.extra_path)
return_str += "ignore merge commits: {0}\n".format(self.ignore_merge_commits)
return_str += "verbosity: {0}\n".format(self.verbosity)
return_str += "[RULES]\n"
for rule in self.rules:
return_str += " {0}: {1}\n".format(rule.id, rule.name)
for option_name, option_value in rule.options.items():
return_str += " {0}={1}\n".format(option_name, option_value.value)
return return_str


GITLINT_CONFIG_TEMPLATE_SRC_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "files/gitlint")

Expand Down
11 changes: 11 additions & 0 deletions gitlint/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ def get_sample(filename=""):
sample = open(sample_path).read()
return sample

@staticmethod
def get_expected(filename="", variable_dict=None):
""" Utility method to read an 'expected' file and return it as a string. Optionally replace template variables
specified by variable_dict. """
expected_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "expected")
expected_path = os.path.join(expected_dir, filename)
expected = open(expected_path).read()
if variable_dict:
expected = expected.format(**variable_dict)
return expected

@staticmethod
def get_rule_rules_path():
return os.path.join(BaseTestCase.SAMPLES_DIR, "user_rules")
Expand Down
26 changes: 26 additions & 0 deletions gitlint/tests/expected/debug_output1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[GENERAL]
config path: {config_path}
extra path: {extra_path}
ignore merge commits: False
verbosity: 1
[RULES]
T1: title-max-length
line-length=20
T6: title-leading-whitespace
T3: title-trailing-punctuation
T4: title-hard-tab
T5: title-must-not-contain-word
words=['WIP']
T7: title-match-regex
regex=.*
B1: body-max-line-length
line-length=30
B5: body-min-length
min-length=20
B6: body-is-missing
ignore-merge-commits=True
B3: body-hard-tab
B4: body-first-line-empty
B7: body-changed-file-mention
files=[]

2 changes: 1 addition & 1 deletion gitlint/tests/samples/config/gitlintconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ignore=title-trailing-whitespace,B2
verbosity = 1
ignore-merge-commits = false
debug = true
debug = false

[title-max-length]
line-length=20
Expand Down
39 changes: 38 additions & 1 deletion gitlint/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@ def test_input_stream(self):
result = self.cli.invoke(cli.cli, input='WIP: title \n')
self.assertEqual(stderr.getvalue(), expected_output)
self.assertEqual(result.exit_code, 3)
self.assertEqual(result.output, "")

def test_silent_mode(self):
with patch('gitlint.display.stderr', new=StringIO()) as stderr:
result = self.cli.invoke(cli.cli, ["--silent"], input='WIP: title \n')
self.assertEqual(stderr.getvalue(), "")
self.assertEqual(result.exit_code, 3)
self.assertEqual(result.output, "")

def test_verbosity(self):
# We only test -v and -vv, more testing is really not required here
# -v
with patch('gitlint.display.stderr', new=StringIO()) as stderr:
result = self.cli.invoke(cli.cli, ["-v"], input='WIP: title \n')
self.assertEqual(stderr.getvalue(), "1: T2\n1: T5\n3: B6\n")
self.assertEqual(result.exit_code, 3)
self.assertEqual(result.output, "")

# -vv
expected_output = "1: T2 Title has trailing whitespace\n" + \
"1: T5 Title contains the word 'WIP' (case-insensitive)\n" + \
"3: B6 Body message is missing\n"

with patch('gitlint.display.stderr', new=StringIO()) as stderr:
result = self.cli.invoke(cli.cli, ["-vv"], input='WIP: title \n')
self.assertEqual(stderr.getvalue(), expected_output)
self.assertEqual(result.exit_code, 3)
self.assertEqual(result.output, "")

# -vvvv: not supported -> should print a config error
with patch('gitlint.display.stderr', new=StringIO()) as stderr:
result = self.cli.invoke(cli.cli, ["-vvvv"], input='WIP: title \n')
self.assertEqual(stderr.getvalue(), "")
self.assertEqual(result.exit_code, CLITests.CONFIG_ERROR_CODE)
self.assertEqual(result.output, "Config Error: Option 'verbosity' must be set between 0 and 3\n")

@patch('gitlint.cli.GitLinter')
def test_config_file(self, _git_linter):
Expand All @@ -71,7 +106,9 @@ def test_config_file_debug(self, _git_linter):
config_path = self.get_sample_path("config/gitlintconfig")
result = self.cli.invoke(cli.cli, ["--config", config_path, "--debug"])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, "Using config from {0}\n".format(config_path))
expected = self.get_expected('debug_output1', {'config_path': config_path,
'extra_path': os.path.abspath(os.getcwd())})
self.assertEqual(result.output, expected)

def test_config_file_negative(self):
# Directory as config file
Expand Down
2 changes: 1 addition & 1 deletion gitlint/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_load_config_from_file(self):

# Do some assertions on the config
self.assertEqual(config.verbosity, 1)
self.assertTrue(config.debug)
self.assertFalse(config.debug)
self.assertFalse(config.ignore_merge_commits)

# ignored rules
Expand Down
11 changes: 11 additions & 0 deletions qa/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ def _create_simple_commit(self, message, out=None, ok_code=None, env=None):
def get_sample_path(filename=""):
samples_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "samples")
return os.path.join(samples_dir, filename)

@staticmethod
def get_expected(filename="", variable_dict=None):
""" Utility method to read an 'expected' file and return it as a string. Optionally replace template variables
specified by variable_dict. """
expected_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "expected")
expected_path = os.path.join(expected_dir, filename)
expected = open(expected_path).read()
if variable_dict:
expected = expected.format(**variable_dict)
return expected
30 changes: 30 additions & 0 deletions qa/expected/debug_output1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[GENERAL]
config path: /vagrant/qa/samples/config/gitlintconfig
extra path: {extra_path}
ignore merge commits: True
verbosity: 2
[RULES]
T1: title-max-length
line-length=20
T2: title-trailing-whitespace
T6: title-leading-whitespace
T4: title-hard-tab
T5: title-must-not-contain-word
words=['WIP']
T7: title-match-regex
regex=.*
B1: body-max-line-length
line-length=30
B5: body-min-length
min-length=20
B6: body-is-missing
ignore-merge-commits=True
B3: body-hard-tab
B4: body-first-line-empty
B7: body-changed-file-mention
files=[]

1: T1 Title exceeds max length (42>20)
1: T5 Title contains the word 'WIP' (case-insensitive)
2: B4 Second line is not empty
3: B1 Line exceeds max length (48>30)
7 changes: 2 additions & 5 deletions qa/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ def test_config_from_file_debug(self):
config_path = self.get_sample_path("config/gitlintconfig")
output = gitlint("--config", config_path, "--debug", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[4])

expected = "Using config from %s\n" % config_path + \
"1: T1 Title exceeds max length (42>20)\n" + \
"1: T5 Title contains the word 'WIP' (case-insensitive)\n" + \
"2: B4 Second line is not empty\n" + \
"3: B1 Line exceeds max length (48>30)\n"
expected = self.get_expected('debug_output1', {'config_path': config_path,
'extra_path': self.tmp_git_repo})

# TODO(jroovers): test for trailing whitespace -> git automatically strips whitespace when passing
# taking a commit message via 'git commit -m'
Expand Down
17 changes: 15 additions & 2 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,19 @@ run_integration_tests(){
}

run_git_check(){
echo "Running gitlint..."
echo -e "Running gitlint...${RED}"
gitlint
local exit_code=$?
echo -ne "$NO_COLOR"
return $exit_code
}

run_lint_check(){
echo "Running pylint..."
echo -e "Running pylint...${RED}"
pylint gitlint qa --rcfile=".pylintrc" -r n
local exit_code=$?
echo -ne "$NO_COLOR"
return $exit_code
}

run_stats(){
Expand All @@ -128,14 +134,21 @@ clean(){
}

run_all(){
local exit_code=0
subtitle "# UNIT TESTS #"
run_unit_tests
exit_code=$((exit_code + $?))
subtitle "# INTEGRATION TESTS #"
run_integration_tests
exit_code=$((exit_code + $?))
subtitle "# STYLE CHECKS #"
run_pep8_check
exit_code=$((exit_code + $?))
run_lint_check
exit_code=$((exit_code + $?))
run_git_check
exit_code=$((exit_code + $?))
return $exit_code
}

uninstall_virtualenv(){
Expand Down

0 comments on commit 9235aeb

Please sign in to comment.