|
| 1 | +/** |
| 2 | + * Tests for DSN file system utilities. |
| 3 | + * |
| 4 | + * Verifies that handleFileError correctly distinguishes expected filesystem |
| 5 | + * errors (silently ignored) from unexpected ones (reported to Sentry). |
| 6 | + */ |
| 7 | + |
| 8 | +import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"; |
| 9 | + |
| 10 | +const captureException = mock(); |
| 11 | + |
| 12 | +mock.module("@sentry/bun", () => ({ |
| 13 | + captureException, |
| 14 | + startSpan: (_opts: unknown, fn: () => unknown) => fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +const { handleFileError } = await import("../../../src/lib/dsn/fs-utils.js"); |
| 18 | + |
| 19 | +/** Create an Error with a `code` property, mimicking Node/Bun errno errors. */ |
| 20 | +function errnoError(code: string, message?: string): Error { |
| 21 | + const err = new Error(message ?? code) as NodeJS.ErrnoException; |
| 22 | + err.code = code; |
| 23 | + return err; |
| 24 | +} |
| 25 | + |
| 26 | +describe("handleFileError", () => { |
| 27 | + beforeEach(() => { |
| 28 | + captureException.mockClear(); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + captureException.mockClear(); |
| 33 | + }); |
| 34 | + |
| 35 | + describe("ignorable errors (should NOT report to Sentry)", () => { |
| 36 | + test("ENOENT — file does not exist", () => { |
| 37 | + handleFileError(errnoError("ENOENT"), { |
| 38 | + operation: "test", |
| 39 | + path: "/missing", |
| 40 | + }); |
| 41 | + expect(captureException).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + test("EACCES — permission denied", () => { |
| 45 | + handleFileError(errnoError("EACCES"), { |
| 46 | + operation: "test", |
| 47 | + path: "/secret", |
| 48 | + }); |
| 49 | + expect(captureException).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + test("EPERM — operation not permitted", () => { |
| 53 | + handleFileError(errnoError("EPERM"), { |
| 54 | + operation: "test", |
| 55 | + path: "/locked", |
| 56 | + }); |
| 57 | + expect(captureException).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + test("EISDIR — path is a directory, not a file", () => { |
| 61 | + handleFileError( |
| 62 | + errnoError("EISDIR", "Directories cannot be read like files"), |
| 63 | + { |
| 64 | + operation: "checkEnvForDsn", |
| 65 | + path: "/project/.env", |
| 66 | + } |
| 67 | + ); |
| 68 | + expect(captureException).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + test("ENOTDIR — path component is not a directory", () => { |
| 72 | + handleFileError(errnoError("ENOTDIR"), { |
| 73 | + operation: "test", |
| 74 | + path: "/file.txt/child", |
| 75 | + }); |
| 76 | + expect(captureException).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe("unexpected errors (SHOULD report to Sentry)", () => { |
| 81 | + test("EIO — I/O error", () => { |
| 82 | + handleFileError(errnoError("EIO"), { |
| 83 | + operation: "test", |
| 84 | + path: "/disk-fail", |
| 85 | + }); |
| 86 | + expect(captureException).toHaveBeenCalledTimes(1); |
| 87 | + }); |
| 88 | + |
| 89 | + test("ENOMEM — out of memory", () => { |
| 90 | + handleFileError(errnoError("ENOMEM"), { operation: "test" }); |
| 91 | + expect(captureException).toHaveBeenCalledTimes(1); |
| 92 | + }); |
| 93 | + |
| 94 | + test("generic Error without code", () => { |
| 95 | + handleFileError(new Error("something broke"), { operation: "test" }); |
| 96 | + expect(captureException).toHaveBeenCalledTimes(1); |
| 97 | + }); |
| 98 | + |
| 99 | + test("non-Error value", () => { |
| 100 | + handleFileError("string error", { operation: "test" }); |
| 101 | + expect(captureException).toHaveBeenCalledTimes(1); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + test("passes context tags and extras to Sentry", () => { |
| 106 | + const error = errnoError("EIO"); |
| 107 | + handleFileError(error, { |
| 108 | + operation: "checkEnvForDsn", |
| 109 | + path: "/project/.env", |
| 110 | + }); |
| 111 | + expect(captureException).toHaveBeenCalledWith(error, { |
| 112 | + tags: { operation: "checkEnvForDsn" }, |
| 113 | + extra: { path: "/project/.env" }, |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments