|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import { |
| 3 | + sanitizeGitignorePattern, |
| 4 | + parseGitignoreContent, |
| 5 | + createIgnoreInstanceFromFile, |
| 6 | + GitignoreParseResult, |
| 7 | +} from "../gitignore-parser" |
| 8 | +import * as fs from "fs/promises" |
| 9 | + |
| 10 | +// Mock fs/promises |
| 11 | +vi.mock("fs/promises") |
| 12 | + |
| 13 | +describe("gitignore-parser", () => { |
| 14 | + beforeEach(() => { |
| 15 | + vi.clearAllMocks() |
| 16 | + // Reset console mocks |
| 17 | + vi.spyOn(console, "warn").mockImplementation(() => {}) |
| 18 | + vi.spyOn(console, "info").mockImplementation(() => {}) |
| 19 | + }) |
| 20 | + |
| 21 | + describe("sanitizeGitignorePattern", () => { |
| 22 | + it("should return null for empty lines and comments", () => { |
| 23 | + expect(sanitizeGitignorePattern("")).toBeNull() |
| 24 | + expect(sanitizeGitignorePattern(" ")).toBeNull() |
| 25 | + expect(sanitizeGitignorePattern("# comment")).toBeNull() |
| 26 | + expect(sanitizeGitignorePattern(" # comment with spaces")).toBeNull() |
| 27 | + }) |
| 28 | + |
| 29 | + it("should handle invalid character ranges like [A-/]", () => { |
| 30 | + const result = sanitizeGitignorePattern("pqh[A-/]") |
| 31 | + expect(result).not.toBeNull() |
| 32 | + expect(result?.transformed).toBe("pqhA") |
| 33 | + expect(result?.reason).toContain("Invalid character range") |
| 34 | + expect(result?.reason).toContain("literal 'A'") |
| 35 | + }) |
| 36 | + |
| 37 | + it("should handle reverse ranges like [Z-A]", () => { |
| 38 | + const result = sanitizeGitignorePattern("test[Z-A]file") |
| 39 | + expect(result).not.toBeNull() |
| 40 | + expect(result?.transformed).toBe("testZfile") |
| 41 | + expect(result?.reason).toContain("Reverse character range") |
| 42 | + }) |
| 43 | + |
| 44 | + it("should handle multiple invalid ranges in one pattern", () => { |
| 45 | + const result = sanitizeGitignorePattern("test[A-/]and[Z-B]") |
| 46 | + expect(result).not.toBeNull() |
| 47 | + // First transformation should handle [A-/] |
| 48 | + expect(result?.transformed).toContain("testA") |
| 49 | + }) |
| 50 | + |
| 51 | + it("should return null for valid patterns", () => { |
| 52 | + expect(sanitizeGitignorePattern("*.log")).toBeNull() |
| 53 | + expect(sanitizeGitignorePattern("node_modules/")).toBeNull() |
| 54 | + expect(sanitizeGitignorePattern("!important.txt")).toBeNull() |
| 55 | + expect(sanitizeGitignorePattern("[a-z]*")).toBeNull() |
| 56 | + expect(sanitizeGitignorePattern("test[A-Z]file")).toBeNull() |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe("parseGitignoreContent", () => { |
| 61 | + it("should parse valid gitignore content successfully", () => { |
| 62 | + const content = ` |
| 63 | +# Dependencies |
| 64 | +node_modules/ |
| 65 | +*.log |
| 66 | +
|
| 67 | +# Build outputs |
| 68 | +dist/ |
| 69 | +build/ |
| 70 | +
|
| 71 | +# Environment |
| 72 | +.env |
| 73 | +.env.local |
| 74 | +` |
| 75 | + const { ignoreInstance, parseResult } = parseGitignoreContent(content, false) |
| 76 | + |
| 77 | + expect(parseResult.validPatterns).toContain("node_modules/") |
| 78 | + expect(parseResult.validPatterns).toContain("*.log") |
| 79 | + expect(parseResult.validPatterns).toContain("dist/") |
| 80 | + expect(parseResult.validPatterns).toContain("build/") |
| 81 | + expect(parseResult.validPatterns).toContain(".env") |
| 82 | + expect(parseResult.validPatterns).toContain(".env.local") |
| 83 | + expect(parseResult.invalidPatterns).toHaveLength(0) |
| 84 | + expect(parseResult.transformedPatterns).toHaveLength(0) |
| 85 | + |
| 86 | + // Test that the ignore instance works |
| 87 | + expect(ignoreInstance.ignores("node_modules/index.js")).toBe(true) |
| 88 | + expect(ignoreInstance.ignores("test.log")).toBe(true) |
| 89 | + expect(ignoreInstance.ignores(".env")).toBe(true) |
| 90 | + expect(ignoreInstance.ignores("src/index.js")).toBe(false) |
| 91 | + }) |
| 92 | + |
| 93 | + it("should handle invalid patterns gracefully", () => { |
| 94 | + const content = ` |
| 95 | +node_modules/ |
| 96 | +pqh[A-/] |
| 97 | +*.log |
| 98 | +[Z-A]invalid |
| 99 | +dist/ |
| 100 | +` |
| 101 | + const { ignoreInstance, parseResult } = parseGitignoreContent(content, false) |
| 102 | + |
| 103 | + // Valid patterns should be parsed |
| 104 | + expect(parseResult.validPatterns).toContain("node_modules/") |
| 105 | + expect(parseResult.validPatterns).toContain("*.log") |
| 106 | + expect(parseResult.validPatterns).toContain("dist/") |
| 107 | + |
| 108 | + // Invalid patterns should be transformed |
| 109 | + expect(parseResult.transformedPatterns).toHaveLength(2) |
| 110 | + expect(parseResult.transformedPatterns[0].original).toBe("pqh[A-/]") |
| 111 | + expect(parseResult.transformedPatterns[0].transformed).toBe("pqhA") |
| 112 | + expect(parseResult.transformedPatterns[1].original).toBe("[Z-A]invalid") |
| 113 | + expect(parseResult.transformedPatterns[1].transformed).toBe("Zinvalid") |
| 114 | + |
| 115 | + // No patterns should be completely invalid after transformation |
| 116 | + expect(parseResult.invalidPatterns).toHaveLength(0) |
| 117 | + |
| 118 | + // Test that the ignore instance works with transformed patterns |
| 119 | + expect(ignoreInstance.ignores("pqhA")).toBe(true) |
| 120 | + expect(ignoreInstance.ignores("Zinvalid")).toBe(true) |
| 121 | + }) |
| 122 | + |
| 123 | + it("should always add .gitignore itself", () => { |
| 124 | + const content = "node_modules/" |
| 125 | + const { parseResult } = parseGitignoreContent(content, false) |
| 126 | + |
| 127 | + expect(parseResult.validPatterns).toContain(".gitignore") |
| 128 | + }) |
| 129 | + |
| 130 | + it("should log warnings when requested", () => { |
| 131 | + const content = ` |
| 132 | +node_modules/ |
| 133 | +pqh[A-/] |
| 134 | +` |
| 135 | + const warnSpy = vi.spyOn(console, "warn") |
| 136 | + |
| 137 | + parseGitignoreContent(content, true) |
| 138 | + |
| 139 | + expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("Transformed gitignore pattern")) |
| 140 | + }) |
| 141 | + |
| 142 | + it("should handle patterns that cannot be sanitized", () => { |
| 143 | + // Create a pattern that will fail even after sanitization attempts |
| 144 | + // We'll mock the ignore library to always throw for a specific pattern |
| 145 | + const content = ` |
| 146 | +node_modules/ |
| 147 | +totally-broken-pattern-\\x00 |
| 148 | +*.log |
| 149 | +` |
| 150 | + const { parseResult } = parseGitignoreContent(content, false) |
| 151 | + |
| 152 | + // Valid patterns should still be parsed |
| 153 | + expect(parseResult.validPatterns).toContain("node_modules/") |
| 154 | + expect(parseResult.validPatterns).toContain("*.log") |
| 155 | + |
| 156 | + // The broken pattern should be in invalidPatterns |
| 157 | + // (This might not actually fail in practice, but the test structure is here) |
| 158 | + // If the pattern doesn't fail, it will be in validPatterns instead |
| 159 | + const brokenPattern = "totally-broken-pattern-\\x00" |
| 160 | + const isValid = parseResult.validPatterns.includes(brokenPattern) |
| 161 | + const isInvalid = parseResult.invalidPatterns.some((p) => p.pattern === brokenPattern) |
| 162 | + const isTransformed = parseResult.transformedPatterns.some((p) => p.original === brokenPattern) |
| 163 | + |
| 164 | + expect(isValid || isInvalid || isTransformed).toBe(true) |
| 165 | + }) |
| 166 | + }) |
| 167 | + |
| 168 | + describe("createIgnoreInstanceFromFile", () => { |
| 169 | + it("should read and parse a gitignore file", async () => { |
| 170 | + const gitignoreContent = ` |
| 171 | +node_modules/ |
| 172 | +*.log |
| 173 | +dist/ |
| 174 | +` |
| 175 | + vi.mocked(fs.readFile).mockResolvedValue(gitignoreContent) |
| 176 | + |
| 177 | + const { ignoreInstance, parseResult } = await createIgnoreInstanceFromFile("/path/to/.gitignore", false) |
| 178 | + |
| 179 | + expect(fs.readFile).toHaveBeenCalledWith("/path/to/.gitignore", "utf8") |
| 180 | + expect(parseResult).not.toBeNull() |
| 181 | + expect(parseResult?.validPatterns).toContain("node_modules/") |
| 182 | + expect(parseResult?.validPatterns).toContain("*.log") |
| 183 | + expect(parseResult?.validPatterns).toContain("dist/") |
| 184 | + |
| 185 | + // Test ignore functionality |
| 186 | + expect(ignoreInstance.ignores("node_modules/index.js")).toBe(true) |
| 187 | + expect(ignoreInstance.ignores("test.log")).toBe(true) |
| 188 | + }) |
| 189 | + |
| 190 | + it("should handle invalid patterns in file", async () => { |
| 191 | + const gitignoreContent = ` |
| 192 | +node_modules/ |
| 193 | +pqh[A-/] |
| 194 | +*.log |
| 195 | +` |
| 196 | + vi.mocked(fs.readFile).mockResolvedValue(gitignoreContent) |
| 197 | + |
| 198 | + const { ignoreInstance, parseResult } = await createIgnoreInstanceFromFile("/path/to/.gitignore", false) |
| 199 | + |
| 200 | + expect(parseResult).not.toBeNull() |
| 201 | + expect(parseResult?.validPatterns).toContain("node_modules/") |
| 202 | + expect(parseResult?.validPatterns).toContain("*.log") |
| 203 | + expect(parseResult?.transformedPatterns).toHaveLength(1) |
| 204 | + expect(parseResult?.transformedPatterns[0].original).toBe("pqh[A-/]") |
| 205 | + expect(parseResult?.transformedPatterns[0].transformed).toBe("pqhA") |
| 206 | + |
| 207 | + // Test that transformed pattern works |
| 208 | + expect(ignoreInstance.ignores("pqhA")).toBe(true) |
| 209 | + }) |
| 210 | + |
| 211 | + it("should handle missing gitignore file gracefully", async () => { |
| 212 | + vi.mocked(fs.readFile).mockRejectedValue(new Error("ENOENT: no such file or directory")) |
| 213 | + |
| 214 | + const infoSpy = vi.spyOn(console, "info") |
| 215 | + const { ignoreInstance, parseResult } = await createIgnoreInstanceFromFile("/path/to/.gitignore", true) |
| 216 | + |
| 217 | + expect(infoSpy).toHaveBeenCalledWith( |
| 218 | + ".gitignore file not found or could not be read, proceeding without gitignore patterns", |
| 219 | + ) |
| 220 | + expect(parseResult).toBeNull() |
| 221 | + |
| 222 | + // Should still ignore .gitignore itself |
| 223 | + expect(ignoreInstance.ignores(".gitignore")).toBe(true) |
| 224 | + }) |
| 225 | + |
| 226 | + it("should not log when logWarnings is false", async () => { |
| 227 | + vi.mocked(fs.readFile).mockRejectedValue(new Error("ENOENT")) |
| 228 | + |
| 229 | + const infoSpy = vi.spyOn(console, "info") |
| 230 | + await createIgnoreInstanceFromFile("/path/to/.gitignore", false) |
| 231 | + |
| 232 | + expect(infoSpy).not.toHaveBeenCalled() |
| 233 | + }) |
| 234 | + }) |
| 235 | + |
| 236 | + describe("real-world gitignore patterns", () => { |
| 237 | + it("should handle complex real-world patterns", () => { |
| 238 | + const content = ` |
| 239 | +# Dependencies |
| 240 | +node_modules/ |
| 241 | +bower_components/ |
| 242 | +
|
| 243 | +# Testing |
| 244 | +coverage/ |
| 245 | +*.lcov |
| 246 | +.nyc_output |
| 247 | +
|
| 248 | +# Production |
| 249 | +build/ |
| 250 | +dist/ |
| 251 | +
|
| 252 | +# Misc |
| 253 | +.DS_Store |
| 254 | +.env.local |
| 255 | +.env.development.local |
| 256 | +.env.test.local |
| 257 | +.env.production.local |
| 258 | +
|
| 259 | +# Logs |
| 260 | +npm-debug.log* |
| 261 | +yarn-debug.log* |
| 262 | +yarn-error.log* |
| 263 | +
|
| 264 | +# IDE |
| 265 | +.idea/ |
| 266 | +.vscode/ |
| 267 | +*.swp |
| 268 | +*.swo |
| 269 | +
|
| 270 | +# OS generated |
| 271 | +Thumbs.db |
| 272 | +Desktop.ini |
| 273 | +
|
| 274 | +# Negation patterns |
| 275 | +!.vscode/settings.json |
| 276 | +!important.log |
| 277 | +` |
| 278 | + const { ignoreInstance, parseResult } = parseGitignoreContent(content, false) |
| 279 | + |
| 280 | + // All patterns should be valid |
| 281 | + expect(parseResult.invalidPatterns).toHaveLength(0) |
| 282 | + expect(parseResult.transformedPatterns).toHaveLength(0) |
| 283 | + |
| 284 | + // Test various ignore scenarios |
| 285 | + expect(ignoreInstance.ignores("node_modules/package.json")).toBe(true) |
| 286 | + expect(ignoreInstance.ignores(".DS_Store")).toBe(true) |
| 287 | + expect(ignoreInstance.ignores("test.swp")).toBe(true) |
| 288 | + expect(ignoreInstance.ignores("yarn-error.log")).toBe(true) |
| 289 | + |
| 290 | + // Note: The ignore library processes patterns in order, and negation patterns |
| 291 | + // only work if they come after a matching positive pattern. |
| 292 | + // Since .vscode/ is ignored first, then !.vscode/settings.json un-ignores it |
| 293 | + expect(ignoreInstance.ignores(".vscode/tasks.json")).toBe(true) |
| 294 | + // The ignore library doesn't handle negation the same way as git |
| 295 | + // It requires the full path to match, not just the filename |
| 296 | + // So we need to test with the exact pattern |
| 297 | + expect(ignoreInstance.ignores("important.log")).toBe(false) // Negated |
| 298 | + }) |
| 299 | + |
| 300 | + it("should handle the specific pattern from the issue: pqh[A-/]", () => { |
| 301 | + const content = "pqh[A-/]" |
| 302 | + const { ignoreInstance, parseResult } = parseGitignoreContent(content, false) |
| 303 | + |
| 304 | + // Should be transformed, not invalid |
| 305 | + expect(parseResult.invalidPatterns).toHaveLength(0) |
| 306 | + expect(parseResult.transformedPatterns).toHaveLength(1) |
| 307 | + expect(parseResult.transformedPatterns[0].original).toBe("pqh[A-/]") |
| 308 | + expect(parseResult.transformedPatterns[0].transformed).toBe("pqhA") |
| 309 | + |
| 310 | + // Should match as git would interpret it |
| 311 | + expect(ignoreInstance.ignores("pqhA")).toBe(true) |
| 312 | + expect(ignoreInstance.ignores("pqh/")).toBe(false) |
| 313 | + expect(ignoreInstance.ignores("pqhB")).toBe(false) |
| 314 | + }) |
| 315 | + }) |
| 316 | +}) |
0 commit comments