Files
wehub-resource-sync 0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

130 lines
3.8 KiB
TypeScript

import fs from 'fs';
import { afterEach, beforeEach, describe, expect, it, type MockInstance, vi } from 'vitest';
import logger from '../../../src/logger';
import { printErrorInformation } from '../../../src/util/errors';
vi.mock('fs');
describe('printErrorInformation', () => {
let infoSpy: MockInstance<typeof logger.info>;
let debugSpy: MockInstance<typeof logger.debug>;
beforeEach(() => {
vi.resetAllMocks();
infoSpy = vi.spyOn(logger, 'info').mockImplementation(() => logger);
debugSpy = vi.spyOn(logger, 'debug').mockImplementation(() => logger);
});
afterEach(() => {
vi.restoreAllMocks();
});
it('does nothing when no errorLogFile is provided', () => {
printErrorInformation();
expect(infoSpy).not.toHaveBeenCalled();
expect(fs.statSync).not.toHaveBeenCalled();
});
it('does nothing when errorLogFile is an empty string', () => {
printErrorInformation('');
expect(infoSpy).not.toHaveBeenCalled();
expect(fs.statSync).not.toHaveBeenCalled();
});
it('does nothing when the error log file does not exist (ENOENT)', () => {
const enoent = new Error('ENOENT') as NodeJS.ErrnoException;
enoent.code = 'ENOENT';
vi.mocked(fs.statSync).mockImplementation(() => {
throw enoent;
});
printErrorInformation('/missing/error.log');
expect(infoSpy).not.toHaveBeenCalled();
// ENOENT should not be logged as debug noise
expect(debugSpy).not.toHaveBeenCalled();
});
it('does nothing when the error log file exists but is empty', () => {
vi.mocked(fs.statSync).mockReturnValue({
isFile: () => true,
size: 0,
} as fs.Stats);
printErrorInformation('/logs/error.log');
expect(infoSpy).not.toHaveBeenCalled();
});
it('does nothing when the path exists but is not a file', () => {
vi.mocked(fs.statSync).mockReturnValue({
isFile: () => false,
size: 1024,
} as fs.Stats);
printErrorInformation('/logs');
expect(infoSpy).not.toHaveBeenCalled();
});
it('logs a debug message for unexpected fs errors (e.g. EACCES)', () => {
const eacces = new Error('EACCES') as NodeJS.ErrnoException;
eacces.code = 'EACCES';
vi.mocked(fs.statSync).mockImplementation(() => {
throw eacces;
});
printErrorInformation('/logs/error.log');
expect(infoSpy).not.toHaveBeenCalled();
expect(debugSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(
expect.stringContaining(
'[errorFileHasContents] Error checking if file has contents: /logs/error.log',
),
{ error: eacces },
);
});
it('prints only the error log path when no debug log is provided', () => {
vi.mocked(fs.statSync).mockReturnValue({
isFile: () => true,
size: 42,
} as fs.Stats);
printErrorInformation('/logs/error.log');
expect(infoSpy).toHaveBeenCalledTimes(1);
const message = infoSpy.mock.calls[0][0] as string;
expect(message).toContain('There were some errors during the operation');
expect(message).toContain('/logs/error.log');
expect(message).not.toContain('Debug log:');
});
it('prints both error and debug log paths when both are provided', () => {
vi.mocked(fs.statSync).mockReturnValue({
isFile: () => true,
size: 42,
} as fs.Stats);
printErrorInformation('/logs/error.log', '/logs/debug.log');
expect(infoSpy).toHaveBeenCalledTimes(1);
const message = infoSpy.mock.calls[0][0] as string;
expect(message).toContain('Error log:');
expect(message).toContain('/logs/error.log');
expect(message).toContain('Debug log:');
expect(message).toContain('/logs/debug.log');
});
it('ignores debugLogFile when errorLogFile is missing/empty', () => {
printErrorInformation(undefined, '/logs/debug.log');
expect(infoSpy).not.toHaveBeenCalled();
expect(fs.statSync).not.toHaveBeenCalled();
});
});