also looking at this
fix(deps-file): anchor the dependency regex to a key, and rewrite each match from its own captures
#1454
3 files
+216
-41
| @@ -192,6 +192,140 @@ dependencies: | ||
| 192 | 192 | }) |
| 193 | 193 | }) |
| 194 | 194 | |
| 195 | describe('comment and multi-occurrence corruption (stacksjs/buddy#1453)', () => { | |
| 196 | const mk = (name: string, currentVersion: string, newVersion: string): PackageUpdate => ({ | |
| 197 | name, | |
| 198 | currentVersion, | |
| 199 | newVersion, | |
| 200 | updateType: 'minor', | |
| 201 | dependencyType: 'dependencies', | |
| 202 | file: 'deps.yaml', | |
| 203 | metadata: undefined, | |
| 204 | }) | |
| 205 | ||
| 206 | it('should not take its replacement text from a package name inside a comment', async () => { | |
| 207 | // Verbatim from the PR that produced a deps.yaml which no longer parsed: | |
| 208 | // the comment mentions `sqlite: latest`, and because the old regex was | |
| 209 | // unanchored that prose was the FIRST match, so its captures drove the | |
| 210 | // replacement for the real key - and its greedy leading `\s*` swallowed | |
| 211 | // the newline, collapsing `sqlite` onto the `postgres` line. | |
| 212 | const content = `# That is not hypothetical: \`sqlite: latest\` was fine only because the SDK did | |
| 213 | # not support sqlite.org and skipped it with a warning. | |
| 214 | dependencies: | |
| 215 | bun: ^1.3.14 | |
| 216 | mysql: ^8.0.45 | |
| 217 | postgres: ^18.0.0 | |
| 218 | sqlite: ^3.52.0 | |
| 219 | ` | |
| 220 | ||
| 221 | const result = await updateDependencyFile('deps.yaml', content, [ | |
| 222 | mk('postgres', '^18.0.0', '18.6'), | |
| 223 | mk('sqlite', '^3.52.0', '3.53.4'), | |
| 224 | ]) | |
| 225 | ||
| 226 | // The whole point: the file still parses. | |
| 227 | expect(() => Bun.YAML.parse(result)).not.toThrow() | |
| 228 | ||
| 229 | // Both keys survive, on their own lines, with their own versions. | |
| 230 | expect(result).toMatch(/^ {2}postgres: \^18\.6$/m) | |
| 231 | expect(result).toMatch(/^ {2}sqlite: \^3\.53\.4$/m) | |
| 232 | ||
| 233 | // The comment is prose and must come out byte-identical. | |
| 234 | expect(result).toContain('# That is not hypothetical: `sqlite: latest` was fine only because the SDK did') | |
| 235 | }) | |
| 236 | ||
| 237 | it('should rewrite each occurrence from its own captures, not the first match', async () => { | |
| 238 | // A `g` regex was used with a single replacement string built from the | |
| 239 | // first match, so the first occurrence's rendered text was written over | |
| 240 | // every other occurrence. Two occurrences with DIFFERENT prefixes are | |
| 241 | // what exposes it: with the same prefix the wrong answer and the right | |
| 242 | // answer are identical. | |
| 243 | const content = `dependencies: | |
| 244 | typescript: ^5.0.0 | |
| 245 | ||
| 246 | devDependencies: | |
| 247 | typescript: ~4.9.0 | |
| 248 | ` | |
| 249 | ||
| 250 | const result = await updateDependencyFile('deps.yaml', content, [ | |
| 251 | mk('typescript', '^5.0.0', '5.6.2'), | |
| 252 | ]) | |
| 253 | ||
| 254 | expect(() => Bun.YAML.parse(result)).not.toThrow() | |
| 255 | // Each line keeps the prefix it was written with. | |
| 256 | expect(result).toMatch(/^ {2}typescript: \^5\.6\.2$/m) | |
| 257 | expect(result).toMatch(/^ {2}typescript: ~5\.6\.2$/m) | |
| 258 | }) | |
| 259 | ||
| 260 | it('should leave a commented-out dependency alone', async () => { | |
| 261 | const content = `dependencies: | |
| 262 | # redis: ^7.4.1 # disabled for now | |
| 263 | sqlite3: ^3.50.3 | |
| 264 | ` | |
| 265 | ||
| 266 | const result = await updateDependencyFile('deps.yaml', content, [ | |
| 267 | mk('redis', '^7.4.1', '7.5.0'), | |
| 268 | ]) | |
| 269 | ||
| 270 | expect(result).toContain('# redis: ^7.4.1 # disabled for now') | |
| 271 | expect(result).not.toContain('7.5.0') | |
| 272 | }) | |
| 273 | ||
| 274 | it('should respect a dynamic version even when it is quoted', async () => { | |
| 275 | // `"*"` captured with its quotes missed the `['latest', '*', ...]` test, | |
| 276 | // so the guard never fired and the prefix logic produced `"*"3.13.5`. | |
| 277 | const content = `dependencies: | |
| 278 | python.org: "*" | |
| 279 | node: "latest" | |
| 280 | ` | |
| 281 | ||
| 282 | const result = await updateDependencyFile('deps.yaml', content, [ | |
| 283 | mk('python.org', '*', '3.13.5'), | |
| 284 | mk('node', 'latest', '22.17.1'), | |
| 285 | ]) | |
| 286 | ||
| 287 | expect(() => Bun.YAML.parse(result)).not.toThrow() | |
| 288 | expect(result).toContain('python.org: "*"') | |
| 289 | expect(result).toContain('node: "latest"') | |
| 290 | expect(result).not.toContain('3.13.5') | |
| 291 | expect(result).not.toContain('22.17.1') | |
| 292 | }) | |
| 293 | ||
| 294 | it('should preserve the quoting style of a version it does update', async () => { | |
| 295 | const content = `dependencies: | |
| 296 | typescript: "^5.0.0" | |
| 297 | node: '^22.12.0' | |
| 298 | ` | |
| 299 | ||
| 300 | const result = await updateDependencyFile('deps.yaml', content, [ | |
| 301 | mk('typescript', '^5.0.0', '5.6.2'), | |
| 302 | mk('node', '^22.12.0', '22.17.1'), | |
| 303 | ]) | |
| 304 | ||
| 305 | expect(() => Bun.YAML.parse(result)).not.toThrow() | |
| 306 | expect(result).toContain('typescript: "^5.6.2"') | |
| 307 | expect(result).toContain('node: \'^22.17.1\'') | |
| 308 | }) | |
| 309 | ||
| 310 | it('should still update a file that did not parse as YAML to begin with', async () => { | |
| 311 | // The parse guard exists to stop THIS function corrupting a file. A file | |
| 312 | // that was already broken is not its business, and bailing there would | |
| 313 | // silently stop updating it. | |
| 314 | const content = `dependencies: | |
| 315 | bun: ^1.3.14 | |
| 316 | : this line is not valid yaml | |
| 317 | ` | |
| 318 | ||
| 319 | expect(() => Bun.YAML.parse(content)).toThrow() | |
| 320 | ||
| 321 | const result = await updateDependencyFile('deps.yaml', content, [ | |
| 322 | mk('bun', '^1.3.14', '1.4.2'), | |
| 323 | ]) | |
| 324 | ||
| 325 | expect(result).toContain('bun: ^1.4.2') | |
| 326 | }) | |
| 327 | }) | |
| 328 | ||
| 195 | 329 | describe('YAML comment preservation', () => { |
| 196 | 330 | it('should preserve inline comments when updating dependencies', async () => { |
| 197 | 331 | const content = `dependencies: |