I'm still investigating this failure.
So far I could find 2 similar scenarios, both based on 1.16.5 with the patch, but one that fails, and the other that succeeds, seemingly consistently.
Case 1: using the 1.16.5 tarball + the patch applied, automake is built and tested within fedora environment (with specific env variables and flags ...), the test seems to pass consistently.
Case 2: using the 1.16.5 tarball + the patch applied, I distclean, bootstrap, reconfigure (using same configure command as case 1) and build manually, the test seems to fail consistently.
Both builds are run within the same chroot.
In the failure case, foo.o doesn't get rebuilt when the header is restored to its original state, whereas the header is listed in the foo.Tpo file. Since foo.o isn't rebuilt, the foo.Tpo file isn't moved to foo.Po, which leads to the test failure.
I'm trying to figure out why in one case foo.o is consistently rebuilt, while not in the other.