const test = require('brittle') const path = require('bare-path') const crypto = require('bare-crypto') const fs = require('.') const Thread = require('bare-thread') const isWindows = Bare.platform === 'win32' test('open + close', async (t) => { t.plan(2) const file = await withFile(t, 'test/fixtures/foo.txt') fs.open(file, (err, fd) => { t.absent(err, 'opened') fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) test('open + close, long path', async (t) => { t.plan(2) const dir = await withDir( t, `test/fixtures/${'a'.repeat(128)}/${'b'.repeat(128)}/${'c'.repeat(128)}` ) const file = await withFile(t, `${dir}/${'d'.repeat(128)}.txt`) fs.open(file, (err, fd) => { t.absent(err, 'opened') fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) test('open + close sync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt') const fd = fs.openSync(file) fs.closeSync(fd) t.pass() }) test('access', async (t) => { t.plan(1) const file = await withFile(t, 'test/fixtures/foo.txt') fs.access(file, (err) => { t.absent(err, 'accessed') }) }) test('access, file missing', async (t) => { t.plan(1) fs.access('test/fixtures/foo.txt', (err) => { t.ok(err, 'file missing') }) }) test('access, is executable', { skip: isWindows }, async (t) => { t.plan(1) const file = await withFile(t, 'test/fixtures/foo.txt', Buffer.alloc(0), { mode: 0o755 }) fs.access(file, fs.constants.X_OK, (err) => { t.absent(err) }) }) test('access, is not executable', { skip: isWindows }, async (t) => { t.plan(1) const file = await withFile(t, 'test/fixtures/foo.txt') fs.access(file, fs.constants.X_OK, (err) => { t.ok(err) }) }) test('access sync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt') fs.accessSync(file) }) test('access async, file missing', async (t) => { try { fs.accessSync('test/fixtures/foo.txt') t.fail('should fail') } catch { t.pass('file missing') } }) test('chmod', async (t) => { t.plan(3) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const { mode: oldMode } = fs.statSync(file) fs.chmod(file, 0o555, (err) => { t.absent(err) const { mode: newMode } = fs.statSync(file) t.ok(oldMode !== newMode) t.is(newMode, isWindows ? 33060 : 33133) }) }) test('chmodSync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const { mode: oldMode } = fs.statSync(file) fs.chmodSync(file, 0o555) const { mode: newMode } = fs.statSync(file) t.ok(oldMode !== newMode) t.is(newMode, isWindows ? 33060 : 33133) }) test('fchmod', async (t) => { t.plan(3) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const fd = fs.openSync(file, 'w+') t.teardown(() => fs.closeSync(fd)) const { mode: oldMode } = fs.statSync(file) fs.fchmod(fd, 0o555, (err) => { t.absent(err) const { mode: newMode } = fs.statSync(file) t.ok(oldMode !== newMode) t.is(newMode, isWindows ? 33060 : 33133) }) }) test('fchmodSync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const fd = fs.openSync(file, 'w+') const { mode: oldMode } = fs.statSync(file) fs.fchmodSync(fd, 0o555) const { mode: newMode } = fs.statSync(file) t.ok(oldMode !== newMode) t.is(newMode, isWindows ? 33060 : 33133) fs.closeSync(fd) }) test('read', async (t) => { t.plan(5) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.open(file, (err, fd) => { t.absent(err, 'opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err) t.is(len, 4) t.alike(data, Buffer.from('foo\n')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) test('read + offset', async (t) => { t.plan(5) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.open(file, (err, fd) => { t.absent(err, 'opened') const data = Buffer.alloc(4) fs.read(fd, data, 2, 2, 0, (err, len) => { t.absent(err) t.is(len, 2) t.alike(data, Buffer.from('\x00\x00fo')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) test('chown', async (t) => { t.plan(3) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const oldStat = fs.statSync(file) fs.chown(file, oldStat.uid, oldStat.gid, (err) => { t.absent(err) const newStat = fs.lstatSync(file) t.is(oldStat.uid, newStat.uid) t.is(oldStat.gid, newStat.gid) }) }) test('chownSync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const oldStat = fs.statSync(file) t.execution(fs.chownSync(file, oldStat.uid, oldStat.gid)) const newStat = fs.lstatSync(file) t.is(oldStat.uid, newStat.uid) t.is(oldStat.gid, newStat.gid) }) test('fchown', async (t) => { t.plan(3) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const fd = fs.openSync(file) t.teardown(() => fs.closeSync(fd)) const oldStat = fs.fstatSync(fd) fs.fchown(fd, oldStat.uid, oldStat.gid, (err) => { t.absent(err) const newStat = fs.fstatSync(fd) t.is(oldStat.uid, newStat.uid) t.is(oldStat.gid, newStat.gid) }) }) test('fchownSync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const fd = fs.openSync(file) t.teardown(() => fs.closeSync(fd)) const oldStat = fs.fstatSync(fd) t.execution(fs.fchownSync(fd, oldStat.uid, oldStat.gid)) const newStat = fs.fstatSync(fd) t.is(oldStat.uid, newStat.uid) t.is(oldStat.gid, newStat.gid) }) test('lchown', async (t) => { t.plan(3) const target = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const link = await withSymlink(t, 'test/fixtures/foo-link.txt', target) const oldStat = fs.lstatSync(link) fs.lchown(link, oldStat.uid, oldStat.gid, (err) => { t.absent(err) const newStat = fs.lstatSync(link) t.is(oldStat.uid, newStat.uid) t.is(oldStat.gid, newStat.gid) }) }) test('lchownSync', async (t) => { const target = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const link = await withSymlink(t, 'test/fixtures/foo-link.txt', target) const oldStat = fs.lstatSync(link) t.execution(fs.lchownSync(link, oldStat.uid, oldStat.gid)) const newStat = fs.lstatSync(link) t.is(oldStat.uid, newStat.uid) t.is(oldStat.gid, newStat.gid) }) test('read + position', async (t) => { t.plan(5) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.open(file, (err, fd) => { t.absent(err, 'opened') const data = Buffer.alloc(2) fs.read(fd, data, 0, 2, 2, (err, len) => { t.absent(err) t.is(len, 2) t.alike(data, Buffer.from('o\n')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) test('read + current position', async (t) => { t.plan(8) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.open(file, (err, fd) => { t.absent(err, 'opened') const data = Buffer.alloc(2) fs.read(fd, data, 0, 2, -1, (err, len) => { t.absent(err) t.is(len, 2) t.alike(data, Buffer.from('fo')) fs.read(fd, data, 0, 2, -1, (err, len) => { t.absent(err) t.is(len, 2) t.alike(data, Buffer.from('o\n')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) }) test('read out of buffer bounds', async (t) => { t.plan(6) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.open(file, (err, fd) => { t.absent(err, 'opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err) t.is(len, 4) fs.read(fd, data, 6, 4, 0, (err, len) => { t.absent(err) t.is(len, 0) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) }) test('read sync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const fd = fs.openSync(file) const data = Buffer.alloc(4) const len = fs.readSync(fd, data, 0, 4, 0) t.is(len, 4) t.alike(data, Buffer.from('foo\n')) fs.closeSync(fd) }) test('write', async (t) => { t.plan(7) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.open(file, 'w+', (err, fd) => { t.absent(err, 'opened') const data = Buffer.from('foo\n') fs.write(fd, data, 0, 4, 0, (err, len) => { t.absent(err) t.is(len, 4) const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err) t.is(len, 4) t.alike(data, Buffer.from('foo\n')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) }) test('write + offset', async (t) => { t.plan(7) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.open(file, 'w+', (err, fd) => { t.absent(err, 'opened') const data = Buffer.from('foo\n') fs.write(fd, data, 2, 2, 2, (err, len) => { t.absent(err) t.is(len, 2) const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err) t.is(len, 4) t.alike(data, Buffer.from('\x00\x00o\n')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) }) test('write + position', async (t) => { t.plan(7) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.open(file, 'w+', (err, fd) => { t.absent(err, 'opened') const data = Buffer.from('o\n') fs.write(fd, data, 0, 2, 2, (err, len) => { t.absent(err) t.is(len, 2) const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err) t.is(len, 4) t.alike(data, Buffer.from('\x00\x00o\n')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) }) test('write + current position', async (t) => { t.plan(9) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.open(file, 'w+', (err, fd) => { t.absent(err, 'opened') const data = Buffer.from('foo\n') fs.write(fd, data, 0, 2, -1, (err, len) => { t.absent(err) t.is(len, 2) fs.write(fd, data, 2, 2, -1, (err, len) => { t.absent(err) t.is(len, 2) const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err) t.is(len, 4) t.alike(data, Buffer.from('foo\n')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) }) }) test('write in append mode', async (t) => { t.plan(9) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.open(file, 'a+', (err, fd) => { t.absent(err, 'opened') fs.write(fd, Buffer.from('foo'), (err, len) => { t.absent(err) t.is(len, 3) fs.write(fd, Buffer.from('bar\n'), (err, len) => { t.absent(err) t.is(len, 4) const data = Buffer.alloc(7) fs.read(fd, data, 0, 7, 0, (err, len) => { t.absent(err) t.is(len, 7) t.alike(data, Buffer.from('foobar\n')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) }) }) test('write string in append mode', async (t) => { t.plan(9) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.open(file, 'a+', (err, fd) => { t.absent(err, 'opened') fs.write(fd, 'foo', (err, len) => { t.absent(err) t.is(len, 3) fs.write(fd, 'bar\n', (err, len) => { t.absent(err) t.is(len, 4) const data = Buffer.alloc(7) fs.read(fd, data, 0, 7, 0, (err, len) => { t.absent(err) t.is(len, 7) t.alike(data, Buffer.from('foobar\n')) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) }) }) test('write out of buffer bounds', async (t) => { t.plan(6) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.open(file, 'w+', (err, fd) => { t.absent(err, 'opened') const data = Buffer.from('foo\n') fs.write(fd, data, 0, 6, 0, (err, len) => { t.absent(err) t.is(len, 4) fs.write(fd, data, 6, 4, 0, (err, len) => { t.absent(err) t.is(len, 0) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) }) test('write string', async (t) => { t.plan(4) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.open(file, 'w+', (err, fd) => { t.absent(err, 'opened') const data = 'foo\n' fs.write(fd, data, (err, len) => { t.absent(err) t.is(len, 4) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) test('write sync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt', false) const fd = fs.openSync(file, 'w+') let data let len data = Buffer.from('foo\n') len = fs.writeSync(fd, data, 0, 4, 0) t.is(len, 4) data = Buffer.alloc(4) len = fs.readSync(fd, data, 0, 4, 0) t.is(len, 4) t.alike(data, Buffer.from('foo\n')) fs.closeSync(fd) }) test('stat', async (t) => { t.plan(2) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.stat(file, (err, st) => { t.absent(err, 'stat') for (const [key, value] of Object.entries(st)) t.comment(key, value) t.ok(st) }) }) test('stat sync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const st = fs.statSync(file) for (const [key, value] of Object.entries(st)) t.comment(key, value) t.ok(st) }) test('stat timestamps are in milliseconds since the epoch', async (t) => { const before = Date.now() const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const after = Date.now() const st = fs.statSync(file) for (const key of ['atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs']) { t.ok(st[key] > before - 60000 && st[key] < after + 60000, key) } }) test('fstat', async (t) => { t.plan(4) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.open(file, (err, fd) => { t.absent(err, 'opened') fs.fstat(fd, (err, st) => { t.absent(err, 'stat') for (const [key, value] of Object.entries(st)) t.comment(key, value) t.ok(st) fs.close(fd, (err) => { t.absent(err, 'closed') }) }) }) }) test('fstat sync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const fd = fs.openSync(file) const st = fs.fstatSync(fd) for (const [key, value] of Object.entries(st)) t.comment(key, value) t.ok(st) fs.closeSync(fd) }) test('lstat', async (t) => { t.plan(3) const target = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const link = await withSymlink(t, 'test/fixtures/foo-link.txt', target) fs.lstat(link, (err, st) => { t.absent(err, 'stat') t.ok(st.isSymbolicLink()) for (const [key, value] of Object.entries(st)) t.comment(key, value) t.ok(st) }) }) test('lstat sync', async (t) => { const target = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const link = await withSymlink(t, 'test/fixtures/foo-link.txt', target) const st = fs.lstatSync(link) t.ok(st.isSymbolicLink()) for (const [key, value] of Object.entries(st)) t.comment(key, value) t.ok(st) }) test('statfs', async (t) => { t.plan(2) const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.statfs(file, (err, st) => { t.absent(err, 'stat') for (const [key, value] of Object.entries(st)) t.comment(key, value) t.ok(st) }) }) test('statfs sync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const st = fs.statfsSync(file) for (const [key, value] of Object.entries(st)) t.comment(key, value) t.ok(st) }) test('ftruncate', async (t) => { t.plan(4) const file = await withFile(t, 'test/fixtures/foo.txt', 'hello world') fs.open(file, 'r+', (err, fd) => { t.absent(err, 'opened') fs.ftruncate(fd, 5, (err) => { t.absent(err, 'truncated') fs.readFile(file, (err, data) => { t.absent(err, 'file read') t.alike(data, Buffer.from('hello'), 'check file content') }) }) }) }) test('ftruncateSync', async (t) => { t.plan(1) const file = await withFile(t, 'test/fixtures/foo.txt', 'hello world') const fd = fs.openSync(file, 'r+') fs.ftruncateSync(fd, 5) fs.readFile(file, (err, data) => t.alike(data, Buffer.from('hello'))) }) test('truncate', async (t) => { t.plan(3) const file = await withFile(t, 'test/fixtures/foo.txt', 'hello world') fs.truncate(file, 5, (err) => { t.absent(err, 'truncated') fs.readFile(file, (err, data) => { t.absent(err, 'file read') t.alike(data, Buffer.from('hello'), 'check file content') }) }) }) test('truncateSync', async (t) => { t.plan(1) const file = await withFile(t, 'test/fixtures/foo.txt', 'hello world') fs.truncateSync(file, 5) fs.readFile(file, (err, data) => t.alike(data, Buffer.from('hello'))) }) test('utimes', async (t) => { t.plan(3) const file = await withFile(t, 'test/fixtures/foo.txt') const oldStat = fs.statSync(file) const future = new Date(Date.now() + 1000) fs.utimes(file, future, future, (err) => { t.absent(err) const newStat = fs.statSync(file) t.ok(oldStat.atimeMs < newStat.atimeMs) t.ok(oldStat.mtimeMs < newStat.mtimeMs) }) }) test('utimes sync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt') const oldStat = fs.statSync(file) const future = Date.now() / 1000 + 1000 fs.utimesSync(file, future, future) const newStat = fs.statSync(file) t.ok(oldStat.atimeMs < newStat.atimeMs) t.ok(oldStat.mtimeMs < newStat.mtimeMs) }) test('lutimes', async (t) => { t.plan(3) const target = await withFile(t, 'test/fixtures/foo.txt') const link = await withSymlink(t, 'test/fixtures/foo-link.txt', target) const oldStat = fs.lstatSync(link) const future = new Date(Date.now() + 1000) fs.lutimes(link, future, future, (err) => { t.absent(err) const newStat = fs.lstatSync(link) t.ok(oldStat.atimeMs < newStat.atimeMs) t.ok(oldStat.mtimeMs < newStat.mtimeMs) }) }) test('lutimes sync', async (t) => { const target = await withFile(t, 'test/fixtures/foo.txt') const link = await withSymlink(t, 'test/fixtures/foo-link.txt', target) const oldStat = fs.lstatSync(link) const future = Date.now() / 1000 + 1000 fs.lutimesSync(link, future, future) const newStat = fs.lstatSync(link) t.ok(oldStat.atimeMs < newStat.atimeMs) t.ok(oldStat.mtimeMs < newStat.mtimeMs) }) test('futimes', async (t) => { t.plan(3) const file = await withFile(t, 'test/fixtures/foo.txt') const fd = fs.openSync(file, 'w+') const oldStat = fs.fstatSync(fd) const future = new Date(Date.now() + 1000) fs.futimes(fd, future, future, (err) => { t.absent(err) const newStat = fs.statSync(file) t.ok(oldStat.atimeMs < newStat.atimeMs) t.ok(oldStat.mtimeMs < newStat.mtimeMs) fs.closeSync(fd) }) }) test('futimes sync', async (t) => { const file = await withFile(t, 'test/fixtures/foo.txt') const fd = fs.openSync(file, 'w+') const oldStat = fs.fstatSync(fd) const future = Date.now() / 1000 + 1000 fs.futimesSync(fd, future, future) const newStat = fs.statSync(file) t.ok(oldStat.atimeMs < newStat.atimeMs) t.ok(oldStat.mtimeMs < newStat.mtimeMs) fs.closeSync(fd) }) test('opendir + close', async (t) => { t.plan(2) await withDir(t, 'test/fixtures/dir') fs.opendir('test/fixtures/dir', (err, dir) => { t.absent(err, 'opened') dir.close((err) => { t.absent(err, 'closed') }) }) }) test('opendirSync + closeSync', async (t) => { await withDir(t, 'test/fixtures/dir') const dir = fs.opendirSync('test/fixtures/dir') dir.closeSync() }) test('opendir + asyncIterator', async (t) => { t.plan(2) await withDir(t, 'test/fixtures/dir') await withFile(t, 'test/fixtures/dir/foo.txt', 'hello\n') fs.opendir('test/fixtures/dir', async (err, dir) => { t.absent(err, 'opened') for await (const entry of dir) { t.comment(entry) } t.pass('iterated') }) }) test('opendirSync + iterator', async (t) => { await withDir(t, 'test/fixtures/dir') await withFile(t, 'test/fixtures/dir/foo.txt', 'hello\n') const dir = fs.opendirSync('test/fixtures/dir') for (const entry of dir) { t.comment(entry) } t.pass('iterated') }) test('readdir', async (t) => { t.plan(2) await withDir(t, 'test/fixtures/dir') await withFile(t, 'test/fixtures/dir/foo.txt', 'hello\n') fs.readdir('test/fixtures/dir', (err, files) => { t.absent(err, 'read') for (const entry of files) { t.comment(entry) } t.pass('iterated') }) }) test('readdir + withFileTypes: true', async (t) => { t.plan(2) await withDir(t, 'test/fixtures/dir') await withFile(t, 'test/fixtures/dir/foo.txt', 'hello\n') fs.readdir('test/fixtures/dir', { withFileTypes: true }, (err, files) => { t.absent(err, 'read') for (const entry of files) { t.comment(entry) } t.pass('iterated') }) }) test('readdir + recursive: true', async (t) => { t.plan(2) await withDir(t, 'test/fixtures/foo/bar/baz') await withFile(t, 'test/fixtures/foo/bar/baz/foo.txt', 'hello\n') fs.readdir('test/fixtures/foo', { recursive: true }, (err, files) => { t.absent(err, 'read') for (const entry of files) { t.comment(entry) } t.pass('iterated') }) }) test('readdirSync', async (t) => { await withDir(t, 'test/fixtures/dir') await withFile(t, 'test/fixtures/dir/foo.txt', 'hello\n') const files = fs.readdirSync('test/fixtures/dir') for (const entry of files) { t.comment(entry) } t.pass('iterated') }) test('readdirSync + withFileTypes: true', async (t) => { await withDir(t, 'test/fixtures/dir') await withFile(t, 'test/fixtures/dir/foo.txt', 'hello\n') const files = fs.readdirSync('test/fixtures/dir', { withFileTypes: true }) for (const entry of files) { t.comment(entry) } t.pass('iterated') }) test('readdirSync + recursive: true', async (t) => { await withDir(t, 'test/fixtures/foo/bar/baz') await withFile(t, 'test/fixtures/foo/bar/baz/foo.txt', 'hello\n') const files = fs.readdirSync('test/fixtures/foo', { recursive: true }) for (const entry of files) { t.comment(entry) } t.pass('iterated') }) test('readFile, file missing', async (t) => { t.plan(1) fs.readFile('test/fixtures/foo.txt', (err) => { t.ok(err, 'file missing') }) }) test('writeFile + readFile', async (t) => { t.plan(3) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.writeFile(file, Buffer.from('foo\n'), (err) => { t.absent(err) fs.readFile(file, (err, data) => { t.absent(err) t.alike(data, Buffer.from('foo\n')) }) }) }) test('appendFile + readFile', async (t) => { t.plan(4) const file = await withFile(t, 'test/fixtures/foo.txt', false) fs.appendFile(file, Buffer.from('foo\n'), (err) => { t.absent(err) fs.appendFile(file, Buffer.from('bar\n'), (err) => { t.absent(err) fs.readFile(file, (err, data) => { t.absent(err) t.alike(data, Buffer.from('foo\nbar\n')) }) }) }) }) test('mkdir', async (t) => { t.plan(3) const dir = await withDir(t, 'test/fixtures/foo', false) fs.mkdir(dir, (err) => { if (err) t.pass('dir exists') else t.pass('made dir') fs.stat(dir, (err, st) => { t.absent(err, 'stat') t.ok(st.isDirectory(), 'is dir') }) }) }) test('mkdir recursive', async (t) => { t.plan(3) await withDir(t, 'test/fixtures/foo', false) const dir = await withDir(t, 'test/fixtures/foo/bar/baz', false) fs.mkdir(dir, { recursive: true }, (err) => { t.absent(err, 'made dir') fs.stat(dir, (err, st) => { t.absent(err, 'stat') t.ok(st.isDirectory(), 'is dir') }) }) }) test('mkdtemp', async (t) => { t.plan(2) fs.mkdtemp('test/fixtures/foo-', (err, dir) => { t.absent(err, 'directory created') withDir(t, dir, false) t.ok(dir, dir) }) }) test('mkdtempSync', (t) => { const dir = fs.mkdtempSync('test/fixtures/foo-') withDir(t, dir, false) t.ok(dir, dir) }) test('copyFile', async (t) => { t.plan(11) await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.copyFile('test/fixtures/foo.txt', 'test/fixtures/bar.txt', (err) => { t.absent(err, 'file copied') fs.open('test/fixtures/foo.txt', (err, fd) => { t.absent(err, 'original copy opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err, 'read original copy') t.is(len, 4) t.alike(data, Buffer.from('foo\n'), 'check original copy content') fs.close(fd, (err) => { t.absent(err, 'original copy closed') }) }) }) fs.open('test/fixtures/bar.txt', (err, fd) => { t.absent(err, 'new copy opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err, 'read new copy') t.is(len, 4) t.alike(data, Buffer.from('foo\n'), 'check new copy content') fs.close(fd, (err) => { t.absent(err, 'new copy closed') }) }) }) }) }) test('copyFileSync', async (t) => { t.plan(10) await withFile(t, 'test/fixtures/foo.txt', 'foo\n') fs.copyFileSync('test/fixtures/foo.txt', 'test/fixtures/bar.txt') fs.open('test/fixtures/foo.txt', (err, fd) => { t.absent(err, 'original copy opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err, 'read original copy') t.is(len, 4) t.alike(data, Buffer.from('foo\n'), 'check original copy content') fs.close(fd, (err) => { t.absent(err, 'original copy closed') }) }) }) fs.open('test/fixtures/bar.txt', (err, fd) => { t.absent(err, 'new copy opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err, 'read new copy') t.is(len, 4) t.alike(data, Buffer.from('foo\n'), 'check new copy content') fs.close(fd, (err) => { t.absent(err, 'new copy closed') }) }) }) }) test('copyFile with COPYFILE_EXCL', async (t) => { t.plan(2) await withFile(t, 'test/fixtures/foo.txt', 'foo\n') await withFile(t, 'test/fixtures/bar.txt', 'bar\n') fs.copyFile( 'test/fixtures/foo.txt', 'test/fixtures/bar.txt', fs.constants.COPYFILE_EXCL, (err) => { t.ok(err) t.is(err.code, 'EEXIST') } ) }) test('copyFileSync with COPYFILE_EXCL', async (t) => { t.plan(1) await withFile(t, 'test/fixtures/foo.txt', 'foo\n') await withFile(t, 'test/fixtures/bar.txt', 'bar\n') t.exception( () => fs.copyFileSync('test/fixtures/foo.txt', 'test/fixtures/bar.txt', fs.constants.COPYFILE_EXCL), /file already exists/ ) }) test('cp', async (t) => { t.plan(11) await withDir(t, 'test/fixtures/dir/foo/bar/baz') await withFile(t, 'test/fixtures/dir/foo/bar/baz/foo.txt', 'foo\n') await withDir(t, 'test/fixtures/dir2', false) fs.cp('test/fixtures/dir', 'test/fixtures/dir2', { recursive: true }, (err) => { t.absent(err, 'directory copied') fs.open('test/fixtures/dir/foo/bar/baz/foo.txt', (err, fd) => { t.absent(err, 'original copy opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err, 'read original copy') t.is(len, 4) t.alike(data, Buffer.from('foo\n'), 'check original copy content') fs.close(fd, (err) => { t.absent(err, 'original copy closed') }) }) }) fs.open('test/fixtures/dir2/foo/bar/baz/foo.txt', (err, fd) => { t.absent(err, 'new copy opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err, 'read new copy') t.is(len, 4) t.alike(data, Buffer.from('foo\n'), 'check new copy content') fs.close(fd, (err) => { t.absent(err, 'new copy closed') }) }) }) }) }) test('cp sync', async (t) => { t.plan(11) await withDir(t, 'test/fixtures/dir/foo/bar/baz') await withFile(t, 'test/fixtures/dir/foo/bar/baz/foo.txt', 'foo\n') await withDir(t, 'test/fixtures/dir2', false) t.execution( fs.cpSync('test/fixtures/dir', 'test/fixtures/dir2', { recursive: true }), 'directory copied' ) fs.open('test/fixtures/dir/foo/bar/baz/foo.txt', (err, fd) => { t.absent(err, 'original copy opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err, 'read original copy') t.is(len, 4) t.alike(data, Buffer.from('foo\n'), 'check original copy content') fs.close(fd, (err) => { t.absent(err, 'original copy closed') }) }) }) fs.open('test/fixtures/dir2/foo/bar/baz/foo.txt', (err, fd) => { t.absent(err, 'new copy opened') const data = Buffer.alloc(4) fs.read(fd, data, 0, 4, 0, (err, len) => { t.absent(err, 'read new copy') t.is(len, 4) t.alike(data, Buffer.from('foo\n'), 'check new copy content') fs.close(fd, (err) => { t.absent(err, 'new copy closed') }) }) }) }) test('link', async (t) => { t.plan(3) const target = await withFile(t, 'test/fixtures/foo.txt', 'foo') const path = await withFile(t, 'test/fixtures/bar.txt', false) fs.link(target, path, (err) => { t.absent(err) fs.readFile(path, (err, data) => { t.absent(err) t.alike(data, Buffer.from('foo')) }) }) }) test('linkSync', async (t) => { t.plan(3) const target = await withFile(t, 'test/fixtures/foo.txt', 'foo') const path = await withFile(t, 'test/fixtures/bar.txt', false) t.execution(fs.linkSync(target, path)) fs.readFile(path, (err, data) => { t.absent(err) t.alike(data, Buffer.from('foo')) }) }) test('realpath', async (t) => { t.plan(2) await withDir(t, 'test/fixtures/foo') const link = await withSymlink(t, 'test/fixtures/foo-link', 'foo') fs.realpath(link, (err, link) => { t.absent(err) t.is(link, path.resolve('test/fixtures/foo')) }) }) test('realpath sync', async (t) => { await withDir(t, 'test/fixtures/foo') const link = await withSymlink(t, 'test/fixtures/foo-link', 'foo') t.is(fs.realpathSync(link), path.resolve('test/fixtures/foo')) }) test('readlink', async (t) => { t.plan(2) const target = await withDir(t, 'test/fixtures/foo') const link = await withSymlink(t, 'test/fixtures/foo-link', 'foo') fs.readlink(link, (err, link) => { t.absent(err) t.is(link, isWindows ? path.resolve(target) : 'foo') }) }) test('readlink sync', async (t) => { const target = await withDir(t, 'test/fixtures/foo') const link = await withSymlink(t, 'test/fixtures/foo-link', 'foo') t.is(fs.readlinkSync(link), isWindows ? path.resolve(target) : 'foo') }) test('symlink', async (t) => { t.plan(3) const target = await withDir(t, 'test/fixtures/foo') const link = await withSymlink(t, 'test/fixtures/foo-link') fs.symlink('foo', link, (err) => { t.absent(err) fs.readlink(link, (err, link) => { t.absent(err) t.is(link, isWindows ? path.resolve(target) : 'foo') }) }) }) test('symlink sync', async (t) => { const target = await withDir(t, 'test/fixtures/foo') const link = await withSymlink(t, 'test/fixtures/foo-link') fs.symlinkSync('foo', link) t.is(fs.readlinkSync(link), isWindows ? path.resolve(target) : 'foo') }) test('symlink to a file', async (t) => { await withFile(t, 'test/fixtures/foo.txt', 'foo\n') const link = await withSymlink(t, 'test/fixtures/foo-link') fs.symlinkSync('foo.txt', link) t.ok(fs.lstatSync(link).isSymbolicLink()) t.is(fs.readFileSync(link, 'utf8'), 'foo\n') }) test('cp copies symbolic links', async (t) => { await withDir(t, 'test/fixtures/dir') await withFile(t, 'test/fixtures/dir/foo.txt', 'foo\n') await withSymlink(t, 'test/fixtures/dir/link', 'foo.txt') await withDir(t, 'test/fixtures/dir2', false) await fs.promises.cp('test/fixtures/dir', 'test/fixtures/dir2', { recursive: true }) t.ok(fs.lstatSync('test/fixtures/dir2/link').isSymbolicLink()) t.is(fs.readlinkSync('test/fixtures/dir2/link'), fs.readlinkSync('test/fixtures/dir/link')) }) test('cp sync copies symbolic links', async (t) => { await withDir(t, 'test/fixtures/dir') await withFile(t, 'test/fixtures/dir/foo.txt', 'foo\n') await withSymlink(t, 'test/fixtures/dir/link', 'foo.txt') await withDir(t, 'test/fixtures/dir2', false) fs.cpSync('test/fixtures/dir', 'test/fixtures/dir2', { recursive: true }) t.ok(fs.lstatSync('test/fixtures/dir2/link').isSymbolicLink()) t.is(fs.readlinkSync('test/fixtures/dir2/link'), fs.readlinkSync('test/fixtures/dir/link')) }) test('cp sync replaces an existing symbolic link', async (t) => { await withDir(t, 'test/fixtures/dir') await withSymlink(t, 'test/fixtures/dir/link', 'foo.txt') await withDir(t, 'test/fixtures/dir2') await withSymlink(t, 'test/fixtures/dir2/link', 'bar.txt') fs.cpSync('test/fixtures/dir', 'test/fixtures/dir2', { recursive: true }) t.is(fs.readlinkSync('test/fixtures/dir2/link'), fs.readlinkSync('test/fixtures/dir/link')) }) test('createReadStream', async (t) => { t.plan(1) const expected = crypto.randomBytes(1024 * 16 /* 16 KiB */) const file = await withFile(t, 'test/fixtures/foo', expected) const stream = fs.createReadStream(file) const read = [] stream .on('data', (data) => read.push(data)) .on('end', () => t.alike(Buffer.concat(read), expected)) }) test('createReadStream, large file', async (t) => { t.plan(1) const expected = crypto.randomBytes(1024 * 512 /* 512 KiB */) const file = await withFile(t, 'test/fixtures/foo', expected) const stream = fs.createReadStream(file) const read = [] stream .on('data', (data) => read.push(data)) .on('end', () => t.alike(Buffer.concat(read), expected)) }) test('createReadStream, character device', { skip: isWindows }, (t) => { t.plan(1) const stream = fs.createReadStream('/dev/zero') stream.on('data', (data) => { t.alike(data, Buffer.alloc(data.byteLength)) stream.destroy() }) }) test('createWriteStream', async (t) => { t.plan(2) const file = await withFile(t, 'test/fixtures/foo') const stream = fs.createWriteStream(file) stream.on('close', () => fs.readFile(file, (err, data) => { t.absent(err) t.alike(data, Buffer.from('hello world')) }) ) stream.write('hello') stream.end(' world') }) test('sync methods', async (t) => { t.plan(4) const file = await withFile(t, 'test/fixtures/foo.txt') const fd = fs.openSync(file, 'w+') t.teardown(() => fs.closeSync(fd)) t.execution(fs.fsyncSync(fd), 'fsyncSync') t.execution(fs.fdatasyncSync(fd), 'fdatasyncSync') fs.fsync(fd, (err) => { t.absent(err, 'fsync') fs.fdatasync(fd, (err) => t.absent(err, 'fdatasync')) }) }) test('teardown with read enqueued from exit listener', (t) => { t.plan(1) // Enqueuing file I/O from an `exit` listener means the request can never // complete back into JavaScript, as the runtime is already tearing down. // Cleaning up the in-flight request during teardown must not crash. const thread = new Thread(require.resolve('./test/threads/read-from-exit-listener')) thread.join() t.pass('thread torn down without crashing') }) test('watch, missing path', (t) => { t.exception(() => fs.watch('test/fixtures/does-not-exist'), /no such file or directory/) }) test('watch, missing path during teardown', (t) => { t.plan(1) const thread = new Thread(require.resolve('./test/threads/watch-missing-path')) thread.join() t.pass('thread torn down without crashing') }) async function withFile(t, path, data = Buffer.alloc(0), opts = {}) { if (data) await fs.promises.writeFile(path, data, opts) t.teardown(() => fs.promises.rm(path, { force: true })) return path } async function withSymlink(t, path, target = false) { if (target) { try { await fs.promises.rm(path) } catch {} await fs.promises.symlink(target, path) } t.teardown(() => fs.promises.rm(path, { force: true })) return path } async function withDir(t, path, create = true) { if (create) await fs.promises.mkdir(path, { recursive: true }) t.teardown(() => fs.promises.rm(path, { force: true, recursive: true })) return path }