Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test suite #295

Merged
merged 3 commits into from Apr 5, 2016
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add test for trailing whitespace

  • Loading branch information
aneeshusa committed Apr 4, 2016
commit 4a62b0510f2a058e215366eb894d47a70372b242
@@ -60,5 +60,5 @@ remove-old-build-logs:
cron.present:
- name: 'find /home/servo/buildbot/master/*/*.bz2 -mtime +5 -delete'
- user: root
- minute: 1
- minute: 1
- hour: 0
@@ -0,0 +1,42 @@
import itertools
import re

from .util import colon, color, display_path, paths, Failure, Success


def display_trailing_whitespace(whitespace):
# Show trailing whitespace with printing characters and in red
# To make it easy to see what needs to be removed
replaced = whitespace.replace(' ', '-').replace('\t', r'\t')
return color(31, replaced)


def display_failure(failure):
path, line_number, match = failure
line = match.group(1) + display_trailing_whitespace(match.group(2))
return display_path(path) + colon() + str(line_number) + colon() + line


def check_whitespace(path):
CHECK_REGEX = re.compile(r'(.*?)(\s+)$')
trailing_whitespace = []
with open(path, 'r', encoding='utf-8') as file_to_check:
try:
for line_number, line in enumerate(file_to_check):
line = line.rstrip('\r\n')
match = CHECK_REGEX.match(line)
if match is not None:
trailing_whitespace.append((path, line_number, match))
except UnicodeDecodeError:
pass # Not a text (UTF-8) file
return trailing_whitespace


def run():
failures = list(itertools.chain(*map(check_whitespace, paths())))

if len(failures) == 0:
return Success("No trailing whitespace found")
else:
output = '\n'.join([display_failure(failure) for failure in failures])
return Failure("Trailing whitespace found on files and lines:", output)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.