From 0d0c79eecc0376cf2180d9f0748d17984da55a2f Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Thu, 28 Jul 2022 12:15:16 -0700 Subject: [PATCH] Add an indent checker, fix some lines that break it This is pretty dumb and breaks on valid code, but it's okay enough for our purposes I think. If I made it any more complex I'd want to start looking for a third party parser. Signed-off-by: Adam Williamson --- lib/anacondatest.pm | 6 +++-- lib/installedtest.pm | 3 ++- lib/utils.pm | 3 ++- perllint.py | 57 +++++++++++++++++++++++++++++++++++++------- 4 files changed, 56 insertions(+), 13 deletions(-) diff --git a/lib/anacondatest.pm b/lib/anacondatest.pm index 7d427fcd..07691f38 100644 --- a/lib/anacondatest.pm +++ b/lib/anacondatest.pm @@ -21,7 +21,8 @@ sub post_fail_hook { if (check_screen "anaconda_error_report", 10) { assert_and_click "anaconda_error_report"; $has_traceback = 1; - } elsif (check_screen "anaconda_text_error", 10) { # also for text install + } elsif (check_screen "anaconda_text_error", 10) { + # also for text install type_string "1\n"; $has_traceback = 1; } @@ -128,7 +129,8 @@ sub root_console { # 0 means use console_login's default, non-zero values # passed to console_login timeout => 0, - @_); + @_ + ); if (get_var("SERIAL_CONSOLE")) { # select first virtio terminal, we rely on anaconda having run # a root shell on it for us diff --git a/lib/installedtest.pm b/lib/installedtest.pm index a47ef3db..379bf024 100644 --- a/lib/installedtest.pm +++ b/lib/installedtest.pm @@ -18,7 +18,8 @@ sub root_console { my %args = ( tty => 1, # what TTY to login to timeout => 0, # passed through to console_login - @_); + @_ + ); if (get_var("SERIAL_CONSOLE")) { # select the first virtio terminal, for now we assume we can # always use that (we may have to make this smarter in future) diff --git a/lib/utils.pm b/lib/utils.pm index b713cca0..ed9daa63 100644 --- a/lib/utils.pm +++ b/lib/utils.pm @@ -150,7 +150,8 @@ sub console_login { password => get_var("ROOT_PASSWORD", "weakpassword"), # default is 10 seconds, set below, 0 means 'default' timeout => 0, - @_); + @_ + ); $args{timeout} ||= 10; # Since we do not test many serial console tests, and we probably diff --git a/perllint.py b/perllint.py index 39132f3c..f8636db5 100755 --- a/perllint.py +++ b/perllint.py @@ -24,14 +24,17 @@ if any line contains a tab, ends with whitespace characters (which includes lines containing *only* whitespace characters), or starts with a number of spaces that is not divisible by four. If run with --write, it will strip trailing whitespace (including turning lines -that contain only whitespace into blank lines). It does not currently -attempt to replace tabs or correct leading whitespace. +that contain only whitespace into blank lines) and correct indentation +(including removing tabs in indentation). It does not do anything +with tabs found outside of leading indentation. """ import glob import os +import re import sys +HEREDOCRE = re.compile(r"<<['\"]?(\w*)['\"]?;\s*$") def run(write): """ @@ -57,12 +60,19 @@ def check_file(filename, write): with open(filename, "r", encoding="UTF-8") as readfh: lines = readfh.readlines() newlines = [] + indent = 0 + # indicator whether we're in a heredoc block + heredoc = None for (num, line) in enumerate(lines, 1): - # tab check - if "\t" in line: - print(f"Line {num} of file {filename} contains a tab!") - failed = True - + # if we're in a heredoc, skip all checks, and see if we + # ended it + if heredoc: + if line.strip(" \t").rstrip(" \t\n") == heredoc: + heredoc = None + # write the line back to newlines + if write: + newlines.append(line) + continue # trailing whitespace check and replace newline = line.rstrip("\n").rstrip(" \t") + "\n" if newline != line: @@ -74,16 +84,45 @@ def check_file(filename, write): line = newline # correct number of starting spaces check + # skip non-indented comment lines + if line.startswith("# "): + # write the line back to newlines + if write: + newlines.append(line) + continue + # first update indent if line closes a block + if line.strip(" \t").startswith("}") or line.strip(" \t").startswith(")"): + indent -= 4 spaces = 0 for char in line: if char != " ": break spaces += 1 - if spaces > 0 and spaces % 4 != 0: + if spaces != indent and line != "\n": print( - f"Line {num} of file {filename} starts with an invalid number of spaces!" + f"Line {num} of file {filename} starts with an invalid number of spaces! Expected {indent} got {spaces}" ) failed = True + if write: + line = line.lstrip() + if line.startswith("\t"): + print(f"Line {num} of file {filename} has a tab in leading whitespace!") + line = line.strip(" \t") + line = " " * indent + line + # now update indent if line opens a block (and isn't a comment) + if not line.strip(" \t").startswith("#"): + if line.rstrip(" \t\n").endswith("{") or line.rstrip(" \t\n").endswith("("): + indent += 4 + + # tab check + if "\t" in line: + print(f"Line {num} of file {filename} contains a tab!") + failed = True + + # did the line start a heredoc? + herematch = HEREDOCRE.search(line) + if herematch: + heredoc = herematch.group(1) # write the line back to newlines if write: