radio-astro-tools / spectral-cube

Library for reading and analyzing astrophysical spectral data cubes
http://spectral-cube.rtfd.org
BSD 3-Clause "New" or "Revised" License
96 stars 63 forks source link

[0.4.1] Attempt to write to test data directory during test #451

Open olebole opened 6 years ago

olebole commented 6 years ago

When running the test suite, when spectral-cube is installed system-wide (in /usr/lib/pythonX), some tests fails since they try to write to the tests dir, which is not writable for normal users. This results in a failure of the CI test suite on Debian:


    def test_3d_beams_roundtrip():
        c = SpectralCube.read(path('vda_beams.fits'))
        np.testing.assert_almost_equal(c.beams[0].major.value, 0.1)
>       c.write(path('vda_beams_out.fits'))

/usr/lib/python3/dist-packages/spectral_cube/tests/test_io.py:54: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3/dist-packages/spectral_cube/spectral_cube.py:1911: in write
    write(filename, self, overwrite=overwrite, format=format)
/usr/lib/python3/dist-packages/spectral_cube/io/core.py:66: in write
    write_fits_cube(filename, cube, overwrite=overwrite)
/usr/lib/python3/dist-packages/spectral_cube/io/fits.py:210: in write_fits_cube
    hdulist.writeto(filename, overwrite=overwrite)
/usr/lib/python3/dist-packages/astropy/utils/decorators.py:507: in wrapper
    return function(*args, **kwargs)
/usr/lib/python3/dist-packages/astropy/io/fits/hdu/hdulist.py:878: in writeto
    fileobj = _File(fileobj, mode='ostream', overwrite=overwrite)
/usr/lib/python3/dist-packages/astropy/utils/decorators.py:507: in wrapper
    return function(*args, **kwargs)
/usr/lib/python3/dist-packages/astropy/io/fits/file.py:150: in __init__
    self._open_filename(fileobj, mode, overwrite)
/usr/lib/python3/dist-packages/astropy/io/fits/file.py:506: in _open_filename
    self._file = fileobj_open(self.name, IO_FITS_MODES[mode])
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

filename = '/usr/lib/python3/dist-packages/spectral_cube/tests/data/vda_beams_out.fits'
mode = 'wb'

    def fileobj_open(filename, mode):
        """[...]"""

>       return open(filename, mode, buffering=0)
E       PermissionError: [Errno 13] Permission denied: '/usr/lib/python3/dist-packages/spectral_cube/tests/data/vda_beams_out.fits'

/usr/lib/python3/dist-packages/astropy/io/fits/util.py:409: PermissionError
___________________________ test_4d_beams_roundtrip ____________________________

    def test_4d_beams_roundtrip():
        # not sure if 4d can round-trip...
        c = SpectralCube.read(path('sdav_beams.fits'))
        np.testing.assert_almost_equal(c.beams[0].major.value, 0.1)
>       c.write(path('sdav_beams_out.fits'))

/usr/lib/python3/dist-packages/spectral_cube/tests/test_io.py:68: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3/dist-packages/spectral_cube/spectral_cube.py:1911: in write
    write(filename, self, overwrite=overwrite, format=format)
/usr/lib/python3/dist-packages/spectral_cube/io/core.py:66: in write
    write_fits_cube(filename, cube, overwrite=overwrite)
/usr/lib/python3/dist-packages/spectral_cube/io/fits.py:210: in write_fits_cube
    hdulist.writeto(filename, overwrite=overwrite)
/usr/lib/python3/dist-packages/astropy/utils/decorators.py:507: in wrapper
    return function(*args, **kwargs)
/usr/lib/python3/dist-packages/astropy/io/fits/hdu/hdulist.py:878: in writeto
    fileobj = _File(fileobj, mode='ostream', overwrite=overwrite)
/usr/lib/python3/dist-packages/astropy/utils/decorators.py:507: in wrapper
    return function(*args, **kwargs)
/usr/lib/python3/dist-packages/astropy/io/fits/file.py:150: in __init__
    self._open_filename(fileobj, mode, overwrite)
/usr/lib/python3/dist-packages/astropy/io/fits/file.py:506: in _open_filename
    self._file = fileobj_open(self.name, IO_FITS_MODES[mode])
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

filename = '/usr/lib/python3/dist-packages/spectral_cube/tests/data/sdav_beams_out.fits'
mode = 'wb'

    def fileobj_open(filename, mode):
        """[...]"""

>       return open(filename, mode, buffering=0)
E       PermissionError: [Errno 13] Permission denied: '/usr/lib/python3/dist-packages/spectral_cube/tests/data/sdav_beams_out.fits'

/usr/lib/python3/dist-packages/astropy/io/fits/util.py:409: PermissionError
________________________________ test_strip_wcs ________________________________

    def test_strip_wcs():

        header1 = fits.Header.fromtextfile(path('cubewcs1.hdr'))
        header1_stripped = strip_wcs_from_header(header1)

        with open(path('cubewcs1.hdr'),'r') as fh:
            hdrlines = fh.readlines()

        hdrlines.insert(-20,"\n")
        hdrlines.insert(-1,"\n")
>       with open(path('cubewcs1_blanks.hdr'),'w') as fh:
E       PermissionError: [Errno 13] Permission denied: '/usr/lib/python3/dist-packages/spectral_cube/tests/data/cubewcs1_blanks.hdr'

/usr/lib/python3/dist-packages/spectral_cube/tests/test_wcs_utils.py:124: PermissionError
keflavich commented 6 years ago

It looks like it was OK for the setup script to write the test data originally, though? Before running the tests, this script: https://github.com/radio-astro-tools/spectral-cube/blob/master/spectral_cube/tests/data/make_test_cubes.py is run to generate the test data.

I suppose we can just allow PermissionErrors to solve this issue.

olebole commented 6 years ago

Yes, the test data was generated during the build of the package.

olebole commented 4 years ago

Just as an update for version 0.5.0: This error disappeared on its original place, however there is now a new case here:

https://github.com/radio-astro-tools/spectral-cube/blob/df078f433b775c91dda3e7c7df8496c68244324d/spectral_cube/tests/test_wcs_utils.py#L159-L167

IMO the new file name should be replaced by a tempfile as well.