Compare commits

...

1 Commits

Author SHA1 Message Date
Jarred Sumner
62fcf8a1e0 Add a couple node tests that are now passing 2024-10-10 16:22:52 -07:00
5 changed files with 207 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
//#FILE: test-fs-buffertype-writesync.js
//#SHA1: 6af4aca43ae7299ed310d17733db6dcc43d0ed2b
//-----------------
'use strict';
const fs = require('fs');
test('fs.writeSync throws for invalid data input', () => {
const invalidInputs = [
true, false, 0, 1, Infinity, () => {}, {}, [], undefined, null,
];
invalidInputs.forEach((value) => {
expect(() => fs.writeSync(1, value)).toThrow(expect.objectContaining({
message: expect.stringMatching(/"buffer"/),
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
}));
});
});
//<#END_FILE: test-fs-buffertype-writesync.js

View File

@@ -0,0 +1,62 @@
//#FILE: test-fs-read-stream-resume.js
//#SHA1: ce30da9de55a395441a4670145bc3a57a2e1763e
//-----------------
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const fs = require('fs');
const path = require('path');
const file = path.join(__dirname, '..', 'fixtures', 'x.txt');
test('fs.createReadStream resume behavior', (done) => {
let data = '';
let first = true;
const stream = fs.createReadStream(file);
stream.setEncoding('utf8');
const dataHandler = jest.fn((chunk) => {
data += chunk;
if (first) {
first = false;
stream.resume();
}
});
stream.on('data', dataHandler);
process.nextTick(() => {
stream.pause();
setTimeout(() => {
stream.resume();
}, 100);
});
stream.on('end', () => {
expect(data).toBe('xyz\n');
expect(dataHandler).toHaveBeenCalled();
done();
});
});
//<#END_FILE: test-fs-read-stream-resume.js

View File

@@ -0,0 +1,41 @@
//#FILE: test-fs-read-zero-length.js
//#SHA1: bda4b0f0c821a8479ffbf0a9099444eed6ee5c4e
//-----------------
'use strict';
const fs = require('fs');
const path = require('path');
const fixturesPath = path.join(__dirname, '..', 'fixtures');
const filepath = path.join(fixturesPath, 'x.txt');
let fd;
beforeAll(() => {
fd = fs.openSync(filepath, 'r');
});
afterAll(() => {
fs.closeSync(fd);
});
test('fs.read with zero length buffer (async)', (done) => {
const bufferAsync = Buffer.alloc(0);
fs.read(fd, bufferAsync, 0, 0, 0, (err, bytesRead) => {
expect(err).toBeNull();
expect(bytesRead).toBe(0);
expect(bufferAsync).toEqual(Buffer.alloc(0));
done();
});
});
test('fs.readSync with zero length buffer', () => {
const bufferSync = Buffer.alloc(0);
const bytesRead = fs.readSync(fd, bufferSync, 0, 0, 0);
expect(bufferSync).toEqual(Buffer.alloc(0));
expect(bytesRead).toBe(0);
});
//<#END_FILE: test-fs-read-zero-length.js

View File

@@ -0,0 +1,25 @@
//#FILE: test-fs-realpath-native.js
//#SHA1: add12c89cd17b16ae70ae1cfe943ce49157b2e68
//-----------------
'use strict';
const fs = require('fs');
const path = require('path');
const filename = __filename.toLowerCase();
test('fs.realpathSync.native works correctly', () => {
const result = fs.realpathSync.native(filename);
expect(result.toLowerCase()).toBe(filename);
});
test('fs.realpath.native works correctly', async () => {
const result = await new Promise((resolve, reject) => {
fs.realpath.native(filename, (err, res) => {
if (err) reject(err);
else resolve(res);
});
});
expect(result.toLowerCase()).toBe(filename);
});
//<#END_FILE: test-fs-realpath-native.js

View File

@@ -0,0 +1,58 @@
//#FILE: test-fs-watch-recursive-delete.js
//#SHA1: 00ca669f5bbedc8645a0e2ab48bd2f4200ab8175
//-----------------
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const tmpdir = path.join(os.tmpdir(), 'test-fs-watch-recursive-delete');
beforeAll(() => {
if (fs.existsSync(tmpdir)) {
fs.rmSync(tmpdir, { recursive: true, force: true });
}
fs.mkdirSync(tmpdir, { recursive: true });
});
afterAll(() => {
fs.rmSync(tmpdir, { recursive: true, force: true });
});
// Skip test for SunOS and IBMi
const isSunOS = os.platform() === 'sunos';
const isIBMi = os.platform() === 'aix' && os.type() === 'OS400';
if (isSunOS || isIBMi) {
test.skip('SunOS behaves differently or IBMi does not support `fs.watch()`', () => {});
} else {
test('fs.watch recursive delete', (done) => {
const parentDir = path.join(tmpdir, 'parent');
const childDir = path.join(parentDir, 'child');
const testFile = path.join(childDir, 'test.tmp');
fs.mkdirSync(childDir, { recursive: true });
fs.writeFileSync(testFile, 'test');
const onFileUpdate = jest.fn((eventType, filename) => {
// We are only checking for the filename to avoid having Windows, Linux and Mac specific assertions
if (fs.readdirSync(parentDir).length === 0) {
watcher.close();
expect(onFileUpdate).toHaveBeenCalled();
done();
}
});
const watcher = fs.watch(parentDir, { recursive: true }, onFileUpdate);
// We must wait a bit for `fs.rm()` to let the watcher be set up properly
setTimeout(() => {
fs.rm(childDir, { recursive: true }, (err) => {
expect(err).toBeNull();
});
}, 500);
}, 10000); // Increase timeout to 10 seconds
}
//<#END_FILE: test-fs-watch-recursive-delete.js