Skip to content

Commit

Permalink
[ci] Check for trailing newlines and spaces
Browse files Browse the repository at this point in the history
This adds a short lint to ensure that all files have a single trailing
newline and no trailing whitespaces.
  • Loading branch information
driazati committed Oct 17, 2022
1 parent b1c8c90 commit ba2c2e2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ci/scripts/git_skip_ci_globs.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
# microTVM
"apps/microtvm/poetry.lock",
"apps/microtvm/pyproject.toml",
"tests/lint/*",
"tests/scripts/task_lint.sh",
]


Expand Down
51 changes: 51 additions & 0 deletions tests/lint/trailing_newlines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import fileinput
import os


def has_one_trailing_newline(filename: str) -> bool:
"""
Returns True if 'filename' has a single trailing newline
"""
with open(filename, "rb") as f:
start_bytes = len(f.read(2))
if start_bytes == 0:
# empty file
return True
elif start_bytes == 1:
# 1 byte file
return False
else:
# skip to the end
f.seek(-2, os.SEEK_END)
end_bytes = f.read(2)

# should be a non-newline followed by a newline
return end_bytes[0] != ord("\n") and end_bytes[1] == ord("\n")


if __name__ == "__main__":
exit_code = 1
for line in fileinput.input():
filename = line.rstrip()
if not has_one_trailing_newline(filename):
exit_code = 0
print(filename)
exit(exit_code)
39 changes: 39 additions & 0 deletions tests/lint/whitespace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

set -euo pipefail

status=0

if git --no-pager grep -Il '' -- . | ./tests/lint/trailing_newlines.py; then
echo "The above files are missing a trailing newline or have too many trailing newlines"
status=1
fi

if git --no-pager grep -In '[[:blank:]]$' -- .; then
echo "The above files have trailing spaces"
status=1
fi

if [ $status == "1" ]; then
echo "Found whitespace lint failures, 'pre-commit run --all-files' can auto-correct them"
exit 1
else
echo "Found no whitespace lint failures"
exit 0
fi
3 changes: 3 additions & 0 deletions tests/scripts/task_lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ function shard1 {
}

function shard2 {
echo "check whitespace..."
tests/lint/whitespace.sh

echo "Linting the Python code with pylint..."
tests/lint/pylint.sh

Expand Down

0 comments on commit ba2c2e2

Please sign in to comment.