'Test saving updates to a file where the header is one block smaller
than before, and in the case where the heade ris one block larger than
before.'

@pytest.mark.xfail(((platform.system() == 'Windows') and (not NUMPY_LT_1_12)), reason='https://github.com/astropy/astropy/issues/5797')
def test_update_resized_header(self):

    data = np.arange(100)
    hdu = fits.PrimaryHDU(data=data)
    idx = 1
    while (len(str(hdu.header)) <= 2880):
        hdu.header['TEST{}'.format(idx)] = idx
        idx += 1
    orig_header = hdu.header.copy()
    hdu.writeto(self.temp('temp.fits'))
    with fits.open(self.temp('temp.fits'), mode='update') as hdul:
        while (len(str(hdul[0].header)) > 2880):
            del hdul[0].header[(-1)]
    with fits.open(self.temp('temp.fits')) as hdul:
        assert (hdul[0].header == orig_header[:(-1)])
        assert (hdul[0].data == data).all()
    with fits.open(self.temp('temp.fits'), mode='update') as hdul:
        idx = 101
        while (len(str(hdul[0].header)) <= (2880 * 2)):
            hdul[0].header['TEST{}'.format(idx)] = idx
            idx += 1
        hdul[0].data[0] = 27
    with fits.open(self.temp('temp.fits')) as hdul:
        assert (hdul[0].header[:(-37)] == orig_header[:(-1)])
        assert (hdul[0].data[0] == 27)
        assert (hdul[0].data[1:] == data[1:]).all()
