Merge remote-tracking branch 'origin/master'
[idea/community.git] / python / helpers / pydev / tests_pydevd_python / test_pydev_monkey.py
1 import sys
2 import os
3 import unittest
4 try:
5     from _pydev_bundle import pydev_monkey
6 except:
7     sys.path.append(os.path.dirname(os.path.dirname(__file__)))
8     from _pydev_bundle import pydev_monkey
9 from pydevd import SetupHolder
10 from _pydev_bundle.pydev_monkey import pydev_src_dir
11
12
13
14 class TestCase(unittest.TestCase):
15
16     def test_monkey(self):
17         original = SetupHolder.setup
18
19         try:
20             SetupHolder.setup = {'client':'127.0.0.1', 'port': '0'}
21             check='''C:\\bin\\python.exe -u -c connect(\\"127.0.0.1\\")'''
22             sys.original_argv = []
23             self.assertEqual(
24                 'C:\\bin\\python.exe -u -c import sys; '
25                 'sys.path.append(r\'%s\'); '
26                 'import pydevd; pydevd.settrace(host=\'127.0.0.1\', port=0, suspend=False, '
27                     'trace_only_current_thread=False, patch_multiprocessing=True); '
28                     'sys.original_argv = []; '
29                     'connect("127.0.0.1")' % pydev_src_dir,
30                 pydev_monkey.patch_arg_str_win(check)
31             )
32         finally:
33             SetupHolder.setup = original
34
35     def test_str_to_args_windows(self):
36         self.assertEqual(['a', 'b'], pydev_monkey.str_to_args_windows('a "b"'))
37
38     def test_monkey_patch_args_indc(self):
39         original = SetupHolder.setup
40
41         try:
42             SetupHolder.setup = {'client':'127.0.0.1', 'port': '0'}
43             check=['C:\\bin\\python.exe', '-u', '-c', 'connect(\\"127.0.0.1\\")']
44             sys.original_argv = []
45             self.assertEqual(pydev_monkey.patch_args(check), [
46                 'C:\\bin\\python.exe',
47                 '-u',
48                 '-c',
49                 (
50                     'import sys; sys.path.append(r\'%s\'); import pydevd; '
51                     'pydevd.settrace(host=\'127.0.0.1\', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True); '
52                     'sys.original_argv = []; '
53                     'connect(\\"127.0.0.1\\")'
54                 ) % pydev_src_dir
55             ])
56         finally:
57             SetupHolder.setup = original
58
59     def test_monkey_patch_args_module(self):
60         original = SetupHolder.setup
61
62         try:
63             SetupHolder.setup = {'client':'127.0.0.1', 'port': '0'}
64             check=['C:\\bin\\python.exe', '-m', 'test']
65             sys.original_argv = ['pydevd', '--multiprocess']
66             if sys.platform == 'win32':
67                 self.assertEqual(pydev_monkey.patch_args(check), [
68                     'C:\\bin\\python.exe',
69                     '"pydevd"',
70                     '"--module"',
71                     '"--multiprocess"',
72                     'test',
73                 ])
74             else:
75                 self.assertEqual(pydev_monkey.patch_args(check), [
76                     'C:\\bin\\python.exe',
77                     'pydevd',
78                     '--module',
79                     '--multiprocess',
80                     'test',
81                 ])
82         finally:
83             SetupHolder.setup = original
84
85     def test_monkey_patch_args_no_indc(self):
86         original = SetupHolder.setup
87
88         try:
89             SetupHolder.setup = {'client':'127.0.0.1', 'port': '0'}
90             check=['C:\\bin\\python.exe', 'connect(\\"127.0.0.1\\")']
91             sys.original_argv = ['my', 'original', 'argv']
92             if sys.platform == 'win32':
93                 self.assertEqual(pydev_monkey.patch_args(check), [
94                     'C:\\bin\\python.exe', '"my"', '"original"', '"argv"', 'connect(\\"127.0.0.1\\")'])
95             else:
96                 self.assertEqual(pydev_monkey.patch_args(check), [
97                     'C:\\bin\\python.exe', 'my', 'original', 'argv', 'connect(\\"127.0.0.1\\")'])
98         finally:
99             SetupHolder.setup = original
100
101     def test_monkey_patch_args_no_indc_with_pydevd(self):
102         original = SetupHolder.setup
103
104         try:
105             SetupHolder.setup = {'client':'127.0.0.1', 'port': '0'}
106             check=['C:\\bin\\python.exe', 'pydevd.py', 'connect(\\"127.0.0.1\\")', 'bar']
107             sys.original_argv = ['my', 'original', 'argv']
108
109             self.assertEqual(pydev_monkey.patch_args(check), [
110                 'C:\\bin\\python.exe', 'pydevd.py', 'connect(\\"127.0.0.1\\")', 'bar'])
111         finally:
112             SetupHolder.setup = original
113
114     def test_monkey_patch_args_no_indc_without_pydevd(self):
115         original = SetupHolder.setup
116
117         try:
118             SetupHolder.setup = {'client':'127.0.0.1', 'port': '0'}
119             check=['C:\\bin\\python.exe', 'target.py', 'connect(\\"127.0.0.1\\")', 'bar']
120             sys.original_argv = ['pydevd.py', '--a=1', 'b', '--c=2', '--file', 'ignore_this.py']
121
122             if sys.platform == 'win32':
123                 self.assertEqual(pydev_monkey.patch_args(check), [
124                     'C:\\bin\\python.exe',
125                     '"pydevd.py"',
126                     '"--a=1"',
127                     '"b"',
128                     '"--c=2"',
129                     '"--file"',
130                     'target.py',
131                     'connect(\\"127.0.0.1\\")',
132                     'bar',
133                 ])
134             else:
135                 self.assertEqual(pydev_monkey.patch_args(check), [
136                     'C:\\bin\\python.exe',
137                     'pydevd.py',
138                     '--a=1',
139                     'b',
140                     '--c=2',
141                     '--file',
142                     'target.py',
143                     'connect(\\"127.0.0.1\\")',
144                     'bar',
145                 ])
146         finally:
147             SetupHolder.setup = original
148
149 if __name__ == '__main__':
150     unittest.main()