file tests

This commit is contained in:
2021-03-03 15:22:22 -06:00
parent 716c4bc476
commit 6c554f2e76
4 changed files with 53 additions and 2 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
package-lock.json package-lock.json
.idea/ .idea/
node_modules/ node_modules/
coverage/

View File

@@ -3,7 +3,8 @@
"version": "1.3.1-r1", "version": "1.3.1-r1",
"description": "A Node.js module for interfacing with Repertory's remote mount API", "description": "A Node.js module for interfacing with Repertory's remote mount API",
"scripts": { "scripts": {
"test": "jest" "test": "jest",
"coverage": "jest --collect-coverage"
}, },
"keywords": [ "keywords": [
"repertory", "repertory",

View File

@@ -0,0 +1,49 @@
import file from '../io/file';
jest.mock('../ops/index.js', () => (
{
...(jest.requireActual('../ops/index.js')),
close_file: jest.fn(),
}
));
import {close_file} from '../ops/index';
test(`can close a closed file`, async () => {
const f = new file();
expect(await f.close()).toEqual(0);
});
test(`'get_size' fails on closed file`, async () => {
const f = new file();
await expect(f.get_size()).rejects.toThrow(Error);
});
test(`'read' fails on closed file`, async () => {
const f = new file();
await expect(f.read(0, 10)).rejects.toThrow(Error);
});
test(`'truncate' fails on closed file`, async () => {
const f = new file();
await expect(f.truncate(0)).rejects.toThrow(Error);
});
test(`'write' fails on closed file`, async () => {
const f = new file();
await expect(f.write(0, Buffer.alloc(2))).rejects.toThrow(Error);
});
test(`handle is set to null on close`, async () => {
const f = new file(null, 1, '/path');
close_file.mockReturnValue(0);
expect(await f.close()).toEqual(0);
expect(f.handle).toBeNull();
});
test(`handle is not changed on close if return is not 0`, async () => {
const f = new file(null, 1, '/path');
close_file.mockReturnValue(1);
expect(await f.close()).toEqual(1);
expect(f.handle).toBe(1);
});

View File

@@ -5,7 +5,7 @@ let next_thread_id = 1;
export default class file { export default class file {
constructor(conn, handle, remote_path) { constructor(conn, handle, remote_path) {
this.conn = conn; this.conn = conn;
this.handle = handle; this.handle = handle || null;
this.remote_path = remote_path; this.remote_path = remote_path;
this.thread_id = next_thread_id++; this.thread_id = next_thread_id++;
} }