PR #66461 introduced a regression that resulted in an in correct block in the file if the block to be inserted did not end with a line separator. Fix this bug and add tests to cover this scenario.