From 55e9f8b0b29191c067051e58aa4317900f4ec52a Mon Sep 17 00:00:00 2001 From: Estelle DeBlois Date: Sun, 15 Mar 2015 13:56:25 -0400 Subject: [PATCH] tests --- .jshintrc | 5 +- .travis.yml | 22 +++ package.json | 9 +- tests/.jshintrc | 45 +++++ .../nw/nwjs/nwjs.app/Contents/MacOS/nwjs | 0 .../node_modules/nw/nwjs/Contents/MacOS/nwjs | 0 tests/fixtures/project-empty/.gitkeep | 0 .../project-linux/node_modules/nw/nwjs/nw | 0 .../project-win32/node_modules/nw/nwjs/nw.exe | 0 tests/helpers/expect.js | 6 + tests/mocha-jshint-test.js | 3 + tests/runner.js | 22 +++ tests/unit/commands/nw-test.js | 167 +++++++++++++++++ tests/unit/helpers/find-nw-test.js | 168 ++++++++++++++++++ 14 files changed, 443 insertions(+), 4 deletions(-) create mode 100644 .travis.yml create mode 100644 tests/.jshintrc create mode 100644 tests/fixtures/project-darwin-1/node_modules/nw/nwjs/nwjs.app/Contents/MacOS/nwjs create mode 100644 tests/fixtures/project-darwin-2/node_modules/nw/nwjs/Contents/MacOS/nwjs create mode 100644 tests/fixtures/project-empty/.gitkeep create mode 100644 tests/fixtures/project-linux/node_modules/nw/nwjs/nw create mode 100644 tests/fixtures/project-win32/node_modules/nw/nwjs/nw.exe create mode 100644 tests/helpers/expect.js create mode 100644 tests/mocha-jshint-test.js create mode 100644 tests/runner.js create mode 100644 tests/unit/commands/nw-test.js create mode 100644 tests/unit/helpers/find-nw-test.js diff --git a/.jshintrc b/.jshintrc index 08096ef..7db25d2 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,10 +1,8 @@ { "predef": [ - "document", - "window", "-Promise" ], - "browser": true, + "browser": false, "boss": true, "curly": true, "debug": false, @@ -16,6 +14,7 @@ "laxbreak": false, "newcap": true, "noarg": true, + "node": true, "noempty": false, "nonew": false, "nomen": false, diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8bb46d0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,22 @@ +language: node_js + +sudo: false + +node_js: + - "0.10" + - "0.12" + - "iojs" + +cache: + directories: + - node_modules + +before_install: + - npm config set spin false + - npm install -g npm@^2 + +install: + - npm install + +script: + - npm test diff --git a/package.json b/package.json index 27f25ce..2fd4938 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,14 @@ "rsvp": "^3.0.17" }, "devDependencies": { - "ember-cli": "0.2.0-beta.1" + "chai": "^2.1.1", + "chai-as-promised": "^4.3.0", + "ember-cli": "0.2.0-beta.1", + "glob": "^5.0.3", + "mocha": "^2.2.1", + "mocha-jshint": "^1.0.0", + "mock-spawn": "^0.2.4", + "mockery": "^1.4.0" }, "keywords": [ "ember-addon", diff --git a/tests/.jshintrc b/tests/.jshintrc new file mode 100644 index 0000000..0114f2e --- /dev/null +++ b/tests/.jshintrc @@ -0,0 +1,45 @@ +{ + "predef": [ + "console", + "it", + "describe", + "beforeEach", + "afterEach", + "before", + "after", + "-Promise" + ], + "expr": true, + "proto": true, + "strict": true, + "indent": 2, + "camelcase": true, + "node": true, + "browser": false, + "boss": true, + "curly": true, + "latedef": "nofunc", + "debug": false, + "devel": false, + "eqeqeq": true, + "evil": true, + "forin": false, + "immed": false, + "laxbreak": false, + "newcap": true, + "noarg": true, + "noempty": false, + "quotmark": true, + "nonew": false, + "nomen": false, + "onevar": false, + "plusplus": false, + "regexp": false, + "undef": true, + "unused": true, + "sub": true, + "trailing": true, + "white": false, + "eqnull": true, + "esnext": true +} diff --git a/tests/fixtures/project-darwin-1/node_modules/nw/nwjs/nwjs.app/Contents/MacOS/nwjs b/tests/fixtures/project-darwin-1/node_modules/nw/nwjs/nwjs.app/Contents/MacOS/nwjs new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/project-darwin-2/node_modules/nw/nwjs/Contents/MacOS/nwjs b/tests/fixtures/project-darwin-2/node_modules/nw/nwjs/Contents/MacOS/nwjs new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/project-empty/.gitkeep b/tests/fixtures/project-empty/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/project-linux/node_modules/nw/nwjs/nw b/tests/fixtures/project-linux/node_modules/nw/nwjs/nw new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/project-win32/node_modules/nw/nwjs/nw.exe b/tests/fixtures/project-win32/node_modules/nw/nwjs/nw.exe new file mode 100644 index 0000000..e69de29 diff --git a/tests/helpers/expect.js b/tests/helpers/expect.js new file mode 100644 index 0000000..46b3ea7 --- /dev/null +++ b/tests/helpers/expect.js @@ -0,0 +1,6 @@ +var chai = require('chai'); +var chaiAsPromised = require('chai-as-promised'); + +chai.use(chaiAsPromised); + +module.exports = chai.expect; diff --git a/tests/mocha-jshint-test.js b/tests/mocha-jshint-test.js new file mode 100644 index 0000000..0293707 --- /dev/null +++ b/tests/mocha-jshint-test.js @@ -0,0 +1,3 @@ +var mochaJSHint = require('mocha-jshint'); + +mochaJSHint(['lib']); diff --git a/tests/runner.js b/tests/runner.js new file mode 100644 index 0000000..97157c7 --- /dev/null +++ b/tests/runner.js @@ -0,0 +1,22 @@ +'use strict'; + +var glob = require('glob'); +var Mocha = require('mocha'); + +var mocha = new Mocha({ + reporter: 'spec' +}); + +var root = 'tests/'; + +function addFiles(mocha, files) { + glob.sync(root + files).forEach(mocha.addFile.bind(mocha)); +} + +addFiles(mocha, '/**/*-test.js'); + +mocha.run(function(failures) { + process.on('exit', function() { + process.exit(failures); + }); +}); diff --git a/tests/unit/commands/nw-test.js b/tests/unit/commands/nw-test.js new file mode 100644 index 0000000..a13c646 --- /dev/null +++ b/tests/unit/commands/nw-test.js @@ -0,0 +1,167 @@ +'use strict'; + +var path = require('path'); +var mockery = require('mockery'); +var mockSpawn = require('mock-spawn'); +var Command = require('ember-cli/lib/models/command'); +var Task = require('ember-cli/lib/models/task'); +var MockUI = require('ember-cli/tests/helpers/mock-ui'); +var MockAnalytics = require('ember-cli/tests/helpers/mock-analytics'); +var RSVP = require('rsvp'); +var expect = require('../../helpers/expect'); + +describe("ember nw command", function() { + var NWCommand, ui, analytics, project, spawn, _envNW; + + beforeEach(function() { + spawn = mockSpawn(); + mockery.enable({ useCleanCache: true }); + mockery.registerMock('child_process', { spawn: spawn }); + mockery.warnOnUnregistered(false); + + _envNW = process.env.NW_PATH; + delete process.env.NW_PATH; + + var nwObject = require('../../../lib/commands/nw'); + NWCommand = Command.extend(nwObject); + + ui = new MockUI(); + analytics = new MockAnalytics(); + + project = { + isEmberCLIProject: function() { + return true; + }, + root: path.join(__dirname, '..', '..', 'fixtures', 'project-empty') + }; + }); + + afterEach(function() { + process.env.NW_PATH = _envNW; + + mockery.deregisterAll(); + mockery.resetCache(); + mockery.disable(); + }); + + it("should build the project before running nw.js", function() { + var tasks = []; + + var command = new NWCommand({ + ui: ui, + analytics: analytics, + project: project, + settings: {}, + buildWatch: function() { + tasks.push('buildWatch'); + return RSVP.resolve(); + }, + runNW: function() { + tasks.push('runNW'); + return RSVP.resolve(); + } + }).validateAndRun(); + + return expect(command).to.be.fulfilled + .then(function() { + expect(tasks).to.deep.equal(['buildWatch', 'runNW']); + }); + }); + + it("should not run nw.js when the build fails", function() { + var tasks = []; + + var command = new NWCommand({ + ui: ui, + analytics: analytics, + project: project, + settings: {}, + buildWatch: function() { + tasks.push('buildWatch'); + return RSVP.reject(); + }, + runNW: function() { + tasks.push('runNW'); + return RSVP.resolve(); + } + }).validateAndRun(); + + return expect(command).to.be.rejected + .then(function() { + expect(tasks).to.deep.equal(['buildWatch']); + }); + }); + + it("should not keep watching if nw.js fails to run", function() { + var tasks = []; + + var command = new NWCommand({ + ui: ui, + analytics: analytics, + project: project, + settings: {}, + buildWatch: function() { + tasks.push('buildWatch'); + return RSVP.resolve(); + }, + runNW: function() { + tasks.push('runNW'); + return RSVP.reject(); + } + }).validateAndRun(); + + return expect(command).to.be.rejected + .then(function() { + expect(tasks).to.deep.equal(['buildWatch', 'runNW']); + }); + }); + + it("should spawn a 'nw' process with the right arguments", function() { + var command = new NWCommand({ + ui: ui, + analytics: analytics, + project: project, + settings: {}, + buildWatch: function() { + return RSVP.resolve(); + } + }).validateAndRun(); + + return expect(command).to.be.fulfilled + .then(function() { + expect(spawn.calls.length).to.equal(1); + expect(spawn.calls[0].command).to.equal('nw'); + expect(spawn.calls[0].args).to.deep.equal(['.']); + + expect(ui.output).to.contain("Starting nw.js..."); + expect(ui.output).to.contain("nw.js exited."); + }); + }); + + it("should print a friendly message when the 'nw' command cannot be found", function() { + var command = new NWCommand({ + ui: ui, + analytics: analytics, + project: project, + settings: {}, + buildWatch: function() { + return RSVP.resolve(); + } + }).validateAndRun(); + + spawn.sequence.add(function() { + this.emit('error', { code: 'ENOENT' }); + }); + + return expect(command).to.be.rejected + .then(function() { + expect(spawn.calls.length).to.equal(1); + expect(spawn.calls[0].command).to.equal('nw'); + expect(spawn.calls[0].args).to.deep.equal(['.']); + + expect(ui.output).to.contain("Starting nw.js..."); + expect(ui.output).to.contain("Error running the following command: nw"); + expect(ui.output).to.contain("re-run the blueprint"); + }); + }); +}); diff --git a/tests/unit/helpers/find-nw-test.js b/tests/unit/helpers/find-nw-test.js new file mode 100644 index 0000000..2614576 --- /dev/null +++ b/tests/unit/helpers/find-nw-test.js @@ -0,0 +1,168 @@ +'use strict'; + +var path = require('path'); +var mockery = require('mockery'); +var expect = require('../../helpers/expect'); + +describe("The command to start NW.js", function() { + var fixturePath; + + before(function() { + fixturePath = path.resolve(__dirname, '..', '..', 'fixtures'); + }); + + describe("when the `nw` npm package is installed", function() { + before(function() { + mockery.enable({ useCleanCache: true }); + mockery.warnOnUnregistered(false); + }); + + after(function() { + mockery.disable(); + }); + + describe("and the platform is Mac", function() { + var findNW; + + before(function() { + mockery.registerMock('os', { + platform: function() { + return 'darwin'; + } + }); + + findNW = require('../../../lib/helpers/find-nw'); + }); + + after(function() { + mockery.deregisterMock('os'); + mockery.resetCache(); + }); + + it("should be `node_modules/nw/nwjs/nwjs.app/Contents/MacOS/nwjs`", function() { + var project = { + root: path.join(fixturePath, 'project-darwin-1') + }; + + var nw = findNW(project); + var expectedNW = ['node_modules', 'nw', 'nwjs', 'nwjs.app', 'Contents', 'MacOS', 'nwjs'].join(path.sep); + + expect(nw).to.contain(expectedNW); + }); + + it("should be `node_modules/nw/nwjs/Contents/MacOS/nwjs`", function() { + var project = { + root: path.join(fixturePath, 'project-darwin-2') + }; + + var nw = findNW(project); + var expectedNW = ['node_modules', 'nw', 'nwjs', 'Contents', 'MacOS', 'nwjs'].join(path.sep); + + expect(nw).to.contain(expectedNW); + }); + }); + + describe("and the platform is Windows", function() { + var findNW; + + before(function() { + mockery.registerMock('os', { + platform: function() { + return 'win32'; + } + }); + + findNW = require('../../../lib/helpers/find-nw'); + }); + + after(function() { + mockery.deregisterMock('os'); + mockery.resetCache(); + }); + + it("should be `node_modules/nw/nwjs/nw.exe`", function() { + var project = { + root: path.join(fixturePath, 'project-win32') + }; + + var nw = findNW(project); + var expectedNW = ['node_modules', 'nw', 'nwjs', 'nw.exe'].join(path.sep); + + expect(nw).to.contain(expectedNW); + }); + }); + + describe("and the platform is not Mac or Windows", function() { + var findNW; + + before(function() { + mockery.registerMock('os', { + platform: function() { + return 'linux'; + } + }); + + findNW = require('../../../lib/helpers/find-nw'); + }); + + after(function() { + mockery.deregisterMock('os'); + mockery.resetCache(); + }); + + it("should be `node_modules/nw/nwjs/nw`", function() { + var project = { + root: path.join(fixturePath, 'project-linux') + }; + + var nw = findNW(project); + var expectedNW = ['node_modules', 'nw', 'nwjs', 'nw'].join(path.sep); + + expect(nw).to.contain(expectedNW); + }); + }); + }); + + describe("when the `nw` npm package is not installed", function() { + var findNW, _envNW; + + before(function() { + findNW = require('../../../lib/helpers/find-nw'); + }); + + beforeEach(function() { + _envNW = process.env.NW_PATH; + }); + + afterEach(function() { + process.env.NW_PATH = _envNW; + }); + + describe("and the `NW_PATH` environment variable is set", function() { + it("should be the value of `NW_PATH`", function() { + var project = { + root: path.join(fixturePath, 'project-empty') + }; + + var envNW = '/custom/path/to/nw'; + process.env.NW_PATH = envNW; + + var nw = findNW(project); + expect(nw).to.equal(envNW); + }); + }); + + describe("and the `NW_PATH` environment variable is not set", function() { + it("should be `nw`", function() { + var project = { + root: path.join(fixturePath, 'project-empty') + }; + + delete process.env.NW_PATH; + + var nw = findNW(project); + expect(nw).to.equal('nw'); + }); + }); + }); +});