patchtest/selftest: Extract head-attached test loop into function
Move the loop that run the tests in head attached tests into a function
'test_head_attached'. Also add an explicit check for the case where no patches
are found and exit with an error.