Skip to content

Commit

Permalink
test: add support for windows in pino-rotate-file
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele committed Sep 9, 2024
1 parent 27cd6e1 commit 896bfe4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
19 changes: 14 additions & 5 deletions packages/npm/pino-rotate-file/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import * as fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import * as sonicBoom from 'sonic-boom';
import type { Mock } from 'vitest';
import { afterEach, describe, expect, test, vi } from 'vitest';
import { pinoRotateFile } from '../index.js';

function toPlatformPath(name: string): string {
return name.replaceAll(path.posix.sep, path.sep);
}

function toPlatformPaths(paths: string[]): string[] {
return paths.map((name) => toPlatformPath(name));
}

vi.useFakeTimers();

const nowMock = vi.fn(() => Date.now());
global.Date.now = nowMock;

const readdirSpy = vi.spyOn(fs, 'readdir') as unknown as Mock<[path: String], Promise<string[]>>;
const readdirSpy = vi.spyOn(fs, 'readdir') as unknown as Mock<(path: string) => Promise<string[]>>;
const unlinkSpy = vi.spyOn(fs, 'unlink');
const accessSpy = vi.spyOn(fs, 'access');
const mkdirSpy = vi.spyOn(fs, 'mkdir');
Expand Down Expand Up @@ -55,7 +64,7 @@ describe('initial file creation', () => {

await pinoRotateFile({ dir: 'foo' });
// SonicBoom instance is created with the appropriate destination
expect(sonicBoomConstructorSpy).toHaveBeenCalledWith({ dest: 'foo/2022-01-01.log' });
expect(sonicBoomConstructorSpy).toHaveBeenCalledWith({ dest: toPlatformPath('foo/2022-01-01.log') });
});

test('it creates the dir if mkdir is true', async () => {
Expand All @@ -75,7 +84,7 @@ describe('initial file creation', () => {
await pinoRotateFile({ dir: 'foo', mkdir: true });
// mkdir is called with the correct path
expect(mkdirSpy).toHaveBeenCalledWith('foo', { recursive: true });
expect(sonicBoomConstructorSpy).toHaveBeenCalledWith({ dest: 'foo/2022-01-01.log' });
expect(sonicBoomConstructorSpy).toHaveBeenCalledWith({ dest: toPlatformPath('foo/2022-01-01.log') });
});
});

Expand All @@ -90,6 +99,6 @@ test('it cleans up', async () => {

// first 2 files were deleted
expect(unlinkSpy).toHaveBeenCalledTimes(2);
expect(unlinkSpy).toHaveBeenNthCalledWith(1, 'foo/2022-01-01.log');
expect(unlinkSpy).toHaveBeenNthCalledWith(2, 'foo/2022-01-02.log');
expect(unlinkSpy).toHaveBeenNthCalledWith(1, toPlatformPath('foo/2022-01-01.log'));
expect(unlinkSpy).toHaveBeenNthCalledWith(2, toPlatformPath('foo/2022-01-02.log'));
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import { expect, test, vi } from 'vitest';
import { readdirRecurse, readdirRecurseAsync, ReadMode } from '../index.js';

function platform(paths: string[]): string[] {
function toPlatformPath(paths: string[]): string[] {
return paths.map((name) => name.replaceAll(path.posix.sep, path.sep));
}

Expand Down Expand Up @@ -112,12 +112,12 @@ test('async iterator stream consumption', async () => {
files.push(file);
}

expect(files).toStrictEqual(platform(['test/dir1', 'test/dir2', 'test/dir1/file1.sh', 'test/dir1/file2.js']));
expect(files).toStrictEqual(toPlatformPath(['test/dir1', 'test/dir2', 'test/dir1/file1.sh', 'test/dir1/file2.js']));
});

test('promise based consumption', async () => {
expect(await readdirRecurseAsync(joinPath('test'), { readMode: ReadMode.both })).toStrictEqual(
platform(['test/dir1', 'test/dir2', 'test/dir1/file1.sh', 'test/dir1/file2.js']),
toPlatformPath(['test/dir1', 'test/dir2', 'test/dir1/file1.sh', 'test/dir1/file2.js']),
);

const catchCb = vi.fn();
Expand All @@ -129,22 +129,22 @@ test('promise based consumption', async () => {

test('read modes', async () => {
expect(await readdirRecurseAsync(joinPath('test'), { readMode: ReadMode.file })).toStrictEqual(
platform(['test/dir1/file1.sh', 'test/dir1/file2.js']),
toPlatformPath(['test/dir1/file1.sh', 'test/dir1/file2.js']),
);

expect(await readdirRecurseAsync(joinPath('test'), { readMode: ReadMode.dir })).toStrictEqual(
platform(['test/dir1', 'test/dir2']),
toPlatformPath(['test/dir1', 'test/dir2']),
);
});

test('file extension filter', async () => {
expect(
await readdirRecurseAsync(joinPath('test'), { readMode: ReadMode.both, fileExtensions: ['sh'] }),
).toStrictEqual(platform(['test/dir1', 'test/dir2', 'test/dir1/file1.sh']));
).toStrictEqual(toPlatformPath(['test/dir1', 'test/dir2', 'test/dir1/file1.sh']));

expect(
await readdirRecurseAsync(joinPath('test'), { readMode: ReadMode.file, fileExtensions: ['sh'] }),
).toStrictEqual(platform(['test/dir1/file1.sh']));
).toStrictEqual(toPlatformPath(['test/dir1/file1.sh']));
});

test('warnings', async () => {
Expand Down

0 comments on commit 896bfe4

Please sign in to comment.