@@ -158,6 +158,16 @@ def _check_script(self, script_name, expected_file,
158
158
self .assert_ (printed_package in data )
159
159
self .assert_ (printed_argv0 in data )
160
160
161
+ def _check_import_error (self , script_name , expected_msg ,
162
+ * cmd_line_switches ):
163
+ run_args = cmd_line_switches + (script_name ,)
164
+ exit_code , data = _run_python (* run_args )
165
+ if verbose :
166
+ print 'Output from test script %r:' % script_name
167
+ print data
168
+ print 'Expected output: %r' % expected_msg
169
+ self .assert_ (expected_msg in data )
170
+
161
171
def test_basic_script (self ):
162
172
with temp_dir () as script_dir :
163
173
script_name = _make_test_script (script_dir , 'script' )
@@ -182,6 +192,11 @@ def test_directory_compiled(self):
182
192
os .remove (script_name )
183
193
self ._check_script (script_dir , compiled_name , script_dir , '' )
184
194
195
+ def test_directory_error (self ):
196
+ with temp_dir () as script_dir :
197
+ msg = "can't find '__main__.py' in %r" % script_dir
198
+ self ._check_import_error (script_dir , msg )
199
+
185
200
def test_zipfile (self ):
186
201
with temp_dir () as script_dir :
187
202
script_name = _make_test_script (script_dir , '__main__' )
@@ -195,6 +210,13 @@ def test_zipfile_compiled(self):
195
210
zip_name , run_name = _make_test_zip (script_dir , 'test_zip' , compiled_name )
196
211
self ._check_script (zip_name , run_name , zip_name , '' )
197
212
213
+ def test_zipfile_error (self ):
214
+ with temp_dir () as script_dir :
215
+ script_name = _make_test_script (script_dir , 'not_main' )
216
+ zip_name , run_name = _make_test_zip (script_dir , 'test_zip' , script_name )
217
+ msg = "can't find '__main__.py' in %r" % zip_name
218
+ self ._check_import_error (zip_name , msg )
219
+
198
220
def test_module_in_package (self ):
199
221
with temp_dir () as script_dir :
200
222
pkg_dir = os .path .join (script_dir , 'test_pkg' )
@@ -215,6 +237,47 @@ def test_module_in_subpackage_in_zipfile(self):
215
237
launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg.test_pkg.script' , zip_name )
216
238
self ._check_script (launch_name , run_name , run_name , 'test_pkg.test_pkg' )
217
239
240
+ def test_package (self ):
241
+ with temp_dir () as script_dir :
242
+ pkg_dir = os .path .join (script_dir , 'test_pkg' )
243
+ _make_test_pkg (pkg_dir )
244
+ script_name = _make_test_script (pkg_dir , '__main__' )
245
+ launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg' )
246
+ self ._check_script (launch_name , script_name ,
247
+ script_name , 'test_pkg' )
248
+
249
+ def test_package_compiled (self ):
250
+ with temp_dir () as script_dir :
251
+ pkg_dir = os .path .join (script_dir , 'test_pkg' )
252
+ _make_test_pkg (pkg_dir )
253
+ script_name = _make_test_script (pkg_dir , '__main__' )
254
+ compiled_name = _compile_test_script (script_name )
255
+ os .remove (script_name )
256
+ launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg' )
257
+ self ._check_script (launch_name , compiled_name ,
258
+ compiled_name , 'test_pkg' )
259
+
260
+ def test_package_error (self ):
261
+ with temp_dir () as script_dir :
262
+ pkg_dir = os .path .join (script_dir , 'test_pkg' )
263
+ _make_test_pkg (pkg_dir )
264
+ msg = ("'test_pkg' is a package and cannot "
265
+ "be directly executed" )
266
+ launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg' )
267
+ self ._check_import_error (launch_name , msg )
268
+
269
+ def test_package_recursion (self ):
270
+ with temp_dir () as script_dir :
271
+ pkg_dir = os .path .join (script_dir , 'test_pkg' )
272
+ _make_test_pkg (pkg_dir )
273
+ main_dir = os .path .join (pkg_dir , '__main__' )
274
+ _make_test_pkg (main_dir )
275
+ msg = ("Cannot use package as __main__ module; "
276
+ "'test_pkg' is a package and cannot "
277
+ "be directly executed" )
278
+ launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg' )
279
+ self ._check_import_error (launch_name , msg )
280
+
218
281
219
282
def test_main ():
220
283
test .test_support .run_unittest (CmdLineTest )
0 commit comments