Repository: oppia/oppia

Issue:
Pre-commit linter should fail hard when there are no files to be checked.
**Describe the bug**
We had a gitter conversation where @ujjwalgulecha ran `python scripts/pre_commit_linter.py --verbose`. This command doesn't check anything, but a lot of output is printed so it looks as though lint checks have run.

Instead the lint script should just fail noisily in this case and say that nothing was checked.

**Observed behavior**
A lot of random output is printed.

**Expected behavior**
The lint script should fail noisily and say that nothing was checked.


Pre-commit linter should fail hard when there are no files to be checked.
**Describe the bug**
We had a gitter conversation where @ujjwalgulecha ran `python scripts/pre_commit_linter.py --verbose`. This command doesn't check anything, but a lot of output is printed so it looks as though lint checks have run.

Instead the lint script should just fail noisily in this case and say that nothing was checked.

**Observed behavior**
A lot of random output is printed.

**Expected behavior**
The lint script should fail noisily and say that nothing was checked.




Discussion:
Hi @seanlip, this is the output produced:

```
python scripts/pre_commit_linter.py --verbose                                                           (question-player-backend-service)
Starting linter...
Starting CSS, Javascript and Python Linting
----------------------------------------


Starting js component name and count check
----------------------------------------
Starting directive scope check
----------------------------------------
Starting sorted dependencies check
----------------------------------------

SUCCESS  Sorted dependencies check passed
----------------------------------------
Starting controller dependency line break check
----------------------------------------
Starting HTML directive name check
----------------------------------------
Starting import-order checks
----------------------------------------
Starting docstring checks
----------------------------------------
Starting comment checks
----------------------------------------
Starting HTML tag and attribute check
----------------------------------------
Starting HTML linter...
----------------------------------------

Starting Pattern Checks
----------------------------------------
Starting copyright notice check
----------------------------------------
Summary of Errors:
----------------------------------------


SUCCESS  Js component name and count check passed

SUCCESS  Directive scope check passed

SUCCESS  Controller dependency line break check passed


There are no files to be checked.

SUCCESS   Import order checks passed

SUCCESS   Docstring check passed

SUCCESS   Comments check passed
SUCCESS  HTML tag and attribute check passed

----------------------------------------

SUCCESS   HTML linting passed
HTML linting finished.


There are no files to be checked.
SUCCESS  Copyright notice check passed


---------------------------
All Checks Passed.
---------------------------
There are no JavaScript files to lint.
There are no CSS files to lint.
There are no Python files to lint.
There are no CSS files to lint.
``` 

The output clearly states that no files have been checked and therefore, all checks pass. Why do we want the script to fail noisily when there are no files to be checked? 
There are cases when changes are made to markdown files for which there are no lint checks. All checks should pass in that case.
Please let me know if I am not getting this. Thanks!
Oh, that's a good point (re markdown files). Thanks for bringing that up!

OK, I'm fine with not failing noisily. But can we omit all the other "SUCCESS: passed" lines if the reality is that no checks were run? SUCCESS is a bit misleading here. It implies lint checks have run and succeeded whereas in reality no file has actually been linted.
Hi @seanlip, @apb7 we can add a return statement before the check runs if there are no files found for that check.
For example:
```No files found for Docstring check``` etc. for other checks.
Any suggestions here?
I'll defer to @apb7 to advise on the mechanics. As long as we make it really clear/obvious that no files have been checked, I'm fine with whatever solution the dev workflow team comes up with.
@YashJipkate, @lilithxxx any suggestions here? Thanks.
> Hi @seanlip, @apb7 we can add a return statement before the check runs if there are no files found for that check.
> For example:
> `No files found for Docstring check` etc. for other checks.
> Any suggestions here?

Hi @apb7, suggestions here?
Hi @apb7, ping.
@lilithxxx and @YashJipkate think this sounds OK, so @anubhavsinha98, please go ahead. Thanks! 
@anubhavsinha98 how is this going?
Sorry for the delay, removed my assignment. 
Can I work on this?
@sauravpratihar, Yeah, you can work on this issue! 
Hi @seanlip, this is the output produced:

