Skip to content

Commit 756f8c0

Browse files
author
John Doe
committed
feat: add file sink classes
1 parent c03b2c4 commit 756f8c0

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

packages/utils/src/lib/file-sink-json.int.test.ts renamed to packages/utils/src/lib/file-sink-jsonl.int.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as os from 'node:os';
33
import * as path from 'node:path';
44
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
55
import { teardownTestFolder } from '@code-pushup/test-utils';
6-
import { JsonlFileSink, recoverJsonlFile } from './file-sink-json.js';
6+
import { JsonlFileSink, recoverJsonlFile } from './file-sink-jsonl.js';
77

88
describe('JsonlFileSink integration', () => {
99
const baseDir = path.join(os.tmpdir(), 'file-sink-json-int-tests');

packages/utils/src/lib/file-sink-json.unit.test.ts renamed to packages/utils/src/lib/file-sink-jsonl.unit.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
jsonlDecode,
88
jsonlEncode,
99
recoverJsonlFile,
10-
} from './file-sink-json.js';
10+
} from './file-sink-jsonl.js';
1111

1212
describe('jsonlEncode', () => {
1313
it('should encode object to JSON string', () => {
@@ -207,10 +207,36 @@ describe('JsonlFileSink', () => {
207207
`${records.map(record => JSON.stringify(record)).join('\n')}\n`,
208208
);
209209

210+
sink.repack();
211+
expect(fs.readFileSync(filePath, 'utf8')).toBe(
212+
`${JSON.stringify(records[0])}\n${JSON.stringify(records[1])}\n`,
213+
);
214+
});
215+
216+
it('repack() should accept output path', () => {
217+
const filePath = '/tmp/jsonl-repack-test.jsonl';
218+
const sink = new JsonlFileSink<JsonObj>({ filePath });
219+
const records = [
220+
{ key: 'value', number: 42 },
221+
{ key: 'value', number: 42 },
222+
];
223+
224+
fs.writeFileSync(
225+
filePath,
226+
`${records.map(record => JSON.stringify(record)).join('\n')}\n`,
227+
);
228+
210229
const outputPath = '/tmp/jsonl-repack-output.jsonl';
211230
sink.repack(outputPath);
212231
expect(fs.readFileSync(outputPath, 'utf8')).toBe(
213232
`${JSON.stringify(records[0])}\n${JSON.stringify(records[1])}\n`,
214233
);
215234
});
235+
236+
it('should do nothing on finalize()', () => {
237+
const sink = new JsonlFileSink<JsonObj>({
238+
filePath: '/tmp/jsonl-finalize-test.jsonl',
239+
});
240+
expect(() => sink.finalize()).not.toThrow();
241+
});
216242
});

packages/utils/src/lib/file-sink-text.unit.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ describe('FileSink', () => {
251251
});
252252

253253
it('repack() should recover records and write them to output path', () => {
254+
const mockRecover = vi.fn();
255+
const filePath = '/tmp/test-file.txt';
256+
const sink = new FileSink({
257+
filePath,
258+
recover: mockRecover,
259+
});
260+
const records = ['record1', 'record2'];
261+
mockRecover.mockReturnValue({ records, errors: [], partialTail: null });
262+
263+
sink.repack();
264+
expect(mockRecover).toHaveBeenCalled();
265+
expect(fs.readFileSync(filePath, 'utf8')).toBe('record1\n\nrecord2\n');
266+
});
267+
268+
it('repack() should accept output path', () => {
254269
const mockRecover = vi.fn();
255270
const sink = new FileSink({
256271
filePath: '/tmp/test-file.txt',

0 commit comments

Comments
 (0)