Skip to content

Commit

Permalink
Removed per-backend testing
Browse files Browse the repository at this point in the history
Removed testing support for non-sync backends
Removed testing support for non-prop backends
  • Loading branch information
james-pre committed Mar 5, 2024
1 parent 76b7696 commit 918a7af
Show file tree
Hide file tree
Showing 24 changed files with 234 additions and 540 deletions.
22 changes: 7 additions & 15 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
import { Stats, FileType, type BigIntStats } from '../src/stats';
import { configure as _configure, fs, InMemory, AsyncMirror, Overlay } from '../src/index';
import { fs } from '../src/index';
import * as path from 'path';
import { statSync, readFileSync, readdirSync } from 'fs';
import type { BackendConfig } from '../src/backends/backend';

export const fixturesDir = 'test/fixtures/files/node';

function copy(_fs: typeof fs, _p: string) {
const p = path.posix.resolve('/', path.posix.relative(fixturesDir, _p));
function copy(_p: string) {
const p = path.posix.relative(fixturesDir, _p);
const stats = statSync(_p);

if (!stats.isDirectory()) {
_fs.writeFileSync(p, readFileSync(_p));
fs.writeFileSync(p, readFileSync(_p));
return;
}

if (p != '/') {
_fs.mkdirSync(p);
fs.mkdirSync(p);
}
for (const file of readdirSync(_p)) {
copy(_fs, path.join(_p, file));
copy(path.join(_p, file));
}
}

export async function configure(config: BackendConfig): Promise<void> {
await _configure(config);
copy(fs, fixturesDir);
}
copy(fixturesDir);

export { fs };

export function createMockStats(mode: number | bigint): Stats | BigIntStats {
return new Stats(FileType.FILE, -1, mode);
}

const tests: BackendConfig[] = [{ backend: AsyncMirror, sync: InMemory, async: InMemory }, { backend: InMemory }, { backend: Overlay, readable: InMemory, writable: InMemory }];

export const backends: [string, BackendConfig][] = tests.map(test => [test.backend.name, test]);
8 changes: 4 additions & 4 deletions test/tests/chmod.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fs, createMockStats, fixturesDir } from '../common';
import { fs, createMockStats } from '../common';
import * as path from 'path';
import { jest } from '@jest/globals';

Expand All @@ -8,7 +8,7 @@ const modeSync = 0o644;

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

