Skip to content

Commit

Permalink
Supports Buffer as input
Browse files Browse the repository at this point in the history
Checks that `writeFile()` could handle a Buffer as the content of the file.
  • Loading branch information
JbIPS authored and lexoyo committed Feb 10, 2018
1 parent 1c62975 commit fd4fe61
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 19 deletions.
5 changes: 5 additions & 0 deletions lib/unifile-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ class GitHubConnector {
.then(() => {
session.token = sessionCopy.token;
return session.token;
})
.catch((e) => {
// Override default error message ('Requires authentication')
if(e.code === UnifileError.EACCES) throw new UnifileError(401, 'Bad credentials');
});
}

Expand Down Expand Up @@ -896,6 +900,7 @@ class GitHubConnector {
const {code, message} = (() => {
const defaultMessage = JSON.parse(body).message;
switch (res.statusCode) {
case 401: // fallthrough
case 403:
return {code: UnifileError.EACCES, message: defaultMessage};
case 404:
Expand Down
10 changes: 10 additions & 0 deletions test/unifile-dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ describe('DropboxConnector', function() {
});
});

it('writes into a file with a Buffer', function() {
return connector.writeFile(session, 'unifile_writeFile/file1.txt', Buffer.from(data))
.then(() => {
return connector.readFile(session, 'unifile_writeFile/file1.txt');
})
.then((content) => {
return expect(content.toString()).to.equal(data);
});
});

after('Remove folder', function() {
if(isEnvValid()) connector.rmdir(session, 'unifile_writeFile');
else this.skip();
Expand Down
12 changes: 10 additions & 2 deletions test/unifile-fs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

const Promise = require('bluebird');
const Fs = Promise.promisifyAll(require('fs'), {suffix: 'Promised'});
const Path = require('path');
const Os = require('os');
const Fs = require('fs');
const {Readable, Writable} = require('stream');
const chai = require('chai');
chai.use(require('chai-as-promised'));
Expand Down Expand Up @@ -315,7 +316,14 @@ describe('FsConnector', function() {
it('writes into a file', function() {
return connector.writeFile({}, Path.join(Os.tmpdir(), filename), data)
.then(() => {
Fs.statSync(Path.join(Os.tmpdir(), filename)).should.exist;
return Fs.readFilePromised(Path.join(Os.tmpdir(), filename), 'utf8').should.become(data);
});
});

it('writes into a file with a Buffer', function() {
return connector.writeFile({}, Path.join(Os.tmpdir(), filename), Buffer.from(data))
.then(() => {
return Fs.readFilePromised(Path.join(Os.tmpdir(), filename), 'utf8').should.become(data);
});
});

Expand Down
9 changes: 8 additions & 1 deletion test/unifile-ftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,14 @@ describe('FtpConnector', function() {
it('writes into a file', function() {
return connector.writeFile(session, 'tmp.test', data)
.then(() => {
Fs.readFilePromised('tmp.test', 'utf8').should.become(data);
return Fs.readFilePromised('tmp.test', 'utf8').should.become(data);
});
});

it('writes into a file with a Buffer', function() {
return connector.writeFile(session, 'tmp.test', Buffer.from(data))
.then(() => {
return Fs.readFilePromised('tmp.test', 'utf8').should.become(data);
});
});

Expand Down
33 changes: 17 additions & 16 deletions test/unifile-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,23 @@ describe('GitHubConnector', function() {
it('writes into a file', function() {
return connector.writeFile(session, 'unifile_writeFile/test/testFile', data)
.then(() => {
return connector.readFile(session, 'unifile_writeFile/test/testFile').then((content) => {
return expect(content.toString()).to.equal(data);
});
return connector.readFile(session, 'unifile_writeFile/test/testFile');
})
.then((content) => {
return expect(content.toString()).to.equal(data);
})
.then(() => {
return connector.unlink(session, 'unifile_writeFile/test/testFile');
});
});

it('writes into a file with a Buffer', function() {
return connector.writeFile(session, 'unifile_writeFile/test/testFile', Buffer.from(data))
.then(() => {
return connector.readFile(session, 'unifile_writeFile/test/testFile');
})
.then((content) => {
return expect(content.toString()).to.equal(data);
})
.then(() => {
return connector.unlink(session, 'unifile_writeFile/test/testFile');
Expand Down Expand Up @@ -676,19 +690,6 @@ describe('GitHubConnector', function() {
} else this.skip();
});

it('throws an error if wrong credentials', function(done) {
const stream = connector.createReadStream(Object.assign({}, session, {
token: 'aoa'
}), 'aouoeuoeu');
stream.on('error', (err) => {
expect(err.message).to.equal('Bad credentials');
done();
});
stream.on('data', () => {
done(new Error('Should not emit this event'));
});
});

it('throws an error if the path does not exist', function(done) {
const stream = connector.createReadStream(session, 'aouoeuoeu');
stream.on('error', (err) => {
Expand Down
6 changes: 6 additions & 0 deletions test/unifile-sftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,12 @@ describe('SFTPConnector', function() {
.then(() => Fs.readFilePromised('tmp.test', 'utf8').should.become(data))
.then(() => Fs.unlinkPromised('tmp.test'));
});

it('writes into a file with a Buffer', function() {
return connector.writeFile(session, 'tmp.test', Buffer.from(data))
.then(() => Fs.readFilePromised('tmp.test', 'utf8').should.become(data))
.then(() => Fs.unlinkPromised('tmp.test'));
});
});

describe('createWriteStream()', function() {
Expand Down

0 comments on commit fd4fe61

Please sign in to comment.