From 7e3e7818d980986b4e5b656ad258351ae2d22cee Mon Sep 17 00:00:00 2001 From: sebv Date: Sat, 27 Dec 2014 21:10:05 +0800 Subject: [PATCH] extracted tempdir to appium-support --- lib/helpers.js | 6 +-- lib/tempdir.js | 60 ------------------------ package.json | 1 + test/functional/android/app-path-base.js | 4 +- 4 files changed, 6 insertions(+), 65 deletions(-) delete mode 100644 lib/tempdir.js diff --git a/lib/helpers.js b/lib/helpers.js index 703472866..27225dd65 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -7,14 +7,14 @@ var logger = require('./server/logger.js').get('appium') , path = require('path') , exec = require('child_process').exec , osType = require('os').type() - , tempdir = require('./tempdir') + , tempDir = require('appium-support').tempDir , AdmZip = require('adm-zip'); exports.downloadFile = function (fileUrl, suffix, cb) { // We will be downloading the files to a directory, so make sure it's there // This step is not required if you have manually created the directory - tempdir.open({prefix: 'appium-app', suffix: suffix}, function (err, info) { + tempDir.open({prefix: 'appium-app', suffix: suffix}).nodeify(function (err, info) { fs.close(info.fd); var file = fs.createWriteStream(info.path); request(fileUrl).pipe(file).on('close', function () { @@ -28,7 +28,7 @@ exports.copyLocalZip = function (localZipPath, cb) { logger.debug("Copying local zip to tmp dir"); fs.stat(localZipPath, function (err) { if (err) return cb(err); - tempdir.open({prefix: 'appium-app', suffix: '.zip'}, function (err, info) { + tempDir.open({prefix: 'appium-app', suffix: '.zip'}).nodeify(function (err, info) { var infile = fs.createReadStream(localZipPath); var outfile = fs.createWriteStream(info.path); infile.pipe(outfile).on('close', function () { diff --git a/lib/tempdir.js b/lib/tempdir.js deleted file mode 100644 index 80a3e3abe..000000000 --- a/lib/tempdir.js +++ /dev/null @@ -1,60 +0,0 @@ -/* This library is originated from temp.js at http://github.com/bruce/node-temp */ -"use strict"; - -var fs = require('fs') - , os = require('os') - , path = require('path') - , cnst = require('constants'); - -var RDWR_EXCL = cnst.O_CREAT | cnst.O_TRUNC | cnst.O_RDWR | cnst.O_EXCL; - -var tempDir = function () { - var now = new Date(); - var filePath = path.join(os.tmpDir(), - [now.getYear(), now.getMonth(), now.getDate(), - '-', - process.pid, - '-', - (Math.random() * 0x100000000 + 1).toString(36)].join('')); - if (!fs.existsSync(filePath)) { - fs.mkdirSync(filePath); - } - return filePath; -}; - -var generateName = function (rawAffixes, defaultPrefix) { - var affixes = parseAffixes(rawAffixes, defaultPrefix); - var name = [affixes.prefix, affixes.suffix].join(''); - return path.join(tempDir(), name); -}; - - -var open = function (affixes, callback) { - var filePath = generateName(affixes, 'f-'); - fs.open(filePath, RDWR_EXCL, 384, function (err, fd) { - if (callback) - callback(err, {path: filePath, fd: fd}); - }); -}; - -var parseAffixes = function (rawAffixes, defaultPrefix) { - var affixes = {prefix: null, suffix: null}; - if (rawAffixes) { - switch (typeof(rawAffixes)) { - case 'string': - affixes.prefix = rawAffixes; - break; - case 'object': - affixes = rawAffixes; - break; - default: - throw ("Unknown affix declaration: " + affixes); - } - } else { - affixes.prefix = defaultPrefix; - } - return affixes; -}; - -exports.open = open; - diff --git a/package.json b/package.json index 11ecc012d..1feaa8fc0 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "appium-adb": "=1.6.0", "appium-atoms": "=0.0.5", "appium-instruments": "=1.5.3", + "appium-support": "=0.0.2", "appium-uiauto": "=1.10.2", "argparse": "~0.1.15", "async": "~0.9.0", diff --git a/test/functional/android/app-path-base.js b/test/functional/android/app-path-base.js index b8ab19850..1cfb0ceeb 100644 --- a/test/functional/android/app-path-base.js +++ b/test/functional/android/app-path-base.js @@ -4,7 +4,7 @@ var initSession = require('../../helpers/session').initSession , getTitle = require('../../helpers/title').getTitle , _ = require('underscore') , getAppPath = require('../../helpers/app').getAppPath - , tempdir = require('../../../lib/tempdir') + , tempDir = require('appium-support').tempDir , ncp = require('ncp'); require('../../helpers/setup-chai.js'); @@ -15,7 +15,7 @@ module.exports.spacesTest = function (desired) { var oldAppPath = getAppPath('ApiDemos'); var newAppPath = '/tmp/App With Spaces.apk'; before(function (done) { - tempdir.open({prefix: 'app with spaces', suffix: '.apk'}, function (err, info) { + tempDir.open({prefix: 'app with spaces', suffix: '.apk'}).nodeify(function (err, info) { if (err) return done(err); console.log("Copying '" + oldAppPath + "' to '" + info.path + "'"); ncp(oldAppPath, info.path, function (err) {