Skip to content

Commit 22f5ae3

Browse files
committed
Remove subprocess calls from tests
1 parent fc50526 commit 22f5ae3

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

news/no-subprocess.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* Removed subprocess calls in test functions.

tests/test_morphsqueeze.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import subprocess
2-
31
import numpy as np
42
import pytest
53
from numpy.polynomial import Polynomial
64

5+
from diffpy.morph.morphapp import create_option_parser, single_morph
76
from diffpy.morph.morphs.morphsqueeze import MorphSqueeze
87

98
squeeze_coeffs_dic = [
@@ -119,7 +118,9 @@ def test_morphsqueeze(x_morph, x_target, squeeze_coeffs):
119118
),
120119
],
121120
)
122-
def test_morphsqueeze_extrapolate(user_filesystem, squeeze_coeffs, wmsg_gen):
121+
def test_morphsqueeze_extrapolate(
122+
user_filesystem, capsys, squeeze_coeffs, wmsg_gen
123+
):
123124
x_morph = np.linspace(0, 10, 101)
124125
y_morph = np.sin(x_morph)
125126
x_target = x_morph
@@ -143,12 +144,19 @@ def test_morphsqueeze_extrapolate(user_filesystem, squeeze_coeffs, wmsg_gen):
143144
morph_file, target_file = create_morph_data_file(
144145
user_filesystem / "cwd_dir", x_morph, y_morph, x_target, y_target
145146
)
146-
run_cmd = ["diffpy.morph"]
147-
run_cmd.extend(["--squeeze=" + ",".join(map(str, coeffs))])
148-
run_cmd.extend([str(morph_file), str(target_file)])
149-
run_cmd.append("-n")
150-
result = subprocess.run(run_cmd, capture_output=True, text=True)
151-
assert expected_wmsg in result.stderr
147+
148+
parser = create_option_parser()
149+
(opts, pargs) = parser.parse_args(
150+
[
151+
"--squeeze",
152+
",".join(map(str, coeffs)),
153+
f"{morph_file.as_posix()}",
154+
f"{target_file.as_posix()}",
155+
"-n",
156+
]
157+
)
158+
with pytest.warns(UserWarning, match=expected_wmsg):
159+
single_morph(parser, opts, pargs, stdout_flag=False)
152160

153161

154162
def create_morph_data_file(

tests/test_refine.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from diffpy.morph.morph_helpers.transformpdftordf import TransformXtalPDFtoRDF
1010
from diffpy.morph.morph_helpers.transformrdftopdf import TransformXtalRDFtoPDF
11+
from diffpy.morph.morphapp import create_option_parser, single_morph
1112
from diffpy.morph.morphs.morphchain import MorphChain
1213
from diffpy.morph.morphs.morphfuncx import MorphFuncx
1314
from diffpy.morph.morphs.morphrgrid import MorphRGrid
@@ -181,7 +182,7 @@ def stretch(x, y, stretch):
181182

182183
assert res < err
183184

184-
def test_refine_grid_bad(self, user_filesystem):
185+
def test_refine_grid_bad(self, user_filesystem, capsys):
185186
grid = numpy.arange(2)
186187
func = numpy.sin(grid)
187188
grid1, func1, grid2, func2 = grid, func, grid, func
@@ -208,9 +209,7 @@ def test_refine_grid_bad(self, user_filesystem):
208209
actual_error_message = str(error.value)
209210
assert actual_error_message == expected_error_message
210211

211-
# call from command line
212-
import subprocess
213-
212+
# Test from command line
214213
data_dir_path = user_filesystem / "cwd_dir"
215214
morph_file = data_dir_path / "morph_data"
216215
morph_data_text = [
@@ -224,18 +223,18 @@ def test_refine_grid_bad(self, user_filesystem):
224223
]
225224
target_data_text = "\n".join(target_data_text)
226225
target_file.write_text(target_data_text)
227-
run_cmd = ["diffpy.morph"]
226+
run_cmd = []
228227
for key, value in config.items():
229228
run_cmd.append(f"--{key}")
230229
run_cmd.append(f"{value}")
231230
run_cmd.extend([str(morph_file), str(target_file)])
232231
run_cmd.append("-n")
233-
result = subprocess.run(run_cmd, capture_output=True, text=True)
234-
expected_error_message = (
235-
"diffpy.morph: error: " + expected_error_message
236-
)
237-
actual_error_message = result.stderr.strip()
238-
assert actual_error_message == expected_error_message
232+
parser = create_option_parser()
233+
(opts, pargs) = parser.parse_args(run_cmd)
234+
with pytest.raises(SystemExit):
235+
single_morph(parser, opts, pargs, stdout_flag=False)
236+
_, err = capsys.readouterr()
237+
assert expected_error_message in actual_error_message
239238

240239

241240
# End of class TestRefine

0 commit comments

Comments
 (0)