|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import contextlib |
| 16 | +import io |
| 17 | +import json |
| 18 | +import unittest |
| 19 | + |
| 20 | +from pyfakefs import fake_filesystem_unittest |
| 21 | + |
| 22 | +import merge_json_results |
| 23 | + |
| 24 | + |
| 25 | +class TestMergeResults(fake_filesystem_unittest.TestCase): |
| 26 | + |
| 27 | + @fake_filesystem_unittest.patchfs(allow_root_user=True) |
| 28 | + def test_full_run(self, fs): |
| 29 | + with open('/in1.json', 'w') as f: |
| 30 | + json.dump({ |
| 31 | + 'num_tests': 2, |
| 32 | + 'failures': [ |
| 33 | + {'path': 'path/to/failure1', 'output': 'foo'}, |
| 34 | + ] |
| 35 | + }, f) |
| 36 | + |
| 37 | + with open('/in2.json', 'w') as f: |
| 38 | + json.dump({ |
| 39 | + 'num_tests': 3, |
| 40 | + 'failures': [ |
| 41 | + {'path': 'path/to/failure2', 'output': 'bar 42\nbar 43'}, |
| 42 | + {'path': 'path/to/failure3', 'output': 'baz'}, |
| 43 | + ] |
| 44 | + }, f) |
| 45 | + |
| 46 | + f = io.StringIO() |
| 47 | + with contextlib.redirect_stdout(f): |
| 48 | + merge_json_results.main([ |
| 49 | + '--json-input', '/in1.json', |
| 50 | + '--json-input', '/in2.json', |
| 51 | + '--json-output', '/output.json', |
| 52 | + ]) |
| 53 | + |
| 54 | + # Verify the output. |
| 55 | + self.assertEqual( |
| 56 | + 'Merged results for 5 tests and 3 failures.', |
| 57 | + f.getvalue().strip()) |
| 58 | + |
| 59 | + # Verify the results written to the json output file. |
| 60 | + with open('/output.json') as f: |
| 61 | + actual_results = json.load(f) |
| 62 | + |
| 63 | + expected_results = { |
| 64 | + 'num_tests': 5, |
| 65 | + 'failures': [ |
| 66 | + {'path': 'path/to/failure1', 'output': 'foo'}, |
| 67 | + {'path': 'path/to/failure2', 'output': 'bar 42\nbar 43'}, |
| 68 | + {'path': 'path/to/failure3', 'output': 'baz'}, |
| 69 | + ], |
| 70 | + } |
| 71 | + self.assertEqual(expected_results, actual_results) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + unittest.main() |
0 commit comments