Navigation Menu

Skip to content

Commit

Permalink
Tests to sanity check our config.json file
Browse files Browse the repository at this point in the history
Test that the file is valid JSON

Test that the file does not contain any "weird" characters: we noticed that
editing the config file through the github web interface on a mobile phone had
accidentally introduced some unicode ‪\u202a LEFT-TO-RIGHT EMBEDDING and
‬\u202c POP DIRECTIONAL FORMATTING chracters, which caused the script to
silently fail to process certain rules.
  • Loading branch information
natbat committed May 23, 2017
1 parent fda6a49 commit 59035aa
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion tests.py
@@ -1,4 +1,7 @@
import unittest
import json
import unicodedata
import re
from scan_and_retweet import tweet_matches_rules

class TestTweetMatchesRules(unittest.TestCase):
Expand Down Expand Up @@ -49,5 +52,32 @@ def test_rule_empty_list_means_wildcard(self):
self.assertTrue(tweet_matches_rules(screen_name, full_text, rules))



class TestSanityCheckConfig(unittest.TestCase):

def test_config_is_valid_json(self):
try:
json.load(open('config.json'))
except ValueError, e:
self.fail('Invalid JSON in config file')

def test_config_has_no_weird_characters(self):
"""
On occasion, editing code on a phone can accidentally introduce weird
invisible characters which break the bot. Check that the config file
only includes alphanumeric characters and JSON punctuation.
"""
allowed_chars_regex = re.compile(r'[^a-zA-Z0-9\[\]\{\}\s":,_]')
with open('config.json') as fp:
contents = fp.read().decode('utf8')
match = allowed_chars_regex.search(contents)
if match:
weird_char = match.group(0)
line_number = contents[:match.start()].count('\n') + 1
self.fail('config.json contains a weird character on line %d: %s %r' % (
line_number, unicodedata.name(weird_char), weird_char
))


if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 59035aa

Please sign in to comment.