Skip to content

Commit

Permalink
Remove atomic files on all errors
Browse files Browse the repository at this point in the history
Fixes #156.
  • Loading branch information
kevva committed Jan 21, 2015
1 parent b5629b2 commit 5c8f1fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ exports.viewport = function (obj, options, cb) {
exports.save = function (streams, cb) {
var files = [];

process.on('SIGINT', function () {
function end(cb) {
eachAsync(files, function (file, i, next) {
rm(file, next);
}, process.exit);
});
}, cb);
}

process.on('SIGINT', end.bind(null, process.exit));

eachAsync(streams, function (stream, i, next) {
mkdir(this.dest(), function (err) {
Expand All @@ -112,9 +114,14 @@ exports.save = function (streams, cb) {
files.push(write.__atomicTmp);

stream.on('warn', this.emit.bind(this, 'warn'));
stream.on('error', next);
stream.on('error', function (err) {
end(next.bind(null, err));
});

pipe.on('finish', next);
pipe.on('error', next);
pipe.on('error', function (err) {
end(next.bind(null, err));
});
}.bind(this));
}.bind(this), function (err) {
if (err) {
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ test('save image', function (t) {
});
});

test('remove temporary files on error', function (t) {
t.plan(4);

var pageres = new Pageres()
.src('this-is-a-error-site.io', ['1024x768'])
.dest(__dirname);

pageres.run(function (err) {
t.assert(err);
t.assert(err.message === 'Couldn\'t load url: http://this-is-a-error-site.io');

fs.readdir(__dirname, function (err, files) {
t.assert(!err, err);
t.assert(files.indexOf('this-is-a-error-site.io.png') === -1);
});
});
});

test('generate screenshot using the CLI', function (t) {
t.plan(2);

Expand Down

0 comments on commit 5c8f1fa

Please sign in to comment.