Skip to content

Commit

Permalink
Merge pull request #520 from noseglid/no-recursive-loop
Browse files Browse the repository at this point in the history
No recursive loop
  • Loading branch information
noseglid authored May 22, 2017
2 parents 0487f3f + 3da8e76 commit 299d276
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 18 deletions.
16 changes: 9 additions & 7 deletions lib/target-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}

Expand All @@ -161,15 +163,15 @@ class TargetManager extends EventEmitter {
});
}

fillTargets(path) {
fillTargets(path, refreshOnEmpty = true) {
if (!this.targetsView) {
return;
}

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));
}
Expand Down Expand Up @@ -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);
Expand Down
132 changes: 121 additions & 11 deletions spec/build-keymap-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
});
});

Expand All @@ -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',
Expand All @@ -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/);
});
});
});
Expand Down
17 changes: 17 additions & 0 deletions spec/build-targets-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit 299d276

Please sign in to comment.