skypyproject / skypy

SkyPy: A package for modelling the Universe.
BSD 3-Clause "New" or "Revised" License
118 stars 38 forks source link

Logging test fails for new astropy file overwrite error message #497

Closed rrjbca closed 3 years ago

rrjbca commented 3 years ago

Astropy PR #12179 changed the error message when trying to overwrite an existing file without specifying overwrite=True. Our logging tests fail to catch this new error message when testing with the astropy main branch. See recent compatibility workflow logs or reproduce locally on the skypy main branch:

>> tox -v -e py39-test-dev

============================================================================= FAILURES =============================================================================
___________________________________________________________________________ test_logging ___________________________________________________________________________

capsys = <_pytest.capture.CaptureFixture object at 0x122007a00>
tmp_path = PosixPath('/private/var/folders/nw/43x1r_9n2235_0prypvp329w0000gp/T/pytest-14/test_logging0')

    def test_logging(capsys, tmp_path):

        # Run skypy with default verbosity and check log is empty
        config_filename = get_pkg_data_filename('data/test_config.yml')
        output_filename = str(tmp_path / 'logging.fits')
        skypy.main([config_filename, output_filename])
        out, err = capsys.readouterr()
        assert(not err)

        # Run again with increased verbosity and capture log. Force an exception by
        # not using the "--overwrite" flag when the output file already exists.
        with pytest.raises(SystemExit):
            skypy.main([config_filename, output_filename, '--verbose'])
        out, err = capsys.readouterr()

        # Determine all DAG jobs and function calls from config
        config = load_skypy_yaml(config_filename)
        cosmology = config.pop('cosmology', None)
        tables = config.pop('tables', {})
        config.update({k: v.pop('.init', Call(Table)) for k, v in tables.items()})
        columns = [f'{t}.{c}' for t, cols in tables.items() for c in cols]
        functions = [f for f in config.values() if isinstance(f, Call)]
        functions += [f for t, cols in tables.items() for f in cols.values() if isinstance(f, Call)]

        # Check all jobs appear in the log
        for job in list(config) + list(tables) + columns:
            log_string = f"[INFO] skypy.pipeline: Generating {job}"
            assert(log_string in err)

        # Check all functions appear in the log
        for f in functions:
            log_string = f"[INFO] skypy.pipeline: Calling {f.function.__name__}"
            assert(log_string in err)

        # Check cosmology appears in the log
        if cosmology:
            assert("[INFO] skypy.pipeline: Setting cosmology" in err)

        # Check writing output file is in the log
        assert(f"[INFO] skypy: Writing {output_filename}" in err)

        # Check error for existing output file is in the log
>       assert(f"[ERROR] skypy: File {output_filename!r} already exists." in err)
E       assert "[ERROR] skypy: File '/private/var/folders/nw/43x1r_9n2235_0prypvp329w0000gp/T/pytest-14/test_logging0/logging.fits' already exists." in '2021-10-26 10:12:54,832 [INFO] skypy.pipeline: Setting cosmology\n2021-10-26 10:12:54,832 [INFO] skypy.pipeline: Call...test-14/test_logging0/logging.fits already exists. If you mean to replace it then use the argument "overwrite=True".\n'

../../.tox/py39-test-dev/lib/python3.9/site-packages/skypy/pipeline/tests/test_skypy.py:96: AssertionError