1414ROOT = Path (__file__ ).parent
1515
1616
17- def test_parser (tmpdir ):
17+ def test_parser_js (tmpdir ):
1818 """Test translating metrics to Javascript files."""
1919 tmpdir = Path (str (tmpdir ))
2020
@@ -39,14 +39,56 @@ def test_parser(tmpdir):
3939 # Make sure descriptions made it in
4040 with (tmpdir / "corePing.js" ).open ("r" , encoding = "utf-8" ) as fd :
4141 content = fd .read ()
42+ assert "use strict" in content
4243 assert "True if the user has set Firefox as the default browser." in content
4344
4445 with (tmpdir / "telemetry.js" ).open ("r" , encoding = "utf-8" ) as fd :
4546 content = fd .read ()
47+ assert "use strict" in content
4648 assert "جمع 搜集" in content
4749
4850 with (tmpdir / "gleanInternalMetrics.js" ).open ("r" , encoding = "utf-8" ) as fd :
4951 content = fd .read ()
52+ assert "use strict" in content
53+ assert 'category: ""' in content
54+
55+
56+ def test_parser_ts (tmpdir ):
57+ """Test translating metrics to Typescript files."""
58+ tmpdir = Path (str (tmpdir ))
59+
60+ translate .translate (
61+ ROOT / "data" / "core.yaml" ,
62+ "typescript" ,
63+ tmpdir ,
64+ None ,
65+ {"allow_reserved" : True },
66+ )
67+
68+ assert set (x .name for x in tmpdir .iterdir ()) == set (
69+ [
70+ "corePing.ts" ,
71+ "telemetry.ts" ,
72+ "environment.ts" ,
73+ "dottedCategory.ts" ,
74+ "gleanInternalMetrics.ts" ,
75+ ]
76+ )
77+
78+ # Make sure descriptions made it in
79+ with (tmpdir / "corePing.ts" ).open ("r" , encoding = "utf-8" ) as fd :
80+ content = fd .read ()
81+ assert "use strict" not in content
82+ assert "True if the user has set Firefox as the default browser." in content
83+
84+ with (tmpdir / "telemetry.ts" ).open ("r" , encoding = "utf-8" ) as fd :
85+ content = fd .read ()
86+ assert "use strict" not in content
87+ assert "جمع 搜集" in content
88+
89+ with (tmpdir / "gleanInternalMetrics.ts" ).open ("r" , encoding = "utf-8" ) as fd :
90+ content = fd .read ()
91+ assert "use strict" not in content
5092 assert 'category: ""' in content
5193
5294
@@ -87,7 +129,42 @@ def test_metric_class_name():
87129 extra_keys = {"my_extra" : {"description" : "an extra" }},
88130 )
89131
90- assert javascript .class_name (event .type ) == "Glean._private.EventMetricType"
132+ assert javascript .class_name (event .type ) == "EventMetricType"
133+
134+ boolean = metrics .Boolean (
135+ type = "boolean" ,
136+ category = "category" ,
137+ name = "metric" ,
138+ bugs = ["http://bugzilla.mozilla.com/12345" ],
139+ notification_emails = ["nobody@example.com" ],
140+ description = "description..." ,
141+ expires = "never" ,
142+ )
143+ assert javascript .class_name (boolean .type ) == "BooleanMetricType"
144+
145+ ping = pings .Ping (
146+ name = "custom" ,
147+ description = "description..." ,
148+ include_client_id = True ,
149+ bugs = ["http://bugzilla.mozilla.com/12345" ],
150+ notification_emails = ["nobody@nowhere.com" ],
151+ )
152+ assert javascript .class_name (ping .type ) == "PingType"
153+
154+
155+ def test_import_path ():
156+ event = metrics .Event (
157+ type = "event" ,
158+ category = "category" ,
159+ name = "metric" ,
160+ bugs = ["http://bugzilla.mozilla.com/12345" ],
161+ notification_emails = ["nobody@example.com" ],
162+ description = "description..." ,
163+ expires = "never" ,
164+ extra_keys = {"my_extra" : {"description" : "an extra" }},
165+ )
166+
167+ assert javascript .import_path (event .type ) == "metrics/event"
91168
92169 boolean = metrics .Boolean (
93170 type = "boolean" ,
@@ -98,7 +175,7 @@ def test_metric_class_name():
98175 description = "description..." ,
99176 expires = "never" ,
100177 )
101- assert javascript .class_name (boolean .type ) == "Glean._private.BooleanMetricType "
178+ assert javascript .import_path (boolean .type ) == "metrics/boolean "
102179
103180 ping = pings .Ping (
104181 name = "custom" ,
@@ -107,7 +184,7 @@ def test_metric_class_name():
107184 bugs = ["http://bugzilla.mozilla.com/12345" ],
108185 notification_emails = ["nobody@nowhere.com" ],
109186 )
110- assert javascript .class_name (ping .type ) == "Glean._private.PingType "
187+ assert javascript .import_path (ping .type ) == "ping "
111188
112189
113190# TODO: Activate once Glean.js adds support for labeled metric types in Bug 1682573.
@@ -189,5 +266,5 @@ def test_arguments_are_generated_in_deterministic_order(tmpdir):
189266 with (tmpdir / "event.js" ).open ("r" , encoding = "utf-8" ) as fd :
190267 content = fd .read ()
191268 content = " " .join (content .split ())
192- expected = 'new Glean._private. EventMetricType({ category: "event", name: "example", sendInPings: ["events"], lifetime: "ping", disabled: false, }, ["alice", "bob", "charlie"]), ' # noqa
269+ expected = 'export const example = new EventMetricType({ category: "event", name: "example", sendInPings: ["events"], lifetime: "ping", disabled: false, }, ["alice", "bob", "charlie"]); ' # noqa
193270 assert expected in content
0 commit comments