|
1 | 1 | import { describe, it, expect, beforeAll, afterAll } from "vitest"; |
2 | 2 | import fs from "node:fs/promises"; |
3 | 3 | import path from "node:path"; |
4 | | -import os from "node:os"; |
5 | | -import { listMatchingFiles } 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-")); |
| 4 | +import { hasConflict, createSkipDirectoryMatcher, listMatchingFiles, backupFile } from "./utils"; // adjust import |
| 5 | +import { basicMatcher } from "./matcher"; |
| 6 | + |
| 7 | +const matcher = basicMatcher; |
| 8 | + |
| 9 | +const TMP = path.join(process.cwd(), "tmp-test"); |
| 10 | + |
| 11 | +beforeAll(async () => { |
| 12 | + await fs.rm(TMP, { recursive: true, force: true }); |
| 13 | + await fs.mkdir(TMP, { recursive: true }); |
| 14 | + |
| 15 | + // Create dirs & files |
| 16 | + await fs.writeFile(path.join(TMP, "clean.txt"), "hello"); |
| 17 | + await fs.writeFile( |
| 18 | + path.join(TMP, "conflict.txt"), |
| 19 | + "line1\n<<<<<<< ours\nconflict\n=======\ntheirs\n>>>>>>>", |
| 20 | + ); |
| 21 | + await fs.mkdir(path.join(TMP, "src")); |
| 22 | + await fs.writeFile(path.join(TMP, "src", "index.ts"), "console.log('ok')"); |
| 23 | + await fs.mkdir(path.join(TMP, "node_modules")); |
| 24 | + await fs.writeFile(path.join(TMP, "node_modules", "ignore.js"), "ignored"); |
| 25 | +}); |
31 | 26 |
|
32 | | - conflictedFile = path.join(tmpDir, "conflicted.json"); |
33 | | - cleanFile = path.join(tmpDir, "clean.json"); |
34 | | - ignoredFile = path.join(tmpDir, "ignored.txt"); |
| 27 | +afterAll(async () => { |
| 28 | + await fs.rm(TMP, { recursive: true, force: true }); |
| 29 | +}); |
35 | 30 |
|
36 | | - await fs.writeFile(conflictedFile, conflictContent, "utf8"); |
37 | | - await fs.writeFile(cleanFile, cleanContent, "utf8"); |
38 | | - await fs.writeFile(ignoredFile, cleanContent, "utf8"); |
| 31 | +describe("hasConflict", () => { |
| 32 | + it("detects conflict markers", () => { |
| 33 | + expect(hasConflict("<<<<<<<\n=======\n>>>>>>>")).toBe(true); |
39 | 34 | }); |
40 | | - |
41 | | - afterAll(async () => { |
42 | | - await fs.rm(tmpDir, { recursive: true, force: true }); |
| 35 | + it("returns false for clean content", () => { |
| 36 | + expect(hasConflict("no conflicts here")).toBe(false); |
43 | 37 | }); |
| 38 | +}); |
44 | 39 |
|
45 | | - it("collects only conflicted files by default", async () => { |
| 40 | +describe("listMatchingFiles", () => { |
| 41 | + it("finds only conflicted files by default", async () => { |
46 | 42 | const files = await listMatchingFiles({ |
47 | | - root: tmpDir, |
48 | | - fileFilter: file => file.endsWith(".json"), |
| 43 | + root: TMP, |
| 44 | + include: ["**/*.txt"], |
| 45 | + exclude: ["node_modules/**"], |
| 46 | + matcher, |
49 | 47 | }); |
50 | | - |
51 | | - const paths = files.map(f => f.filePath); |
52 | | - |
53 | | - expect(paths).toContain(conflictedFile); |
54 | | - expect(paths).not.toContain(cleanFile); |
55 | | - expect(paths).not.toContain(ignoredFile); |
56 | | - |
57 | | - // check content as well |
58 | | - const conflicted = files.find(f => f.filePath === conflictedFile); |
59 | | - expect(conflicted?.content).toContain("<<<<<<<"); |
| 48 | + const names = files.map(f => path.basename(f.filePath)); |
| 49 | + expect(names).toContain("conflict.txt"); |
| 50 | + expect(names).not.toContain("clean.txt"); |
60 | 51 | }); |
61 | 52 |
|
62 | | - it("collects conflicted + clean files when includeNonConflicted is true", async () => { |
| 53 | + it("includes non-conflicted if option enabled", async () => { |
63 | 54 | const files = await listMatchingFiles({ |
64 | | - root: tmpDir, |
65 | | - fileFilter: file => file.endsWith(".json"), |
| 55 | + root: TMP, |
| 56 | + include: ["**/*.txt"], |
| 57 | + exclude: [], |
| 58 | + matcher, |
66 | 59 | includeNonConflicted: true, |
67 | 60 | }); |
68 | | - |
69 | | - const paths = files.map(f => f.filePath); |
70 | | - |
71 | | - expect(paths).toContain(conflictedFile); |
72 | | - expect(paths).toContain(cleanFile); |
73 | | - expect(paths).not.toContain(ignoredFile); |
74 | | - |
75 | | - const clean = files.find(f => f.filePath === cleanFile); |
76 | | - expect(clean?.content).toContain('"value": 42'); |
| 61 | + const names = files.map(f => path.basename(f.filePath)); |
| 62 | + expect(names).toEqual(expect.arrayContaining(["clean.txt", "conflict.txt"])); |
77 | 63 | }); |
78 | 64 |
|
79 | | - it("skips files that do not match fileFilter", async () => { |
| 65 | + it("only files - no dirs", async () => { |
80 | 66 | const files = await listMatchingFiles({ |
81 | | - root: tmpDir, |
82 | | - fileFilter: file => file.endsWith(".json"), |
| 67 | + root: TMP, |
| 68 | + include: ["*.txt"], |
| 69 | + exclude: [], |
| 70 | + matcher, |
83 | 71 | }); |
| 72 | + const names = files.map(f => path.basename(f.filePath)); |
| 73 | + expect(names).toEqual(expect.arrayContaining(["conflict.txt"])); |
| 74 | + }); |
| 75 | +}); |
84 | 76 |
|
85 | | - const paths = files.map(f => f.filePath); |
86 | | - expect(paths).not.toContain(ignoredFile); |
| 77 | +describe("backupFile", () => { |
| 78 | + it("creates backup copy under .merge-backups", async () => { |
| 79 | + const src = path.join(TMP, "clean.txt"); |
| 80 | + const backup = await backupFile(src); |
| 81 | + const exists = await fs.readFile(backup, "utf8"); |
| 82 | + expect(exists).toBe("hello"); |
| 83 | + expect(backup).toMatch(/\.merge-backups/); |
87 | 84 | }); |
88 | 85 | }); |
0 commit comments