##// END OF EJS Templates
code: format style and py3 compatability
marcink -
r589:bd50ae6f default
parent child Browse files
Show More
@@ -1,122 +1,132 b''
1 # RhodeCode VCSServer provides access to different vcs backends via network.
1 # RhodeCode VCSServer provides access to different vcs backends via network.
2 # Copyright (C) 2014-2018 RhodeCode GmbH
2 # Copyright (C) 2014-2018 RhodeCode GmbH
3 #
3 #
4 # This program is free software; you can redistribute it and/or modify
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
7 # (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software Foundation,
15 # along with this program; if not, write to the Free Software Foundation,
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
18 import io
18 import io
19 import os
19 import os
20 import sys
20 import sys
21
21
22 import pytest
22 import pytest
23
23
24 from vcsserver import subprocessio
24 from vcsserver import subprocessio
25
25
26
26
27 @pytest.fixture(scope='module')
27 @pytest.fixture(scope='module')
28 def environ():
28 def environ():
29 """Delete coverage variables, as they make the tests fail."""
29 """Delete coverage variables, as they make the tests fail."""
30 env = dict(os.environ)
30 env = dict(os.environ)
31 for key in env.keys():
31 for key in env.keys():
32 if key.startswith('COV_CORE_'):
32 if key.startswith('COV_CORE_'):
33 del env[key]
33 del env[key]
34
34
35 return env
35 return env
36
36
37
37
38 def _get_python_args(script):
38 def _get_python_args(script):
39 return [sys.executable, '-c',
39 return [sys.executable, '-c', 'import sys; import time; import shutil; ' + script]
40 'import sys; import time; import shutil; ' + script]
41
40
42
41
43 def test_raise_exception_on_non_zero_return_code(environ):
42 def test_raise_exception_on_non_zero_return_code(environ):
44 args = _get_python_args('sys.exit(1)')
43 args = _get_python_args('sys.exit(1)')
45 with pytest.raises(EnvironmentError):
44 with pytest.raises(EnvironmentError):
46 list(subprocessio.SubprocessIOChunker(args, shell=False, env=environ))
45 list(subprocessio.SubprocessIOChunker(args, shell=False, env=environ))
47
46
48
47
49 def test_does_not_fail_on_non_zero_return_code(environ):
48 def test_does_not_fail_on_non_zero_return_code(environ):
50 args = _get_python_args('sys.exit(1)')
49 args = _get_python_args('sys.exit(1)')
51 output = ''.join(subprocessio.SubprocessIOChunker(
50 output = ''.join(
52 args, shell=False, fail_on_return_code=False, env=environ))
51 subprocessio.SubprocessIOChunker(
52 args, shell=False, fail_on_return_code=False, env=environ
53 )
54 )
53
55
54 assert output == ''
56 assert output == ''
55
57
56
58
57 def test_raise_exception_on_stderr(environ):
59 def test_raise_exception_on_stderr(environ):
58 args = _get_python_args('sys.stderr.write("X"); time.sleep(1);')
60 args = _get_python_args('sys.stderr.write("X"); time.sleep(1);')
59 with pytest.raises(EnvironmentError) as excinfo:
61 with pytest.raises(EnvironmentError) as excinfo:
60 list(subprocessio.SubprocessIOChunker(args, shell=False, env=environ))
62 list(subprocessio.SubprocessIOChunker(args, shell=False, env=environ))
61
63
62 assert 'exited due to an error:\nX' in str(excinfo.value)
64 assert 'exited due to an error:\nX' in str(excinfo.value)
63
65
64
66
65 def test_does_not_fail_on_stderr(environ):
67 def test_does_not_fail_on_stderr(environ):
66 args = _get_python_args('sys.stderr.write("X"); time.sleep(1);')
68 args = _get_python_args('sys.stderr.write("X"); time.sleep(1);')
67 output = ''.join(subprocessio.SubprocessIOChunker(
69 output = ''.join(
68 args, shell=False, fail_on_stderr=False, env=environ))
70 subprocessio.SubprocessIOChunker(
71 args, shell=False, fail_on_stderr=False, env=environ
72 )
73 )
69
74
70 assert output == ''
75 assert output == ''
71
76
72
77
73 @pytest.mark.parametrize('size', [1, 10**5])
78 @pytest.mark.parametrize('size', [1, 10 ** 5])
74 def test_output_with_no_input(size, environ):
79 def test_output_with_no_input(size, environ):
75 print type(environ)
80 print(type(environ))
76 data = 'X'
81 data = 'X'
77 args = _get_python_args('sys.stdout.write("%s" * %d)' % (data, size))
82 args = _get_python_args('sys.stdout.write("%s" * %d)' % (data, size))
78 output = ''.join(subprocessio.SubprocessIOChunker(
83 output = ''.join(subprocessio.SubprocessIOChunker(args, shell=False, env=environ))
79 args, shell=False, env=environ))
80
84
81 assert output == data * size
85 assert output == data * size
82
86
83
87
84 @pytest.mark.parametrize('size', [1, 10**5])
88 @pytest.mark.parametrize('size', [1, 10 ** 5])
85 def test_output_with_no_input_does_not_fail(size, environ):
89 def test_output_with_no_input_does_not_fail(size, environ):
86 data = 'X'
90 data = 'X'
87 args = _get_python_args(
91 args = _get_python_args('sys.stdout.write("%s" * %d); sys.exit(1)' % (data, size))
88 'sys.stdout.write("%s" * %d); sys.exit(1)' % (data, size))
92 output = ''.join(
89 output = ''.join(subprocessio.SubprocessIOChunker(
93 subprocessio.SubprocessIOChunker(
90 args, shell=False, fail_on_return_code=False, env=environ))
94 args, shell=False, fail_on_return_code=False, env=environ
95 )
96 )
91
97
92 print len(data * size), len(output)
98 print("{} {}".format(len(data * size), len(output)))
93 assert output == data * size
99 assert output == data * size
94
100
95
101
96 @pytest.mark.parametrize('size', [1, 10**5])
102 @pytest.mark.parametrize('size', [1, 10 ** 5])
97 def test_output_with_input(size, environ):
103 def test_output_with_input(size, environ):
98 data = 'X' * size
104 data = 'X' * size
99 inputstream = io.BytesIO(data)
105 inputstream = io.BytesIO(data)
100 # This acts like the cat command.
106 # This acts like the cat command.
101 args = _get_python_args('shutil.copyfileobj(sys.stdin, sys.stdout)')
107 args = _get_python_args('shutil.copyfileobj(sys.stdin, sys.stdout)')
102 output = ''.join(subprocessio.SubprocessIOChunker(
108 output = ''.join(
103 args, shell=False, inputstream=inputstream, env=environ))
109 subprocessio.SubprocessIOChunker(
110 args, shell=False, inputstream=inputstream, env=environ
111 )
112 )
104
113
105 print len(data), len(output)
114 print("{} {}".format(len(data * size), len(output)))
106 assert output == data
115 assert output == data
107
116
108
117
109 @pytest.mark.parametrize('size', [1, 10**5])
118 @pytest.mark.parametrize('size', [1, 10 ** 5])
110 def test_output_with_input_skipping_iterator(size, environ):
119 def test_output_with_input_skipping_iterator(size, environ):
111 data = 'X' * size
120 data = 'X' * size
112 inputstream = io.BytesIO(data)
121 inputstream = io.BytesIO(data)
113 # This acts like the cat command.
122 # This acts like the cat command.
114 args = _get_python_args('shutil.copyfileobj(sys.stdin, sys.stdout)')
123 args = _get_python_args('shutil.copyfileobj(sys.stdin, sys.stdout)')
115
124
116 # Note: assigning the chunker makes sure that it is not deleted too early
125 # Note: assigning the chunker makes sure that it is not deleted too early
117 chunker = subprocessio.SubprocessIOChunker(
126 chunker = subprocessio.SubprocessIOChunker(
118 args, shell=False, inputstream=inputstream, env=environ)
127 args, shell=False, inputstream=inputstream, env=environ
128 )
119 output = ''.join(chunker.output)
129 output = ''.join(chunker.output)
120
130
121 print len(data), len(output)
131 print("{} {}".format(len(data * size), len(output)))
122 assert output == data
132 assert output == data
General Comments 0
You need to be logged in to leave comments. Login now