@@ -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