~ubuntu-branches/ubuntu/karmic/python3.0/karmic

« back to all changes in this revision

Viewing changes to Lib/test/test_cmd_line_script.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-16 17:18:23 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090216171823-1d5cm5qnnjvmnzzm
Tags: 3.0.1-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
        compiled_name = script_name + 'o'
76
76
    return compiled_name
77
77
 
78
 
def _make_test_zip(zip_dir, zip_basename, script_name):
 
78
def _make_test_zip(zip_dir, zip_basename, script_name, name_in_zip=None):
79
79
    zip_filename = zip_basename+os.path.extsep+"zip"
80
80
    zip_name = os.path.join(zip_dir, zip_filename)
81
81
    zip_file = zipfile.ZipFile(zip_name, 'w')
82
 
    zip_file.write(script_name, os.path.basename(script_name))
 
82
    if name_in_zip is None:
 
83
        name_in_zip = os.path.basename(script_name)
 
84
    zip_file.write(script_name, name_in_zip)
83
85
    zip_file.close()
84
 
    # if verbose:
 
86
    #if verbose:
85
87
    #    zip_file = zipfile.ZipFile(zip_name, 'r')
86
88
    #    print("Contents of %r:" % zip_name)
87
89
    #    zip_file.printdir()
88
90
    #    zip_file.close()
89
 
    return zip_name
 
91
    return zip_name, os.path.join(zip_name, name_in_zip)
90
92
 
91
93
def _make_test_pkg(pkg_dir):
92
94
    os.mkdir(pkg_dir)
93
95
    _make_test_script(pkg_dir, '__init__', '')
94
96
 
 
97
def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
 
98
                       source=test_source, depth=1):
 
99
    init_name = _make_test_script(zip_dir, '__init__', '')
 
100
    init_basename = os.path.basename(init_name)
 
101
    script_name = _make_test_script(zip_dir, script_basename, source)
 
102
    pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
 
103
    script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
 
104
    zip_filename = zip_basename+os.extsep+'zip'
 
105
    zip_name = os.path.join(zip_dir, zip_filename)
 
106
    zip_file = zipfile.ZipFile(zip_name, 'w')
 
107
    for name in pkg_names:
 
108
        init_name_in_zip = os.path.join(name, init_basename)
 
109
        zip_file.write(init_name, init_name_in_zip)
 
110
    zip_file.write(script_name, script_name_in_zip)
 
111
    zip_file.close()
 
112
    os.unlink(init_name)
 
113
    os.unlink(script_name)
 
114
    #if verbose:
 
115
    #    zip_file = zipfile.ZipFile(zip_name, 'r')
 
116
    #    print 'Contents of %r:' % zip_name
 
117
    #    zip_file.printdir()
 
118
    #    zip_file.close()
 
119
    return zip_name, os.path.join(zip_name, script_name_in_zip)
 
120
 
95
121
# There's no easy way to pass the script directory in to get
96
122
# -m to work (avoiding that is the whole point of making
97
123
# directories and zipfiles executable!)
98
124
# So we fake it for testing purposes with a custom launch script
99
125
launch_source = """\
100
126
import sys, os.path, runpy
101
 
sys.path[0:0] = os.path.dirname(__file__)
 
127
sys.path.insert(0, %s)
102
128
runpy._run_module_as_main(%r)
103
129
"""
104
130
 
105
 
def _make_launch_script(script_dir, script_basename, module_name):
106
 
    return _make_test_script(script_dir, script_basename,
107
 
                             launch_source % module_name)
 
131
def _make_launch_script(script_dir, script_basename, module_name, path=None):
 
132
    if path is None:
 
133
        path = "os.path.dirname(__file__)"
 
134
    else:
 
135
        path = repr(path)
 
136
    source = launch_source % (path, module_name)
 
137
    return _make_test_script(script_dir, script_basename, source)
108
138
 
109
139
class CmdLineTest(unittest.TestCase):
110
140
    def _check_script(self, script_name, expected_file,
155
185
    def test_zipfile(self):
156
186
        with temp_dir() as script_dir:
157
187
            script_name = _make_test_script(script_dir, '__main__')
158
 
            zip_name = _make_test_zip(script_dir, 'test_zip', script_name)
159
 
            self._check_script(zip_name, None, zip_name, '')
 
188
            zip_name, run_name = _make_test_zip(script_dir, 'test_zip', script_name)
 
189
            self._check_script(zip_name, run_name, zip_name, '')
160
190
 
161
191
    def test_zipfile_compiled(self):
162
192
        with temp_dir() as script_dir:
163
193
            script_name = _make_test_script(script_dir, '__main__')
164
194
            compiled_name = _compile_test_script(script_name)
165
 
            zip_name = _make_test_zip(script_dir, 'test_zip', compiled_name)
166
 
            self._check_script(zip_name, None, zip_name, '')
 
195
            zip_name, run_name = _make_test_zip(script_dir, 'test_zip', compiled_name)
 
196
            self._check_script(zip_name, run_name, zip_name, '')
167
197
 
168
198
    def test_module_in_package(self):
169
199
        with temp_dir() as script_dir:
171
201
            _make_test_pkg(pkg_dir)
172
202
            script_name = _make_test_script(pkg_dir, 'script')
173
203
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script')
174
 
            self._check_script(launch_name, script_name,
175
 
                               script_name, 'test_pkg')
 
204
            self._check_script(launch_name, script_name, script_name, 'test_pkg')
 
205
 
 
206
    def test_module_in_package_in_zipfile(self):
 
207
        with temp_dir() as script_dir:
 
208
            zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script')
 
209
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name)
 
210
            self._check_script(launch_name, run_name, run_name, 'test_pkg')
 
211
 
 
212
    def test_module_in_subpackage_in_zipfile(self):
 
213
        with temp_dir() as script_dir:
 
214
            zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2)
 
215
            launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name)
 
216
            self._check_script(launch_name, run_name, run_name, 'test_pkg.test_pkg')
176
217
 
177
218
 
178
219
def test_main():