diff options
author | UK992 <urbankrajnc92@gmail.com> | 2016-08-20 22:51:46 +0200 |
---|---|---|
committer | UK992 <urbankrajnc92@gmail.com> | 2016-08-20 22:51:46 +0200 |
commit | 74dba5cbdd35f2564db4d489f9bed17cbc190cf6 (patch) | |
tree | d124576ef055b55dcf4c4031c4b69a6787f015d6 /python | |
parent | 0ad905ae422a490f8d0f4a3666f453a319a985b7 (diff) | |
download | servo-74dba5cbdd35f2564db4d489f9bed17cbc190cf6.tar.gz servo-74dba5cbdd35f2564db4d489f9bed17cbc190cf6.zip |
Check for errors in tidy config file
Diffstat (limited to 'python')
-rw-r--r-- | python/tidy/servo_tidy/tidy.py | 50 | ||||
-rw-r--r-- | python/tidy/servo_tidy_tests/servo-tidy.toml | 13 | ||||
-rw-r--r-- | python/tidy/servo_tidy_tests/test_tidy.py | 7 |
3 files changed, 69 insertions, 1 deletions
diff --git a/python/tidy/servo_tidy/tidy.py b/python/tidy/servo_tidy/tidy.py index d861cd62159..39071f3127e 100644 --- a/python/tidy/servo_tidy/tidy.py +++ b/python/tidy/servo_tidy/tidy.py @@ -667,6 +667,52 @@ def check_spec(file_name, lines): 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): config_file = toml.loads(content) 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): + # check config file for errors + config_errors = check_config_file(CONFIG_FILE_PATH, progress) # standard checks files_to_check = filter_files('.', only_changed_files, progress) 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_errors = check_wpt_lint_errors(get_wpt_files(only_changed_files, progress)) # 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 for error in errors: colorama.init() diff --git a/python/tidy/servo_tidy_tests/servo-tidy.toml b/python/tidy/servo_tidy_tests/servo-tidy.toml new file mode 100644 index 00000000000..0aea4527b99 --- /dev/null +++ b/python/tidy/servo_tidy_tests/servo-tidy.toml @@ -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 = [] diff --git a/python/tidy/servo_tidy_tests/test_tidy.py b/python/tidy/servo_tidy_tests/test_tidy.py index bb1cb8ce61c..c8751ea59c4 100644 --- a/python/tidy/servo_tidy_tests/test_tidy.py +++ b/python/tidy/servo_tidy_tests/test_tidy.py @@ -23,6 +23,13 @@ class CheckTidiness(unittest.TestCase): with self.assertRaises(StopIteration): 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): errors = tidy.collect_errors_for_files(iterFile('wrong_space.rs'), [], [tidy.check_by_line], print_text=False) self.assertEqual('trailing whitespace', errors.next()[2]) |