epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Schema of the JSON summary file written out by the GM tool. |
| 7 | |
| 8 | This must be kept in sync with the kJsonKey_ constants in gm_expectations.cpp ! |
| 9 | """ |
| 10 | |
| 11 | __author__ = 'Elliot Poger' |
| 12 | |
| 13 | |
| 14 | # system-level imports |
| 15 | import json |
| 16 | |
| 17 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 18 | # Key strings used in GM results JSON files (both expected-results.json and |
| 19 | # actual-results.json). |
| 20 | # |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 21 | # These constants must be kept in sync with the kJsonKey_ constants in |
| 22 | # gm_expectations.cpp ! |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 23 | |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 24 | JSONKEY_ACTUALRESULTS = 'actual-results' |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 25 | # Tests whose results failed to match expectations. |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 26 | JSONKEY_ACTUALRESULTS_FAILED = 'failed' |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 27 | # Tests whose results failed to match expectations, but IGNOREFAILURE causes |
| 28 | # us to take them less seriously. |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 29 | JSONKEY_ACTUALRESULTS_FAILUREIGNORED = 'failure-ignored' |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 30 | # Tests for which we do not have any expectations. They may be new tests that |
| 31 | # we haven't had a chance to check in expectations for yet, or we may have |
| 32 | # consciously decided to leave them without expectations because we are unhappy |
| 33 | # with the results (although we should try to move away from that, and instead |
| 34 | # check in expectations with the IGNOREFAILURE flag set). |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 35 | JSONKEY_ACTUALRESULTS_NOCOMPARISON = 'no-comparison' |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 36 | # Tests whose results matched their expectations. |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 37 | JSONKEY_ACTUALRESULTS_SUCCEEDED = 'succeeded' |
| 38 | |
epoger@google.com | 53953b4 | 2013-07-02 20:22:27 +0000 | [diff] [blame] | 39 | JSONKEY_EXPECTEDRESULTS = 'expected-results' |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 40 | # One or more [HashType/DigestValue] pairs representing valid results for this |
| 41 | # test. Typically, there will just be one pair, but we allow for multiple |
| 42 | # expectations, and the test will pass if any one of them is matched. |
epoger@google.com | 53953b4 | 2013-07-02 20:22:27 +0000 | [diff] [blame] | 43 | JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS = 'allowed-digests' |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 44 | # If IGNOREFAILURE is set to True, a failure of this test will be reported |
| 45 | # within the FAILUREIGNORED section (thus NOT causing the buildbots to go red) |
| 46 | # rather than the FAILED section (which WOULD cause the buildbots to go red). |
epoger@google.com | 53953b4 | 2013-07-02 20:22:27 +0000 | [diff] [blame] | 47 | JSONKEY_EXPECTEDRESULTS_IGNOREFAILURE = 'ignore-failure' |
| 48 | |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 49 | # Allowed hash types for test expectations. |
epoger@google.com | 53953b4 | 2013-07-02 20:22:27 +0000 | [diff] [blame] | 50 | JSONKEY_HASHTYPE_BITMAP_64BITMD5 = 'bitmap-64bitMD5' |
| 51 | |
epoger@google.com | 4075fd4 | 2013-06-04 17:50:36 +0000 | [diff] [blame] | 52 | def LoadFromString(file_contents): |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 53 | """Loads the JSON summary written out by the GM tool. |
| 54 | Returns a dictionary keyed by the values listed as JSONKEY_ constants |
| 55 | above.""" |
epoger@google.com | 4075fd4 | 2013-06-04 17:50:36 +0000 | [diff] [blame] | 56 | # TODO(epoger): we should add a version number to the JSON file to ensure |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 57 | # that the writer and reader agree on the schema (raising an exception |
| 58 | # otherwise). |
epoger@google.com | 4075fd4 | 2013-06-04 17:50:36 +0000 | [diff] [blame] | 59 | json_dict = json.loads(file_contents) |
epoger@google.com | 9d33154 | 2013-05-28 15:25:38 +0000 | [diff] [blame] | 60 | return json_dict |
epoger@google.com | 4075fd4 | 2013-06-04 17:50:36 +0000 | [diff] [blame] | 61 | |
| 62 | def LoadFromFile(file_path): |
| 63 | """Loads the JSON summary written out by the GM tool. |
| 64 | Returns a dictionary keyed by the values listed as JSONKEY_ constants |
| 65 | above.""" |
| 66 | file_contents = open(file_path, 'r').read() |
| 67 | return LoadFromString(file_contents) |
epoger@google.com | a783f2b | 2013-07-08 17:51:58 +0000 | [diff] [blame] | 68 | |
| 69 | def WriteToFile(json_dict, file_path): |
| 70 | """Writes the JSON summary in json_dict out to file_path.""" |
| 71 | with open(file_path, 'w') as outfile: |
| 72 | json.dump(json_dict, outfile, sort_keys=True, indent=2) |