|
60 | 60 | $writer->write('test guidelines content');
|
61 | 61 |
|
62 | 62 | $content = file_get_contents($tempFile);
|
63 |
| - expect($content)->toBe("<laravel-boost-guidelines>\ntest guidelines content\n</laravel-boost-guidelines>"); |
| 63 | + expect($content)->toBe("<laravel-boost-guidelines>\ntest guidelines content\n</laravel-boost-guidelines>\n"); |
64 | 64 |
|
65 | 65 | unlink($tempFile);
|
66 | 66 | });
|
|
77 | 77 | $writer->write('new guidelines');
|
78 | 78 |
|
79 | 79 | $content = file_get_contents($tempFile);
|
80 |
| - expect($content)->toBe("# Existing content\n\nSome text here.\n\n===\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>"); |
| 80 | + expect($content)->toBe("# Existing content\n\nSome text here.\n\n===\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>\n"); |
81 | 81 |
|
82 | 82 | unlink($tempFile);
|
83 | 83 | });
|
|
95 | 95 | $writer->write('updated guidelines');
|
96 | 96 |
|
97 | 97 | $content = file_get_contents($tempFile);
|
98 |
| - expect($content)->toBe("# Header\n\n<laravel-boost-guidelines>\nupdated guidelines\n</laravel-boost-guidelines>\n\n# Footer"); |
| 98 | + expect($content)->toBe("# Header\n\n<laravel-boost-guidelines>\nupdated guidelines\n</laravel-boost-guidelines>\n\n# Footer\n"); |
| 99 | + |
| 100 | + unlink($tempFile); |
| 101 | +}); |
| 102 | + |
| 103 | +test('it avoids adding extra newline if one already exists', function () { |
| 104 | + $tempFile = tempnam(sys_get_temp_dir(), 'boost_test_'); |
| 105 | + $initialContent = "# Header\n\n<laravel-boost-guidelines>\nold guidelines\n</laravel-boost-guidelines>\n\n# Footer\n"; |
| 106 | + file_put_contents($tempFile, $initialContent); |
| 107 | + |
| 108 | + $agent = Mockery::mock(Agent::class); |
| 109 | + $agent->shouldReceive('guidelinesPath')->andReturn($tempFile); |
| 110 | + $agent->shouldReceive('frontmatter')->andReturn(false); |
| 111 | + |
| 112 | + $writer = new GuidelineWriter($agent); |
| 113 | + $writer->write('updated guidelines'); |
| 114 | + |
| 115 | + $content = file_get_contents($tempFile); |
| 116 | + expect($content)->toBe("# Header\n\n<laravel-boost-guidelines>\nupdated guidelines\n</laravel-boost-guidelines>\n\n# Footer\n"); |
| 117 | + |
| 118 | + // Assert no double newline at the end |
| 119 | + expect(substr($content, -2))->not->toBe("\n\n"); |
| 120 | + // Assert still ends with exactly one newline |
| 121 | + expect(substr($content, -1))->toBe("\n"); |
99 | 122 |
|
100 | 123 | unlink($tempFile);
|
101 | 124 | });
|
|
114 | 137 |
|
115 | 138 | $content = file_get_contents($tempFile);
|
116 | 139 | // Should replace in-place, preserving structure
|
117 |
| - expect($content)->toBe("Start\n<laravel-boost-guidelines>\nsingle line\n</laravel-boost-guidelines>\nEnd"); |
| 140 | + expect($content)->toBe("Start\n<laravel-boost-guidelines>\nsingle line\n</laravel-boost-guidelines>\nEnd\n"); |
118 | 141 |
|
119 | 142 | unlink($tempFile);
|
120 | 143 | });
|
|
133 | 156 |
|
134 | 157 | $content = file_get_contents($tempFile);
|
135 | 158 | // Should replace first occurrence, second block remains untouched due to non-greedy matching
|
136 |
| - expect($content)->toBe("Start\n<laravel-boost-guidelines>\nreplacement\n</laravel-boost-guidelines>\nMiddle\n<laravel-boost-guidelines>\nsecond\n</laravel-boost-guidelines>\nEnd"); |
| 159 | + expect($content)->toBe("Start\n<laravel-boost-guidelines>\nreplacement\n</laravel-boost-guidelines>\nMiddle\n<laravel-boost-guidelines>\nsecond\n</laravel-boost-guidelines>\nEnd\n"); |
137 | 160 |
|
138 | 161 | unlink($tempFile);
|
139 | 162 | });
|
|
165 | 188 | $writer->write('my guidelines');
|
166 | 189 |
|
167 | 190 | $content = file_get_contents($tempFile);
|
168 |
| - expect($content)->toBe("# Title\n\nParagraph 1\n\nParagraph 2\n\n===\n\n<laravel-boost-guidelines>\nmy guidelines\n</laravel-boost-guidelines>"); |
| 191 | + expect($content)->toBe("# Title\n\nParagraph 1\n\nParagraph 2\n\n===\n\n<laravel-boost-guidelines>\nmy guidelines\n</laravel-boost-guidelines>\n"); |
169 | 192 |
|
170 | 193 | unlink($tempFile);
|
171 | 194 | });
|
|
182 | 205 | $writer->write('first guidelines');
|
183 | 206 |
|
184 | 207 | $content = file_get_contents($tempFile);
|
185 |
| - expect($content)->toBe("<laravel-boost-guidelines>\nfirst guidelines\n</laravel-boost-guidelines>"); |
| 208 | + expect($content)->toBe("<laravel-boost-guidelines>\nfirst guidelines\n</laravel-boost-guidelines>\n"); |
186 | 209 |
|
187 | 210 | unlink($tempFile);
|
188 | 211 | });
|
|
199 | 222 | $writer->write('clean guidelines');
|
200 | 223 |
|
201 | 224 | $content = file_get_contents($tempFile);
|
202 |
| - expect($content)->toBe("<laravel-boost-guidelines>\nclean guidelines\n</laravel-boost-guidelines>"); |
| 225 | + expect($content)->toBe("<laravel-boost-guidelines>\nclean guidelines\n</laravel-boost-guidelines>\n"); |
203 | 226 |
|
204 | 227 | unlink($tempFile);
|
205 | 228 | });
|
|
218 | 241 |
|
219 | 242 | expect($result)->toBe(GuidelineWriter::REPLACED);
|
220 | 243 | $content = file_get_contents($tempFile);
|
221 |
| - expect($content)->toBe("# Title\n\n<other-rules>\nShould not be touched\n</other-rules>\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>\n\n<custom-config>\nAlso untouched\n</custom-config>"); |
| 244 | + expect($content)->toBe("# Title\n\n<other-rules>\nShould not be touched\n</other-rules>\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>\n\n<custom-config>\nAlso untouched\n</custom-config>\n"); |
222 | 245 |
|
223 | 246 | unlink($tempFile);
|
224 | 247 | });
|
|
248 | 271 | ->and($content)->toContain('More content here.');
|
249 | 272 |
|
250 | 273 | // Verify exact structure
|
251 |
| - expect($content)->toBe("# My Project\n\n<laravel-boost-guidelines>\nupdated guidelines from boost\n</laravel-boost-guidelines>\n\n# User Added Section\nThis content was added by the user after the guidelines.\n\n## Another user section\nMore content here."); |
| 274 | + expect($content)->toBe("# My Project\n\n<laravel-boost-guidelines>\nupdated guidelines from boost\n</laravel-boost-guidelines>\n\n# User Added Section\nThis content was added by the user after the guidelines.\n\n## Another user section\nMore content here.\n"); |
252 | 275 |
|
253 | 276 | unlink($tempFile);
|
254 | 277 | });
|
|
269 | 292 | $writer->write('new guidelines');
|
270 | 293 |
|
271 | 294 | $content = file_get_contents($tempFile);
|
272 |
| - expect($content)->toBe("---\nalwaysApply: true\n---\n# Existing content\n\nSome text here.\n\n===\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>"); |
| 295 | + expect($content)->toBe("---\nalwaysApply: true\n---\n# Existing content\n\nSome text here.\n\n===\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>\n"); |
273 | 296 |
|
274 | 297 | unlink($tempFile);
|
275 | 298 | });
|
|
286 | 309 | $writer->write('new guidelines');
|
287 | 310 |
|
288 | 311 | $content = file_get_contents($tempFile);
|
289 |
| - expect($content)->toBe("---\ncustomOption: true\n---\n# Existing content\n\nSome text here.\n\n===\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>"); |
| 312 | + expect($content)->toBe("---\ncustomOption: true\n---\n# Existing content\n\nSome text here.\n\n===\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>\n"); |
290 | 313 |
|
291 | 314 | unlink($tempFile);
|
292 | 315 | });
|
|
304 | 327 |
|
305 | 328 | expect($result)->toBe(GuidelineWriter::NEW);
|
306 | 329 | $content = file_get_contents($tempFile);
|
307 |
| - expect($content)->toBe("# Existing content\n\nSome text here.\n\n===\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>"); |
| 330 | + expect($content)->toBe("# Existing content\n\nSome text here.\n\n===\n\n<laravel-boost-guidelines>\nnew guidelines\n</laravel-boost-guidelines>\n"); |
308 | 331 |
|
309 | 332 | unlink($tempFile);
|
310 | 333 | });
|
0 commit comments