Skip to content

Commit

Permalink
Tests (#29)
Browse files Browse the repository at this point in the history
* changes var to const

* changes tape to test

* more destructs

* cleanup

* changes ok to true

* starts adding fd test (#17)

* adds missing fd tests
  • Loading branch information
tcme committed May 31, 2017
1 parent eabe50b commit a8c2b82
Show file tree
Hide file tree
Showing 21 changed files with 435 additions and 343 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -5,7 +5,6 @@
"main": "lib/main.js",
"scripts": {
"test": "tape test/test.js | tap-difflet",
"test-length": "tape test/test-functions.js | tap-spec",
"coverage": "nyc --reporter=lcov --reporter=text tape test/test.js",
"codecov": "npm run coverage && codecov",
"prettier": "prettier-eslint --write \"{lib,test}/*.js\"",
Expand Down
72 changes: 36 additions & 36 deletions test/access.test.js
@@ -1,19 +1,19 @@
var file = require('../');
const file = require('../');

var tape = require('tape');
var resolve = require('path').resolve;
var fs = require('fs');
const test = require('tape');
const { resolve } = require('path');
const fs = require('fs');

var dir = resolve(__dirname, 'data/access');
var fileR = resolve(dir, 'file-r.txt');
var fileRW = resolve(dir, 'file-rw.txt');
var fileRWX = resolve(dir, 'file-rwx.txt');
var fileRX = resolve(dir, 'file-rx.txt');
var fileW = resolve(dir, 'file-w.txt');
var fileWX = resolve(dir, 'file-wx.txt');
var fileX = resolve(dir, 'file-x.txt');
const dir = resolve(__dirname, 'data/access');
const fileR = resolve(dir, 'file-r.txt');
const fileRW = resolve(dir, 'file-rw.txt');
const fileRWX = resolve(dir, 'file-rwx.txt');
const fileRX = resolve(dir, 'file-rx.txt');
const fileW = resolve(dir, 'file-w.txt');
const fileWX = resolve(dir, 'file-wx.txt');
const fileX = resolve(dir, 'file-x.txt');

tape('setup access', t => {
test('setup access', t => {
file
.mkdir(dir)
.then(() => {
Expand All @@ -39,8 +39,8 @@ tape('setup access', t => {
});
});

tape('access without mode', t => {
var files = [fileR, fileRW, fileRWX, fileRX, fileW, fileWX, fileX];
test('access without mode', t => {
const files = [fileR, fileRW, fileRWX, fileRX, fileW, fileWX, fileX];

Promise.all(
files.map(path => {
Expand All @@ -55,7 +55,7 @@ tape('access without mode', t => {
t.pass('access correct');
})
.then(() => {
var nofile = resolve(dir, 'file-does-not-exist.txt');
const nofile = resolve(dir, 'file-does-not-exist.txt');
files.push(nofile);
return Promise.all(
files.map(path => {
Expand All @@ -67,8 +67,8 @@ tape('access without mode', t => {
t.end();
})
.catch(error => {
t.ok(error, error);
t.ok(error instanceof Error, 'is Error');
t.true(error, error);
t.true(error instanceof Error, 'is Error');
t.equal(error.code, 'ENOENT', 'error.code is ENOENT');
t.equal(error.path, nofile);
});
Expand All @@ -78,7 +78,7 @@ tape('access without mode', t => {
});
});

tape('access with mode', t => {
test('access with mode', t => {
Promise.all([
file.access(fileR, 'r'),
file.access(fileRW, 'rw'),
Expand Down Expand Up @@ -126,7 +126,7 @@ tape('access with mode', t => {
});
});

tape('access error with mode', t => {
test('access error with mode', t => {
Promise.all([
file.access(fileR, 'r'),
file.access(fileRW, fs.R_OK | fs.W_OK),
Expand All @@ -141,18 +141,18 @@ tape('access error with mode', t => {
t.end();
})
.catch(error => {
t.ok(error, error);
t.ok(error instanceof Error, 'is Error');
t.ok(
t.true(error, error);
t.true(error instanceof Error, 'is Error');
t.true(
/^(EACCES|EPERM)$/.test(error.code),
'error.code is EACCES (or EPERM on Windows)'
);
t.ok([fileWX, fileX].indexOf(error.path) > -1, 'is fileWX or fileX');
t.true([fileWX, fileX].indexOf(error.path) > -1, 'is fileWX or fileX');
t.end();
});
});

tape('access fail', t => {
test('access fail', t => {
function fail() {
t.fail('should not pass');
t.end();
Expand All @@ -162,9 +162,9 @@ tape('access fail', t => {
.access(fileR, 'rwx')
.then(fail)
.catch(error => {
t.ok(error, error);
t.ok(error instanceof Error, 'is Error');
t.ok(
t.true(error, error);
t.true(error instanceof Error, 'is Error');
t.true(
/^(EACCES|EPERM)$/.test(error.code),
'error.code is EACCES (or EPERM on Windows)'
);
Expand All @@ -173,8 +173,8 @@ tape('access fail', t => {
})
.then(() => {
return file.access(fileR, 'w').then(fail).catch(error => {
t.ok(error, error);
t.ok(
t.true(error, error);
t.true(
/^(EACCES|EPERM)$/.test(error.code),
'error.code is EACCES (or EPERM on Windows)'
);
Expand All @@ -191,9 +191,9 @@ tape('access fail', t => {
})
.then(fail)
.catch(error => {
t.ok(error, error);
t.ok(error instanceof Error, 'is Error');
t.ok(
t.true(error, error);
t.true(error instanceof Error, 'is Error');
t.true(
/^(EACCES|EPERM)$/.test(error.code),
'error.code is EACCES (or EPERM on Windows)'
);
Expand All @@ -210,9 +210,9 @@ tape('access fail', t => {
})
.then(fail)
.catch(error => {
t.ok(error, error);
t.ok(error instanceof Error, 'is Error');
t.ok(
t.true(error, error);
t.true(error instanceof Error, 'is Error');
t.true(
/^(EACCES|EPERM)$/.test(error.code),
'error.code is EACCES (or EPERM on Windows)'
);
Expand Down
45 changes: 33 additions & 12 deletions test/append.test.js
@@ -1,18 +1,18 @@
var file = require('../');
const file = require('../');

var tape = require('tape');
var resolve = require('path').resolve;
var readFileSync = require('fs').readFileSync;
var writeFileSync = require('fs').writeFileSync;
const test = require('tape');
const { resolve } = require('path');
const { readFileSync } = require('fs');
const { writeFileSync } = require('fs');

var filepath = resolve(__dirname, './data/append.txt');
const filepath = resolve(__dirname, './data/append.txt');

tape('setup append', t => {
test('setup append', t => {
writeFileSync(filepath, 'abc');
t.end();
});

tape('append', t => {
test('append', t => {
file
.appendFile(filepath, 'def')
.then(() => {
Expand All @@ -26,7 +26,7 @@ tape('append', t => {
});
});

tape('append a buffer', t => {
test('append a buffer', t => {
file
.appendFile(filepath, new Buffer('ghi'))
.then(() => {
Expand All @@ -40,7 +40,28 @@ tape('append a buffer', t => {
});
});

tape('append error', t => {
test('append to fd', t => {
file
.open(filepath, {
flags: 'a+'
})
.then(fd => {
return file.appendFile(fd, 'jkl').then(() => fd);
})
.then(fd => file.appendFile(fd, 'mno'))
// .then(fd => file.close(fd))
.then(() => {
t.pass('appended data to fd');
t.equal(readFileSync(filepath, 'utf8'), 'abcdefghijklmno');
t.end();
})
.catch(error => {
t.error(error);
t.end();
});
});

test('append error', t => {
file
.appendFile(filepath, '', {
flag: 'r'
Expand All @@ -50,8 +71,8 @@ tape('append error', t => {
t.end();
})
.catch(error => {
t.ok(error, error);
t.ok(
t.true(error, error);
t.true(
/^(EBADF|EPERM)$/.test(error.code),
'error.code is EBADF (or EPERM on Windows)'
);
Expand Down
34 changes: 17 additions & 17 deletions test/chmod.test.js
@@ -1,39 +1,39 @@
var file = require('../');
const file = require('../');

var tape = require('tape');
var resolve = require('path').resolve;
var statSync = require('fs').statSync;
var writeFileSync = require('fs').writeFileSync;
const test = require('tape');
const { resolve } = require('path');
const { statSync } = require('fs');
const { writeFileSync } = require('fs');

var filepath = resolve(__dirname, './data/chmod.txt');
const filepath = resolve(__dirname, './data/chmod.txt');

function isPermission(mode, permission) {
mode = '0' + (mode & parseInt('0777', 8)).toString(8);
return mode === permission;
}

if (process.platform != 'win32') {
tape('setup chmod', t => {
test('setup chmod', t => {
writeFileSync(filepath, 'chmod test\n');
t.end();
});

tape('chmod', t => {
test('chmod', t => {
file
.chmod(filepath, {
mode: '0700' // nobody else
})
.then(() => {
var stats = statSync(filepath);
t.ok(isPermission(stats.mode, '0700'), 'permission set to 0700');
const stats = statSync(filepath);
t.true(isPermission(stats.mode, '0700'), 'permission set to 0700');

return file.chmod(filepath, {
mode: parseInt('0755', 8)
});
})
.then(() => {
var stats = statSync(filepath);
t.ok(isPermission(stats.mode, '0755'), 'permission set to 0755');
const stats = statSync(filepath);
t.true(isPermission(stats.mode, '0755'), 'permission set to 0755');
t.end();
})
.catch(error => {
Expand All @@ -42,7 +42,7 @@ if (process.platform != 'win32') {
});
});

tape('chmod error', t => {
test('chmod error', t => {
file
.chmod(-1, {
mode: '0777'
Expand All @@ -52,23 +52,23 @@ if (process.platform != 'win32') {
t.end();
})
.catch(error => {
t.ok(error, error);
t.true(error, error);
t.equal(error.code, 'EBADF', 'error.code is EBADF');
t.equal(error.syscall, 'fchmod', 'error.syscal is fchmod');
t.end();
});
});

tape('chmod error 2', t => {
test('chmod error 2', t => {
file
.chmod(filepath)
.then(() => {
t.fail('should not chmod');
t.end();
})
.catch(error => {
t.ok(error, error);
t.ok(error instanceof TypeError, 'is TypeError');
t.true(error, error);
t.true(error instanceof TypeError, 'is TypeError');
t.end();
});
});
Expand Down
22 changes: 11 additions & 11 deletions test/chown.test.js
@@ -1,23 +1,23 @@
var file = require('../');
const file = require('../');

var tape = require('tape');
var resolve = require('path').resolve;
var statSync = require('fs').statSync;
var writeFileSync = require('fs').writeFileSync;
const test = require('tape');
const { resolve } = require('path');
const { statSync } = require('fs');
const { writeFileSync } = require('fs');

var filepath = resolve(__dirname, './data/chown.txt');
const filepath = resolve(__dirname, './data/chown.txt');

if (process.platform != 'win32') {
tape('setup chown', t => {
test('setup chown', t => {
writeFileSync(filepath, 'chown test\n');
t.end();
});

tape('chown', t => {
test('chown', t => {
file
.chown(filepath)
.then(() => {
var stats = statSync(filepath);
const stats = statSync(filepath);

t.equal(stats.uid, process.getuid(), 'uid is process.getuid');
t.equal(stats.gid, process.getgid(), 'gid is process.getgid');
Expand All @@ -29,15 +29,15 @@ if (process.platform != 'win32') {
});
});

tape('chown error', t => {
test('chown error', t => {
file
.chown(-1)
.then(() => {
t.fail('should not chown');
t.end();
})
.catch(error => {
t.ok(error, error);
t.true(error, error);
t.equal(error.code, 'EBADF', 'error.code is EBADF');
t.equal(error.syscall, 'fchown', 'error.syscal is fchown');
t.end();
Expand Down

0 comments on commit a8c2b82

Please sign in to comment.