Skip to content

Commit

Permalink
Check for errors in tidy config file
Browse files Browse the repository at this point in the history
  • Loading branch information
UK992 committed Aug 20, 2016
1 parent 0ad905a commit 74dba5c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
50 changes: 49 additions & 1 deletion python/tidy/servo_tidy/tidy.py
Expand Up @@ -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", {})
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions 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 = []
7 changes: 7 additions & 0 deletions python/tidy/servo_tidy_tests/test_tidy.py
Expand Up @@ -23,6 +23,13 @@ def assertNoMoreErrors(self, errors):
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])
Expand Down

0 comments on commit 74dba5c

Please sign in to comment.