88import pytest
99
1010
11- def call (arguments , env = None ):
11+ def call (arguments , expected_returncode = 0 , env = None ):
1212 """
13- return a triple with (returncode, stdout, stderr) from the call to subprocess
13+ Calls the command specificied by arguments and checks the returncode via assert
14+ return (stdout, stderr) from the call to subprocess
1415 """
15- result = ()
1616 if sys .version_info > (3 , 5 ):
1717 out = subprocess .run (
1818 arguments ,
1919 env = env ,
2020 stdout = subprocess .PIPE ,
2121 stderr = subprocess .PIPE )
22- result = (out .returncode , out .stdout .decode ("utf-8" ), out .stderr .decode ("utf-8" ))
22+ assert out .returncode == expected_returncode
23+ stdout , stderr = (out .stdout , out .stderr )
2324 else :
2425 p = subprocess .Popen (
2526 arguments ,
2627 env = env ,
2728 stdout = subprocess .PIPE ,
2829 stderr = subprocess .PIPE )
2930 stdout , stderr = p .communicate ()
30- p .wait ()
31- result = (p .returncode , stdout .decode ("utf-8" ), stderr .decode ("utf-8" ))
32- return result
31+ assert p .returncode == expected_returncode
32+ return stdout .decode ("utf-8" ), stderr .decode ("utf-8" )
3333
3434
35- def call_with_scorep (file , scorep_arguments = None , env = None ):
35+ def call_with_scorep (file , scorep_arguments = None , expected_returncode = 0 , env = None ):
3636 """
3737 Shortcut for running a python file with the scorep module
3838
39- @return triple (returncode, stdout, stderr) from the call to subprocess
39+ @return ( stdout, stderr) from the call to subprocess
4040 """
4141 arguments = [sys .executable , "-m" , "scorep" ]
4242 if scorep_arguments :
4343 arguments .extend (scorep_arguments )
44- return call (arguments + [file ], env = env )
44+ return call (arguments + [file ], expected_returncode = expected_returncode , env = env )
4545
4646
4747def has_package (name ):
@@ -66,26 +66,27 @@ def scorep_env(tmp_path):
6666 return env
6767
6868
69+ def get_trace_path (env ):
70+ """Return the path to the otf2 trace file given an environment dict"""
71+ return env ["SCOREP_EXPERIMENT_DIRECTORY" ] + "/traces.otf2"
72+
73+
6974def test_has_version ():
7075 import scorep
7176 assert scorep .__version__ is not None
7277
7378
7479def test_user_regions (scorep_env ):
75- trace_path = scorep_env [ "SCOREP_EXPERIMENT_DIRECTORY" ] + "/traces.otf2"
80+ trace_path = get_trace_path ( scorep_env )
7681
77- out = call_with_scorep ("cases/user_regions.py" ,
78- ["--nopython" ],
79- env = scorep_env )
80- std_out = out [1 ]
81- std_err = out [2 ]
82+ std_out , std_err = call_with_scorep ("cases/user_regions.py" ,
83+ ["--nopython" ],
84+ env = scorep_env )
8285
8386 assert std_err == ""
8487 assert std_out == "hello world\n hello world\n hello world3\n hello world4\n "
8588
86- out = call (["otf2-print" , trace_path ])
87- std_out = out [1 ]
88- std_err = out [2 ]
89+ std_out , std_err = call (["otf2-print" , trace_path ])
8990
9091 assert re .search ('ENTER[ ]*[0-9 ]*[0-9 ]*Region: "user:test_region"' , std_out )
9192 assert re .search ('LEAVE[ ]*[0-9 ]*[0-9 ]*Region: "user:test_region"' , std_out )
@@ -98,20 +99,16 @@ def test_user_regions(scorep_env):
9899
99100
100101def test_context (scorep_env ):
101- trace_path = scorep_env [ "SCOREP_EXPERIMENT_DIRECTORY" ] + "/traces.otf2"
102+ trace_path = get_trace_path ( scorep_env )
102103
103- out = call_with_scorep ("cases/context.py" ,
104- ["--noinstrumenter" ],
105- env = scorep_env )
106- std_out = out [1 ]
107- std_err = out [2 ]
104+ std_out , std_err = call_with_scorep ("cases/context.py" ,
105+ ["--noinstrumenter" ],
106+ env = scorep_env )
108107
109108 assert std_err == ""
110109 assert std_out == "hello world\n hello world\n hello world\n "
111110
112- out = call (["otf2-print" , trace_path ])
113- std_out = out [1 ]
114- std_err = out [2 ]
111+ std_out , std_err = call (["otf2-print" , trace_path ])
115112
116113 assert re .search ('ENTER[ ]*[0-9 ]*[0-9 ]*Region: "user:test_region"' , std_out )
117114 assert re .search ('LEAVE[ ]*[0-9 ]*[0-9 ]*Region: "user:test_region"' , std_out )
@@ -120,114 +117,92 @@ def test_context(scorep_env):
120117
121118
122119def test_user_regions_no_scorep (scorep_env ):
123- out = call ([sys .executable ,
124- "cases/user_regions.py" ],
125- env = scorep_env )
126- std_out = out [1 ]
127- std_err = out [2 ]
120+ std_out , std_err = call ([sys .executable ,
121+ "cases/user_regions.py" ],
122+ env = scorep_env )
128123
129124 assert std_err == ""
130125 assert std_out == "hello world\n hello world\n hello world3\n hello world4\n "
131126
132127
133128def test_user_rewind (scorep_env ):
134- trace_path = scorep_env [ "SCOREP_EXPERIMENT_DIRECTORY" ] + "/traces.otf2"
129+ trace_path = get_trace_path ( scorep_env )
135130
136- out = call_with_scorep ("cases/user_rewind.py" , env = scorep_env )
137- std_out = out [1 ]
138- std_err = out [2 ]
131+ std_out , std_err = call_with_scorep ("cases/user_rewind.py" , env = scorep_env )
139132
140133 assert std_err == ""
141134 assert std_out == "hello world\n hello world\n "
142135
143- out = call (["otf2-print" , trace_path ])
144- std_out = out [1 ]
145- std_err = out [2 ]
136+ std_out , std_err = call (["otf2-print" , trace_path ])
146137
147138 assert re .search ('MEASUREMENT_ON_OFF[ ]*[0-9 ]*[0-9 ]*Mode: OFF' , std_out )
148139 assert re .search ('MEASUREMENT_ON_OFF[ ]*[0-9 ]*[0-9 ]*Mode: ON' , std_out )
149140
150141
151142def test_oa_regions (scorep_env ):
152- trace_path = scorep_env [ "SCOREP_EXPERIMENT_DIRECTORY" ] + "/traces.otf2"
143+ trace_path = get_trace_path ( scorep_env )
153144
154- out = call_with_scorep ("cases/oa_regions.py" ,
155- ["--nopython" ],
156- env = scorep_env )
157- std_out = out [1 ]
158- std_err = out [2 ]
145+ std_out , std_err = call_with_scorep ("cases/oa_regions.py" ,
146+ ["--nopython" ],
147+ env = scorep_env )
159148
160149 assert std_err == ""
161150 assert std_out == "hello world\n "
162151
163- out = call (["otf2-print" , trace_path ])
164- std_out = out [1 ]
165- std_err = out [2 ]
152+ std_out , std_err = call (["otf2-print" , trace_path ])
166153
167154 assert std_err == ""
168155 assert re .search ('ENTER[ ]*[0-9 ]*[0-9 ]*Region: "test_region"' , std_out )
169156 assert re .search ('LEAVE[ ]*[0-9 ]*[0-9 ]*Region: "test_region"' , std_out )
170157
171158
172159def test_instrumentation (scorep_env ):
173- trace_path = scorep_env [ "SCOREP_EXPERIMENT_DIRECTORY" ] + "/traces.otf2"
160+ trace_path = get_trace_path ( scorep_env )
174161
175- out = call_with_scorep ("cases/instrumentation.py" ,
176- ["--nocompiler" ],
177- env = scorep_env )
178- std_out = out [1 ]
179- std_err = out [2 ]
162+ std_out , std_err = call_with_scorep ("cases/instrumentation.py" ,
163+ ["--nocompiler" ],
164+ env = scorep_env )
180165
181166 assert std_err == ""
182167 assert std_out == "hello world\n baz\n bar\n "
183168
184- out = call (["otf2-print" , trace_path ])
185- std_out = out [1 ]
186- std_err = out [2 ]
169+ std_out , std_err = call (["otf2-print" , trace_path ])
187170
188171 assert std_err == ""
189172 assert re .search ('ENTER[ ]*[0-9 ]*[0-9 ]*Region: "__main__:foo"' , std_out )
190173 assert re .search ('LEAVE[ ]*[0-9 ]*[0-9 ]*Region: "__main__:foo"' , std_out )
191174
192175
193176def test_user_instrumentation (scorep_env ):
194- trace_path = scorep_env [ "SCOREP_EXPERIMENT_DIRECTORY" ] + "/traces.otf2"
177+ trace_path = get_trace_path ( scorep_env )
195178
196- out = call_with_scorep ("cases/user_instrumentation.py" ,
197- ["--nocompiler" , "--noinstrumenter" ],
198- env = scorep_env )
199- std_out = out [1 ]
200- std_err = out [2 ]
179+ std_out , std_err = call_with_scorep ("cases/user_instrumentation.py" ,
180+ ["--nocompiler" , "--noinstrumenter" ],
181+ env = scorep_env )
201182
202183 assert std_err == ""
203184 assert std_out == "hello world\n baz\n bar\n "
204185
205- out = call (["otf2-print" , trace_path ])
206- std_out = out [1 ]
207- std_err = out [2 ]
186+ std_out , std_err = call (["otf2-print" , trace_path ])
208187
209188 assert std_err == ""
210189 assert re .search ('ENTER[ ]*[0-9 ]*[0-9 ]*Region: "__main__:foo"' , std_out )
211190 assert re .search ('LEAVE[ ]*[0-9 ]*[0-9 ]*Region: "__main__:foo"' , std_out )
212191
213192
214193def test_error_region (scorep_env ):
215- trace_path = scorep_env [ "SCOREP_EXPERIMENT_DIRECTORY" ] + "/traces.otf2"
194+ trace_path = get_trace_path ( scorep_env )
216195
217- out = call_with_scorep ("cases/error_region.py" ,
218- ["--nocompiler" , "--noinstrumenter" ],
219- env = scorep_env )
220- std_out = out [1 ]
221- std_err = out [2 ]
196+ std_out , std_err = call_with_scorep ("cases/error_region.py" ,
197+ ["--nocompiler" , "--noinstrumenter" ],
198+ env = scorep_env )
222199
223200 assert std_err == \
224201 'SCOREP_BINDING_PYTHON ERROR: There was a region exit without an enter!\n ' + \
225202 'SCOREP_BINDING_PYTHON ERROR: For details look for "error_region" in the trace or profile.\n '
226203 assert std_out == ""
227204
228- out = call (["otf2-print" , trace_path ])
229- std_out = out [1 ]
230- std_err = out [2 ]
205+ std_out , std_err = call (["otf2-print" , trace_path ])
231206
232207 assert std_err == ""
233208 assert re .search ('ENTER[ ]*[0-9 ]*[0-9 ]*Region: "error_region"' , std_out )
@@ -241,22 +216,19 @@ def test_error_region(scorep_env):
241216@requires_package ('mpi4py' )
242217@requires_package ('numpy' )
243218def test_mpi (scorep_env ):
244- out = call (["mpirun" ,
245- "-n" ,
246- "2" ,
247- "-mca" ,
248- "btl" ,
249- "^openib" ,
250- sys .executable ,
251- "-m" ,
252- "scorep" ,
253- "--mpp=mpi" ,
254- "--nocompiler" ,
255- "cases/mpi.py" ],
256- env = scorep_env )
257-
258- std_out = out [1 ]
259- std_err = out [2 ]
219+ std_out , std_err = call (["mpirun" ,
220+ "-n" ,
221+ "2" ,
222+ "-mca" ,
223+ "btl" ,
224+ "^openib" ,
225+ sys .executable ,
226+ "-m" ,
227+ "scorep" ,
228+ "--mpp=mpi" ,
229+ "--nocompiler" ,
230+ "cases/mpi.py" ],
231+ env = scorep_env )
260232
261233 expected_std_out = r"\[0[0-9]\] \[0. 1. 2. 3. 4.\]\n\[0[0-9]] \[0. 1. 2. 3. 4.\]\n"
262234
@@ -265,11 +237,10 @@ def test_mpi(scorep_env):
265237
266238
267239def test_call_main (scorep_env ):
268- out = call_with_scorep ("cases/call_main.py" ,
269- ["--nocompiler" ],
270- env = scorep_env )
271- std_out = out [1 ]
272- std_err = out [2 ]
240+ std_out , std_err = call_with_scorep ("cases/call_main.py" ,
241+ ["--nocompiler" ],
242+ expected_returncode = 1 ,
243+ env = scorep_env )
273244
274245 expected_std_err = r"scorep: Someone called scorep\.__main__\.main"
275246 expected_std_out = ""
@@ -278,11 +249,9 @@ def test_call_main(scorep_env):
278249
279250
280251def test_dummy (scorep_env ):
281- out = call_with_scorep ("cases/instrumentation.py" ,
282- ["--instrumenter-type=dummy" ],
283- env = scorep_env )
284- std_out = out [1 ]
285- std_err = out [2 ]
252+ std_out , std_err = call_with_scorep ("cases/instrumentation.py" ,
253+ ["--instrumenter-type=dummy" ],
254+ env = scorep_env )
286255
287256 assert std_err == ""
288257 assert std_out == "hello world\n baz\n bar\n "
@@ -291,20 +260,16 @@ def test_dummy(scorep_env):
291260
292261@requires_python3
293262def test_numpy_dot (scorep_env ):
294- trace_path = scorep_env [ "SCOREP_EXPERIMENT_DIRECTORY" ] + "/traces.otf2"
263+ trace_path = get_trace_path ( scorep_env )
295264
296- out = call_with_scorep ("cases/numpy_dot.py" ,
297- ["--nocompiler" , "--noinstrumenter" ],
298- env = scorep_env )
299- std_out = out [1 ]
300- std_err = out [2 ]
265+ std_out , std_err = call_with_scorep ("cases/numpy_dot.py" ,
266+ ["--nocompiler" , "--noinstrumenter" ],
267+ env = scorep_env )
301268
302269 assert std_out == "[[ 7 10]\n [15 22]]\n "
303270 assert std_err == ""
304271
305- out = call (["otf2-print" , trace_path ])
306- std_out = out [1 ]
307- std_err = out [2 ]
272+ std_out , std_err = call (["otf2-print" , trace_path ])
308273
309274 assert std_err == ""
310275 assert re .search ('ENTER[ ]*[0-9 ]*[0-9 ]*Region: "numpy.__array_function__:dot"' , std_out )
0 commit comments