diff --git a/lib/target-manager.js b/lib/target-manager.js index 0ab6e5f6..b1fa4ce9 100644 --- a/lib/target-manager.js +++ b/lib/target-manager.js @@ -122,6 +122,8 @@ class TargetManager extends EventEmitter { pathTarget.targets = settings; pathTarget.loading = false; + + return pathTarget; }).catch(err => { atom.notifications.addError('Ooops. Something went wrong.', { detail: err.message, @@ -131,12 +133,12 @@ class TargetManager extends EventEmitter { }); }); - return Promise.all(pathPromises).then(entries => { - this.fillTargets(require('./utils').activePath()); + return Promise.all(pathPromises).then(pathTargets => { + this.fillTargets(require('./utils').activePath(), false); this.emit('refresh-complete'); this.busyRegistry && this.busyRegistry.end('build.refresh-targets'); - if (entries.length === 0) { + if (pathTargets.length === 0) { return; } @@ -161,7 +163,7 @@ class TargetManager extends EventEmitter { }); } - fillTargets(path) { + fillTargets(path, refreshOnEmpty = true) { if (!this.targetsView) { return; } @@ -169,7 +171,7 @@ class TargetManager extends EventEmitter { const activeTarget = this.getActiveTarget(path); activeTarget && this.targetsView.setActiveTarget(activeTarget.name); - this.getTargets(path) + this.getTargets(path, refreshOnEmpty) .then(targets => targets.map(t => t.name)) .then(targetNames => this.targetsView && this.targetsView.setItems(targetNames)); } @@ -206,13 +208,13 @@ class TargetManager extends EventEmitter { }); } - getTargets(path) { + getTargets(path, refreshOnEmpty = true) { const pathTarget = this.pathTargets.find(pt => pt.path === path); if (!pathTarget) { return Promise.resolve([]); } - if (pathTarget.targets.length === 0) { + if (refreshOnEmpty && pathTarget.targets.length === 0) { return this.refreshTargets([ pathTarget.path ]).then(() => pathTarget.targets); } return Promise.resolve(pathTarget.targets); diff --git a/spec/build-keymap-spec.js b/spec/build-keymap-spec.js index 54597a28..abe612a7 100644 --- a/spec/build-keymap-spec.js +++ b/spec/build-keymap-spec.js @@ -3,6 +3,7 @@ import fs from 'fs-extra'; import path from 'path'; import temp from 'temp'; +import specHelpers from 'atom-build-spec-helpers'; import os from 'os'; describe('Keymap', () => { @@ -63,9 +64,88 @@ describe('Keymap', () => { }); runs(() => { - const bindings = atom.keymaps.findKeyBindings({ command: 'build:trigger:Custom: keymapped build' }); - expect(bindings.length).toEqual(1); - expect(bindings[0].keystrokes).toEqual('ctrl-alt-k'); + expect(workspaceElement.querySelector('.terminal').terminal.getContent()).toMatch(/default/); + atom.commands.dispatch(workspaceElement, 'build:toggle-panel'); + }); + + waitsFor(() => { + return !workspaceElement.querySelector('.build .title'); + }); + + runs(() => { + const key = atom.keymaps.constructor.buildKeydownEvent('k', { ctrl: true, alt: true, target: workspaceElement }); + atom.keymaps.handleKeyboardEvent(key); + }); + + waitsFor(() => { + return workspaceElement.querySelector('.build .title') && + workspaceElement.querySelector('.build .title').classList.contains('success'); + }); + + runs(() => { + expect(workspaceElement.querySelector('.terminal').terminal.getContent()).toMatch(/keymapped/); + }); + }); + + it('should not changed the set active build', () => { + fs.writeFileSync(directory + '.atom-build.json', JSON.stringify({ + name: 'The default build', + cmd: 'echo default', + targets: { + 'keymapped build': { + cmd: 'echo keymapped', + keymap: 'ctrl-alt-k' + } + } + })); + + runs(() => atom.commands.dispatch(workspaceElement, 'build:trigger')); + + waitsFor(() => { + return workspaceElement.querySelector('.build .title') && + workspaceElement.querySelector('.build .title').classList.contains('success'); + }); + + runs(() => { + expect(workspaceElement.querySelector('.terminal').terminal.getContent()).toMatch(/default/); + atom.commands.dispatch(workspaceElement, 'build:toggle-panel'); + }); + + waitsFor(() => { + return !workspaceElement.querySelector('.build .title'); + }); + + runs(() => { + const key = atom.keymaps.constructor.buildKeydownEvent('k', { ctrl: true, alt: true, target: workspaceElement }); + atom.keymaps.handleKeyboardEvent(key); + }); + + waitsFor(() => { + return workspaceElement.querySelector('.build .title') && + workspaceElement.querySelector('.build .title').classList.contains('success'); + }); + + runs(() => { + expect(workspaceElement.querySelector('.terminal').terminal.getContent()).toMatch(/keymapped/); + atom.commands.dispatch(workspaceElement, 'build:toggle-panel'); + }); + + waitsFor(() => { + return !workspaceElement.querySelector('.build .title'); + }); + + runs(() => { + atom.commands.dispatch(workspaceElement, 'build:trigger'); + }); + + waitsFor(() => { + return workspaceElement.querySelector('.build .title') && + workspaceElement.querySelector('.build .title').classList.contains('success'); + }); + + runs(() => { + expect(workspaceElement.querySelector('.terminal').terminal.getContent()).toMatch(/default/); + atom.commands.dispatch(workspaceElement, 'build:toggle-panel'); }); }); @@ -82,18 +162,32 @@ describe('Keymap', () => { })); runs(() => atom.commands.dispatch(workspaceElement, 'build:trigger')); + waitsFor(() => { return workspaceElement.querySelector('.build .title') && workspaceElement.querySelector('.build .title').classList.contains('success'); }); runs(() => { - const bindings = atom.keymaps.findKeyBindings({ command: 'build:trigger:Custom: keymapped build' }); - expect(bindings.length).toEqual(1); - expect(bindings[0].keystrokes).toEqual('ctrl-alt-k'); + expect(workspaceElement.querySelector('.terminal').terminal.getContent()).toMatch(/default/); + }); + + waitsFor(() => { + return !workspaceElement.querySelector('.build .title'); + }); + + runs(() => { + const key = atom.keymaps.constructor.buildKeydownEvent('k', { ctrl: true, alt: true, target: workspaceElement }); + atom.keymaps.handleKeyboardEvent(key); + }); + + waitsFor(() => { + return workspaceElement.querySelector('.build .title') && + workspaceElement.querySelector('.build .title').classList.contains('success'); }); runs(() => { + expect(workspaceElement.querySelector('.terminal').terminal.getContent()).toMatch(/keymapped/); atom.commands.dispatch(workspaceElement, 'build:toggle-panel'); fs.writeFileSync(directory + '.atom-build.json', JSON.stringify({ name: 'The default build', @@ -107,16 +201,32 @@ describe('Keymap', () => { })); }); - runs(() => atom.commands.dispatch(workspaceElement, 'build:trigger')); + waitsForPromise(() => specHelpers.awaitTargets()); + + waitsFor(() => { + return !workspaceElement.querySelector('.build .title'); + }); + + runs(() => { + const key = atom.keymaps.constructor.buildKeydownEvent('k', { ctrl: true, alt: true, target: workspaceElement }); + atom.keymaps.handleKeyboardEvent(key); + }); + + waits(300); + + runs(() => { + expect(workspaceElement.querySelector('.build')).not.toExist(); + const key = atom.keymaps.constructor.buildKeydownEvent('x', { ctrl: true, target: workspaceElement }); + atom.keymaps.handleKeyboardEvent(key); + }); + waitsFor(() => { return workspaceElement.querySelector('.build .title') && - workspaceElement.querySelector('.build .title').classList.contains('success'); + workspaceElement.querySelector('.build .title').classList.contains('success'); }); runs(() => { - const bindings = atom.keymaps.findKeyBindings({ command: 'build:trigger:Custom: keymapped build' }); - expect(bindings.length).toEqual(1); - expect(bindings[0].keystrokes).not.toEqual('ctrl-alt-x'); + expect(workspaceElement.querySelector('.terminal').terminal.getContent()).toMatch(/ctrl-x new file/); }); }); }); diff --git a/spec/build-targets-spec.js b/spec/build-targets-spec.js index 99053107..b4b28fba 100644 --- a/spec/build-targets-spec.js +++ b/spec/build-targets-spec.js @@ -48,6 +48,23 @@ describe('Target', () => { try { fs.removeSync(directory); } catch (e) { console.warn('Failed to clean up: ', e); } }); + describe('when no targets exists', () => { + it('should show a notification', () => { + runs(() => { + atom.commands.dispatch(workspaceElement, 'build:select-active-target'); + }); + + waitsFor(() => { + return workspaceElement.querySelector('.select-list.build-target'); + }); + + runs(() => { + const targets = [ ...workspaceElement.querySelectorAll('.select-list li.build-target') ].map(el => el.textContent); + expect(targets).toEqual([]); + }); + }); + }); + describe('when multiple targets exists', () => { it('should list those targets in a SelectListView (from .atom-build.json)', () => { waitsForPromise(() => {