Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brianc committed Mar 4, 2012
1 parent 0976817 commit 0fcfa7c
Showing 1 changed file with 53 additions and 26 deletions.
79 changes: 53 additions & 26 deletions test/index.js
Expand Up @@ -4,34 +4,27 @@ var should = require('should');

var tailed = require(__dirname + '/../lib');

var writeText = function(path, text) {
var create = function(path) {
var fd = fs.openSync(path, 'w');
fs.closeSync(fd);
console.log('created %s', path);
}

var write = function(path, text) {
var fd = fs.openSync(path, 'a');
console.log('writing %s to %s', text, path);
fs.writeSync(fd, text, fs.fstatSync(fd).size);
fs.closeSync(fd);
};

var rm = function(path) {
fs.unlinkSync(path);
console.log('deleted %s', path);
}

var file = path.join(__dirname, 'temp.txt');

describe('tailed', function() {
beforeEach(function() {
//ensure file exists
writeText(file, '');
console.log('created ', file);
});


afterEach(function(done) {
//delete the file

process.nextTick(function() {
try{
fs.unlinkSync(file);
console.log('deleted %s', file);
} catch(e) { console.error(e) }
done();
})
});

describe('ctor args', function() {

it('requires valid filename', function() {
Expand All @@ -41,24 +34,58 @@ describe('tailed', function() {
});

it('defaults to utf8 encoding', function() {
tailed(file, function(err, tail) {
var path = __dirname + '/x';
create(path);
tailed(path, function(err, tail) {
should.equal(null);
tail.should.not.equal(null)
rm(path);
});
});
});

describe('canary test', function(){
});

it('emits data', function(done) {
describe('tailed', function() {
describe('canary tests', function() {
beforeEach(function() {
create(file);
});

afterEach(function() {
rm(file);
});

it('single message', function(done) {
tailed(file, function(err, tail) {
if(err) done(err);
tail.close();
done();
tail.once('data', function(data) {
tail.close();
data.should.equal('hi');
done();
});
write(file, 'hi');
});
});

it('multiple messages', function(done) {
tailed(file, function(err, tail) {
if(err) done(err);
tail.once('data', function(data) {
data.should.equal('one');
tail.once('data', function(data) {
data.should.equal('two');
tail.once('data', function(data) {
tail.close();
data.should.equal('three');
done();
});
write(file, 'three');
});
write(file, 'two');
});
write(file, 'one');
})
});
});

});

0 comments on commit 0fcfa7c

Please sign in to comment.