22import lxml .html .diff
33import os
44import os .path
5+ import re
56import sys
67import unittest
78import yaml
89
910from lxml .html import builder as B
1011from readability_lxml import readability
1112
13+
14+ DIFF_SUFFIX = '-diff.html'
1215ORIGINAL_SUFFIX = '-orig.html'
1316READABLE_SUFFIX = '-rdbl.html'
1417RESULT_SUFFIX = '-result.html'
15- DIFF_SUFFIX = '-diff.html '
18+ YAML_EXTENSION = '.yaml '
1619
1720TESTDIR = os .path .dirname (__file__ )
1821TEST_DATA_PATH = os .path .join (TESTDIR , 'test_data' )
2124
2225class ReadabilityTest :
2326
24- def __init__ (self , dir_path , name , orig_path , rdbl_path ):
27+ def __init__ (self , dir_path , enabled , name , desc , orig_path , rdbl_path ):
2528 self .dir_path = dir_path
29+ self .enabled = enabled
2630 self .name = name
31+ self .desc = desc
2732 self .orig_path = orig_path
2833 self .rdbl_path = rdbl_path
2934
@@ -44,6 +49,11 @@ def __init__(self, test_data, result_html, diff_html):
4449 self .diff_html = diff_html
4550
4651
52+ def read_yaml (path ):
53+ with open (path , 'r' ) as f :
54+ return yaml .load (f )
55+
56+
4757def strip_with_suffix (suffix , files ):
4858 filtered = [x for x in files if x .endswith (suffix )]
4959 stripped = [x .replace (suffix , '' ) for x in filtered ]
@@ -61,22 +71,19 @@ def check_missing(lhs, rhs, rhs_description):
6171 raise Exception (s )
6272
6373
64- def resolve_test_names (files ):
65- orig_names = strip_with_suffix (ORIGINAL_SUFFIX , files )
66- rdbl_names = strip_with_suffix (READABLE_SUFFIX , files )
67- check_missing (orig_names , rdbl_names , READABLE_SUFFIX )
68- check_missing (rdbl_names , orig_names , ORIGINAL_SUFFIX )
69- return orig_names
70-
71-
7274def make_path (dir_path , name , suffix ):
7375 return os .path .join (dir_path , '' .join ([name , suffix ]))
7476
75-
76- def make_readability_test (dir_path , name ):
77+ def make_readability_test (dir_path , name , spec_dict ):
78+ if 'enabled' in spec_dict :
79+ enabled = spec_dict ['enabled' ]
80+ else :
81+ enabled = True
7782 return ReadabilityTest (
7883 dir_path ,
84+ enabled ,
7985 name ,
86+ spec_dict ['test_description' ],
8087 make_path (dir_path , name , ORIGINAL_SUFFIX ),
8188 make_path (dir_path , name , READABLE_SUFFIX )
8289 )
@@ -89,8 +96,14 @@ def load_test_data(test):
8996
9097
9198def load_readability_tests (dir_path , files ):
92- names = resolve_test_names (files )
93- return [make_readability_test (dir_path , name ) for name in names ]
99+ yaml_files = [f for f in files if f .endswith (YAML_EXTENSION )]
100+ yaml_paths = [os .path .join (dir_path , f ) for f in yaml_files ]
101+ names = [re .sub ('.yaml$' , '' , f ) for f in yaml_files ]
102+ spec_dicts = [read_yaml (p ) for p in yaml_paths ]
103+ return [
104+ make_readability_test (dir_path , name , spec_dict )
105+ for (name , spec_dict ) in zip (names , spec_dicts )
106+ ]
94107
95108
96109def execute_test (test_data ):
@@ -169,13 +182,23 @@ def write_result(output_dir_path, result):
169182 write_output_fragment (html , output_dir_path , test_name , suffix )
170183
171184
185+ def print_test_info (test ):
186+ name_string = '%s' % test .name
187+ if test .enabled :
188+ skipped = ''
189+ else :
190+ skipped = ' (SKIPPED)'
191+ print ('%20s: %s%s' % (name_string , test .desc , skipped ))
192+
172193def run_readability_tests ():
173194 files = os .listdir (TEST_DATA_PATH )
174195 tests = load_readability_tests (TEST_DATA_PATH , files )
175196 for test in tests :
176- test_data = load_test_data (test )
177- result = execute_test (test_data )
178- write_result (TEST_OUTPUT_PATH , result )
197+ print_test_info (test )
198+ if test .enabled :
199+ test_data = load_test_data (test )
200+ result = execute_test (test_data )
201+ write_result (TEST_OUTPUT_PATH , result )
179202
180203
181204class TestStripWithSuffix (unittest .TestCase ):
0 commit comments