Test suite fails on windows, cannot open already open file
The pytest
suite is failing on windows because on that platform you cannot open
an already open file. The issues I think all stem from the use of the input_file
test fixture in tests/functional/cli/
which opens a temporary file and then provides it to the test function, only for the .name
attribute to be accessed and the file to be opened again by the opened_file
context manager.
A better option might be to use the pytest builtin tmp_path
fixture to create a temporary directory on-the-fly, and then write the kat
script to it before yielding the name of the file only:
@pytest.fixture
def input_file(tmp_path):
tmpfile = tmp_path / "kat.tmp"
with open(tmpfile, "w") as fobj:
fobj.write(SCRIPT)
yield str(tmpfile)
Test failures
================================== FAILURES ===================================
__________________________________ test_info __________________________________
cli = <click.testing.CliRunner object at 0x0000021C464DBF40>
input_file = <tempfile._TemporaryFileWrapper object at 0x0000021C464DB280>
def test_info(cli, input_file):
"""Test info output."""
cli_result = cli.invoke(info, [input_file.name])
> assert "Summary:" in sanitized_output(cli_result)
E assert 'Summary:' in "Usage: info [OPTIONS] INPUT_FILE\nTry 'info --help' for help.\n\nError: Invalid value for 'INPUT_FILE': 'C:\\\\Users\\\\spxdmm\\\\AppData\\\\Local\\\\Temp\\\\tmpda47y2rj': Permission denied"
E + where "Usage: info [OPTIONS] INPUT_FILE\nTry 'info --help' for help.\n\nError: Invalid value for 'INPUT_FILE': 'C:\\\\Users\\\\spxdmm\\\\AppData\\\\Local\\\\Temp\\\\tmpda47y2rj': Permission denied" = sanitized_output(<Result SystemExit(2)>)
tests\functional\cli\test_info.py:10: AssertionError
_________________________ test_run_same_as_python_api _________________________
isolated_cli = <click.testing.CliRunner object at 0x0000021C46222B50>
input_file = <tempfile._TemporaryFileWrapper object at 0x0000021C465E3670>
def test_run_same_as_python_api(isolated_cli, input_file):
"""Test that running kat script via CLI same as calling :meth:`finesse.script.parse_file`."""
cli_result = isolated_cli.invoke(run, [input_file.name, "--no-plot"])
> api_model = parse_file(input_file.name)
%SRC_DIR%\tests\functional\cli\test_run.py:13:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
%PREFIX%\lib\site-packages\finesse\script\__init__.py:55: in parse_file
with opened_file(path, "r") as fobj:
%PREFIX%\lib\contextlib.py:113: in __enter__
return next(self.gen)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
fobj = 'C:\\Users\\spxdmm\\AppData\\Local\\Temp\\tmprwyb281c', mode = 'r'
@contextmanager
def opened_file(fobj, mode):
"""Get an open file regardless of whether a string or an already open file is
passed.
Parameters
----------
fobj : str or file-like
The path or file object to ensure is open. If `fobj` is an already open file object, its
mode is checked to be correct but is otherwise returned as-is. If `fobj` is a string, it is
opened with the specified `mode` and yielded, then closed once the wrapped context exits.
Note that passed open file objects are *not* closed.
mode : str
The mode to ensure `fobj` is opened with.
Yields
------
:py:class:`io.FileIO`
The open file with the specified `mode`.
Raises
------
ValueError
If `fobj` is not a string nor open file, or if `fobj` is open but with a different `mode`.
"""
close = False
if isinstance(fobj, str):
> fobj = open(fobj, mode)
E PermissionError: [Errno 13] Permission denied: 'C:\\Users\\spxdmm\\AppData\\Local\\Temp\\tmprwyb281c'
%PREFIX%\lib\site-packages\finesse\utilities\misc.py:215: PermissionError
_______________________________ test_trace_flag _______________________________
isolated_cli = <click.testing.CliRunner object at 0x0000021C46441A00>
input_file = <tempfile._TemporaryFileWrapper object at 0x0000021C46441550>
def test_trace_flag(isolated_cli, input_file):
"""Test the --trace run flag."""
cli_result = isolated_cli.invoke(run, [input_file.name, "--no-plot", "--trace"])
> assert "Trace:" in sanitized_output(cli_result)
E assert 'Trace:' in "Usage: run [OPTIONS] INPUT_FILE\nTry 'run --help' for help.\n\nError: Invalid value for 'INPUT_FILE': 'C:\\\\Users\\\\spxdmm\\\\AppData\\\\Local\\\\Temp\\\\tmp832a4biu': Permission denied"
E + where "Usage: run [OPTIONS] INPUT_FILE\nTry 'run --help' for help.\n\nError: Invalid value for 'INPUT_FILE': 'C:\\\\Users\\\\spxdmm\\\\AppData\\\\Local\\\\Temp\\\\tmp832a4biu': Permission denied" = sanitized_output(<Result SystemExit(2)>)
%SRC_DIR%\tests\functional\cli\test_run.py:23: AssertionError
_____________________________ test_no_trace_flag ______________________________
isolated_cli = <click.testing.CliRunner object at 0x0000021C462B1E50>
input_file = <tempfile._TemporaryFileWrapper object at 0x0000021C462B1280>
def test_no_trace_flag(isolated_cli, input_file):
"""Test the --trace run flag."""
cli_result = isolated_cli.invoke(run, [input_file.name, "--no-plot", "--no-trace"])
assert "Trace:" not in sanitized_output(cli_result)
> assert cli_result.exit_code == 0
E assert 2 == 0
E + where 2 = <Result SystemExit(2)>.exit_code
%SRC_DIR%\tests\functional\cli\test_run.py:31: AssertionError
______________________________ test_parse_error _______________________________
isolated_cli = <click.testing.CliRunner object at 0x0000021C462B13A0>
def test_parse_error(isolated_cli):
"""Test that parsing errors appear in the output."""
with NamedTemporaryFile("r+") as fobj:
fobj.write("laser l1 __fake_param__=1")
fobj.flush()
cli_result = isolated_cli.invoke(run, [fobj.name, "--no-plot"])
> assert "ERROR:" in sanitized_output(cli_result)
E assert 'ERROR:' in "Usage: run [OPTIONS] INPUT_FILE\nTry 'run --help' for help.\n\nError: Invalid value for 'INPUT_FILE': 'C:\\\\Users\\\\spxdmm\\\\AppData\\\\Local\\\\Temp\\\\tmpa125q_ci': Permission denied"
E + where "Usage: run [OPTIONS] INPUT_FILE\nTry 'run --help' for help.\n\nError: Invalid value for 'INPUT_FILE': 'C:\\\\Users\\\\spxdmm\\\\AppData\\\\Local\\\\Temp\\\\tmpa125q_ci': Permission denied" = sanitized_output(<Result SystemExit(2)>)
%SRC_DIR%\tests\functional\cli\test_run.py:40: AssertionError
============================== warnings summary ===============================
..\_test_env\lib\site-packages\pyreadline\py3k_compat.py:8
%PREFIX%\lib\site-packages\pyreadline\py3k_compat.py:8: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
return isinstance(x, collections.Callable)
..\_test_env\lib\site-packages\sly\lex.py:311
%PREFIX%\lib\site-packages\sly\lex.py:311: DeprecationWarning: Flags not at the start of the expression '(?P<GNUPLOT_START>(?' (truncated)
cpat = cls.regex_module.compile(part, cls.reflags)
..\_test_env\lib\site-packages\sly\lex.py:327
%PREFIX%\lib\site-packages\sly\lex.py:327: DeprecationWarning: Flags not at the start of the expression '(?P<obsolete>(color)' (truncated)
cls._master_re = cls.regex_module.compile('|'.join(parts), cls.reflags)
tests\validation\plane_wave\test_sig_vs_gradient.py:163
%SRC_DIR%\tests\validation\plane_wave\test_sig_vs_gradient.py:163: DeprecationWarning: invalid escape sequence \%
@pytest.mark.xfail(reason='Weird 0.1\% error in readout method, unsure why')
tests/functional/script/katscript/parse/test_expressions.py::test_expressions_lazy[exp(1j*pi)-expected5]
%PREFIX%\lib\site-packages\finesse\components\general.py:714: ComplexWarning: Casting complex values to real discards the imaginary part
self.value = value
tests/validation/actions/test_noise.py::test_siso_laser_amplitude
tests/validation/actions/test_noise.py::test_siso_laser_amplitude
tests/validation/actions/test_noise.py::test_siso_laser_amplitude_plot
tests/validation/actions/test_noise.py::test_siso_laser_amplitude_plot
tests/validation/actions/test_noise.py::test_siso_laser_amplitude_plot
tests/validation/actions/test_noise.py::test_siso_laser_amplitude_plot
tests/validation/plane_wave/test_pd1_vs_sd1.py::test_amp
tests/validation/plane_wave/test_pd1_vs_sd1.py::test_frq
tests/validation/plane_wave/test_pd1_vs_sd1.py::test_phs
%PREFIX%\lib\site-packages\finesse\components\readout.py:141: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
QShot0Workspace(self, sim, False, output_info=oinfo)
tests/validation/components/test_phase_modulator_bessel.py::test_bessel
%SRC_DIR%\tests\validation\components\test_phase_modulator_bessel.py:43: RuntimeWarning: invalid value encountered in true_divide
diff = np.max(np.abs(mod_data - bessel_fcn)/np.abs(bessel_fcn))
tests/validation/plane_wave/test_sig_vs_gradient.py::test_error_signal_slope
tests/validation/plane_wave/test_sig_vs_gradient.py::test_error_signal_slope
tests/validation/plane_wave/test_sig_vs_gradient.py::test_error_pd1
tests/validation/plane_wave/test_sig_vs_gradient.py::test_error_pd1
tests/validation/plane_wave/test_sig_vs_gradient.py::test_error_readout
tests/validation/plane_wave/test_sig_vs_gradient.py::test_error_readout
%PREFIX%\lib\site-packages\finesse\components\readout.py:348: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
QShotNWorkspace(self, sim, 1, False, output_info=oinfo)
-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
FAILED tests/functional/cli/test_info.py::test_info - assert 'Summary:' in "U...
FAILED tests/functional/cli/test_run.py::test_run_same_as_python_api - Permis...
FAILED tests/functional/cli/test_run.py::test_trace_flag - assert 'Trace:' in...
FAILED tests/functional/cli/test_run.py::test_no_trace_flag - assert 2 == 0
FAILED tests/functional/cli/test_run.py::test_parse_error - assert 'ERROR:' i...
= 5 failed, 2284 passed, 18 skipped, 38 xfailed, 25 xpassed, 21 warnings in 163.26s (0:02:43) =