test_jogfile_output_key.py (2114B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import io 6 import sys 7 from os import path 8 from pathlib import Path 9 10 import mozunit 11 12 ROOT_PATH = path.abspath( 13 path.join( 14 path.dirname(__file__), 15 path.pardir, 16 path.pardir, 17 path.pardir, 18 path.pardir, 19 path.pardir, 20 ) 21 ) 22 23 WEBEXT_METRICS_PATH = path.join( 24 ROOT_PATH, "browser", "extensions", "newtab", "webext-glue", "metrics" 25 ) 26 sys.path.append(WEBEXT_METRICS_PATH) 27 import glean_utils 28 29 # Shenanigans to access expect helper and metrics and pings test yaml under glean python tests 30 GLEAN_TESTS_PATH = path.join( 31 ROOT_PATH, "toolkit", "components", "glean", "tests", "pytest" 32 ) 33 sys.path.append(GLEAN_TESTS_PATH) 34 from expect_helper import expect 35 36 # Shenanigans to import run_glean_parser 37 GLEAN_BUILD_SCRIPTS_PATH = path.join( 38 ROOT_PATH, "toolkit", "components", "glean", "build_scripts", "glean_parser_ext" 39 ) 40 sys.path.append(GLEAN_BUILD_SCRIPTS_PATH) 41 42 import run_glean_parser 43 44 45 def test_jogfile_output_key(): 46 """ 47 A regression test. Very fragile. 48 It generates a jogfile with keys for metrics_test.yaml and compares that JSON with an expected output JSON file. 49 50 To generate new expected output files, set `UPDATE_EXPECT=1` when running the test suite: 51 52 UPDATE_EXPECT=1 mach test toolkit/components/glean/tests/pytest 53 """ 54 55 options = {"allow_reserved": False} 56 57 input_files = [ 58 Path(GLEAN_TESTS_PATH) / "metrics_test.yaml", 59 Path(GLEAN_TESTS_PATH) / "pings_test.yaml", 60 ] 61 62 all_objs, options = run_glean_parser.parse_with_options(input_files, options) 63 64 output_fd = io.StringIO() 65 glean_utils.output_file_with_key(all_objs, output_fd, options) 66 67 # Compare the generated output with expected output using JSON comparison by passing is_JSON as True 68 expect( 69 Path(path.dirname(__file__)) / "jogfile_output_key", output_fd.getvalue(), True 70 ) 71 72 73 if __name__ == "__main__": 74 mozunit.main()