|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from "vitest"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import os from "node:os"; |
| 5 | +import { collectFiles } from "./utils"; |
| 6 | + |
| 7 | +const conflictContent = ` |
| 8 | +{ |
| 9 | +<<<<<<< ours |
| 10 | + "value": 1 |
| 11 | +======= |
| 12 | + "value": 2 |
| 13 | +>>>>>>> theirs |
| 14 | +} |
| 15 | +`; |
| 16 | + |
| 17 | +const cleanContent = ` |
| 18 | +{ |
| 19 | + "value": 42 |
| 20 | +} |
| 21 | +`; |
| 22 | + |
| 23 | +describe("collectFiles", () => { |
| 24 | + let tmpDir: string; |
| 25 | + let conflictedFile: string; |
| 26 | + let cleanFile: string; |
| 27 | + let ignoredFile: string; |
| 28 | + |
| 29 | + beforeAll(async () => { |
| 30 | + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "collect-files-")); |
| 31 | + |
| 32 | + conflictedFile = path.join(tmpDir, "conflicted.json"); |
| 33 | + cleanFile = path.join(tmpDir, "clean.json"); |
| 34 | + ignoredFile = path.join(tmpDir, "ignored.txt"); |
| 35 | + |
| 36 | + await fs.writeFile(conflictedFile, conflictContent, "utf8"); |
| 37 | + await fs.writeFile(cleanFile, cleanContent, "utf8"); |
| 38 | + await fs.writeFile(ignoredFile, cleanContent, "utf8"); |
| 39 | + }); |
| 40 | + |
| 41 | + afterAll(async () => { |
| 42 | + await fs.rm(tmpDir, { recursive: true, force: true }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("collects only conflicted files by default", async () => { |
| 46 | + const files = await collectFiles({ |
| 47 | + root: tmpDir, |
| 48 | + fileFilter: file => file.endsWith(".json"), |
| 49 | + }); |
| 50 | + |
| 51 | + expect(files).toContain(conflictedFile); |
| 52 | + expect(files).not.toContain(cleanFile); |
| 53 | + expect(files).not.toContain(ignoredFile); |
| 54 | + }); |
| 55 | + |
| 56 | + it("collects conflicted + clean files when includeNonConflicted is true", async () => { |
| 57 | + const files = await collectFiles({ |
| 58 | + root: tmpDir, |
| 59 | + fileFilter: file => file.endsWith(".json"), |
| 60 | + includeNonConflicted: true, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(files).toContain(conflictedFile); |
| 64 | + expect(files).toContain(cleanFile); |
| 65 | + expect(files).not.toContain(ignoredFile); |
| 66 | + }); |
| 67 | + |
| 68 | + it("skips files that do not match fileFilter", async () => { |
| 69 | + const files = await collectFiles({ |
| 70 | + root: tmpDir, |
| 71 | + fileFilter: file => file.endsWith(".json"), |
| 72 | + }); |
| 73 | + |
| 74 | + expect(files).not.toContain(ignoredFile); |
| 75 | + }); |
| 76 | +}); |
0 commit comments