health-check: Add check for newline at ends of files

Fixes a couple of files that were missing them.

BUG=b:242605601
TEST=./tools/health-check --fix

Change-Id: I620d6a939cb824e014002152584aacfc5dfdf7e8
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3835648
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: Dennis Kempin <denniskempin@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
This commit is contained in:
Dennis Kempin 2022-08-17 19:59:27 +00:00 committed by crosvm LUCI
parent 38b9268cb8
commit 7a4d4d6b2a
14 changed files with 42 additions and 22 deletions

View file

@ -3,6 +3,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import re
import sys
from datetime import datetime
@ -10,15 +11,7 @@ from pathlib import Path
from typing import List
from impl.check_code_hygiene import has_crlf_line_endings
from impl.common import (
CROSVM_ROOT,
argh,
chdir,
cmd,
cwd_context,
parallel,
run_main,
)
from impl.common import CROSVM_ROOT, argh, chdir, cmd, cwd_context, parallel, run_main
from impl.health_check import Check, CheckContext, run_checks
@ -146,6 +139,24 @@ def check_infra_tests(context: CheckContext):
recipes("test run --py3-only").fg(quiet=True)
def check_file_ends_with_newline(context: CheckContext):
"Checks if files end with a newline."
for file_path in context.modified_files:
with file_path.open("rb") as file:
# Skip empty files
file.seek(0, os.SEEK_END)
if file.tell() == 0:
continue
# Check last byte of the file
file.seek(-1, os.SEEK_END)
file_end = file.read(1)
if file_end.decode("utf-8") != "\n":
if context.fix:
file_path.write_text(file_path.read_text() + "\n")
else:
raise Exception(f"File does not end with a newline {file_path}")
# List of all checks and on which files they should run.
CHECKS: List[Check] = [
Check(
@ -202,6 +213,10 @@ CHECKS: List[Check] = [
],
can_fix=True,
),
Check(
check_file_ends_with_newline,
exclude=["**.h264", "**.vp8", "**.bin", "**.png", "**.min.js", "infra/**.json"],
),
Check(check_crlf_line_endings),
]

View file

@ -573,7 +573,10 @@ def add_verbose_args(parser: argparse.ArgumentParser):
def all_tracked_files():
return (Path(f) for f in cmd("git ls-files").lines())
for line in cmd("git ls-files").lines():
file = Path(line)
if file.is_file():
yield file
def find_source_files(extension: str, ignore: List[str] = []):

View file

@ -69,7 +69,9 @@ def list_file_diff():
if upstream:
for line in git("diff --name-status", upstream).lines():
parts = line.split("\t", 1)
yield (parts[0].strip(), Path(parts[1].strip()))
file = Path(parts[1].strip())
if file.is_file():
yield (parts[0].strip(), file)
else:
print("WARNING: Not tracking a branch. Checking all files.")
for file in all_tracked_files():