From ea0d76f9697daf98db91cd3fef3369cd9aa498b3 Mon Sep 17 00:00:00 2001 From: "xuanji.jw" Date: Sat, 2 Jul 2016 00:18:56 +0800 Subject: [PATCH] refactor to node-isimulator@1.0.x, update usage and test cases --- .eslintrc | 1 + .travis.yml | 22 ++++-- HISTORY.md | 5 ++ package.json | 38 +++++----- src/index.js | 52 +++---------- src/mergeCfg.js | 9 +-- test/fixtures/mobile.config.js | 2 +- test/index-test.js | 133 ++++++++++++++++++--------------- test/mergeCfg-test.js | 28 ++++--- 9 files changed, 141 insertions(+), 149 deletions(-) diff --git a/.eslintrc b/.eslintrc index 2a344ce..c9b7c05 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,5 @@ { + "parser": "babel-eslint", "extends": "eslint-config-airbnb/base", "rules": { "no-console": [0] diff --git a/.travis.yml b/.travis.yml index a4198d6..ac38569 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,16 @@ -language: node_js - -node_js: - - "4" - - "5" - +language: + - objective-c +osx_image: xcode7.3 +env: + - DEVICE=9.2 +install: + - rm -rf ~/.nvm + - git clone https://github.com/creationix/nvm.git ~/.nvm + - source ~/.nvm/nvm.sh + - nvm install v6.2.2 + - node --version + - npm install +script: + - npm run test after_success: - - npm run coveralls + - npm run coveralls \ No newline at end of file diff --git a/HISTORY.md b/HISTORY.md index 8f4892d..3190b89 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,11 @@ --- +## 0.2.0 + +upgrade: node-isimulator to 1.0.x +brand new style + ## 0.1.4 fix: npmjs bad layout diff --git a/package.json b/package.json index c212136..d0d6be3 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "main": "./lib", "scripts": { "build": "rm -rf lib && babel src --out-dir lib", - "test": "babel-node $(npm bin)/babel-istanbul cover $(npm bin)/_mocha --no-timeouts", + "test": "babel-node $(npm bin)/babel-istanbul cover $(npm bin)/_mocha -- --no-timeouts", "lint": "eslint --ext .js src test/*-test.js", "coveralls": "cat ./coverage/lcov.info | coveralls" }, @@ -20,25 +20,26 @@ "lint" ], "dependencies": { - "co": "^4.6.0", - "node-isimulator": "~0.1.7" + "lodash.isequal": "^4.2.0", + "node-isimulator": "^1.0.2" }, "devDependencies": { - "babel-cli": "~6.2.0", - "babel-core": "~6.1.21", - "babel-istanbul": "^0.5.9", - "babel-plugin-add-module-exports": "~0.1.1", - "babel-preset-es2015": "~6.1.18", - "babel-preset-stage-0": "~6.1.18", - "coveralls": "^2.11.6", + "babel-cli": "^6.10.1", + "babel-core": "^6.10.4", + "babel-eslint": "^6.1.0", + "babel-istanbul": "^0.11.0", + "babel-plugin-add-module-exports": "^0.1.2", + "babel-plugin-transform-runtime": "^6.9.0", + "babel-preset-es2015": "^6.9.0", + "babel-preset-stage-0": "^6.5.0", + "babel-runtime": "^6.9.2", + "coveralls": "^2.11.9", "dora": "*", "dora-plugin-config-manager": "~0.2.2", - "eslint": "~1.10.3", - "eslint-config-airbnb": "~5.0.0", - "expect": "^1.14.0", - "mocha": "~2.4.5", - "pre-commit": "~1.1.2", - "sinon": "^1.17.3" + "eslint": "^2.7.0", + "eslint-config-airbnb": "^6.2.0", + "expect.js": "^0.3.1", + "mocha": "^2.5.3" }, "babel": { "presets": [ @@ -46,7 +47,8 @@ "stage-0" ], "plugins": [ - "add-module-exports" + "add-module-exports", + "transform-runtime" ] }, "files": [ @@ -54,4 +56,4 @@ "package.json", "README.md" ] -} +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 636ea3a..e669fa7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,62 +1,30 @@ -import { type } from 'os'; -import Simulator from 'node-isimulator'; -import co from 'co'; -import EventEmitter from 'events'; -const eventEmitter = new EventEmitter(); - +import start from 'node-isimulator'; +import isEqual from 'lodash.isequal'; import mergeCfg from './mergeCfg'; let lastOpts = {}; let simOpts = {}; -let sim = {}; - -eventEmitter.on('initializationSimOpts', () => { - if (type() === 'Darwin') { - sim = new Simulator(lastOpts); - co(sim.start(lastOpts.scheme)); - } -}); - -eventEmitter.on('changeSimOptsNeedReopen', () => { - if (type() === 'Darwin') { - co(sim.killAll()); - } - eventEmitter.emit('initializationSimOpts'); -}); - -eventEmitter.on('changeSimOptsDoNotNeedReopen', () => { - if (type() === 'Darwin') { - sim.scheme = lastOpts.scheme; - co(sim.start(lastOpts.scheme)); - } -}); export default { - 'server.before'() { - const { query, get, set } = this; - set('simulatorEmitter', eventEmitter); + async 'server.before'() { + const { query, get } = this; + simOpts = get('_global_simOpts') || {}; lastOpts = mergeCfg(query, simOpts); - eventEmitter.emit('initializationSimOpts'); - const configManagerEmitter = get('configManagerEmitter'); if (!configManagerEmitter) { return; } - configManagerEmitter.on('_global_simOpts', () => { + configManagerEmitter.on('_global_simOpts', async () => { simOpts = get('_global_simOpts'); const changeFinalOpts = mergeCfg(query, simOpts); - if (changeFinalOpts.application !== lastOpts.application - || changeFinalOpts.device !== lastOpts.device - || changeFinalOpts.os !== lastOpts.os) { - lastOpts = changeFinalOpts; - eventEmitter.emit('changeSimOptsNeedReopen'); - } else if (changeFinalOpts.scheme !== lastOpts.scheme) { - lastOpts = changeFinalOpts; - eventEmitter.emit('changeSimOptsDoNotNeedReopen'); + if (!isEqual(lastOpts, changeFinalOpts)) { + await start(changeFinalOpts); } }); + + await start(lastOpts); }, }; diff --git a/src/mergeCfg.js b/src/mergeCfg.js index 125d0ee..bd4f5f3 100644 --- a/src/mergeCfg.js +++ b/src/mergeCfg.js @@ -1,13 +1,12 @@ function mergeCfg(query, globalSimOpts) { const options = { - prefix: 'antm', application: 'mobilesafari', - device: 'iPhone-6', - os: '', - appPath: '', + bundleId: '', + device: 'iPhone 6', downloadURL: '', + prefix: 'antm', scheme: 'http://127.0.0.1', - verbose: false, + sdk: '', }; const finalOpts = { ...options, ...query, ...globalSimOpts }; diff --git a/test/fixtures/mobile.config.js b/test/fixtures/mobile.config.js index bf5f688..ca054a6 100644 --- a/test/fixtures/mobile.config.js +++ b/test/fixtures/mobile.config.js @@ -1 +1 @@ -var simOpts = { scheme: 'http://m.baidu.com' }; module.exports.simOpts = simOpts; \ No newline at end of file +var simOpts = { prefix:'doraPlugin', scheme: 'http://m.baidu.com' }; module.exports.simOpts = simOpts; \ No newline at end of file diff --git a/test/index-test.js b/test/index-test.js index ab72e4a..ae51085 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -1,77 +1,88 @@ import { join } from 'path'; import { writeFileSync } from 'fs'; - -import sinon from 'sinon'; -import expect from 'expect'; +import expect from 'expect.js'; import dora from 'dora'; +import { simUtil } from 'node-isimulator'; const oldCwd = process.cwd(); const fixtures = join(oldCwd, 'test/fixtures'); -let simulatorEmitter = {}; -const spyInitial = sinon.spy(); -const spyReOpen = sinon.spy(); -const spyJustOpen = sinon.spy(); -describe('index', function sim() { - this.timeout(25000); - before(done => { - process.chdir(fixtures); - dora({ - port: 12346, - plugins: [ - 'config-manager?path=./mobile.config.js|simOpts', - '../../src/index?{scheme:"http://127.0.0.1:12345"}', - { - 'server.after': function after() { - const { get } = this; - simulatorEmitter = get('simulatorEmitter'); - simulatorEmitter.on('initializationSimOpts', spyInitial); - simulatorEmitter.on('changeSimOptsNeedReopen', spyReOpen); - simulatorEmitter.on('changeSimOptsDoNotNeedReopen', spyJustOpen); - }, - }, - ], - cwd: fixtures, - verbose: true, - }); - setTimeout(done, 15000); +describe('index', () => { + const simOpts = { + sdk: '', + udid: '', + }; + before(async done => { + try { + simOpts.sdk = await simUtil.getLatestSDK(); + process.chdir(fixtures); + dora({ + port: 12346, + plugins: [ + 'config-manager?path=./mobile.config.js|simOpts', + '../../src/index', + ], + cwd: fixtures, + verbose: true, + }, done); + } catch (e) { + done(e); + } }); - after(done => { - writeFileSync( - join(fixtures, './mobile.config.js'), - `var simOpts = { scheme: 'http://m.baidu.com' }; module.exports.simOpts = simOpts;`, - 'utf-8' - ); - process.chdir(oldCwd); - done(); + afterEach(async done => { + try { + await simUtil.killAllSimulators(); + await simUtil.deleteDevice(simOpts.udid); + simOpts.udid = ''; + if (simOpts.rewrite) { + writeFileSync( + join(fixtures, './mobile.config.js'), + "var simOpts = { prefix:'doraPlugin', scheme: 'http://m.baidu.com' }; " + + 'module.exports.simOpts = simOpts;', + 'utf-8' + ); + simOpts.rewrite = false; + } + done(); + } catch (e) { + done(e); + } }); - it('changeSimOptsDoNotNeedReopen should called', done => { - writeFileSync( - join(fixtures, './mobile.config.js'), - `var simOpts = { scheme: 'http://m.taobao.com' }; module.exports.simOpts = simOpts;`, - 'utf-8' - ); - - setTimeout(() => { - expect(spyReOpen.called).toEqual(false); - expect(spyJustOpen.called).toEqual(true); + it('should create a simulator named doraPluginSim--iPhone-6--${latestsdk}', async done => { + try { + const normalizeSDK = simOpts.sdk.replace(/\./, '-'); + const simName = `doraPluginsim--iPhone-6--${normalizeSDK}`; + const sim = await simUtil.getUdidBySimName(simName); + expect(sim).to.not.be.empty(); + simOpts.udid = sim[0]; done(); - }, 10000); + } catch (e) { + done(e); + } }); - it('changeSimOptsNeedReopen should called', done => { - writeFileSync( - join(fixtures, './mobile.config.js'), - `var simOpts = { device: 'iPhone-5s', scheme: 'https://www.npmjs.com/package/dora-plugin-simulator' }; module.exports.simOpts = simOpts;`, - 'utf-8' - ); - - setTimeout(() => { - expect(spyReOpen.called).toEqual(true); - expect(spyInitial.called).toEqual(true); - done(); - }, 15000); + it('change mobile.config.js should create doraPluginSim--iPhone-5s--${latestsdk}', done => { + try { + writeFileSync( + join(fixtures, './mobile.config.js'), + "var simOpts = { prefix:'doraPlugin', device: 'iPhone 5s' }; " + + 'module.exports.simOpts = simOpts;', + 'utf-8' + ); + setTimeout(async () => { + const normalizeSDK = simOpts.sdk.replace(/\./, '-'); + const simName = `doraPluginsim--iPhone-5s--${normalizeSDK}`; + const sim = await simUtil.getUdidBySimName(simName); + console.log(sim) + expect(sim).to.not.be.empty(); + simOpts.udid = sim[0]; + simOpts.rewrite = true; + done(); + }, 2000); + } catch (e) { + done(e); + } }); }); diff --git a/test/mergeCfg-test.js b/test/mergeCfg-test.js index 5fc40ba..c399916 100644 --- a/test/mergeCfg-test.js +++ b/test/mergeCfg-test.js @@ -1,21 +1,19 @@ import mergeCfg from '../src/mergeCfg'; -import expect from 'expect'; +import expect from 'expect.js'; describe('mergeCfg', () => { it('merge with query', done => { const query = { scheme: 'http://m.alipay.com', - verbose: true, }; - expect(mergeCfg(query)).toEqual({ - prefix: 'antm', + expect(mergeCfg(query)).to.eql({ application: 'mobilesafari', - device: 'iPhone-6', - os: '', - appPath: '', + bundleId: '', + device: 'iPhone 6', downloadURL: '', + prefix: 'antm', scheme: 'http://m.alipay.com', - verbose: true, + sdk: '', }); done(); }); @@ -23,20 +21,20 @@ describe('mergeCfg', () => { it('merge with query and global simOpts', done => { const query = { scheme: 'http://m.alipay.com', - verbose: true, }; const simOpts = { - device: 'iPhone-5s', + device: 'iPhone 5s', scheme: 'http://oo.xx.com', + verbose: true, }; - expect(mergeCfg(query, simOpts)).toEqual({ - prefix: 'antm', + expect(mergeCfg(query, simOpts)).to.eql({ application: 'mobilesafari', - device: 'iPhone-5s', - os: '', - appPath: '', + bundleId: '', + device: 'iPhone 5s', downloadURL: '', + prefix: 'antm', scheme: 'http://oo.xx.com', + sdk: '', verbose: true, }); done();