File: test_bat_jy.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (411 lines) | stat: -rw-r--r-- 14,046 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
'''Tests jython.bat using the --print option'''

import os
import sys
import unittest
import tempfile

from test import test_support

from java.lang import IllegalThreadStateException
from java.lang import Runtime
from java.lang import System
from java.lang import Thread
from java.io import File
from java.io import BufferedReader;
from java.io import InputStreamReader;

class Monitor(Thread):
    def __init__(self, process):
        self.process = process
        self.output = ''

    def run(self):
        reader = BufferedReader(InputStreamReader(self.getStream()))
        try:
            line = reader.readLine()
            while line:
                self.output += line
                line = reader.readLine()
        finally:
            reader.close()
    
    def getOutput(self):
        return self.output

class StdoutMonitor(Monitor):
    def __init_(self, process):
        Monitor.__init__(self, process)

    def getStream(self):
        return self.process.getInputStream()

class StderrMonitor(Monitor):
    def __init_(self, process):
        Monitor.__init__(self, process)

    def getStream(self):
        return self.process.getErrorStream()

class StarterProcess:
    def writeStarter(self, args, javaHome, jythonHome, jythonOpts, internals=False):
        (starter, starterPath) = tempfile.mkstemp(suffix='.bat', prefix='starter', text=True)
        starter.close()
        outfilePath = starterPath[:-4] + '.out'
        starter = open(starterPath, 'w') # open starter as simple file
        try:
            if javaHome:
                starter.write('set JAVA_HOME=%s\n' % javaHome)
            if jythonHome:
                starter.write('set JYTHON_HOME=%s\n' % jythonHome)
            if jythonOpts:
                starter.write('set JYTHON_OPTS=%s\n' % jythonOpts)
            if internals:
                starter.write('set _JYTHON_OPTS=leaking_internals\n')
                starter.write('set _JYTHON_HOME=c:/leaking/internals\n')
            starter.write(self.buildCommand(args, outfilePath))
            return (starterPath, outfilePath)
        finally:
            starter.close()

    def buildCommand(self, args, outfilePath):
        line = ''
        for arg in args:
            line += arg
            line += ' '
        line += '> '
        line += outfilePath
        line += ' 2>&1'
        return line

    def getOutput(self, outfilePath):
        lines = ''
        outfile = open(outfilePath, 'r')
        try:
            for line in outfile.readlines():
                lines += line
        finally:
            outfile.close()
        return lines

    def isAlive(self, process):
        try:
            process.exitValue()
            return False
        except IllegalThreadStateException:
            return True

    def run(self, args, javaHome, jythonHome, jythonOpts, internals=False):
        ''' creates a start script, executes it and captures the output '''
        (starterPath, outfilePath) = self.writeStarter(args, javaHome, jythonHome, jythonOpts, internals)
        try:
            process = Runtime.getRuntime().exec(starterPath)
            stdoutMonitor = StdoutMonitor(process)
            stderrMonitor = StderrMonitor(process)
            stdoutMonitor.start()
            stderrMonitor.start()
            while self.isAlive(process):
                Thread.sleep(300)
            return self.getOutput(outfilePath)
        finally:
            os.remove(starterPath)
            os.remove(outfilePath)

