This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
golden_tests.py
101 lines (83 loc) · 2.83 KB
/
golden_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Run golden output tests.
The golden tests are a convenient way to make sure that a "small" change
does not break anyone else.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from collections import namedtuple
import os
import subprocess
import sys
GOLDEN_CASES_DIR = 'src/googleapis/codegen/testdata/golden'
GOLDEN_DISCOVERY_DIR = 'src/googleapis/codegen/testdata/golden_discovery'
VERBOSE = False
Test = namedtuple('Test', [
'language',
'variant',
'input',
'options',
'golden_file'])
def FindTests():
"""Finds golden files and returns Test cases for each."""
for root, _, files in os.walk(GOLDEN_CASES_DIR):
path_parts = root.split('/')
if path_parts[-3] == 'golden':
language = path_parts[-2]
variant = path_parts[-1]
for golden_file in files:
input, _ = golden_file.split('.')
options = None
if input.endswith('_monolithic'):
input = input[0:-11]
options = ['--monolithic_source_name=sink'] # pure hackery
yield Test(
language = language,
variant = variant,
input = input,
options = options,
golden_file = os.path.join(root, golden_file))
def Generate(language, variant, input, options, out_file):
cmd = [
'python',
'src/googleapis/codegen/generate_library.py',
'--input=%s/%s.json' % (GOLDEN_DISCOVERY_DIR, input),
'--language=%s' % language,
'--language_variant=%s' % variant,
'--output_format=txt',
'--output_file=%s' % out_file,
]
if options:
cmd.extend(options)
try:
if VERBOSE:
print('generate cmd: %s' % ' '.join(cmd))
subprocess.check_call(cmd, stdout=sys.stdout, stderr=sys.stderr)
except subprocess.CalledProcessError as e:
msg = '(%s, %s, %s, %s)' % (language, variant, input, options)
print('FAIL: generate(%s), cmd=[%s]' % (msg, ' '.join(cmd)))
return False
return True
def RunTest(test):
# Fix this
out_file = '/tmp/%s.new' % test.golden_file.split('/')[-1]
if Generate(test.language, test.variant, test.input, test.options, out_file):
cmd = ['diff', '--brief', test.golden_file, out_file]
try:
subprocess.check_call(cmd, stderr=sys.stderr)
print('PASS: %s, %s, %s, %s' % (test.language, test.variant, test.input, test.options))
except subprocess.CalledProcessError as e:
print('FAIL: %s' % str(test))
def main(argv):
if len(argv) > 1:
raise app.UsageError('Too many command-line arguments.')
src_path = os.path.join(os.getcwd(), 'src')
python_path = os.environ.get('PYTHONPATH')
if python_path:
os.environ['PYTHONPATH'] = '%s:%s' % (src_path, python_path)
else:
os.environ['PYTHONPATH'] = src_path
for test in FindTests():
RunTest(test)
if __name__ == '__main__':
main(sys.argv)