Skip to content

Commit

Permalink
Merge branch 'bash-only'
Browse files Browse the repository at this point in the history
Minor cleanup
Fixes #3
  • Loading branch information
pocc committed Jul 22, 2019
2 parents 19d0bd6 + 2e37e73 commit d43e9c1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
__pycache__/
venv/
.idea/
.plist
.vscode/
*.plist
*cache/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ in the hook's args list.
## Testing

To run the tests and verify `clang-format`, `clang-tidy`, and `oclint` are
working as expected on your system, use `pytest tests/test.py --runslow`.
working as expected on your system, use `pytest tests/hooks_test.py --runslow`.

## Additional Resources

Expand Down
14 changes: 1 addition & 13 deletions hooks/clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,10 @@ function parse_args {
}


# If there are differences, this script will fail (and pre-commit should show this)
function diff_formatted {
if [[ " ${args[*]} " == *" -i "* ]]; then
# Here, diff compares what the file is before and after transform
diff "$filename" <(clang-format "${args[@]}"; cat "$filename")
else
# Otherwise clang-format should send output to stdout, and can be compared
diff "$filename" <(clang-format "${args[@]}")
fi
}


function clangformat_main {
check_installed "$CMD"
parse_args
diff_formatted
diff_formatted "${args[@]}" "$filename"
}

clangformat_main
20 changes: 19 additions & 1 deletion hooks/utils
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function check_installed {
function assert_version {
actual_version="$1"
expected_version="$2"
if [ "$actual_version" != "$expected_version" ]; then
if [[ "$actual_version" != "$expected_version"* ]]; then
echo -e "ERR: Expected version $expected_version, but system version is $actual_version" 1>&2
echo -e "Edit your pre-commit config or use a different version of $CMD" 1>&2
exit 1
Expand All @@ -30,6 +30,8 @@ function assert_version {
# Where 0 is the file pre-commit sends to the utility
# See https://github.com/pre-commit/pre-commit/issues/1000
function parse_ddash_args {
# Quoting $1 interferes with array creation
# shellcheck disable=SC2206
args=($1)
last_idx="$((${#args[@]}-1))"
caller_proc=$(ps -o comm= $PPID)
Expand Down Expand Up @@ -60,3 +62,19 @@ function parse_ddash_args {
echo "ERR: No file arg found. args =" "${args[@]}"
fi
}


# If there are differences, this script will fail (and pre-commit should show this)
function diff_formatted {
# Quoting $1 interferes with array creation
# shellcheck disable=SC2206
args=($1)
filename="$2"
if [[ " ${args[*]} " == *" -i "* ]]; then
# Here, diff compares what the file is before and after transform
diff "$filename" <(clang-format "${args[@]}"; cat "$filename")
else
# Otherwise clang-format should send output to stdout, and can be compared
diff "$filename" <(clang-format "${args[@]}")
fi
}
48 changes: 42 additions & 6 deletions tests/hooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_clang_format_version_err(self):
output = sp.check_output(["clang-format", "--version"], text=True)
cf_version = re.search(r"version ([\S]+)", output).group(1)

clang_version_err = r"""ERR: Expected version 0, but system version is {}
clang_version_err = r"""ERR: Expected version 0.0.0, but system version is {}
Edit your pre-commit config or use a different version of clang-format
""".format(
cf_version
Expand All @@ -64,7 +64,7 @@ def test_clang_format_version_err(self):
filelist=self.okfiles,
expected_output=clang_version_err,
expected_retcode=1,
version="0",
version="0.0.0",
)

@staticmethod
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_clang_tidy_version_err(self):
output = sp.check_output(["clang-tidy", "--version"], text=True)
ct_version = re.search(r"version ([\S]+)", output).group(1)

clang_version_err = r"""ERR: Expected version 0, but system version is {}
clang_version_err = r"""ERR: Expected version 0.0.0, but system version is {}
Edit your pre-commit config or use a different version of clang-tidy
""".format(
ct_version
Expand All @@ -115,7 +115,7 @@ def test_clang_tidy_version_err(self):
filelist=self.okfiles,
expected_output=clang_version_err,
expected_retcode=1,
version="0",
version="0.0.0",
)

def run_clang_tidy(
Expand Down Expand Up @@ -175,7 +175,7 @@ def test_oclint_version_err(self):
output = sp.check_output(["oclint", "--version"], text=True)
oclint_ver = re.search(r"OCLint version ([\S]+)\.", output).group(1)

clang_version_err = r"""ERR: Expected version 0, but system version is {}
clang_version_err = r"""ERR: Expected version 0.0.0, but system version is {}
Edit your pre-commit config or use a different version of oclint
""".format(
oclint_ver
Expand All @@ -184,7 +184,7 @@ def test_oclint_version_err(self):
filelist=self.okfiles,
expected_output=clang_version_err,
expected_retcode=1,
version="0",
version="0.0.0",
)

def run_oclint(
Expand All @@ -210,6 +210,42 @@ def run_oclint(
assert actual == expected
assert retcode == expected_retcode

@staticmethod
def test_sticky_version_minor():
"""Verify that 6.0 matches minor versions like 6.0.1."""
cmds = ["bash", "-c", ". ./hooks/utils; assert_version '{}' '6.0'"]
cmds[2] = cmds[2].format("6.0.1")
child = sp.Popen(cmds, stdout=sp.PIPE, stderr=sp.PIPE)
child_out, child_err = child.communicate()
assert child_out == b""
assert child_err == b""
assert child.returncode == 0

@staticmethod
def test_sticky_version_extended():
cmds = ["bash", "-c", ". ./hooks/utils; assert_version '{}' '6.0'"]
cmds[2] = cmds[2].format("6.0.0-1ubuntu2")
child = sp.Popen(cmds, stdout=sp.PIPE, stderr=sp.PIPE)
child_out, child_err = child.communicate()
assert child_out == b""
assert child_err == b""
assert child.returncode == 0

@staticmethod
def test_sticky_version_err():
cmds = ["bash", "-c", ". ./hooks/utils; assert_version '{}' '6.0'"]
cmds[2] = cmds[2].format("6.1.0")
child = sp.Popen(cmds, stdout=sp.PIPE, stderr=sp.PIPE)
child_out, child_err = child.communicate()
print(child_err)
assert child_out == b""
assert (
child_err
== b"ERR: Expected version 6.0, but system version is 6.1.0\n"
+ b"Edit your pre-commit config or use a different version of \n"
)
assert child.returncode == 1

@staticmethod
def get_all_output(cmds, filename):
"""Helper fn to get stderr and stdout from llvm command.
Expand Down

0 comments on commit d43e9c1

Please sign in to comment.