Skip to content

Commit

Permalink
Fixed more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Mar 6, 2024
1 parent 9fee2b1 commit c0800ca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 114 deletions.
5 changes: 0 additions & 5 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Stats, FileType, type BigIntStats } from '../src/stats';
import { fs } from '../src/index';
import * as path from 'path';
import { statSync, readFileSync, readdirSync } from 'fs';
Expand All @@ -25,7 +24,3 @@ function copy(_p: string) {
copy(fixturesDir);

export { fs };

export function createMockStats(mode: number | bigint): Stats | BigIntStats {
return new Stats(FileType.FILE, -1, mode);
}
107 changes: 25 additions & 82 deletions test/tests/chmod.test.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,47 @@
import { fs, createMockStats } from '../common';
import * as path from 'path';
import { jest } from '@jest/globals';
import { fs } from '../common';

const isWindows = process.platform === 'win32';
const asyncMode = 0o777;
const modeSync = 0o644;
const syncMode = 0o644;

describe('chmod tests', () => {
it('should change file mode using chmod', async () => {
test('chmod', async () => {
const file1 = 'a.js';

jest.spyOn(fs, 'chmod').mockImplementation(async (path, mode) => {
expect(path).toBe(file1);
expect(mode).toBe(asyncMode.toString(8));
});
await fs.promises.chmod(file1, asyncMode.toString(8));

jest.spyOn(fs, 'chmodSync').mockImplementation((path, mode) => {
expect(path).toBe(file1);
expect(mode).toBe(modeSync);
});
const stats = await fs.promises.stat(file1);
expect(stats.mode & 0o777).toBe(asyncMode);

jest.spyOn(fs, 'statSync').mockReturnValue(createMockStats(isWindows ? asyncMode & 0o777 : asyncMode));

await changeFileMode(file1);
fs.chmodSync(file1, syncMode);
expect(fs.statSync(file1).mode & 0o777).toBe(syncMode);
});

it('should change file mode using fchmod', async () => {
test('fchmod', async () => {
const file2 = 'a1.js';

jest.spyOn(fs, 'open').mockImplementation(async (path, flags, mode) => {
expect(path).toBe(file2);
expect(flags).toBe('a');
return 123;
});

jest.spyOn(fs, 'fchmod').mockImplementation(async (fd, mode) => {
expect(fd).toBe(123);
expect(mode).toBe(asyncMode.toString(8));
});
const handle = await fs.promises.open(file2, 'a', 0o644);

jest.spyOn(fs, 'fchmodSync').mockImplementation((fd, mode) => {
expect(fd).toBe(123);
expect(mode).toBe(modeSync);
});
await handle.chmod(asyncMode);
const stats = await handle.stat();

jest.spyOn(fs, 'fstatSync').mockReturnValue(createMockStats(isWindows ? asyncMode & 0o777 : asyncMode));
expect(stats.mode & 0o777).toBe(asyncMode);

await changeFileMode(file2);
fs.fchmodSync(handle.fd, syncMode);
expect(fs.statSync(file2).mode & 0o777).toBe(syncMode);
});

it('should change symbolic link mode using lchmod', async () => {
const link = path.join('symbolic-link');
const file2 = 'a1.js';

jest.spyOn(fs, 'unlinkSync').mockImplementation(path => {
expect(path).toBe(link);
});
test('lchmod', async () => {
const link = 'symbolic-link';
const target = 'a1.js';

jest.spyOn(fs, 'symlinkSync').mockImplementation((target, path) => {
expect(target).toBe(file2);
expect(path).toBe(link);
});
await fs.promises.unlink(link);
await fs.promises.symlink(target, link);
await fs.promises.lchmod(link, asyncMode);

jest.spyOn(fs, 'lchmod').mockImplementation(async (path, mode) => {
expect(path).toBe(link);
expect(mode).toBe(asyncMode);
});
const stats = await fs.promises.lstat(link);
expect(stats.mode & 0o777).toBe(asyncMode);

jest.spyOn(fs, 'lchmodSync').mockImplementation((path, mode) => {
expect(path).toBe(link);
expect(mode).toBe(modeSync);
});

jest.spyOn(fs, 'lstatSync').mockReturnValue(createMockStats(isWindows ? asyncMode & 0o777 : asyncMode));

await changeSymbolicLinkMode(link, file2);
fs.lchmodSync(link, syncMode);
expect(fs.lstatSync(link).mode & 0o777).toBe(syncMode);
});
});

async function changeFileMode(file: string): Promise<void> {
await fs.promises.chmod(file, asyncMode.toString(8));

const statResult = await fs.promises.stat(file);
expect(statResult.mode & 0o777).toBe(isWindows ? asyncMode & 0o777 : asyncMode);

fs.chmodSync(file, modeSync);
const statSyncResult = fs.statSync(file);
expect(statSyncResult.mode & 0o777).toBe(isWindows ? modeSync & 0o777 : modeSync);
}

async function changeSymbolicLinkMode(link: string, target: string): Promise<void> {
await fs.promises.unlink(link);
await fs.promises.symlink(target, link);

await fs.promises.lchmod(link, asyncMode);

const lstatResult = await fs.promises.lstat(link);
expect(lstatResult.mode & 0o777).toBe(isWindows ? asyncMode & 0o777 : asyncMode);

fs.lchmodSync(link, modeSync);
const lstatSyncResult = fs.lstatSync(link);
expect(lstatSyncResult.mode & 0o777).toBe(isWindows ? modeSync & 0o777 : modeSync);
}
8 changes: 1 addition & 7 deletions test/tests/writeFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { fs } from '../common';

import { jest } from '@jest/globals';
import { decode } from '../../src';

const s =
'南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、经济现状。\n';

describe('fs.writeFile', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('should write and read file with specified content', async () => {
const filename = 'test.txt';
Expand All @@ -27,8 +22,7 @@ describe('fs.writeFile', () => {
const buf = Buffer.from(s, 'utf8');

await fs.promises.writeFile(filename, buf);
const buffer = await fs.promises.readFile;
filename;
const buffer = await fs.promises.readFile(filename);
expect(buf.length).toBe(buffer.length);

await fs.promises.unlink(filename);
Expand Down
20 changes: 0 additions & 20 deletions test/tests/writeFileSync.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { fs } from '../common';
import { jest } from '@jest/globals';

describe('File Writing with Custom Mode', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('should write file synchronously with custom mode', async () => {
const file = 'testWriteFileSync.txt';
const mode = 0o755;

jest.spyOn(fs, 'openSync').mockImplementation((...args) => {
return fs.openSync.apply(fs, args);
});

jest.spyOn(fs, 'closeSync').mockImplementation((...args) => {
return fs.closeSync.apply(fs, args);
});

fs.writeFileSync(file, '123', { mode: mode });

const content = fs.readFileSync(file, { encoding: 'utf8' });
Expand All @@ -33,14 +21,6 @@ describe('File Writing with Custom Mode', () => {
const file = 'testAppendFileSync.txt';
const mode = 0o755;

jest.spyOn(fs, 'openSync').mockImplementation((...args) => {
return fs.openSync.apply(fs, args);
});

jest.spyOn(fs, 'closeSync').mockImplementation((...args) => {
return fs.closeSync.apply(fs, args);
});

fs.appendFileSync(file, 'abc', { mode: mode });

const content = fs.readFileSync(file, { encoding: 'utf8' });
Expand Down

0 comments on commit c0800ca

Please sign in to comment.