Skip to content

Commit

Permalink
Call ka-lint instead of always using khan-linter's version.
Browse files Browse the repository at this point in the history
Summary:
hook_lib was hard-coded to use khan-linter's ka-lint (which resolves
to runlint.py) when doing pre-push linting.  But we want it to use
webapp's ka-lint when linting in webapp.

We do this by having the git hook call out the "ka-lint" binary that
is found on the user's path.  For now, that will only be the one in
khan-linter, so current behavior should be unchanged.  But eventually
I'll change `ka-lint` on the path to be a multi-plexer that calls out
to webapp's version for files in webapp, and khan-linter's version for
everything else.  When that happens the git hook will start using the
correct ka-lint automatically!

Test Plan:
I temporarily changed this commit to say:
    stdin = subprocess.PIPE)
and ran
   % python -c 'import hook_lib; print hook_lib.lint_files(["hook_lib.py"])'
   hook_lib.py:31:31: E251 unexpected spaces around keyword / parameter equals
   hook_lib.py:31:33: E251 unexpected spaces around keyword / parameter equals
   2
So it correctly emitted lint errors to stdout and return a non-zero rc
due to the lint errors.

Then I ran
   % env PATH=$HOME/khan/webapp/testing:$PATH python -c 'import hook_lib; print hook_lib.lint_files(["hook_lib.py"])'
   /home/csilvers/khan/webapp/testing/ka-lint must be run from some directory in webapp-root
   1
showing that a) it properly could execute webapp's ka-lint when need
be, and b) properly returns an rc of 1 on lint framework errors.

Then undid my temporary change and ran:
   % python -c 'import hook_lib; print hook_lib.lint_files(["hook_lib.py"])'
   0
so everything works properly when properly linting.

Reviewers: benkraft, davidbraley

Reviewed By: benkraft, davidbraley

Subscribers: kevinb

Differential Revision: https://phabricator.khanacademy.org/D63491
  • Loading branch information
csilvers committed May 28, 2020
1 parent b1cf8a2 commit e03e3ee
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions hook_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,30 @@
"""

import re
import subprocess
import sys

import six

import runlint


def lint_files(files_to_lint):
"""Given a list of filenames in the commit, lint them all.
Emits errors it sees to stderr.
We use `ka_lint` to lint them all, which in most repos will call
runlint.py (in this directory) but in other repos -- notably webapp
-- will do its own thing. In all cases, ka_lint will emit lint
errors to stdout, lint-framework errors to stderr, and have an rc of
0 if linting was successful or 1 if there were lint or framework
errors.
Returns the number of lint errors seen.
This helper also returns 0 if linting was successful or 1 otherwise.
"""
(lint_errors, framework_errors) = runlint.main(files_to_lint,
blacklist='yes')
return lint_errors + framework_errors
# We could pass in the files-to-lint on the commandline, but using
# --stdin means we don't need to worry about how many files there are.
p = subprocess.Popen(['ka-lint', '--stdin', '--blacklist=yes'],
stdin=subprocess.PIPE)
p.communicate(input='\n'.join(files_to_lint))
return p.wait()


def lint_commit_message(commit_message):
Expand Down

0 comments on commit e03e3ee

Please sign in to comment.