class BaseTest(unittest.TestCase):
    def quote(self, s):
        return '"' + s + '"'

    def unquote(self, s):
        if len(s) > 0:
            if s[:1] == '"':
                s = s[1:]
        if len(s) > 0:
            if s[-1:] == '"':
                s = s[:-1]
        return s

    def getHomeDir(self):
        ex = sys.executable
        tail = ex[-15:]
        if tail == '\\bin\\jython.bat':
            home = ex[:-15]
        else:
            home = ex[:-11] # \jython.bat
        return home

    def assertOutput(self, flags=None, javaHome=None, jythonHome=None, jythonOpts=None, internals=False):
        args = [self.quote(sys.executable), '--print']
        memory = None
        stack = None
        prop = None
        jythonArgs = None
        boot = False
        jdb = False
        if flags:
            for flag in flags:
                if flag[:2] == '-J':
                    if flag[2:6] == '-Xmx':
                        memory = flag[6:]
                    elif flag[2:6] == '-Xss':
                        stack = flag[6:]
                    elif flag[2:4] == '-D':
                        prop = flag[2:]
                elif flag[:2] == '--':
                    if flag[2:6] == 'boot':
                        boot = True
                    elif flag[2:5] == 'jdb':
                        jdb = True
                else:
                    if jythonArgs:
                        jythonArgs += ' '
                        jythonArgs += flag
                    else:
                        jythonArgs = flag
                    jythonArgs = jythonArgs.replace('%%', '%') # workaround two .bat files
                args.append(flag)
        process = StarterProcess()
        out = process.run(args, javaHome, jythonHome, jythonOpts, internals)
        self.assertNotEquals('', out)
        homeIdx = out.find('-Dpython.home=')
        java = 'java'
        if javaHome:
            java = self.quote(self.unquote(javaHome) + '\\bin\\java')
        elif jdb:
            java = 'jdb'
        if not memory:
            memory = '512m'
        if not stack:
            stack = '1152k'
        beginning = java + ' '
        if prop:
            beginning += ' ' + prop
        beginning += ' -Xmx' + memory + ' -Xss' + stack + ' '
        self.assertEquals(beginning, out[:homeIdx])
        executableIdx = out.find('-Dpython.executable=')
        homeDir = self.getHomeDir()
        if jythonHome:
            homeDir = self.unquote(jythonHome)
        home = '-Dpython.home=' + self.quote(homeDir) + ' '
        self.assertEquals(home, out[homeIdx:executableIdx])
        if boot:
            classpathFlag = '-Xbootclasspath/a:'
        else:
            classpathFlag = '-classpath'
        classpathIdx = out.find(classpathFlag)
        executable = '-Dpython.executable=' + self.quote(sys.executable) + ' '
        if not boot:
            executable += ' '
        self.assertEquals(executable, out[executableIdx:classpathIdx])
        # ignore full contents of classpath at the moment
        classIdx = out.find('org.python.util.jython')
        self.assertTrue(classIdx > classpathIdx)
        restIdx = classIdx + len('org.python.util.jython')
        rest = out[restIdx:].strip()
        if jythonOpts:
            self.assertEquals(self.quote(jythonOpts), rest)
        else:
            if jythonArgs:
                self.assertEquals(jythonArgs, rest)
            else:
                self.assertEquals('', rest)

class VanillaTest(BaseTest):
    def test_plain(self):
        self.assertOutput()

class JavaHomeTest(BaseTest):
    def test_unquoted(self):
        # for the build bot, try to specify a real java home
        javaHome = System.getProperty('java.home', 'C:\\Program Files\\Java\\someJava')
        self.assertOutput(javaHome=javaHome)

    def test_quoted(self):
        self.assertOutput(javaHome=self.quote('C:\\Program Files\\Java\\someJava'))

    # this currently fails, meaning we accept only quoted (x86) homes ...
    def __test_x86_unquoted(self):
        self.assertOutput(javaHome='C:\\Program Files (x86)\\Java\\someJava')

    def test_x86_quoted(self):
        self.assertOutput(javaHome=self.quote('C:\\Program Files (x86)\\Java\\someJava'))
        
class JythonHomeTest(BaseTest):
    def createJythonJar(self, parentDir):
        jar = File(parentDir, 'jython.jar')
        if not jar.exists():
            self.assertTrue(jar.createNewFile())
        return jar

    def cleanup(self, tmpdir, jar=None):
        if jar and jar.exists():
            self.assertTrue(jar.delete())
        os.rmdir(tmpdir)

    def test_unquoted(self):
        jythonHomeDir = tempfile.mkdtemp()
        jar = self.createJythonJar(jythonHomeDir)
        self.assertOutput(jythonHome=jythonHomeDir)
        self.cleanup(jythonHomeDir, jar)

    def test_quoted(self):
        jythonHomeDir = tempfile.mkdtemp()
        jar = self.createJythonJar(jythonHomeDir)
        self.assertOutput(jythonHome=self.quote(jythonHomeDir))
        self.cleanup(jythonHomeDir, jar)

class JythonOptsTest(BaseTest):
    def test_single(self):
        self.assertOutput(jythonOpts='myOpt')
        
    def test_multiple(self):
        self.assertOutput(jythonOpts='some arbitrary options')

class InternalsTest(BaseTest):
    def test_no_leaks(self):
        self.assertOutput(internals=True)

