@@ -115,3 +115,35 @@ def test_mutually_exclusive_parameters_raise(coder_with_file):
115115 assert result .startswith ("Error: Must specify exactly one of" )
116116 assert file_path .read_text ().startswith ("first line" )
117117 coder .io .tool_error .assert_called ()
118+
119+
120+ def test_trailing_newline_preservation (coder_with_file ):
121+ coder , file_path = coder_with_file
122+ insert_block .Tool .execute (
123+ coder ,
124+ file_path = "example.txt" ,
125+ content = "inserted line" ,
126+ position = "top" ,
127+ )
128+
129+ content = file_path .read_text ()
130+ assert content .endswith ("\n " ), "File should preserve trailing newline"
131+ coder .io .tool_error .assert_not_called ()
132+
133+
134+ def test_no_trailing_newline_preservation (coder_with_file ):
135+ coder , file_path = coder_with_file
136+
137+ content_without_trailing_newline = "first line\n second line"
138+ file_path .write_text (content_without_trailing_newline )
139+
140+ insert_block .Tool .execute (
141+ coder ,
142+ file_path = "example.txt" ,
143+ content = "inserted line" ,
144+ position = "top" ,
145+ )
146+
147+ content = file_path .read_text ()
148+ assert not content .endswith ("\n " ), "File should preserve lack of trailing newline"
149+ coder .io .tool_error .assert_not_called ()
0 commit comments