```
python scripts/pre_commit_linter.py --verbose                                                           (question-player-backend-service)
Starting linter...
Starting CSS, Javascript and Python Linting
----------------------------------------


Starting js component name and count check
----------------------------------------
Starting directive scope check
----------------------------------------
Starting sorted dependencies check
----------------------------------------

SUCCESS  Sorted dependencies check passed
----------------------------------------
Starting controller dependency line break check
----------------------------------------
Starting HTML directive name check
----------------------------------------
Starting import-order checks
----------------------------------------
Starting docstring checks
----------------------------------------
Starting comment checks
----------------------------------------
Starting HTML tag and attribute check
----------------------------------------
Starting HTML linter...
----------------------------------------

Starting Pattern Checks
----------------------------------------
Starting copyright notice check
----------------------------------------
Summary of Errors:
----------------------------------------


SUCCESS  Js component name and count check passed

SUCCESS  Directive scope check passed

SUCCESS  Controller dependency line break check passed


There are no files to be checked.

SUCCESS   Import order checks passed

SUCCESS   Docstring check passed

SUCCESS   Comments check passed
SUCCESS  HTML tag and attribute check passed

----------------------------------------

SUCCESS   HTML linting passed
HTML linting finished.


There are no files to be checked.
SUCCESS  Copyright notice check passed


---------------------------
All Checks Passed.
---------------------------
There are no JavaScript files to lint.
There are no CSS files to lint.
There are no Python files to lint.
There are no CSS files to lint.
``` 

The output clearly states that no files have been checked and therefore, all checks pass. Why do we want the script to fail noisily when there are no files to be checked? 
There are cases when changes are made to markdown files for which there are no lint checks. All checks should pass in that case.
Please let me know if I am not getting this. Thanks!
Oh, that's a good point (re markdown files). Thanks for bringing that up!

OK, I'm fine with not failing noisily. But can we omit all the other "SUCCESS: passed" lines if the reality is that no checks were run? SUCCESS is a bit misleading here. It implies lint checks have run and succeeded whereas in reality no file has actually been linted.
Hi @seanlip, @apb7 we can add a return statement before the check runs if there are no files found for that check.
For example:
```No files found for Docstring check``` etc. for other checks.
Any suggestions here?
I'll defer to @apb7 to advise on the mechanics. As long as we make it really clear/obvious that no files have been checked, I'm fine with whatever solution the dev workflow team comes up with.
@YashJipkate, @lilithxxx any suggestions here? Thanks.
> Hi @seanlip, @apb7 we can add a return statement before the check runs if there are no files found for that check.
> For example:
> `No files found for Docstring check` etc. for other checks.
> Any suggestions here?

Hi @apb7, suggestions here?
Hi @apb7, ping.
@lilithxxx and @YashJipkate think this sounds OK, so @anubhavsinha98, please go ahead. Thanks! 
@anubhavsinha98 how is this going?
Sorry for the delay, removed my assignment. 
Can I work on this?
@sauravpratihar, Yeah, you can work on this issue! 

Resolving patch:
diff --git a/scripts/pre_commit_linter.py b/scripts/pre_commit_linter.py
--- a/scripts/pre_commit_linter.py
+++ b/scripts/pre_commit_linter.py
@@ -2572,6 +2572,13 @@ def main():
     # will be made True, which will represent verbose mode.
     verbose_mode_enabled = bool(parsed_args.verbose)
     all_filepaths = _get_all_filepaths(parsed_args.path, parsed_args.files)
+
+    if len(all_filepaths) == 0:
+        print '---------------------------'
+        print 'No files to check.'
+        print '---------------------------'
+        sys.exit(1)
+
     lint_checks_manager = LintChecksManager(all_filepaths, verbose_mode_enabled)
     all_messages = lint_checks_manager.perform_all_lint_checks()
     _print_complete_summary_of_errors()


Test patch: