Skip to content

Commit 51de41f

Browse files
Use __file__-relative paths for test fixtures (#1445)
## Summary Two tests hardcode relative paths (`tests/testdata/...` and `databricks/__init__.py`) that only resolve when pytest's cwd is the SDK root. Switch them to `pathlib.Path(__file__)`-based paths so they work no matter where pytest is launched. ## Why Today's upstream invocation (`make test` from the SDK root) is the happy case, so it goes unnoticed. But pytest from any other directory — a different CWD, a CI runner that builds the source tree elsewhere, or a Bazel test sandbox — fails the tests with `FileNotFoundError`. ## What changed ### Interface changes None. ### Behavioral changes None (same fixture files, just resolved by absolute path). ### Internal changes - `tests/test_model_serving_auth.py`: `\"tests/testdata/model-serving-test-token\"` and `\"...-v2\"` parameters replaced with constants computed from `pathlib.Path(__file__).parent / \"testdata\"`. - `tests/test_init_file.py`: `open(\"databricks/__init__.py\")` replaced with `open(pathlib.Path(__file__).parent.parent / \"databricks\" / \"__init__.py\")`. ## How is this tested? - `make test` from the SDK root: 2073 passed, 3 skipped, same baseline as main. - `pytest tests/test_model_serving_auth.py tests/test_init_file.py` from outside the SDK root (e.g. `/tmp`): now passes (previously fails with `FileNotFoundError`). NO_CHANGELOG=true
1 parent 1047d7b commit 51de41f

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

tests/test_init_file.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import hashlib
2+
import pathlib
3+
4+
# databricks/__init__.py lives one directory above this test, regardless
5+
# of where pytest was invoked from.
6+
_INIT_FILE = pathlib.Path(__file__).parent.parent / "databricks" / "__init__.py"
27

38

49
def test_init_file_contents():
@@ -7,7 +12,7 @@ def test_init_file_contents():
712
813
Also see https://github.com/databricks/databricks-sdk-py/issues/343#issuecomment-1866029118.
914
"""
10-
with open("databricks/__init__.py") as f:
15+
with open(_INIT_FILE) as f:
1116
init_file_contents = f.read()
1217

1318
# This hash is the expected hash of the contents of `src/databricks/__init__.py`.

tests/test_model_serving_auth.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pathlib
12
import threading
23
import time
34

@@ -8,6 +9,12 @@
89

910
from .conftest import raises
1011

12+
# Test fixture files live next to this test, regardless of where pytest
13+
# was invoked from.
14+
_TESTDATA = pathlib.Path(__file__).parent / "testdata"
15+
_MODEL_SERVING_TEST_TOKEN = str(_TESTDATA / "model-serving-test-token")
16+
_MODEL_SERVING_TEST_TOKEN_V2 = str(_TESTDATA / "model-serving-test-token-v2")
17+
1118
default_auth_base_error_message = (
1219
"default auth: cannot configure default credentials, "
1320
"please check https://docs.databricks.com/en/dev-tools/auth.html#databricks-client-unified-authentication "
@@ -24,31 +31,31 @@
2431
("DB_MODEL_SERVING_HOST_URL", "x"),
2532
],
2633
["DATABRICKS_MODEL_SERVING_HOST_URL"],
27-
"tests/testdata/model-serving-test-token",
34+
_MODEL_SERVING_TEST_TOKEN,
2835
),
2936
(
3037
[
3138
("IS_IN_DATABRICKS_MODEL_SERVING_ENV", "true"),
3239
("DB_MODEL_SERVING_HOST_URL", "x"),
3340
],
3441
["DATABRICKS_MODEL_SERVING_HOST_URL"],
35-
"tests/testdata/model-serving-test-token",
42+
_MODEL_SERVING_TEST_TOKEN,
3643
),
3744
(
3845
[
3946
("IS_IN_DB_MODEL_SERVING_ENV", "true"),
4047
("DATABRICKS_MODEL_SERVING_HOST_URL", "x"),
4148
],
4249
["DB_MODEL_SERVING_HOST_URL"],
43-
"tests/testdata/model-serving-test-token",
50+
_MODEL_SERVING_TEST_TOKEN,
4451
),
4552
(
4653
[
4754
("IS_IN_DATABRICKS_MODEL_SERVING_ENV", "true"),
4855
("DATABRICKS_MODEL_SERVING_HOST_URL", "x"),
4956
],
5057
["DB_MODEL_SERVING_HOST_URL"],
51-
"tests/testdata/model-serving-test-token",
58+
_MODEL_SERVING_TEST_TOKEN,
5259
),
5360
],
5461
)
@@ -92,7 +99,7 @@ def test_model_serving_auth(env_values, del_env_values, oauth_file_name, monkeyp
9299
), # In Model Serving and Invalid File Name
93100
(
94101
[],
95-
"tests/testdata/model-serving-test-token",
102+
_MODEL_SERVING_TEST_TOKEN,
96103
), # Not in Model Serving and Valid File Name
97104
],
98105
)
@@ -122,7 +129,7 @@ def test_model_serving_auth_refresh(monkeypatch, mocker):
122129
# patch mlflow to read the file from the test directory
123130
monkeypatch.setattr(
124131
"databricks.sdk.credentials_provider.ModelServingAuthProvider._MODEL_DEPENDENCY_OAUTH_TOKEN_FILE_PATH",
125-
"tests/testdata/model-serving-test-token",
132+
_MODEL_SERVING_TEST_TOKEN,
126133
)
127134
mocker.patch("databricks.sdk.config.Config._known_file_config_loader")
128135

@@ -136,7 +143,7 @@ def test_model_serving_auth_refresh(monkeypatch, mocker):
136143
# Simulate refreshing the token by patching to to a new file
137144
monkeypatch.setattr(
138145
"databricks.sdk.credentials_provider.ModelServingAuthProvider._MODEL_DEPENDENCY_OAUTH_TOKEN_FILE_PATH",
139-
"tests/testdata/model-serving-test-token-v2",
146+
_MODEL_SERVING_TEST_TOKEN_V2,
140147
)
141148

142149
monkeypatch.setattr(
@@ -172,7 +179,7 @@ def test_agent_user_credentials(monkeypatch, mocker):
172179
monkeypatch.setenv("DB_MODEL_SERVING_HOST_URL", "x")
173180
monkeypatch.setattr(
174181
"databricks.sdk.credentials_provider.ModelServingAuthProvider._MODEL_DEPENDENCY_OAUTH_TOKEN_FILE_PATH",
175-
"tests/testdata/model-serving-test-token",
182+
_MODEL_SERVING_TEST_TOKEN,
176183
)
177184

178185
invokers_token_val = "databricks_invokers_token"

0 commit comments

Comments
 (0)