jest.spyOn(fs, 'chmod').mockImplementation(async (path, mode) => {
expect(path).toBe(file1);
Expand All @@ -26,7 +26,7 @@ describe('chmod tests', () => {
});

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

jest.spyOn(fs, 'open').mockImplementation(async (path, flags, mode) => {
expect(path).toBe(file2);
Expand All @@ -51,7 +51,7 @@ describe('chmod tests', () => {

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

jest.spyOn(fs, 'unlinkSync').mockImplementation(path => {
expect(path).toBe(link);
Expand Down
73 changes: 28 additions & 45 deletions test/tests/error-messages.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { fs, fixturesDir } from '../common';
import * as path from 'path';

import { fs } from '../common';
import type { ApiError } from '../../src/ApiError';

const existingFile = path.join(fixturesDir, 'exit.js');
const existingFile = 'exit.js';

const expectError = async (fn: (...args) => Promise<unknown>, p: string, ...args) => {
let error: ApiError;
Expand Down Expand Up @@ -33,54 +31,39 @@ const expectSyncError = (fn: (...args) => unknown, p: string, ...args) => {

describe('Error tests', () => {
it('should handle async operations with error', async () => {
const fn = path.join(fixturesDir, 'non-existent');
const fn = 'non-existent';

await expectError(fs.promises.stat, fn);

if (!fs.getMount('/').metadata.readonly) {
await expectError(fs.promises.mkdir, existingFile, 0o666);
await expectError(fs.promises.rmdir, fn);
await expectError(fs.promises.rmdir, existingFile);
await expectError(fs.promises.rename, fn, 'foo');
await expectError(fs.promises.open, fn, 'r');
await expectError(fs.promises.readdir, fn);
await expectError(fs.promises.unlink, fn);
await expectError(fs.promises.link, fn, 'foo');

if (fs.getMount('/').metadata.supportsProperties) {
await expectError(fs.promises.chmod, fn, 0o666);
}
}

await expectError(fs.promises.mkdir, existingFile, 0o666);
await expectError(fs.promises.rmdir, fn);
await expectError(fs.promises.rmdir, existingFile);
await expectError(fs.promises.rename, fn, 'foo');
await expectError(fs.promises.open, fn, 'r');
await expectError(fs.promises.readdir, fn);
await expectError(fs.promises.unlink, fn);
await expectError(fs.promises.link, fn, 'foo');
await expectError(fs.promises.chmod, fn, 0o666);
await expectError(fs.promises.lstat, fn);
await expectError(fs.promises.readlink, fn);
});

// Sync operations
if (fs.getMount('/').metadata.synchronous) {
it('should handle sync operations with error', () => {
const fn = path.join(fixturesDir, 'non-existent');
const existingFile = path.join(fixturesDir, 'exit.js');

expectSyncError(fs.statSync, fn);
it('should handle sync operations with error', () => {
const fn = 'non-existent';
const existingFile = 'exit.js';

if (!fs.getMount('/').metadata.readonly) {
expectSyncError(fs.mkdirSync, existingFile, 0o666);
expectSyncError(fs.rmdirSync, fn);
expectSyncError(fs.rmdirSync, existingFile);
expectSyncError(fs.renameSync, fn, 'foo');
expectSyncError(fs.openSync, fn, 'r');
expectSyncError(fs.readdirSync, fn);
expectSyncError(fs.unlinkSync, fn);
expectSyncError(fs.linkSync, fn, 'foo');

if (fs.getMount('/').metadata.supportsProperties) {
expectSyncError(fs.chmodSync, fn, 0o666);
}
}

expectSyncError(fs.lstatSync, fn);
expectSyncError(fs.readlinkSync, fn);
});
}
expectSyncError(fs.statSync, fn);
expectSyncError(fs.mkdirSync, existingFile, 0o666);
expectSyncError(fs.rmdirSync, fn);
expectSyncError(fs.rmdirSync, existingFile);
expectSyncError(fs.renameSync, fn, 'foo');
expectSyncError(fs.openSync, fn, 'r');
expectSyncError(fs.readdirSync, fn);
expectSyncError(fs.unlinkSync, fn);
expectSyncError(fs.linkSync, fn, 'foo');
expectSyncError(fs.chmodSync, fn, 0o666);
expectSyncError(fs.lstatSync, fn);
expectSyncError(fs.readlinkSync, fn);
});
});
41 changes: 8 additions & 33 deletions test/tests/exists.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,20 @@
import { backends, fs, configure, fixturesDir } from '../common';
import * as path from 'path';
import { fs } from '../common';

describe.each(backends)('%s fs.exists', (name, options) => {
const configured = configure(options);
let exists: boolean;
let doesNotExist: boolean;
const f = path.join(fixturesDir, 'x.txt');

beforeAll(() => {
return new Promise<void>(resolve => {
fs.exists(f, y => {
exists = y;
resolve();
});
});
});

beforeAll(() => {
return new Promise<void>(resolve => {
fs.exists(f + '-NO', y => {
doesNotExist = y;
resolve();
});
});
});
describe('fs.exists', () => {
const f = 'x.txt';

it('should return true for an existing file', async () => {
await configured;
const exists = await fs.promises.exists(f);
expect(exists).toBe(true);
});

it('should return false for a non-existent file', async () => {
await configured;
expect(doesNotExist).toBe(false);
const exists = await fs.promises.exists(f + '-NO');
expect(exists).toBe(false);
});

it('should have sync methods that behave the same', async () => {
await configured;
if (fs.getMount('/').metadata.synchronous) {
expect(fs.existsSync(f)).toBe(true);
expect(fs.existsSync(f + '-NO')).toBe(false);
}
expect(fs.existsSync(f)).toBe(true);
expect(fs.existsSync(f + '-NO')).toBe(false);
});
});
46 changes: 18 additions & 28 deletions test/tests/fsync.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
import { backends, fs, configure } from '../common';
import * as path from 'path';
// Import promisify
import { fixturesDir } from '../common';
import { fs } from '../common';
import { FileHandle } from '../../src/emulation/promises';

describe.each(backends)('%s fs.fileSync', (name, options) => {
const configured = configure(options);
const file = path.join(fixturesDir, 'a.js');
describe('fs.fileSync', () => {
const file = 'a.js';

if (!fs.getMount('/').metadata.readonly) {
let handle: FileHandle;
let handle: FileHandle;

beforeAll(async () => {
handle = await fs.promises.open(file, 'a', 0o777);
});
beforeAll(async () => {
handle = await fs.promises.open(file, 'a', 0o777);
});

if (fs.getMount('/').metadata.synchronous) {
it('should synchronize file data changes (sync)', async () => {
await configured;
fs.fdatasyncSync(handle.fd);
fs.fsyncSync(handle.fd);
});
}
it('should synchronize file data changes (sync)', async () => {
fs.fdatasyncSync(handle.fd);
fs.fsyncSync(handle.fd);
});

it('should synchronize file data changes (async)', async () => {
await configured;
await handle.datasync();
await handle.sync();
});
it('should synchronize file data changes (async)', async () => {
await handle.datasync();
await handle.sync();
});

afterAll(async () => {
await handle.close();
});
}
afterAll(async () => {
await handle.close();
});
});
22 changes: 0 additions & 22 deletions test/tests/long-path.test.ts

This file was deleted.

67 changes: 28 additions & 39 deletions test/tests/mkdir.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
import { backends, fs, configure } from '../common';
import { tmpDir, fixturesDir } from '../common';

describe.each(backends)('%s fs.mkdir', (name, options) => {
const configured = configure(options);

if (!fs.getMount('/').metadata.readonly) {
const pathname1 = tmpDir + '/mkdir-test1';

it('should create a directory and verify its existence', async () => {
await configured;

await fs.promises.mkdir(pathname1);
const exists = await fs.promises.exists(pathname1);
expect(exists).toBe(true);
});

const pathname2 = tmpDir + '/mkdir-test2';

it('should create a directory with custom permissions and verify its existence', async () => {
await configured;

await fs.promises.mkdir(pathname2, 0o777);
const exists = await fs.promises.exists(pathname2);
expect(exists).toBe(true);
});

const pathname3 = tmpDir + '/mkdir-test3/again';

it('should not be able to create multi-level directories', async () => {
await configured;

try {
await fs.promises.mkdir(pathname3, 0o777);
} catch (err) {
expect(err).not.toBeNull();
}
});
}
import { fs } from '../common';

describe('fs.mkdir', () => {
const pathname1 = 'mkdir-test1';

it('should create a directory and verify its existence', async () => {
await fs.promises.mkdir(pathname1);
const exists = await fs.promises.exists(pathname1);
expect(exists).toBe(true);
});

const pathname2 = 'mkdir-test2';

it('should create a directory with custom permissions and verify its existence', async () => {
await fs.promises.mkdir(pathname2, 0o777);
const exists = await fs.promises.exists(pathname2);
expect(exists).toBe(true);
});

const pathname3 = 'mkdir-test3/again';

it('should not be able to create multi-level directories', async () => {
try {
await fs.promises.mkdir(pathname3, 0o777);
} catch (err) {
expect(err).not.toBeNull();
}
});
});
Loading

0 comments on commit 918a7af

Please sign in to comment.