mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Add lint for backticks in shell scripts
The "$(some_command arg1 arg2)" form is preferred to the `some_command arg1 arg2` form because it nests unambiguously. Add a lint for this to tidy.
This commit is contained in:
parent
4bc629b369
commit
79ef9b4efc
5 changed files with 24 additions and 14 deletions
|
@ -27,9 +27,9 @@ if [ ! -d /usr/include ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$(uname)" == "Linux" ]; then
|
if [ "$(uname)" == "Linux" ]; then
|
||||||
LIBCLANG_PATH=/usr/lib/llvm-3.8/lib;
|
LIBCLANG_PATH=/usr/lib/llvm-3.8/lib
|
||||||
else
|
else
|
||||||
LIBCLANG_PATH=`brew --prefix llvm38`/lib/llvm-3.8/lib;
|
LIBCLANG_PATH="$(brew --prefix llvm38)/lib/llvm-3.8/lib"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
./regen.py --target all "$@"
|
./regen.py --target all "$@"
|
||||||
|
|
|
@ -13,9 +13,9 @@ cd "$(dirname $0)"
|
||||||
|
|
||||||
# Setup and build bindgen.
|
# Setup and build bindgen.
|
||||||
if [ "$(uname)" == "Linux" ]; then
|
if [ "$(uname)" == "Linux" ]; then
|
||||||
export LIBCLANG_PATH=/usr/lib/llvm-3.8/lib;
|
export LIBCLANG_PATH=/usr/lib/llvm-3.8/lib
|
||||||
else
|
else
|
||||||
export LIBCLANG_PATH=`brew --prefix llvm38`/lib/llvm-3.8/lib;
|
export LIBCLANG_PATH="$(brew --prefix llvm38)/lib/llvm-3.8/lib"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Make sure we have llvm38.
|
# Make sure we have llvm38.
|
||||||
|
|
|
@ -318,19 +318,23 @@ def check_shell(file_name, lines):
|
||||||
shebang = "#!/usr/bin/env bash"
|
shebang = "#!/usr/bin/env bash"
|
||||||
required_options = {"set -o errexit", "set -o nounset", "set -o pipefail"}
|
required_options = {"set -o errexit", "set -o nounset", "set -o pipefail"}
|
||||||
|
|
||||||
|
did_shebang_check = False
|
||||||
|
|
||||||
if len(lines) == 0:
|
if len(lines) == 0:
|
||||||
yield (0, 'script is an empty file')
|
yield (0, 'script is an empty file')
|
||||||
else:
|
return
|
||||||
if lines[0].rstrip() != shebang:
|
|
||||||
yield (1, 'script does not have shebang "{}"'.format(shebang))
|
|
||||||
|
|
||||||
for idx in range(1, len(lines)):
|
if lines[0].rstrip() != shebang:
|
||||||
stripped = lines[idx].rstrip()
|
yield (1, 'script does not have shebang "{}"'.format(shebang))
|
||||||
|
|
||||||
# Comments or blank lines are ignored. (Trailing whitespace is caught with a separate linter.)
|
for idx in range(1, len(lines)):
|
||||||
if lines[idx].startswith("#") or stripped == "":
|
stripped = lines[idx].rstrip()
|
||||||
continue
|
# Comments or blank lines are ignored. (Trailing whitespace is caught with a separate linter.)
|
||||||
elif stripped in required_options:
|
if lines[idx].startswith("#") or stripped == "":
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not did_shebang_check:
|
||||||
|
if stripped in required_options:
|
||||||
required_options.remove(stripped)
|
required_options.remove(stripped)
|
||||||
else:
|
else:
|
||||||
# The first non-comment, non-whitespace, non-option line is the first "real" line of the script.
|
# The first non-comment, non-whitespace, non-option line is the first "real" line of the script.
|
||||||
|
@ -338,7 +342,10 @@ def check_shell(file_name, lines):
|
||||||
if len(required_options) != 0:
|
if len(required_options) != 0:
|
||||||
formatted = ['"{}"'.format(opt) for opt in required_options]
|
formatted = ['"{}"'.format(opt) for opt in required_options]
|
||||||
yield (idx + 1, "script is missing options {}".format(", ".join(formatted)))
|
yield (idx + 1, "script is missing options {}".format(", ".join(formatted)))
|
||||||
break
|
did_shebang_check = True
|
||||||
|
|
||||||
|
if "`" in stripped:
|
||||||
|
yield (idx + 1, "script should not use backticks for command substitution")
|
||||||
|
|
||||||
|
|
||||||
def check_rust(file_name, lines):
|
def check_rust(file_name, lines):
|
||||||
|
|
|
@ -4,4 +4,6 @@
|
||||||
|
|
||||||
set -o nounset
|
set -o nounset
|
||||||
|
|
||||||
|
# Talking about some `concept in backticks` # shouldn't trigger
|
||||||
echo "hello world"
|
echo "hello world"
|
||||||
|
some_var=`echo "command substitution"`
|
||||||
|
|
|
@ -52,6 +52,7 @@ class CheckTidiness(unittest.TestCase):
|
||||||
errors = tidy.collect_errors_for_files(iterFile('shell_tidy.sh'), [], [tidy.check_shell], print_text=False)
|
errors = tidy.collect_errors_for_files(iterFile('shell_tidy.sh'), [], [tidy.check_shell], print_text=False)
|
||||||
self.assertEqual('script does not have shebang "#!/usr/bin/env bash"', errors.next()[2])
|
self.assertEqual('script does not have shebang "#!/usr/bin/env bash"', errors.next()[2])
|
||||||
self.assertEqual('script is missing options "set -o errexit", "set -o pipefail"', errors.next()[2])
|
self.assertEqual('script is missing options "set -o errexit", "set -o pipefail"', errors.next()[2])
|
||||||
|
self.assertEqual('script should not use backticks for command substitution', errors.next()[2])
|
||||||
self.assertNoMoreErrors(errors)
|
self.assertNoMoreErrors(errors)
|
||||||
|
|
||||||
def test_rust(self):
|
def test_rust(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue