'Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/151
Tests that the EXTNAME keyword works with Primary HDUs as well, and
interacts properly with the .name attribute.  For convenience
hdulist[\'PRIMARY\'] will still refer to the first HDU even if it has an
EXTNAME not equal to \'PRIMARY\'.'

def test_primary_with_extname(self):

    prihdr = fits.Header([('EXTNAME', 'XPRIMARY'), ('EXTVER', 1)])
    hdul = fits.HDUList([fits.PrimaryHDU(header=prihdr)])
    assert ('EXTNAME' in hdul[0].header)
    assert (hdul[0].name == 'XPRIMARY')
    assert (hdul[0].name == hdul[0].header['EXTNAME'])
    info = [(0, 'XPRIMARY', 1, 'PrimaryHDU', 5, (), '', '')]
    assert (hdul.info(output=False) == info)
    assert (hdul['PRIMARY'] is hdul['XPRIMARY'])
    assert (hdul['PRIMARY'] is hdul[('XPRIMARY', 1)])
    hdul[0].name = 'XPRIMARY2'
    assert (hdul[0].header['EXTNAME'] == 'XPRIMARY2')
    hdul.writeto(self.temp('test.fits'))
    with fits.open(self.temp('test.fits')) as hdul:
        assert (hdul[0].name == 'XPRIMARY2')
