diff options
| author | Teddy Wing | 2015-09-27 13:20:55 -0400 |
|---|---|---|
| committer | Teddy Wing | 2015-09-27 13:20:55 -0400 |
| commit | 2a88fb92f4c1fbb3533e589d5c9f4b5c17595d3f (patch) | |
| tree | c8b4ae5f5e20ae0a2e543d98662bc3bf90997488 | |
| parent | 50631f85e73eda9d6738f5993ff5cf26198f8697 (diff) | |
| download | git-hook-pre-commit-python-javascript-syntax-linter-2a88fb92f4c1fbb3533e589d5c9f4b5c17595d3f.tar.bz2 | |
Add JavaScript linting and tests
* Add sample malformed JavaScript files to lint on
* In our tests, copy the `node_modules` directory to make `jshint`
available, then validate that we're correctly linting the staged JS
file
* In our pre-commit hook, lint the staged JavaScript files. If either of
our linting commands fail then exit 1 to abort the commit.
| -rw-r--r-- | pre-commit | 13 | ||||
| -rw-r--r-- | t/150-javascript.t | 37 | ||||
| -rw-r--r-- | t/test.js | 3 | ||||
| -rw-r--r-- | t/uncommitted.js | 1 |
4 files changed, 54 insertions, 0 deletions
@@ -17,7 +17,20 @@ fi # LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 files=$(git diff --cached --name-only --diff-filter=AMR -z $against) python_files=$(echo $files | grep .*\.py) +javascript_files=$(echo $files | grep .*\.js) +flake_exit=0 +jshint_exit=0 if [ -n "$python_files" ]; then flake8 $python_files + flake_exit=$? +fi + +if [ -n "$javascript_files" ]; then + ./node_modules/.bin/jshint $javascript_files + jshint_exit=$? +fi + +if [ !$flake_exit ] || [ !$jshint_exit ]; then + exit 1 fi diff --git a/t/150-javascript.t b/t/150-javascript.t new file mode 100644 index 0000000..208b497 --- /dev/null +++ b/t/150-javascript.t @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Test::More; + +chdir 't/git-repo/' or die $!; + +system('cp -R ../../node_modules .'); +ok !$?; + +system('cp ../*.js .'); +ok !$?; + +my $output = `ls -1 | grep .*\.js`; + +ok $output eq 'test.js +uncommitted.js +', 'Both test JavaScript files are present'; + +system('git add test.js'); +ok !$?; + +$output = `git commit 2>&1`; + +ok $output eq "test.js: line 1, col 21, Missing semicolon. +test.js: line 3, col 1, Bad line breaking before '&&'. +test.js: line 3, col 9, Expected an assignment or function call and instead saw an expression. + +3 errors +", 'jshint ran on committed JavaScript file'; + +$output = `git reset`; +ok !$?; + +done_testing; diff --git a/t/test.js b/t/test.js new file mode 100644 index 0000000..d864322 --- /dev/null +++ b/t/test.js @@ -0,0 +1,3 @@ +var no_semicolon = 0 +1 +&& 0 && 1; diff --git a/t/uncommitted.js b/t/uncommitted.js new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/t/uncommitted.js @@ -0,0 +1 @@ +test |
