Skip to content

Commit d3628a7

Browse files
committed
Create basic version
1 parent 0fa8487 commit d3628a7

File tree

21 files changed

+205
-0
lines changed

21 files changed

+205
-0
lines changed

.travis.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: bash
2+
3+
sudo: false
4+
5+
before_install:
6+
- sudo apt-get -qq update
7+
- sudo apt-get install -y realpath shellcheck
8+
9+
script:
10+
- tests/tests.sh
11+
- shellcheck check-trailing-whitespaces
12+
- shellcheck tests/tests.sh

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright (c) 2017 Fabien Potencier
2+
Dariusz Rumiński
3+
Julien Falque
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is furnished
10+
to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Trailing whitespaces checker
2+
============================
3+
4+
This little bash script recursively iterates all files in the current directory and reports those that have lines with trailing whitespaces. In case a file is found, the script will exit with status code `3`, or `0` otherwise.
5+
6+
Installation
7+
------------
8+
9+
Copy the [check-trailing-whitespaces file](./check-trailing-whitespaces) in a directory that is part of your `PATH` so it can be executed globally.
10+
11+
Usage
12+
-----
13+
14+
Just run the script:
15+
16+
`$ check-trailing-whitespace`

check-trailing-whitespaces

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
files_with_trailing_spaces=$(find . -type f -exec egrep -nH "\\s$" {} \; | sort -fh)
4+
5+
if [[ ${files_with_trailing_spaces} ]]
6+
then
7+
echo -e "\\e[97;41mTrailing whitespaces detected:\\e[0m"
8+
e=$(printf '\033')
9+
echo "${files_with_trailing_spaces}" | sed -E "s/^\\.\\/([^:]+):([0-9]+):(.*[^\t ])?([\t ]+)$/${e}[0;31m - in ${e}[0;33m\\1${e}[0;31m at line ${e}[0;33m\\2\\n ${e}[0;31m>${e}[0m \\3${e}[41;1m\\4${e}[0m/"
10+
11+
exit 3
12+
fi
13+
14+
echo -e "\\e[0;32mNo trailing whitespaces detected.\\e[0m"

tests/no_result/files/file.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$foo = 'foo';

tests/no_result/test

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--STATUS-CODE--
2+
0
3+
--OUTPUT--
4+
\e[0;32mNo trailing whitespaces detected.\e[0m

tests/one_result/files/file1.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$foo = '';

tests/one_result/files/file2.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$foo = '
4+
line with spaces
5+
';

tests/one_result/test

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--STATUS-CODE--
2+
3
3+
--OUTPUT--
4+
\e[97;41mTrailing whitespaces detected:\e[0m
5+
\e[0;31m - in \e[0;33mfile2.php\e[0;31m at line \e[0;33m4
6+
\e[0;31m>\e[0m line with spaces\e[41;1m \e[0m
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$foo = '';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$foo = '
4+
line with tabs
5+
';

tests/one_result_with_tab/test

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
--STATUS-CODE--
2+
3
3+
--OUTPUT--
4+
\e[97;41mTrailing whitespaces detected:\e[0m
5+
\e[0;31m - in \e[0;33mfile2.php\e[0;31m at line \e[0;33m4
6+
\e[0;31m>\e[0m line with tabs\e[41;1m \e[0m
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$foo = '
4+
line with spaces
5+
';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$foo = '
4+
line with spaces
5+
';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$foo = '
4+
line with spaces
5+
';

tests/subdirectories/files/file.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$foo = '
4+
line with spaces
5+
';

tests/subdirectories/test

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--STATUS-CODE--
2+
3
3+
--OUTPUT--
4+
\e[97;41mTrailing whitespaces detected:\e[0m
5+
\e[0;31m - in \e[0;33mdirectory/file.php\e[0;31m at line \e[0;33m4
6+
\e[0;31m>\e[0m line with spaces\e[41;1m \e[0m
7+
\e[0;31m - in \e[0;33mdirectory with spaces/file.php\e[0;31m at line \e[0;33m4
8+
\e[0;31m>\e[0m line with spaces\e[41;1m \e[0m
9+
\e[0;31m - in \e[0;33mdirectory with spaces/subdirectory/file.php\e[0;31m at line \e[0;33m4
10+
\e[0;31m>\e[0m line with spaces\e[41;1m \e[0m
11+
\e[0;31m - in \e[0;33mfile.php\e[0;31m at line \e[0;33m4
12+
\e[0;31m>\e[0m line with spaces\e[41;1m \e[0m

tests/tests.sh

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
run_test() {
4+
echo -ne "Running test \\e[33m\"$(echo "$1" | sed -E "s/.*\\/(.*)\\//\\1/")\"\\e[0m... "
5+
6+
expected_status_code="$(perl -p -e 'BEGIN{undef $/;} s/^--STATUS-CODE--\n(\d+)\n.*$/$1/s' "${1}test")"
7+
expected_output="$(echo -e "$(perl -p -e 'BEGIN{undef $/;} s/^.*--OUTPUT--\n(.*)\n$/$1/s' "${1}test")")"
8+
9+
cd "${1}files" || return 3
10+
output=$(../../../check-trailing-whitespaces)
11+
status_code="$?"
12+
cd ../.. || return 3
13+
14+
if [ "${status_code}" != "${expected_status_code}" ]
15+
then
16+
echo -e "\\e[31mFailing asserting that status code ${status_code} matches expected status code ${expected_status_code}:\\e[0m"
17+
18+
return 3
19+
fi
20+
21+
if [ "${output}" != "${expected_output}" ]
22+
then
23+
echo -e "\\e[31mFailing asserting that output matches expected output:\\e[0m"
24+
diff -u <(echo "${expected_output}" ) <(echo "${output}")
25+
26+
return 3
27+
fi
28+
29+
echo -e "\\e[32mOK\\e[0m"
30+
31+
return 0
32+
}
33+
34+
i=0
35+
failures=0
36+
for test_case in "$(dirname "$(realpath "$0")")"/*/
37+
do
38+
if [ "$i" -gt 0 ]
39+
then
40+
echo
41+
fi
42+
43+
44+
if ! run_test "${test_case}"
45+
then
46+
((++failures))
47+
fi
48+
49+
((++i))
50+
done
51+
52+
if [ "${failures}" -ne 0 ]
53+
then
54+
exit 3
55+
fi
56+
57+
exit 0

tests/two_results/files/file1.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$foo = '
4+
5+
';

tests/two_results/files/file2.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$foo = '
4+
line with spaces
5+
';

tests/two_results/test

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--STATUS-CODE--
2+
3
3+
--OUTPUT--
4+
\e[97;41mTrailing whitespaces detected:\e[0m
5+
\e[0;31m - in \e[0;33mfile1.php\e[0;31m at line \e[0;33m4
6+
\e[0;31m>\e[0m \e[41;1m \e[0m
7+
\e[0;31m - in \e[0;33mfile2.php\e[0;31m at line \e[0;33m4
8+
\e[0;31m>\e[0m line with spaces\e[41;1m \e[0m

0 commit comments

Comments
 (0)