Ensure trailing newline works with empty strings and undef