mirror of
https://github.com/servo/servo.git
synced 2025-06-08 00:23:30 +00:00
Check for errors in tidy config file
This commit is contained in:
parent
0ad905ae42
commit
74dba5cbdd
3 changed files with 69 additions and 1 deletions
|
@ -667,6 +667,52 @@ def check_spec(file_name, lines):
|
||||||
brace_count -= 1
|
brace_count -= 1
|
||||||
|
|
||||||
|
|
||||||
|
def check_config_file(config_file, print_text=True):
|
||||||
|
# Check if config file exists
|
||||||
|
if not os.path.exists(config_file):
|
||||||
|
print("%s config file is required but was not found" % config_file)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Load configs from servo-tidy.toml
|
||||||
|
with open(config_file) as content:
|
||||||
|
conf_file = content.read()
|
||||||
|
lines = conf_file.splitlines(True)
|
||||||
|
|
||||||
|
if print_text:
|
||||||
|
print '\rChecking for config file...'
|
||||||
|
|
||||||
|
current_table = ""
|
||||||
|
for idx, line in enumerate(lines):
|
||||||
|
# Ignore comment lines
|
||||||
|
if line.strip().startswith("#"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Check for invalid tables
|
||||||
|
if re.match("\[(.*?)\]", line.strip()):
|
||||||
|
table_name = re.findall(r"\[(.*?)\]", line)[0].strip()
|
||||||
|
if table_name not in ("configs", "ignore"):
|
||||||
|
yield config_file, idx + 1, "invalid config table [%s]" % table_name
|
||||||
|
current_table = table_name
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Skip if there is no equal sign in line, assuming it's not a key
|
||||||
|
if "=" not in line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
key = line.split("=")
|
||||||
|
key = key[0].strip()
|
||||||
|
|
||||||
|
# Check for invalid keys inside [configs] and [ignore] table
|
||||||
|
if (current_table == "configs" and key not in config or
|
||||||
|
current_table == "ignore" and key not in config["ignore"] or
|
||||||
|
# Any key outside of tables
|
||||||
|
current_table == ""):
|
||||||
|
yield config_file, idx + 1, "invalid config key '%s'" % key
|
||||||
|
|
||||||
|
# Parse config file
|
||||||
|
parse_config(conf_file)
|
||||||
|
|
||||||
|
|
||||||
def parse_config(content):
|
def parse_config(content):
|
||||||
config_file = toml.loads(content)
|
config_file = toml.loads(content)
|
||||||
exclude = config_file.get("ignore", {})
|
exclude = config_file.get("ignore", {})
|
||||||
|
@ -789,6 +835,8 @@ def get_file_list(directory, only_changed_files=False, exclude_dirs=[]):
|
||||||
|
|
||||||
|
|
||||||
def scan(only_changed_files=False, progress=True):
|
def scan(only_changed_files=False, progress=True):
|
||||||
|
# check config file for errors
|
||||||
|
config_errors = check_config_file(CONFIG_FILE_PATH, progress)
|
||||||
# standard checks
|
# standard checks
|
||||||
files_to_check = filter_files('.', only_changed_files, progress)
|
files_to_check = filter_files('.', only_changed_files, progress)
|
||||||
checking_functions = (check_flake8, check_lock, check_webidl_spec, check_json)
|
checking_functions = (check_flake8, check_lock, check_webidl_spec, check_json)
|
||||||
|
@ -800,7 +848,7 @@ def scan(only_changed_files=False, progress=True):
|
||||||
# wpt lint checks
|
# wpt lint checks
|
||||||
wpt_lint_errors = check_wpt_lint_errors(get_wpt_files(only_changed_files, progress))
|
wpt_lint_errors = check_wpt_lint_errors(get_wpt_files(only_changed_files, progress))
|
||||||
# collect errors
|
# collect errors
|
||||||
errors = itertools.chain(errors, dep_license_errors, wpt_lint_errors)
|
errors = itertools.chain(config_errors, errors, dep_license_errors, wpt_lint_errors)
|
||||||
error = None
|
error = None
|
||||||
for error in errors:
|
for error in errors:
|
||||||
colorama.init()
|
colorama.init()
|
||||||
|
|
13
python/tidy/servo_tidy_tests/servo-tidy.toml
Normal file
13
python/tidy/servo_tidy_tests/servo-tidy.toml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
key-outside = ""
|
||||||
|
|
||||||
|
[configs]
|
||||||
|
skip-check-length = false
|
||||||
|
skip-check-licenses = false
|
||||||
|
wrong-key = false
|
||||||
|
|
||||||
|
[wrong]
|
||||||
|
wrong-key = true
|
||||||
|
|
||||||
|
[ignore]
|
||||||
|
files = []
|
||||||
|
directories = []
|
|
@ -23,6 +23,13 @@ class CheckTidiness(unittest.TestCase):
|
||||||
with self.assertRaises(StopIteration):
|
with self.assertRaises(StopIteration):
|
||||||
errors.next()
|
errors.next()
|
||||||
|
|
||||||
|
def test_tidy_config(self):
|
||||||
|
errors = tidy.check_config_file(os.path.join(base_path, 'servo-tidy.toml'))
|
||||||
|
self.assertEqual('invalid config key \'key-outside\'', errors.next()[2])
|
||||||
|
self.assertEqual('invalid config key \'wrong-key\'', errors.next()[2])
|
||||||
|
self.assertEqual('invalid config table [wrong]', errors.next()[2])
|
||||||
|
self.assertNoMoreErrors(errors)
|
||||||
|
|
||||||
def test_spaces_correctnes(self):
|
def test_spaces_correctnes(self):
|
||||||
errors = tidy.collect_errors_for_files(iterFile('wrong_space.rs'), [], [tidy.check_by_line], print_text=False)
|
errors = tidy.collect_errors_for_files(iterFile('wrong_space.rs'), [], [tidy.check_by_line], print_text=False)
|
||||||
self.assertEqual('trailing whitespace', errors.next()[2])
|
self.assertEqual('trailing whitespace', errors.next()[2])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue