13
13
# limitations under the License.
14
14
15
15
import os
16
+ import pathlib
17
+ import tempfile
16
18
from typing import Optional
17
19
import unittest .mock as mock
18
20
@@ -155,6 +157,7 @@ def test_user_agent_not_in_vscode(monkeypatch):
155
157
monkeypatch_client_constructors (monkeypatch )
156
158
provider = create_clients_provider ()
157
159
assert_clients_wo_user_agent (provider , "vscode" )
160
+ assert_clients_wo_user_agent (provider , "googlecloudtools.cloudcode" )
158
161
159
162
# We still need to include attribution to bigframes
160
163
assert_clients_w_user_agent (provider , f"bigframes/{ bigframes .version .__version__ } " )
@@ -165,16 +168,48 @@ def test_user_agent_in_vscode(monkeypatch):
165
168
monkeypatch_client_constructors (monkeypatch )
166
169
provider = create_clients_provider ()
167
170
assert_clients_w_user_agent (provider , "vscode" )
171
+ assert_clients_wo_user_agent (provider , "googlecloudtools.cloudcode" )
168
172
169
173
# We still need to include attribution to bigframes
170
174
assert_clients_w_user_agent (provider , f"bigframes/{ bigframes .version .__version__ } " )
171
175
172
176
177
+ @mock .patch .dict (os .environ , {"VSCODE_PID" : "12345" }, clear = True )
178
+ def test_user_agent_in_vscode_w_extension (monkeypatch ):
179
+ monkeypatch_client_constructors (monkeypatch )
180
+
181
+ with tempfile .TemporaryDirectory () as tmpdir :
182
+ user_home = pathlib .Path (tmpdir )
183
+ extension_dir = (
184
+ user_home / ".vscode" / "extensions" / "googlecloudtools.cloudcode-0.12"
185
+ )
186
+ extension_config = extension_dir / "package.json"
187
+
188
+ # originally extension config does not exist
189
+ assert not extension_config .exists ()
190
+
191
+ # simulate extension installation by creating extension config on disk
192
+ extension_dir .mkdir (parents = True )
193
+ with open (extension_config , "w" ) as f :
194
+ f .write ("{}" )
195
+
196
+ with mock .patch ("pathlib.Path.home" , return_value = user_home ):
197
+ provider = create_clients_provider ()
198
+ assert_clients_w_user_agent (provider , "vscode" )
199
+ assert_clients_w_user_agent (provider , "googlecloudtools.cloudcode" )
200
+
201
+ # We still need to include attribution to bigframes
202
+ assert_clients_w_user_agent (
203
+ provider , f"bigframes/{ bigframes .version .__version__ } "
204
+ )
205
+
206
+
173
207
@mock .patch .dict (os .environ , {}, clear = True )
174
208
def test_user_agent_not_in_jupyter (monkeypatch ):
175
209
monkeypatch_client_constructors (monkeypatch )
176
210
provider = create_clients_provider ()
177
211
assert_clients_wo_user_agent (provider , "jupyter" )
212
+ assert_clients_wo_user_agent (provider , "bigquery_jupyter_plugin" )
178
213
179
214
# We still need to include attribution to bigframes
180
215
assert_clients_w_user_agent (provider , f"bigframes/{ bigframes .version .__version__ } " )
@@ -185,6 +220,37 @@ def test_user_agent_in_jupyter(monkeypatch):
185
220
monkeypatch_client_constructors (monkeypatch )
186
221
provider = create_clients_provider ()
187
222
assert_clients_w_user_agent (provider , "jupyter" )
223
+ assert_clients_wo_user_agent (provider , "bigquery_jupyter_plugin" )
188
224
189
225
# We still need to include attribution to bigframes
190
226
assert_clients_w_user_agent (provider , f"bigframes/{ bigframes .version .__version__ } " )
227
+
228
+
229
+ @mock .patch .dict (os .environ , {"JPY_PARENT_PID" : "12345" }, clear = True )
230
+ def test_user_agent_in_jupyter_with_plugin (monkeypatch ):
231
+ monkeypatch_client_constructors (monkeypatch )
232
+
233
+ def custom_import_module_side_effect (name , package = None ):
234
+ if name == "bigquery_jupyter_plugin" :
235
+ return mock .MagicMock ()
236
+ else :
237
+ import importlib
238
+
239
+ return importlib .import_module (name , package )
240
+
241
+ assert isinstance (
242
+ custom_import_module_side_effect ("bigquery_jupyter_plugin" ), mock .MagicMock
243
+ )
244
+ assert custom_import_module_side_effect ("bigframes" ) is bigframes
245
+
246
+ with mock .patch (
247
+ "importlib.import_module" , side_effect = custom_import_module_side_effect
248
+ ):
249
+ provider = create_clients_provider ()
250
+ assert_clients_w_user_agent (provider , "jupyter" )
251
+ assert_clients_w_user_agent (provider , "bigquery_jupyter_plugin" )
252
+
253
+ # We still need to include attribution to bigframes
254
+ assert_clients_w_user_agent (
255
+ provider , f"bigframes/{ bigframes .version .__version__ } "
256
+ )
0 commit comments