Skip to content

Commit

Permalink
Fixed extra line in file issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rbtdev committed Jan 12, 2017
1 parent 27a4592 commit 2638ffe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ function passwdjs(arg, options) {
if (typeof arg === 'string') {
try {
lines = fs.readFileSync(arg).toString().split('\n');
}
catch (ex) {
lines.splice(lines.length - 1, 1);
} catch (ex) {
setImmediate(function () {
e.emit("error", ex)
});
return e;
}
}
else lines = Array.isArray(arg) ? arg : [].push(arg);
} else lines = Array.isArray(arg) ? arg : [].push(arg);
var opts = options ? options : {
rounds: 10,
json: false
Expand All @@ -36,7 +35,6 @@ function passwdjs(arg, options) {
})

function hash(line, cb) {
if (line.length === 0) return async.setImmediate(cb, null, null);
bcrypt.genSalt(opts.rounds, function (err, salt) {
if (err) return cb(err);
bcrypt.hash(line, salt, function (err, hashed) {
Expand Down
60 changes: 48 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ var hashLines = null;
var hashResult = null;
var lineCount = 0;
var arg = null;
var errorObj = null;


describe("hash array of passwords, emit line event", function (done) {
before(function (done) {
reset(false, false);
reset({
file: false,
json: false,
error: false
});
run(done);
})

it("Should emit a 'line' event for each password", function () {
expect(hash).to.be.a.string;
expect(lineCount).to.equal(arg.length);
Expand All @@ -39,7 +43,11 @@ describe("hash array of passwords, emit line event", function (done) {

describe("hash file of passwords, emit line event", function (done) {
before(function (done) {
reset(false, false);
reset({
file: true,
json: false,
error: false
});
run(done);
})

Expand All @@ -64,7 +72,11 @@ describe("hash file of passwords, emit line event", function (done) {

describe("hash array of passwords, return json obj", function (done) {
before(function (done) {
reset(false, true);
reset({
file: false,
json: true,
error: false
});
run(done);
})

Expand All @@ -89,13 +101,16 @@ describe("hash array of passwords, return json obj", function (done) {

describe("hash file of passwords, return json obj", function (done) {
before(function (done) {
reset(true, true);
reset({
file: true,
json: true,
error: false
});
run(done);
})

it("Should emit a 'done' event", function () {
expect(doneEvent).to.be.true;

});

it("Should return an object", function () {
Expand All @@ -111,11 +126,27 @@ describe("hash file of passwords, return json obj", function (done) {
})
});

describe("handle errors gracefully", function (done) {
before(function (done) {
reset({
file: true,
json: true,
error: true
})
run(done);
})
it("Should emit an error event for file error", function () {
expect(errorEvent).to.be.true;
expect(errorObj).to.be.an.object;
expect(errorObj).to.have.property('code').equals('ENOENT');
})
})

function run(done) {
passwdjs(arg, opts)
.on('line', gotLine)
.on('done', gotDone);
.on('done', gotDone)
.on('error', gotError)

function gotLine(line) {
hashLines.push(line);
Expand All @@ -128,17 +159,21 @@ function run(done) {
done();
}

function gotError(err) {
errorEvent = true;
errorObj = err;
done();
}
}


function reset(file, json) {
function reset(options) {
lineEvent = false;
doneEvent = false;
hash = null;
hashLines = [];
hashResult = null;
lineCount = 0;

arg = [
'passwd1',
'passwd2',
Expand All @@ -148,8 +183,9 @@ function reset(file, json) {
opts = {
rounds: 10,
plaintext: false,
json: json
json: options.json
}

if (file) arg = 'test/test.txt';
if (options.file) arg = 'test/test.txt';
if (options.file && options.error) arg = "xxxxxxx";
}
2 changes: 2 additions & 0 deletions test/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ passwd2
passwd3LongPassword with special characters(*&%&^^$
passwd4
passwd5


0 comments on commit 2638ffe

Please sign in to comment.