@@ -158,6 +158,16 @@ def _check_script(self, script_name, expected_file,
158158 self .assert_ (printed_package in data )
159159 self .assert_ (printed_argv0 in data )
160160
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+
161171 def test_basic_script (self ):
162172 with temp_dir () as script_dir :
163173 script_name = _make_test_script (script_dir , 'script' )
@@ -182,6 +192,11 @@ def test_directory_compiled(self):
182192 os .remove (script_name )
183193 self ._check_script (script_dir , compiled_name , script_dir , '' )
184194
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+
185200 def test_zipfile (self ):
186201 with temp_dir () as script_dir :
187202 script_name = _make_test_script (script_dir , '__main__' )
@@ -195,6 +210,13 @@ def test_zipfile_compiled(self):
195210 zip_name , run_name = _make_test_zip (script_dir , 'test_zip' , compiled_name )
196211 self ._check_script (zip_name , run_name , zip_name , '' )
197212
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+
198220 def test_module_in_package (self ):
199221 with temp_dir () as script_dir :
200222 pkg_dir = os .path .join (script_dir , 'test_pkg' )
@@ -215,6 +237,47 @@ def test_module_in_subpackage_in_zipfile(self):
215237 launch_name = _make_launch_script (script_dir , 'launch' , 'test_pkg.test_pkg.script' , zip_name )
216238 self ._check_script (launch_name , run_name , run_name , 'test_pkg.test_pkg' )
217239
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+
218281
219282def test_main ():
220283 test .test_support .run_unittest (CmdLineTest )
0 commit comments