diff --git a/news/no-subprocess.rst b/news/no-subprocess.rst new file mode 100644 index 0000000..90f37de --- /dev/null +++ b/news/no-subprocess.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* Removed subprocess calls in test functions. diff --git a/tests/test_morphsqueeze.py b/tests/test_morphsqueeze.py index cc741bc..a4b2d6b 100644 --- a/tests/test_morphsqueeze.py +++ b/tests/test_morphsqueeze.py @@ -1,9 +1,8 @@ -import subprocess - import numpy as np import pytest from numpy.polynomial import Polynomial +from diffpy.morph.morphapp import create_option_parser, single_morph from diffpy.morph.morphs.morphsqueeze import MorphSqueeze squeeze_coeffs_dic = [ @@ -119,7 +118,9 @@ def test_morphsqueeze(x_morph, x_target, squeeze_coeffs): ), ], ) -def test_morphsqueeze_extrapolate(user_filesystem, squeeze_coeffs, wmsg_gen): +def test_morphsqueeze_extrapolate( + user_filesystem, capsys, squeeze_coeffs, wmsg_gen +): x_morph = np.linspace(0, 10, 101) y_morph = np.sin(x_morph) x_target = x_morph @@ -143,12 +144,19 @@ def test_morphsqueeze_extrapolate(user_filesystem, squeeze_coeffs, wmsg_gen): morph_file, target_file = create_morph_data_file( user_filesystem / "cwd_dir", x_morph, y_morph, x_target, y_target ) - run_cmd = ["diffpy.morph"] - run_cmd.extend(["--squeeze=" + ",".join(map(str, coeffs))]) - run_cmd.extend([str(morph_file), str(target_file)]) - run_cmd.append("-n") - result = subprocess.run(run_cmd, capture_output=True, text=True) - assert expected_wmsg in result.stderr + + parser = create_option_parser() + (opts, pargs) = parser.parse_args( + [ + "--squeeze", + ",".join(map(str, coeffs)), + f"{morph_file.as_posix()}", + f"{target_file.as_posix()}", + "-n", + ] + ) + with pytest.warns(UserWarning, match=expected_wmsg): + single_morph(parser, opts, pargs, stdout_flag=False) def create_morph_data_file( diff --git a/tests/test_refine.py b/tests/test_refine.py index 76263bf..2c10c19 100644 --- a/tests/test_refine.py +++ b/tests/test_refine.py @@ -8,6 +8,7 @@ from diffpy.morph.morph_helpers.transformpdftordf import TransformXtalPDFtoRDF from diffpy.morph.morph_helpers.transformrdftopdf import TransformXtalRDFtoPDF +from diffpy.morph.morphapp import create_option_parser, single_morph from diffpy.morph.morphs.morphchain import MorphChain from diffpy.morph.morphs.morphfuncx import MorphFuncx from diffpy.morph.morphs.morphrgrid import MorphRGrid @@ -181,7 +182,7 @@ def stretch(x, y, stretch): assert res < err - def test_refine_grid_bad(self, user_filesystem): + def test_refine_grid_bad(self, user_filesystem, capsys): grid = numpy.arange(2) func = numpy.sin(grid) grid1, func1, grid2, func2 = grid, func, grid, func @@ -208,9 +209,7 @@ def test_refine_grid_bad(self, user_filesystem): actual_error_message = str(error.value) assert actual_error_message == expected_error_message - # call from command line - import subprocess - + # Test from command line data_dir_path = user_filesystem / "cwd_dir" morph_file = data_dir_path / "morph_data" morph_data_text = [ @@ -224,18 +223,18 @@ def test_refine_grid_bad(self, user_filesystem): ] target_data_text = "\n".join(target_data_text) target_file.write_text(target_data_text) - run_cmd = ["diffpy.morph"] + run_cmd = [] for key, value in config.items(): run_cmd.append(f"--{key}") run_cmd.append(f"{value}") run_cmd.extend([str(morph_file), str(target_file)]) run_cmd.append("-n") - result = subprocess.run(run_cmd, capture_output=True, text=True) - expected_error_message = ( - "diffpy.morph: error: " + expected_error_message - ) - actual_error_message = result.stderr.strip() - assert actual_error_message == expected_error_message + parser = create_option_parser() + (opts, pargs) = parser.parse_args(run_cmd) + with pytest.raises(SystemExit): + single_morph(parser, opts, pargs, stdout_flag=False) + _, err = capsys.readouterr() + assert expected_error_message in actual_error_message # End of class TestRefine