Skip to content

Commit 8e2cdbf

Browse files
committed
LITE-34131: Fix get_descriptor() to use inspect.getsourcefile()
Instead of broken importlib.resources.files() for non-package modules
1 parent a1a9d3c commit 8e2cdbf

3 files changed

Lines changed: 49 additions & 26 deletions

File tree

connect/eaas/core/extension.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import inspect
33
import json
44
import os
5-
from importlib.resources import files
65
from typing import Dict, List, Union
76

87
import anvil.server
@@ -49,9 +48,15 @@ def get_descriptor(cls) -> dict:
4948
}
5049
```
5150
"""
52-
return json.load(
53-
files(cls.__module__).joinpath('extension.json').open('r'),
51+
source_file = inspect.getsourcefile(cls)
52+
if source_file is None:
53+
source_file = inspect.getfile(cls)
54+
descriptor_path = os.path.join(
55+
os.path.dirname(source_file),
56+
'extension.json',
5457
)
58+
with open(descriptor_path, 'r') as f:
59+
return json.load(f)
5560

5661
@classmethod
5762
def get_variables(cls) -> dict:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "Test Extension",
3+
"description": "A test extension",
4+
"version": "1.0.0",
5+
"audience": ["vendor"]
6+
}

tests/connect/eaas/core/test_extension.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,56 +132,68 @@ async def schedulable2(self, request):
132132
assert MyExtension(None, None, None).schedulable1.__doc__ == 'This is schedulable'
133133

134134

135-
def test_get_descriptor(tmp_path, monkeypatch):
136-
pkg_dir = tmp_path / 'fake_ext'
137-
pkg_dir.mkdir()
138-
(pkg_dir / '__init__.py').write_text('')
139-
descriptor = {
135+
def test_get_descriptor():
136+
# Class defined here means getsourcefile() returns this test file's path,
137+
# so it looks for extension.json in this same directory
138+
class MyExtension(EventsApplicationBase):
139+
pass
140+
141+
assert MyExtension.get_descriptor() == {
140142
'name': 'Test Extension',
141143
'description': 'A test extension',
142144
'version': '1.0.0',
143145
'audience': ['vendor'],
144146
}
145-
(pkg_dir / 'extension.json').write_text(json.dumps(descriptor))
146147

147-
monkeypatch.syspath_prepend(str(tmp_path))
148+
149+
def test_get_descriptor_getsourcefile_returns_none(tmp_path, mocker):
150+
ext_dir = tmp_path / 'my_ext'
151+
ext_dir.mkdir()
152+
descriptor = {
153+
'name': 'Test Extension',
154+
'description': 'A test extension',
155+
'version': '1.0.0',
156+
'audience': ['vendor'],
157+
}
158+
(ext_dir / 'extension.json').write_text(json.dumps(descriptor))
148159

149160
class MyExtension(EventsApplicationBase):
150161
pass
151162

152-
MyExtension.__module__ = 'fake_ext'
163+
mocker.patch('inspect.getsourcefile', return_value=None)
164+
mocker.patch('inspect.getfile', return_value=str(ext_dir / 'webapp.pyc'))
153165

154166
assert MyExtension.get_descriptor() == descriptor
155167

156168

157-
def test_get_descriptor_file_not_found(tmp_path, monkeypatch):
158-
pkg_dir = tmp_path / 'fake_ext_no_json'
159-
pkg_dir.mkdir()
160-
(pkg_dir / '__init__.py').write_text('')
161-
162-
monkeypatch.syspath_prepend(str(tmp_path))
169+
def test_get_descriptor_file_not_found(tmp_path, mocker):
170+
ext_dir = tmp_path / 'my_ext_no_json'
171+
ext_dir.mkdir()
163172

164173
class MyExtension(EventsApplicationBase):
165174
pass
166175

167-
MyExtension.__module__ = 'fake_ext_no_json'
176+
mocker.patch(
177+
'inspect.getsourcefile',
178+
return_value=str(ext_dir / 'webapp.py'),
179+
)
168180

169181
with pytest.raises(FileNotFoundError):
170182
MyExtension.get_descriptor()
171183

172184

173-
def test_get_descriptor_invalid_json(tmp_path, monkeypatch):
174-
pkg_dir = tmp_path / 'fake_ext_bad_json'
175-
pkg_dir.mkdir()
176-
(pkg_dir / '__init__.py').write_text('')
177-
(pkg_dir / 'extension.json').write_text('not valid json{')
178-
179-
monkeypatch.syspath_prepend(str(tmp_path))
185+
def test_get_descriptor_invalid_json(tmp_path, mocker):
186+
ext_dir = tmp_path / 'my_ext_bad_json'
187+
ext_dir.mkdir()
188+
(ext_dir / 'extension.json').write_text('not valid json{')
180189

181190
class MyExtension(EventsApplicationBase):
182191
pass
183192

184-
MyExtension.__module__ = 'fake_ext_bad_json'
193+
mocker.patch(
194+
'inspect.getsourcefile',
195+
return_value=str(ext_dir / 'webapp.py'),
196+
)
185197

186198
with pytest.raises(json.JSONDecodeError):
187199
MyExtension.get_descriptor()

0 commit comments

Comments
 (0)