|
1 | 1 | import { strict as assert } from 'node:assert'; |
2 | | -import { mkdirSync, writeFileSync } from 'node:fs'; |
| 2 | +import { mkdirSync, rmSync, writeFileSync } from 'node:fs'; |
3 | 3 | import { join } from 'node:path'; |
4 | 4 | import { describe, it } from 'node:test'; |
5 | 5 | import { fileURLToPath } from 'node:url'; |
@@ -513,6 +513,44 @@ describe('Glob Loader', () => { |
513 | 513 | assert.ok(warnings.some((w) => w.includes('No files found matching'))); |
514 | 514 | }); |
515 | 515 |
|
| 516 | + it('prunes stale entries when the last file in a collection is deleted', async () => { |
| 517 | + const tempDir = createTempDir(); |
| 518 | + const contentDir = join(fileURLToPath(tempDir), 'src', 'content', 'posts'); |
| 519 | + mkdirSync(contentDir, { recursive: true }); |
| 520 | + writeFileSync(join(contentDir, 'post.md'), '---\ntitle: Post MD\n---\nContent MD'); |
| 521 | + |
| 522 | + const store = new MutableDataStore(); |
| 523 | + const settings = createMinimalSettings(tempDir, { |
| 524 | + contentEntryTypes: [createMarkdownEntryType()], |
| 525 | + }); |
| 526 | + const logger = new AstroLogger({ |
| 527 | + destination: { write: () => true }, |
| 528 | + level: 'silent', |
| 529 | + }); |
| 530 | + |
| 531 | + const collections = { |
| 532 | + posts: defineCollection({ |
| 533 | + loader: glob({ pattern: '*.md', base: 'src/content/posts' }), |
| 534 | + }), |
| 535 | + }; |
| 536 | + |
| 537 | + const contentLayer = new ContentLayer({ |
| 538 | + settings, |
| 539 | + logger, |
| 540 | + store, |
| 541 | + contentConfigObserver: createTestConfigObserver(collections), |
| 542 | + }); |
| 543 | + |
| 544 | + await contentLayer.sync(); |
| 545 | + assert.equal(store.values('posts').length, 1); |
| 546 | + |
| 547 | + // Delete the only file, leaving the (still existing) directory in place |
| 548 | + rmSync(join(contentDir, 'post.md')); |
| 549 | + |
| 550 | + await contentLayer.sync(); |
| 551 | + assert.equal(store.values('posts').length, 0); |
| 552 | + }); |
| 553 | + |
516 | 554 | it('throws on duplicate IDs when prerenderConflictBehavior is error', async () => { |
517 | 555 | const tempDir = createTempDir(); |
518 | 556 | const contentDir = join(fileURLToPath(tempDir), 'src', 'content', 'posts'); |
|
0 commit comments