| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
'''This script is for checking error messages of cracy-complete.''' |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
import yaml |
| 8 |
|
| 9 |
SCRIPT_FILE = os.path.basename(__file__) |
| 10 |
SCRIPT_DIR = os.path.dirname(__file__) |
| 11 |
YAML_TESTS_EXPECTED_INFILE = 'yaml_expected' |
| 12 |
YAML_TESTS_EXPECTED_OUTFILE = 'yaml_expected.new' |
| 13 |
DICTIONARY_TESTS_EXPECTED_INFILE = 'dictionary_expected' |
| 14 |
DICTIONARY_TESTS_EXPECTED_OUTFILE = 'dictionary_expected.new' |
| 15 |
|
| 16 |
os.environ['LANGUAGE'] = 'en' |
| 17 |
|
| 18 |
# We want to import the development version of cracy-complete, |
| 19 |
# not the installed version. |
| 20 |
CRAZY_COMPLETE_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..')) |
| 21 |
sys.path.insert(0, CRAZY_COMPLETE_DIR) |
| 22 |
|
| 23 |
import crazy_complete # noqa: E402 |
| 24 |
|
| 25 |
os.chdir(os.path.dirname(os.path.abspath(__file__))) |
| 26 |
|
| 27 |
class TesterBase: |
| 28 |
def __init__(self, expected_file, new_expected_file): |
| 29 |
self.results = [] |
| 30 |
self.expected_file = expected_file |
| 31 |
self.new_expected_file = new_expected_file |
| 32 |
|
| 33 |
def run(self): |
| 34 |
for file in sorted(os.listdir('.')): |
| 35 |
if os.path.isdir(file): |
| 36 |
self.run_subdirectory(file) |
| 37 |
|
| 38 |
result_content = '\n'.join(self.results) |
| 39 |
|
| 40 |
with open(self.new_expected_file, 'w', encoding='UTF-8') as fh: |
| 41 |
fh.write(result_content) |
| 42 |
|
| 43 |
try: |
| 44 |
with open(self.expected_file, 'r', encoding='UTF-8') as fh: |
| 45 |
expected_content = fh.read() |
| 46 |
except Exception as e: |
| 47 |
print(f'Cannot open `{self.expected_file}`: {e}') |
| 48 |
sys.exit(1) |
| 49 |
|
| 50 |
if expected_content != result_content: |
| 51 |
print(f'{self.expected_file} differs from {self.new_expected_file}.') |
| 52 |
print('Use diff on those files for details') |
| 53 |
sys.exit(1) |
| 54 |
|
| 55 |
def run_subdirectory(self, directory): |
| 56 |
self.results.append(f'======= Directory "{directory}" =======') |
| 57 |
|
| 58 |
for file in sorted(os.listdir(directory)): |
| 59 |
full_path = os.path.join(directory, file) |
| 60 |
basename = file.replace('.yaml', '') |
| 61 |
self.run_test(full_path, basename) |
| 62 |
|
| 63 |
|
| 64 |
class YamlValidatorTester(TesterBase): |
| 65 |
def run_test(self, file, basename): |
| 66 |
print(f'Testing {file}', end=' ... ') |
| 67 |
|
| 68 |
have_error = 'No error' |
| 69 |
|
| 70 |
with open(file, 'r', encoding='UTF-8') as fh: |
| 71 |
content = fh.read() |
| 72 |
|
| 73 |
try: |
| 74 |
parser = crazy_complete.extended_yaml_parser.ExtendedYAMLParser() |
| 75 |
definitions = parser.parse(content) |
| 76 |
crazy_complete.scheme_validator.validate(definitions) |
| 77 |
except crazy_complete.errors.CrazyError as e: |
| 78 |
have_error = str(e) |
| 79 |
|
| 80 |
self.results.append('%-40s | %s' % (basename, have_error)) |
| 81 |
|
| 82 |
print() |
| 83 |
|
| 84 |
|
| 85 |
class DictionaryValidatorTester(TesterBase): |
| 86 |
def run_test(self, file, basename): |
| 87 |
print(f'Testing {file}', end=' ... ') |
| 88 |
|
| 89 |
have_error = 'No error' |
| 90 |
|
| 91 |
with open(file, 'r', encoding='UTF-8') as fh: |
| 92 |
definition = list(yaml.safe_load_all(fh)) |
| 93 |
|
| 94 |
try: |
| 95 |
cmdline = crazy_complete.dictionary_source.dictionaries_to_commandline(definition) |
| 96 |
crazy_complete.bash.generate_completion(cmdline, None) |
| 97 |
except crazy_complete.errors.CrazyError as e: |
| 98 |
have_error = str(e) |
| 99 |
|
| 100 |
self.results.append('%-40s | %s' % (basename, have_error)) |
| 101 |
|
| 102 |
print() |
| 103 |
|
| 104 |
testers = [ |
| 105 |
YamlValidatorTester(YAML_TESTS_EXPECTED_INFILE, YAML_TESTS_EXPECTED_OUTFILE), |
| 106 |
DictionaryValidatorTester(DICTIONARY_TESTS_EXPECTED_INFILE, DICTIONARY_TESTS_EXPECTED_OUTFILE) |
| 107 |
] |
| 108 |
|
| 109 |
for tester in testers: |
| 110 |
tester.run() |