class JavaOptsTest(BaseTest):
    def test_memory(self):
        self.assertOutput(['-J-Xmx321m'])

    def test_stack(self):
        self.assertOutput(['-J-Xss321k'])

    def test_property(self):
        self.assertOutput(['-J-DmyProperty=myValue'])

    def test_property_singlequote(self):
        self.assertOutput(["-J-DmyProperty='myValue'"]) 

    # a space inside value does not work in jython.bat
    def __test_property_singlequote_space(self):
        self.assertOutput(["-J-DmyProperty='my Value'"])

    def test_property_doublequote(self):
        self.assertOutput(['-J-DmyProperty="myValue"']) 

    # a space inside value does not work in jython.bat
    def __test_property_doublequote_space(self):
        self.assertOutput(['-J-DmyProperty="my Value"'])

    def test_property_underscore(self):
        self.assertOutput(['-J-Dmy_Property=my_Value'])

class ArgsTest(BaseTest):
    def test_file(self):
        self.assertOutput(['test.py'])
    
    def test_dash(self):
        self.assertOutput(['-i'])

    def test_combined(self):
        self.assertOutput(['-W', 'action', 'line'])

    def test_singlequoted(self):
        self.assertOutput(['-c', "'import sys;'"])

    def test_doublequoted(self):
        self.assertOutput(['-c', '"print \'something\'"'])

    def test_nestedquotes(self):
        self.assertOutput(['-c', '"print \'something \"really\" cool\'"'])

    def test_nestedquotes2(self):
        self.assertOutput(['-c', "'print \"something \'really\' cool\"'"])

    def test_underscored(self):
        self.assertOutput(['-jar', 'my_stuff.jar'])
    
    def test_property(self):
        self.assertOutput(['-DmyProperty=myValue'])

    def test_property_underscored(self):
        self.assertOutput(['-DmyProperty=my_Value'])

    def test_property_singlequoted(self):
        self.assertOutput(["-DmyProperty='my_Value'"])

    def test_property_doublequoted(self):
        self.assertOutput(['-DmyProperty="my_Value"'])

class DoubleDashTest(BaseTest):
    def test_boot(self):
        self.assertOutput(['--boot'])

    def test_jdb(self):
        self.assertOutput(['--jdb'])

class GlobPatternTest(BaseTest):
    def test_star_nonexisting(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', '*.nonexisting', '*.nonexisting'])

    def test_star_nonexisting_doublequoted(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', '"*.nonexisting"', '"*.nonexisting"'])

    def test_star_nonexistingfile_singlequoted(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', "'*.nonexisting'", "'*.nonexisting'"])

    def test_star_existing(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', '*.bat', '*.bat'])

    def test_star_existing_doublequoted(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', '"*.bat"', '"*.bat"'])

    def test_star_existing_singlequoted(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', "'*.bat'", "'*.bat'"])

class ArgsSpacesTest(BaseTest):
    def test_doublequoted(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', '"part1 part2"', '2nd'])

    def test_singlequoted(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', "'part1 part2'", '2nd'])

    # this test currently fails
    def __test_unbalanced_doublequote(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', 'Scarlet O"Hara', '2nd'])

    def test_unbalanced_singlequote(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', "Scarlet O'Hara", '2nd'])

class ArgsSpecialCharsTest(BaseTest):
    # exclamation marks are still very special ...
    def __test_exclamationmark(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', 'foo!', 'ba!r', '!baz', '!'])

    # because we go through a starter.bat file, we have to simulate % with %%
    def test_percentsign(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', 'foo%%1', '%%1bar', '%%1', '%%'])

    def test_colon(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', 'foo:', ':bar'])

    # a semicolon at the beginning of an arg currently fails (e.g. ;bar)
    def test_semicolon(self):
        self.assertOutput(['-c', 'import sys; print sys.argv[1:]', 'foo;'])

class DummyTest(unittest.TestCase):
    def test_nothing(self):
        pass

def test_main():
    if os._name == 'nt':
        test_support.run_unittest(VanillaTest,
                                  JavaHomeTest,
                                  JythonHomeTest,
                                  JythonOptsTest,
                                  InternalsTest,
                                  JavaOptsTest,
                                  ArgsTest,
                                  DoubleDashTest,
                                  GlobPatternTest,
                                  ArgsSpacesTest,
                                  ArgsSpecialCharsTest)
    else:
        # provide at least one test for the other platforms - happier build bots
        test_support.run_unittest(DummyTest)


if __name__ == '__main__':
    test_main()