'Tests whether a file opened without a specified io.fits mode
(\'readonly\', etc.) is opened in a mode appropriate for the given file
object.'

def test_fileobj_mode_guessing(self):

    self.copy_file('test0.fits')
    for mode in ('r', 'w', 'a'):
        with open(self.temp('test0.fits'), mode) as f:
            pytest.raises(ValueError, fits.HDUList.fromfile, f)
    self.copy_file('test0.fits')
    with open(self.temp('test0.fits'), 'rb') as f:
        with fits.HDUList.fromfile(f) as h:
            assert (h.fileinfo(0)['filemode'] == 'readonly')
    for mode in ('wb', 'ab'):
        with open(self.temp('test0.fits'), mode) as f:
            with fits.HDUList.fromfile(f) as h:
                assert (len(h) == 0)
    self.copy_file('test0.fits')
    with open(self.temp('test0.fits'), 'wb+') as f:
        with fits.HDUList.fromfile(f) as h:
            assert (len(h) == 0)
    self.copy_file('test0.fits')
    with open(self.temp('test0.fits'), 'rb+') as f:
        with fits.HDUList.fromfile(f) as h:
            assert (h.fileinfo(0)['filemode'] == 'update')
    with open(self.temp('test0.fits'), 'ab+') as f:
        with fits.HDUList.fromfile(f) as h:
            assert (h.fileinfo(0)['filemode'] == 'append')
