Skip to content

Commit f4d8e2f

Browse files
authored
Merge pull request benjamingr#8 from piranna/master
Pass arguments to `tmp`
2 parents e1759fe + e13c7a8 commit f4d8e2f

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ var tmp = require("tmp");
22
var Promise = require("bluebird");
33

44

5-
// file
5+
// file
66
module.exports.fileSync = tmp.fileSync;
77
var file = Promise.promisify(tmp.file, {multiArgs: true});
8+
89
module.exports.file = function file$promise() {
910
return file.apply(tmp, arguments).spread(function (path, fd, cleanup) {
1011
return {path: path, fd: fd, cleanup : cleanup };
@@ -13,16 +14,21 @@ module.exports.file = function file$promise() {
1314

1415
module.exports.withFile = function withFile(fn) {
1516
var cleanup;
16-
return module.exports.file.apply(tmp).then(function context(o) {
17+
18+
var params = Array.prototype.slice.call(arguments, 1);
19+
20+
return module.exports.file.apply(tmp, params).then(function context(o) {
1721
cleanup = o.cleanup;
1822
delete o.cleanup;
1923
return fn(o);
2024
}).finally(cleanup);
2125
};
2226

23-
// directory
27+
28+
// directory
2429
module.exports.dirSync = tmp.dirSync;
2530
var dir = Promise.promisify(tmp.dir, {multiArgs: true});
31+
2632
module.exports.dir = function dir$promise() {
2733
return dir.apply(tmp, arguments).spread(function (path, cleanup) {
2834
return {path: path, cleanup: cleanup};
@@ -31,19 +37,21 @@ module.exports.dir = function dir$promise() {
3137

3238
module.exports.withDir = function withDir(fn) {
3339
var cleanup;
34-
return module.exports.dir.apply(tmp, arguments).then(function context(o) {
40+
41+
var params = Array.prototype.slice.call(arguments, 1);
42+
43+
return module.exports.dir.apply(tmp, params).then(function context(o) {
3544
cleanup = o.cleanup;
3645
delete o.cleanup;
3746
return fn(o);
3847
}).finally(cleanup);
3948
};
4049

50+
4151
// name generation
4252
module.exports.tmpNameSync = tmp.tmpNameSync;
4353
module.exports.tmpName = Promise.promisify(tmp.tmpName);
4454

45-
4655
module.exports.tmpdir = tmp.tmpdir;
4756

48-
4957
module.exports.setGracefulCleanup = tmp.setGracefulCleanup;

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "The tmp package with promises support and disposers.",
55
"main": "index.js",
66
"scripts": {
7-
"test": ""
7+
"test": "mocha"
88
},
99
"keywords": [
1010
"tmp",
@@ -22,5 +22,8 @@
2222
"dependencies": {
2323
"bluebird": "^3.3.1",
2424
"tmp": "0.0.28"
25+
},
26+
"devDependencies": {
27+
"mocha": "^3.1.2"
2528
}
2629
}

test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var accessSync = require('fs').accessSync
2+
var assert = require('assert')
3+
var extname = require('path').extname
4+
5+
var tmp = require('.')
6+
7+
8+
describe('withFile', function()
9+
{
10+
it("file don't exists after going out of scope", function()
11+
{
12+
var filepath
13+
14+
return tmp.withFile(function(o)
15+
{
16+
filepath = o.path
17+
18+
accessSync(filepath)
19+
assert.strictEqual(extname(filepath), '.txt')
20+
}, {postfix: '.txt'})
21+
.then(function()
22+
{
23+
assert.throws(function()
24+
{
25+
accessSync(filepath)
26+
})
27+
})
28+
})
29+
})
30+
31+
32+
describe('withDir', function()
33+
{
34+
it("dir don't exists after going out of scope", function()
35+
{
36+
var filepath
37+
38+
return tmp.withDir(function(o)
39+
{
40+
filepath = o.path
41+
42+
accessSync(filepath)
43+
assert.strictEqual(extname(filepath), '.dir')
44+
}, {postfix: '.dir'})
45+
.then(function()
46+
{
47+
assert.throws(function()
48+
{
49+
accessSync(filepath)
50+
})
51+
})
52+
})
53+
})

0 commit comments

Comments
 (0)