From 86f6ad4a11a5507154ddfe124425f3b7f45e58b1 Mon Sep 17 00:00:00 2001 From: Carsten Klein Date: Tue, 11 Jul 2017 17:43:01 +0200 Subject: [PATCH] fix #143 prepend opts.dir || tmpDir if no path is given in the template --- README.md | 8 ++++++-- lib/tmp.js | 6 +++++- test/dir-sync-test.js | 2 +- test/dir-test.js | 2 +- test/file-sync-test.js | 2 +- test/file-test.js | 2 +- test/name-test.js | 2 +- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4e8ce1c..7695bf7 100644 --- a/README.md +++ b/README.md @@ -237,10 +237,14 @@ console.log('Dir: ', tmpobj.name); Creates a new temporary directory with mode `0700` and filename like `/tmp/tmp-nk2J1u`. +IMPORTANT NOTE: template no longer accepts a path. Use the dir option instead if you +require tmp to create your temporary filesystem object in a different place than the +default `tmp.tmpdir`. + ```javascript var tmp = require('tmp'); -tmp.dir({ template: '/tmp/tmp-XXXXXX' }, function _tempDirCreated(err, path) { +tmp.dir({ template: 'tmp-XXXXXX' }, function _tempDirCreated(err, path) { if (err) throw err; console.log('Dir: ', path); @@ -254,7 +258,7 @@ This will behave similarly to the asynchronous version. ```javascript var tmp = require('tmp'); -var tmpobj = tmp.dirSync({ template: '/tmp/tmp-XXXXXX' }); +var tmpobj = tmp.dirSync({ template: 'tmp-XXXXXX' }); console.log('Dir: ', tmpobj.name); ``` diff --git a/lib/tmp.js b/lib/tmp.js index 41b83db..223bba2 100644 --- a/lib/tmp.js +++ b/lib/tmp.js @@ -121,7 +121,11 @@ function _generateTmpName(opts) { // mkstemps like template if (opts.template) { - return opts.template.replace(TEMPLATE_PATTERN, _randomChars(6)); + var template = opts.template; + // make sure that we prepend the tmp path if none was given + if (path.basename(template) == template) + template = path.join(opts.dir || tmpDir, template); + return template.replace(TEMPLATE_PATTERN, _randomChars(6)); } // prefix and postfix diff --git a/test/dir-sync-test.js b/test/dir-sync-test.js index 24d5f40..d525e07 100644 --- a/test/dir-sync-test.js +++ b/test/dir-sync-test.js @@ -56,7 +56,7 @@ vows.describe('Synchronous directory creation').addBatch({ 'when using template': { topic: function () { - return tmp.dirSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }); + return tmp.dirSync({ template: 'clike-XXXXXX-postfix' }); }, 'should return with a name': Test.assertNameSync, diff --git a/test/dir-test.js b/test/dir-test.js index d128fe9..e330cee 100644 --- a/test/dir-test.js +++ b/test/dir-test.js @@ -57,7 +57,7 @@ vows.describe('Directory creation').addBatch({ 'when using template': { topic: function () { - tmp.dir({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback); + tmp.dir({ template: 'clike-XXXXXX-postfix' }, this.callback); }, 'should not return with error': assert.isNull, diff --git a/test/file-sync-test.js b/test/file-sync-test.js index bd3414a..0113428 100644 --- a/test/file-sync-test.js +++ b/test/file-sync-test.js @@ -97,7 +97,7 @@ vows.describe('Synchronous file creation').addBatch({ 'when using template': { topic: function () { - return tmp.fileSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }); + return tmp.fileSync({ template: 'clike-XXXXXX-postfix' }); }, 'should return with a name': Test.assertNameSync, diff --git a/test/file-test.js b/test/file-test.js index a7ee1ab..51214a8 100644 --- a/test/file-test.js +++ b/test/file-test.js @@ -100,7 +100,7 @@ vows.describe('File creation').addBatch({ 'when using template': { topic: function () { - tmp.file({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback); + tmp.file({ template: 'clike-XXXXXX-postfix' }, this.callback); }, 'should not return with an error': assert.isNull, diff --git a/test/name-test.js b/test/name-test.js index e7595b7..ff9489c 100644 --- a/test/name-test.js +++ b/test/name-test.js @@ -40,7 +40,7 @@ vows.describe('Name creation').addBatch({ 'when using template': { topic: function () { - tmp.tmpName({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback); + tmp.tmpName({ template: 'clike-XXXXXX-postfix' }, this.callback); }, 'should not return with error': assert.isNull,