Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_cmd_line_script.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 4264cf00a75c |
| children | 5cce99aa1f58 |
| rev | line source |
|---|---|
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1 # Tests command line execution of scripts |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
3 import unittest |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
4 import os |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
5 import os.path |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
6 import sys |
|
43910
54b973f0f55b
Make this work stand-alone, too.
Guido van Rossum <guido@python.org>
parents:
43907
diff
changeset
|
7 import test.test_support |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
8 import tempfile |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
9 import subprocess |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
10 import py_compile |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
11 import contextlib |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
12 import shutil |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
13 import zipfile |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
14 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
15 verbose = test.test_support.verbose |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
16 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
17 # XXX ncoghlan: Should we consider moving these to test_support? |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
18 from test_cmd_line import _spawn_python, _kill_python |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
19 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
20 def _run_python(*args): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
21 if __debug__: |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
22 p = _spawn_python(*args) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
23 else: |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
24 p = _spawn_python('-O', *args) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
25 stdout_data = _kill_python(p) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
26 return p.wait(), stdout_data |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
27 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
28 @contextlib.contextmanager |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
29 def temp_dir(): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
30 dirname = tempfile.mkdtemp() |
|
43911
b6f5781f5c3f
Fix the OSX failures in this test -- they were due to /tmp being a symlink
Guido van Rossum <guido@python.org>
parents:
43910
diff
changeset
|
31 dirname = os.path.realpath(dirname) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
32 try: |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
33 yield dirname |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
34 finally: |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
35 shutil.rmtree(dirname) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
36 |
| 44116 | 37 test_source = """\ |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
38 # Script may be run with optimisation enabled, so don't rely on assert |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
39 # statements being executed |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
40 def assertEqual(lhs, rhs): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
41 if lhs != rhs: |
| 44116 | 42 raise AssertionError('%r != %r' % (lhs, rhs)) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
43 def assertIdentical(lhs, rhs): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
44 if lhs is not rhs: |
| 44116 | 45 raise AssertionError('%r is not %r' % (lhs, rhs)) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
46 # Check basic code execution |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
47 result = ['Top level assignment'] |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
48 def f(): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
49 result.append('Lower level reference') |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
50 f() |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
51 assertEqual(result, ['Top level assignment', 'Lower level reference']) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
52 # Check population of magic variables |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
53 assertEqual(__name__, '__main__') |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
54 print '__file__==%r' % __file__ |
| 44116 | 55 print '__package__==%r' % __package__ |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
56 # Check the sys module |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
57 import sys |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
58 assertIdentical(globals(), sys.modules[__name__].__dict__) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
59 print 'sys.argv[0]==%r' % sys.argv[0] |
| 44116 | 60 """ |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
61 |
| 44116 | 62 def _make_test_script(script_dir, script_basename, source=test_source): |
| 63 script_filename = script_basename+os.extsep+'py' | |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
64 script_name = os.path.join(script_dir, script_filename) |
| 44116 | 65 script_file = open(script_name, 'w') |
| 66 script_file.write(source) | |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
67 script_file.close() |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
68 return script_name |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
69 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
70 def _compile_test_script(script_name): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
71 py_compile.compile(script_name, doraise=True) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
72 if __debug__: |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
73 compiled_name = script_name + 'c' |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
74 else: |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
75 compiled_name = script_name + 'o' |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
76 return compiled_name |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
77 |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
78 def _make_test_zip(zip_dir, zip_basename, script_name, name_in_zip=None): |
| 44116 | 79 zip_filename = zip_basename+os.extsep+'zip' |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
80 zip_name = os.path.join(zip_dir, zip_filename) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
81 zip_file = zipfile.ZipFile(zip_name, 'w') |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
82 if name_in_zip is None: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
83 name_in_zip = os.path.basename(script_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
84 zip_file.write(script_name, name_in_zip) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
85 zip_file.close() |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
86 #if verbose: |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
87 # zip_file = zipfile.ZipFile(zip_name, 'r') |
| 44116 | 88 # print 'Contents of %r:' % zip_name |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
89 # zip_file.printdir() |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
90 # zip_file.close() |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
91 return zip_name, os.path.join(zip_name, name_in_zip) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
92 |
| 44116 | 93 def _make_test_pkg(pkg_dir): |
| 94 os.mkdir(pkg_dir) | |
| 95 _make_test_script(pkg_dir, '__init__', '') | |
| 96 | |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
97 def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
98 source=test_source, depth=1): |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
99 init_name = _make_test_script(zip_dir, '__init__', '') |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
100 init_basename = os.path.basename(init_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
101 script_name = _make_test_script(zip_dir, script_basename, source) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
102 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
103 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
104 zip_filename = zip_basename+os.extsep+'zip' |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
105 zip_name = os.path.join(zip_dir, zip_filename) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
106 zip_file = zipfile.ZipFile(zip_name, 'w') |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
107 for name in pkg_names: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
108 init_name_in_zip = os.path.join(name, init_basename) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
109 zip_file.write(init_name, init_name_in_zip) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
110 zip_file.write(script_name, script_name_in_zip) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
111 zip_file.close() |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
112 os.unlink(init_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
113 os.unlink(script_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
114 #if verbose: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
115 # zip_file = zipfile.ZipFile(zip_name, 'r') |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
116 # print 'Contents of %r:' % zip_name |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
117 # zip_file.printdir() |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
118 # zip_file.close() |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
119 return zip_name, os.path.join(zip_name, script_name_in_zip) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
120 |
| 44116 | 121 # There's no easy way to pass the script directory in to get |
| 122 # -m to work (avoiding that is the whole point of making | |
| 123 # directories and zipfiles executable!) | |
| 124 # So we fake it for testing purposes with a custom launch script | |
| 125 launch_source = """\ | |
| 126 import sys, os.path, runpy | |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
127 sys.path.insert(0, %s) |
| 44116 | 128 runpy._run_module_as_main(%r) |
| 129 """ | |
| 130 | |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
131 def _make_launch_script(script_dir, script_basename, module_name, path=None): |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
132 if path is None: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
133 path = "os.path.dirname(__file__)" |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
134 else: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
135 path = repr(path) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
136 source = launch_source % (path, module_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
137 return _make_test_script(script_dir, script_basename, source) |
| 44116 | 138 |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
139 class CmdLineTest(unittest.TestCase): |
| 44116 | 140 def _check_script(self, script_name, expected_file, |
| 141 expected_argv0, expected_package, | |
| 142 *cmd_line_switches): | |
| 143 run_args = cmd_line_switches + (script_name,) | |
| 144 exit_code, data = _run_python(*run_args) | |
|
43907
780c728406e6
Enable some test_cmd_line_script debugging output to investigate failure on Mac OSX buildbot
Nick Coghlan <ncoghlan@gmail.com>
parents:
43895
diff
changeset
|
145 if verbose: |
| 44116 | 146 print 'Output from test script %r:' % script_name |
|
43907
780c728406e6
Enable some test_cmd_line_script debugging output to investigate failure on Mac OSX buildbot
Nick Coghlan <ncoghlan@gmail.com>
parents:
43895
diff
changeset
|
147 print data |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
148 self.assertEqual(exit_code, 0) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
149 printed_file = '__file__==%r' % expected_file |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
150 printed_argv0 = 'sys.argv[0]==%r' % expected_argv0 |
| 44116 | 151 printed_package = '__package__==%r' % expected_package |
| 152 if verbose: | |
| 153 print 'Expected output:' | |
| 154 print printed_file | |
| 155 print printed_package | |
| 156 print printed_argv0 | |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
157 self.assert_(printed_file in data) |
| 44116 | 158 self.assert_(printed_package in data) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
159 self.assert_(printed_argv0 in data) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
160 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
161 def test_basic_script(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
162 with temp_dir() as script_dir: |
| 44116 | 163 script_name = _make_test_script(script_dir, 'script') |
| 164 self._check_script(script_name, script_name, script_name, None) | |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
165 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
166 def test_script_compiled(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
167 with temp_dir() as script_dir: |
| 44116 | 168 script_name = _make_test_script(script_dir, 'script') |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
169 compiled_name = _compile_test_script(script_name) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
170 os.remove(script_name) |
| 44116 | 171 self._check_script(compiled_name, compiled_name, compiled_name, None) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
172 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
173 def test_directory(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
174 with temp_dir() as script_dir: |
| 44116 | 175 script_name = _make_test_script(script_dir, '__main__') |
| 176 self._check_script(script_dir, script_name, script_dir, '') | |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
177 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
178 def test_directory_compiled(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
179 with temp_dir() as script_dir: |
| 44116 | 180 script_name = _make_test_script(script_dir, '__main__') |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
181 compiled_name = _compile_test_script(script_name) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
182 os.remove(script_name) |
| 44116 | 183 self._check_script(script_dir, compiled_name, script_dir, '') |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
184 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
185 def test_zipfile(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
186 with temp_dir() as script_dir: |
| 44116 | 187 script_name = _make_test_script(script_dir, '__main__') |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
188 zip_name, run_name = _make_test_zip(script_dir, 'test_zip', script_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
189 self._check_script(zip_name, run_name, zip_name, '') |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
190 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
191 def test_zipfile_compiled(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
192 with temp_dir() as script_dir: |
| 44116 | 193 script_name = _make_test_script(script_dir, '__main__') |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
194 compiled_name = _compile_test_script(script_name) |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
195 zip_name, run_name = _make_test_zip(script_dir, 'test_zip', compiled_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
196 self._check_script(zip_name, run_name, zip_name, '') |
| 44116 | 197 |
| 198 def test_module_in_package(self): | |
| 199 with temp_dir() as script_dir: | |
| 200 pkg_dir = os.path.join(script_dir, 'test_pkg') | |
| 201 _make_test_pkg(pkg_dir) | |
| 202 script_name = _make_test_script(pkg_dir, 'script') | |
| 203 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script') | |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
204 self._check_script(launch_name, script_name, script_name, 'test_pkg') |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
205 |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
206 def test_module_in_package_in_zipfile(self): |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
207 with temp_dir() as script_dir: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
208 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script') |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
209 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
210 self._check_script(launch_name, run_name, run_name, 'test_pkg') |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
211 |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
212 def test_module_in_subpackage_in_zipfile(self): |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
213 with temp_dir() as script_dir: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
214 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
215 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
216 self._check_script(launch_name, run_name, run_name, 'test_pkg.test_pkg') |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
217 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
218 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
219 def test_main(): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
220 test.test_support.run_unittest(CmdLineTest) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
221 test.test_support.reap_children() |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
222 |
| 44116 | 223 if __name__ == '__main__': |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
224 test_main() |
