This uses the logic from tests.rb in man.rb too so that this can be shared in a few places.
Closes Homebrew/homebrew#32743.
Closes Homebrew/homebrew#32472.