|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from io import StringIO |
| 4 | +import shutil |
| 5 | +import pytest |
| 6 | + |
| 7 | +from .utils import HAS_IRAF |
| 8 | + |
| 9 | +from pyraf import iraf |
| 10 | + |
| 11 | +try: |
| 12 | + from iraf import softools |
| 13 | +except ImportError: |
| 14 | + pytest.skip("IRAF softools not available", allow_module_level=True) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(scope="session", autouse=True) |
| 18 | +def setup_testpkg(tmp_path_factory): |
| 19 | + """Copy the test package to a temporary dir, compile the SPP tasks and |
| 20 | + load the package |
| 21 | +
|
| 22 | + """ |
| 23 | + base = tmp_path_factory.mktemp("iraf") |
| 24 | + for fname in (Path(__file__).parent / "testpkg").iterdir(): |
| 25 | + shutil.copy(fname, base) |
| 26 | + iraf.set(testpkg=str(base) + "/") |
| 27 | + cwd = Path.cwd() |
| 28 | + os.chdir(base) |
| 29 | + try: |
| 30 | + iraf.xc("spppars.x") |
| 31 | + finally: |
| 32 | + os.chdir(cwd) |
| 33 | + iraf.task(testpkgDOTpkg="testpkg$testpkg.cl") |
| 34 | + from iraf import testpkg |
| 35 | + |
| 36 | + yield |
| 37 | + |
| 38 | + |
| 39 | +def showpars(taskname, **kwargs): |
| 40 | + """Run the task with given parameters |
| 41 | +
|
| 42 | + Return the output as a dictionary of key-value pairs with proper |
| 43 | + type for the value |
| 44 | +
|
| 45 | + """ |
| 46 | + task = iraf.module.getTask(taskname) |
| 47 | + params = task.getParDict() |
| 48 | + stdout = StringIO() |
| 49 | + task(StdoutAppend=stdout, **kwargs) |
| 50 | + stdout.seek(0) |
| 51 | + res = {} |
| 52 | + for l in stdout.read().splitlines(): |
| 53 | + key, val = l.split("=", 1) |
| 54 | + res[key] = params[key].checkValue(val) |
| 55 | + return res |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize( |
| 59 | + "name,value", |
| 60 | + [ |
| 61 | + ("strpar", "foo.fits"), |
| 62 | + ("intpar", 2), |
| 63 | + ("intpar", -2), |
| 64 | + ("fltpar", 2.0), |
| 65 | + ("fltpar", 2.0e1), |
| 66 | + ("fltpar", 2), |
| 67 | + ("boolpar", True), |
| 68 | + ("boolpar", 1), |
| 69 | + ], |
| 70 | +) |
| 71 | +@pytest.mark.parametrize("task", ["clpars", "simplepars"]) |
| 72 | +def test_simplepars(task, name, value): |
| 73 | + """Check that simple parameters work for CL and SPP tasks""" |
| 74 | + ref = showpars(task) |
| 75 | + shortname = name.split(".", 1)[-1] |
| 76 | + ref[shortname] = value |
| 77 | + assert ref == showpars(task, **{name: value}) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize( |
| 81 | + "name,value", |
| 82 | + [ |
| 83 | + ("psetpar.psintpar", 3), |
| 84 | + ("psintpar", 3), |
| 85 | + ("psetpar.psstrpar", "bar"), |
| 86 | + ("psstrpar", "bar"), |
| 87 | + ], |
| 88 | +) |
| 89 | +@pytest.mark.parametrize("task", ["clpars", "psetpars0", "psetpars1"]) |
| 90 | +def test_pset(task, name, value): |
| 91 | + """Check that parameter sets for for CL and SPP tasks""" |
| 92 | + ref = showpars(task) |
| 93 | + shortname = name.split(".", 1)[-1] |
| 94 | + ref[shortname] = value |
| 95 | + assert ref == showpars(task, **{name: value}) |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.xfail(reason="Not implemented in PyRAF") |
| 99 | +@pytest.mark.parametrize("task", ["clpars", "psetpars0", "psetpars1"]) |
| 100 | +def test_pars_other_pset(task): |
| 101 | + """Check that setting another parameter set works in CL and SPP tasks""" |
| 102 | + ref = showpars(task) |
| 103 | + ref["psstrpar"] = "foobar" |
| 104 | + ref["psintpar"] = -1 |
| 105 | + assert ref == showpars(task, psetpar="psetpar1") |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize( |
| 109 | + "name,value", |
| 110 | + [ |
| 111 | + ("intpar", "-20"), |
| 112 | + ("intpar", "20"), |
| 113 | + ("intpar", "foo"), |
| 114 | + ("fltpar", "-100.5"), |
| 115 | + ("fltpar", "1e3"), |
| 116 | + ("fltpar", "foo"), |
| 117 | + ("boolpar", "foo"), |
| 118 | + ("boolpar", -99), |
| 119 | + ], |
| 120 | +) |
| 121 | +@pytest.mark.parametrize("task", ["clpars", "simplepars"]) |
| 122 | +def test_pars_invalid_value(task, name, value): |
| 123 | + """Check that invalid values raise a ValueError""" |
| 124 | + with pytest.raises(ValueError): |
| 125 | + showpars(task, **{name: value}) |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.parametrize( |
| 129 | + "name,value", |
| 130 | + [ |
| 131 | + ("invalidpar", "foo"), |
| 132 | + ("psetpar", "foo"), |
| 133 | + ], |
| 134 | +) |
| 135 | +@pytest.mark.parametrize("task", ["clpars", "simplepars"]) |
| 136 | +def test_pars_invalid_name(task, name, value): |
| 137 | + """Check that unknown parameters raise a KeyError""" |
| 138 | + with pytest.raises(KeyError): |
| 139 | + showpars(task, **{name: value}) |
0 commit comments