4
4
and construct a new test case.
5
5
6
6
"""
7
+ import argparse
7
8
import errno
8
9
import os
9
10
import os .path
@@ -38,12 +39,13 @@ def write_file(test_name, suffix, data):
38
39
f .write (data )
39
40
return True
40
41
41
- def gen_test (url , test_name , test_description ):
42
- spec_dict = {'url' : url , 'test_description' : test_description }
43
- spec = yaml .dump (spec_dict , default_flow_style = False )
44
- if not write_file (test_name , test .YAML_EXTENSION , spec ):
45
- return False
46
42
43
+ def gen_test (url , test_name , test_description , gen_yaml = True ):
44
+ if gen_yaml :
45
+ spec_dict = {'url' : url , 'test_description' : test_description }
46
+ spec = yaml .dump (spec_dict , default_flow_style = False )
47
+ if not write_file (test_name , test .YAML_EXTENSION , spec ):
48
+ return False
47
49
orig = urllib2 .urlopen (url ).read ()
48
50
if not write_file (test_name , test .ORIGINAL_SUFFIX , orig ):
49
51
return False
@@ -55,21 +57,40 @@ def gen_test(url, test_name, test_description):
55
57
56
58
return True
57
59
58
- USAGE = '''
59
- usage: %s <url> <test name> <test description>
60
- '''
61
-
62
- def usage (prog_name ):
63
- print (USAGE % prog_name )
60
+ DESCRIPTION = 'Create a readability regression test case.'
64
61
65
62
def main ():
66
- if len (sys .argv ) != 4 :
67
- usage (sys .argv [0 ])
68
- return
69
- url = sys .argv [1 ]
70
- test_name = sys .argv [2 ]
71
- test_description = sys .argv [3 ]
72
- result = gen_test (url , test_name , test_description )
63
+ parser = argparse .ArgumentParser (description = DESCRIPTION )
64
+ parser .add_argument (
65
+ '--no-yaml' ,
66
+ dest = 'no_yaml' ,
67
+ action = 'store_const' ,
68
+ const = True ,
69
+ default = False ,
70
+ help = 'if set, no yaml file will be generated'
71
+ )
72
+ parser .add_argument (
73
+ 'url' ,
74
+ metavar = 'url' ,
75
+ help = 'the url for which to generate a test'
76
+ )
77
+ parser .add_argument (
78
+ 'test_name' ,
79
+ metavar = 'test-name' ,
80
+ help = 'the name of the test'
81
+ )
82
+ parser .add_argument (
83
+ 'test_description' ,
84
+ metavar = 'test-description' ,
85
+ help = 'the description of the test'
86
+ )
87
+ args = parser .parse_args ()
88
+ result = gen_test (
89
+ args .url ,
90
+ args .test_name ,
91
+ args .test_description ,
92
+ gen_yaml = not args .no_yaml
93
+ )
73
94
if not result :
74
95
print ('test was not fully generated' )
75
96
0 commit comments