replace string constants for json fields with variables

Summary: public
Now that we have modules, we can gather string constants in one place only.

Reviewed By: jeremydubreil

Differential Revision: D2615791

fb-gh-sync-id: 7cc21e4
master
Jules Villard 9 years ago committed by facebook-github-bot-7
parent 872ce8ea87
commit d50a401cd0

@ -59,11 +59,11 @@ base_parser.add_argument('--html',
def describe_report(report, indent=0): def describe_report(report, indent=0):
filename = report['file'] filename = report[utils.JSON_INDEX_FILENAME]
kind = report['kind'] kind = report[utils.JSON_INDEX_KIND]
line = report['line'] line = report[utils.JSON_INDEX_LINE]
error_type = report['type'] error_type = report[utils.JSON_INDEX_TYPE]
msg = report['qualifier'] msg = report[utils.JSON_INDEX_QUALIFIER]
return '{0}:{1}: {2}: {3}\n {4}{5}\n'.format( return '{0}:{1}: {2}: {3}\n {4}{5}\n'.format(
filename, filename,
line, line,
@ -92,27 +92,31 @@ class Tracer(object):
pass pass
def build_node(self, node): def build_node(self, node):
if node['level'] > self.max_level: if node[utils.JSON_INDEX_TRACE_LEVEL] > self.max_level:
return return
report_line = node['line_number'] report_line = node[utils.JSON_INDEX_TRACE_LINE]
fname = node['filename'] fname = node[utils.JSON_INDEX_TRACE_FILENAME]
self.indenter.newline() self.indenter.newline()
self.indenter.add('%s:%d: %s' % (fname, self.indenter.add('%s:%d: %s' % (
report_line, fname,
node['description'])) report_line,
node[utils.JSON_INDEX_TRACE_DESCRIPTION],
))
self.indenter.newline() self.indenter.newline()
if not self.args.no_source: if not self.args.no_source:
self.indenter.indent_push(node['level']) self.indenter.indent_push(node[utils.JSON_INDEX_TRACE_LEVEL])
self.indenter.add(utils.build_source_context(fname, report_line)) self.indenter.add(utils.build_source_context(fname, report_line))
self.indenter.indent_pop() self.indenter.indent_pop()
self.indenter.newline() self.indenter.newline()
def build_trace(self, trace): def build_trace(self, trace):
total_nodes = len(trace) total_nodes = len(trace)
hidden_nodes = len([None for n in trace if n['level'] > self.max_level]) hidden_nodes = len(
filter(lambda n: n[utils.JSON_INDEX_TRACE_LEVEL] > self.max_level,
trace))
shown_nodes = total_nodes - hidden_nodes shown_nodes = total_nodes - hidden_nodes
hidden_str = '' hidden_str = ''
all_str = 'all ' all_str = 'all '
@ -127,8 +131,8 @@ class Tracer(object):
self.build_node(node) self.build_node(node)
def build_report(self, report): def build_report(self, report):
traces = json.loads(report['trace']) traces = json.loads(report[utils.JSON_INDEX_TRACE])
self.build_trace(traces['trace']) self.build_trace(traces[utils.JSON_INDEX_TRACE])
def __str__(self): def __str__(self):
return str(self.indenter) return str(self.indenter)
@ -139,8 +143,8 @@ class Selector(object):
self.args = args self.args = args
def has_trace(report): def has_trace(report):
trace = json.loads(report['trace']) trace = json.loads(report[utils.JSON_INDEX_TRACE])
return len(trace['trace']) > 0 return len(trace[utils.JSON_INDEX_TRACE]) > 0
self.reports = [report for report in reports if has_trace(report)] self.reports = [report for report in reports if has_trace(report)]
def show_choices(self): def show_choices(self):
@ -308,8 +312,8 @@ def html_list_of_bugs(args, remote_source_template, selector):
def source_uri(report): def source_uri(report):
d = { d = {
'file-name': report['file'], 'file-name': report[utils.JSON_INDEX_FILENAME],
'line-number': report['line'], 'line-number': report[utils.JSON_INDEX_LINE],
} }
if remote_source_template is not None: if remote_source_template is not None:
link = remote_source_template.format(**d) link = remote_source_template.format(**d)

@ -49,12 +49,42 @@ CSV_REPORT_FILENAME = 'report.csv'
JSON_REPORT_FILENAME = 'report.json' JSON_REPORT_FILENAME = 'report.json'
BUGS_FILENAME = 'bugs.txt' BUGS_FILENAME = 'bugs.txt'
# indices in rows of csv reports
CSV_INDEX_CLASS = 0
CSV_INDEX_KIND = 1 CSV_INDEX_KIND = 1
CSV_INDEX_TYPE = 2 CSV_INDEX_TYPE = 2
CSV_INDEX_QUALIFIER = 3 CSV_INDEX_QUALIFIER = 3
CSV_INDEX_SEVERITY = 4
CSV_INDEX_LINE = 5 CSV_INDEX_LINE = 5
CSV_INDEX_PROCEDURE = 6
CSV_INDEX_PROCEDURE_ID = 7
CSV_INDEX_FILENAME = 8 CSV_INDEX_FILENAME = 8
CSV_INDEX_TRACE = 9
CSV_INDEX_KEY = 10
CSV_INDEX_QUALIFIER_TAGS = 11 CSV_INDEX_QUALIFIER_TAGS = 11
CSV_INDEX_HASH = 12
CSV_INDEX_BUG_ID = 13
CSV_INDEX_ALWAYS_REPORT = 14
CSV_INDEX_ADVICE = 15
# field names in rows of json reports
JSON_INDEX_FILENAME = 'file'
JSON_INDEX_HASH = 'hash'
JSON_INDEX_KIND = 'kind'
JSON_INDEX_LINE = 'line'
JSON_INDEX_PROCEDURE = 'procedure'
JSON_INDEX_QUALIFIER = 'qualifier'
JSON_INDEX_QUALIFIER_TAGS = 'qualifier_tags'
JSON_INDEX_SEVERITY = 'file'
JSON_INDEX_TYPE = 'type'
JSON_INDEX_TRACE = 'trace'
JSON_INDEX_TRACE_LEVEL = 'level'
JSON_INDEX_TRACE_FILENAME = 'filename'
JSON_INDEX_TRACE_LINE = 'line_number'
JSON_INDEX_TRACE_DESCRIPTION = 'description'
JSON_INDEX_TRACE_NODE_TAGS = 'node_tags'
JSON_INDEX_TRACE_NODE_TAGS_TAG = 'tags'
JSON_INDEX_TRACE_NODE_TAGS_VALUE = 'value'
QUALIFIER_TAGS = 'qualifier_tags' QUALIFIER_TAGS = 'qualifier_tags'
BUCKET_TAGS = 'bucket' BUCKET_TAGS = 'bucket'

@ -14,6 +14,13 @@ import sys
import tempfile import tempfile
import unittest import unittest
SCRIPTS_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0,
os.path.join(SCRIPTS_DIRECTORY,
os.pardir, 'infer', 'lib', 'python'))
from inferlib import utils
CURRENT_DIR = os.getcwd() CURRENT_DIR = os.getcwd()
REPORT_JSON = 'report.json' REPORT_JSON = 'report.json'
@ -22,15 +29,10 @@ INFER = 'infer'
RECORD_ENV = 'INFER_RECORD_INTEGRATION_TESTS' RECORD_ENV = 'INFER_RECORD_INTEGRATION_TESTS'
FILE = 'file'
HASH = 'hash'
LINE = 'line'
PROCEDURE = 'procedure'
TYPE = 'type'
REPORT_FIELDS = [ REPORT_FIELDS = [
FILE, utils.JSON_INDEX_FILENAME,
PROCEDURE, utils.JSON_INDEX_PROCEDURE,
TYPE, utils.JSON_INDEX_TYPE,
] ]
@ -39,14 +41,18 @@ def should_record_tests():
def quote(s): def quote(s):
return '\"{}\"'.format(s) return '\"%s\"' % s
def string_of_error(e): def string_of_error(e):
msg = e[TYPE] + ' in file ' + quote(e[FILE]) if utils.JSON_INDEX_LINE in e:
msg += ', procedure ' + quote(e[PROCEDURE]) line = ' on line %s ' % e[utils.JSON_INDEX_LINE]
if LINE in e: msg = '%s in file %s, procedure %s%s' % (
msg += ' on line ' + e[LINE] e[utils.JSON_INDEX_TYPE],
quote(e[utils.JSON_INDEX_FILE]),
quote(e[utils.JSON_INDEX_PROCEDURE]),
line,
)
return msg return msg

Loading…
Cancel
Save