1313# limitations under the License.
1414
1515import os
16+ import pathlib
17+ import tempfile
1618from typing import Optional
1719import unittest.mock as mock
1820
@@ -155,6 +157,7 @@ def test_user_agent_not_in_vscode(monkeypatch):
155157 monkeypatch_client_constructors(monkeypatch)
156158 provider = create_clients_provider()
157159 assert_clients_wo_user_agent(provider, "vscode")
160+ assert_clients_wo_user_agent(provider, "googlecloudtools.cloudcode")
158161
159162 # We still need to include attribution to bigframes
160163 assert_clients_w_user_agent(provider, f"bigframes/{bigframes.version.__version__}")
@@ -165,16 +168,48 @@ def test_user_agent_in_vscode(monkeypatch):
165168 monkeypatch_client_constructors(monkeypatch)
166169 provider = create_clients_provider()
167170 assert_clients_w_user_agent(provider, "vscode")
171+ assert_clients_wo_user_agent(provider, "googlecloudtools.cloudcode")
168172
169173 # We still need to include attribution to bigframes
170174 assert_clients_w_user_agent(provider, f"bigframes/{bigframes.version.__version__}")
171175
172176
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+
173207@mock.patch.dict(os.environ, {}, clear=True)
174208def test_user_agent_not_in_jupyter(monkeypatch):
175209 monkeypatch_client_constructors(monkeypatch)
176210 provider = create_clients_provider()
177211 assert_clients_wo_user_agent(provider, "jupyter")
212+ assert_clients_wo_user_agent(provider, "bigquery_jupyter_plugin")
178213
179214 # We still need to include attribution to bigframes
180215 assert_clients_w_user_agent(provider, f"bigframes/{bigframes.version.__version__}")
@@ -185,6 +220,37 @@ def test_user_agent_in_jupyter(monkeypatch):
185220 monkeypatch_client_constructors(monkeypatch)
186221 provider = create_clients_provider()
187222 assert_clients_w_user_agent(provider, "jupyter")
223+ assert_clients_wo_user_agent(provider, "bigquery_jupyter_plugin")
188224
189225 # We still need to include attribution to bigframes
190226 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