Skip to content

Commit

Permalink
symlink now only opens the file once
Browse files Browse the repository at this point in the history
Streamlined some tests
  • Loading branch information
james-pre committed Oct 31, 2024
1 parent 10bc957 commit d614105
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/emulation/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,8 @@ export async function symlink(target: fs.PathLike, path: fs.PathLike, type: fs.s
throw ErrnoError.With('EEXIST', path.toString(), 'symlink');
}

await writeFile(path, target.toString());
const handle = await _open(path, 'r+', 0o644, false);
await using handle = await _open(path, 'w+', 0o644, false);
await handle.writeFile(target.toString());
await handle.file._setType(constants.S_IFLNK);
}
symlink satisfies typeof promises.symlink;
Expand Down
28 changes: 12 additions & 16 deletions tests/fs/chmod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,41 @@ import { fs } from '../common.js';

const asyncMode = 0o777;
const syncMode = 0o644;
const file = 'a.js';

suite('chmod tests', () => {
test('chmod', async () => {
const file1 = 'a.js';
await fs.promises.chmod(file, asyncMode.toString(8));

await fs.promises.chmod(file1, asyncMode.toString(8));
const stats = await fs.promises.stat(file);
assert.equal(stats.mode & 0o777, asyncMode);

const stats = await fs.promises.stat(file1);
assert((stats.mode & 0o777) === asyncMode);

fs.chmodSync(file1, syncMode);
assert((fs.statSync(file1).mode & 0o777) === syncMode);
fs.chmodSync(file, syncMode);
assert.equal(fs.statSync(file).mode & 0o777, syncMode);
});

test('fchmod', async () => {
const file2 = 'a1.js';

const handle = await fs.promises.open(file2, 'a', 0o644);
const handle = await fs.promises.open(file, 'a', 0o644);

await handle.chmod(asyncMode);
const stats = await handle.stat();

assert((stats.mode & 0o777) === asyncMode);
assert.equal(stats.mode & 0o777, asyncMode);

fs.fchmodSync(handle.fd, syncMode);
assert((fs.statSync(file2).mode & 0o777) === syncMode);
assert.equal(fs.statSync(file).mode & 0o777, syncMode);
});

test('lchmod', async () => {
const link = 'symbolic-link';
const target = 'a1.js';

await fs.promises.symlink(target, link);
await fs.promises.symlink(file, link);
await fs.promises.lchmod(link, asyncMode);

const stats = await fs.promises.lstat(link);
assert((stats.mode & 0o777) === asyncMode);
assert.equal(stats.mode & 0o777, asyncMode);

fs.lchmodSync(link, syncMode);
assert((fs.lstatSync(link).mode & 0o777) === syncMode);
assert.equal(fs.lstatSync(link).mode & 0o777, syncMode);
});
});
24 changes: 12 additions & 12 deletions tests/fs/truncate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ const path: string = 'truncate-file.txt',
suite('Truncate, sync', () => {
test('initial write', () => {
fs.writeFileSync(path, data);
assert(fs.statSync(path).size === size);
assert.equal(fs.statSync(path).size, size);
});

test('truncate to 1024', () => {
fs.truncateSync(path, 1024);
assert(fs.statSync(path).size === 1024);
assert.equal(fs.statSync(path).size, 1024);
});

test('truncate to 0', () => {
fs.truncateSync(path);
assert(fs.statSync(path).size === 0);
assert.equal(fs.statSync(path).size, 0);
});

test('write', () => {
fs.writeFileSync(path, data);
assert(fs.statSync(path).size === size);
assert.equal(fs.statSync(path).size, size);
});

let fd: number;
Expand All @@ -35,12 +35,12 @@ suite('Truncate, sync', () => {

test('ftruncate to 1024', () => {
fs.ftruncateSync(fd, 1024);
assert(fs.fstatSync(fd).size === 1024);
assert.equal(fs.fstatSync(fd).size, 1024);
});

test('ftruncate to 0', () => {
fs.ftruncateSync(fd);
assert(fs.fstatSync(fd).size === 0);
assert.equal(fs.fstatSync(fd).size, 0);
});

test('close fd', () => {
Expand All @@ -53,22 +53,22 @@ suite('Truncate, async', () => {
test('initial write', async () => {
await fs.promises.writeFile(path, data);

assert((await statSize(path)) === 1024 * 16);
assert.equal(await statSize(path), 1024 * 16);
});

test('truncate to 1024', async () => {
await fs.promises.truncate(path, 1024);
assert((await statSize(path)) === 1024);
assert.equal(await statSize(path), 1024);
});

test('truncate to 0', async () => {
await fs.promises.truncate(path);
assert((await statSize(path)) === 0);
assert.equal(await statSize(path), 0);
});

test('write', async () => {
await fs.promises.writeFile(path, data);
assert((await statSize(path)) === size);
assert.equal(await statSize(path), size);
});

let handle: FileHandle;
Expand All @@ -79,13 +79,13 @@ suite('Truncate, async', () => {
test('handle.truncate to 1024', async () => {
await handle.truncate(1024);
await handle.sync();
assert((await statSize(path)) === 1024);
assert.equal(await statSize(path), 1024);
});

test('handle.truncate to 0', async () => {
await handle.truncate();
await handle.sync();
assert((await statSize(path)) === 0);
assert.equal(await statSize(path), 0);
});

test('close handle', async () => {
Expand Down

0 comments on commit d614105

Please sign in to comment.