Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quick live fix #4258

Merged
merged 2 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 37 additions & 27 deletions src/live/controller.js → src/live/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@ import EventEmitter from 'events';
import Logger from './logger';
import FileWatcher from './file-watcher';
import LiveModeKeyboardEventObserver from './keyboard-observer';
import LiveModeRunner from './test-runner';

class LiveModeController extends EventEmitter {
constructor (runner) {
private running: boolean;
private restarting: boolean;
private watchingPaused: boolean;
private stopping: boolean;
private logger: Logger;
private runner: LiveModeRunner;
private keyboardObserver: LiveModeKeyboardEventObserver;
private fileWatcher: FileWatcher;

public constructor (runner: LiveModeRunner) {
super();

this.running = false;
this.restarting = false;
this.running = false;
this.restarting = false;
this.watchingPaused = false;
this.stopping = false;
this.logger = new Logger();
this.runner = runner;
this.stopping = false;
this.logger = new Logger();
this.runner = runner;

this.keyboardObserver = this._createKeyboardObserver();
this.fileWatcher = this._createFileWatcher();
this.fileWatcher = this._createFileWatcher();
}

init (files) {
public init (files: string[]): Promise<void> {
this.keyboardObserver.push(this);

this._initFileWatching(files);
Expand All @@ -29,13 +39,13 @@ class LiveModeController extends EventEmitter {
.then(() => this.logger.writeIntroMessage(files));
}

dispose () {
public dispose (): void {
this.fileWatcher.stop();

this.keyboardObserver.remove(this);
}

runTests (sourceChanged) {
public runTests (sourceChanged?: boolean): Promise<void> {
if (this.watchingPaused || this.running)
return Promise.resolve();

Expand All @@ -46,7 +56,7 @@ class LiveModeController extends EventEmitter {
return this.runner.runTests();
}

onTestRunDone (err) {
public onTestRunDone (err: Error): void {
this.running = false;

if (!this.restarting)
Expand All @@ -56,13 +66,13 @@ class LiveModeController extends EventEmitter {
this.logger.err(err);
}

_toggleWatching () {
public toggleWatching (): void {
this.watchingPaused = !this.watchingPaused;

this.logger.writeToggleWatchingMessage(!this.watchingPaused);
}

_stop () {
public stop (): Promise<void> {
if (!this.runner || !this.running) {
this.logger.writeNothingToStopMessage();

Expand All @@ -74,26 +84,26 @@ class LiveModeController extends EventEmitter {
return this.runner.suspend()
.then(() => {
this.restarting = false;
this.running = false;
this.running = false;
});
}

_restart () {
public restart (): Promise<void> {
if (this.restarting || this.watchingPaused)
return Promise.resolve();

this.restarting = true;

if (this.running) {
return this._stop()
return this.stop()
.then(() => this.logger.writeTestsFinishedMessage())
.then(() => this.runTests());
}

return this.runTests();
}

_exit () {
public exit (): Promise<void> {
if (this.stopping)
return Promise.resolve();

Expand All @@ -104,26 +114,26 @@ class LiveModeController extends EventEmitter {
return this.runner ? this.runner.exit() : Promise.resolve();
}

_createFileWatcher () {
return new FileWatcher();
public addFileToWatches (filename: string): void {
this.fileWatcher.addFile(this, filename);
}

_createKeyboardObserver () {
return new LiveModeKeyboardEventObserver();
protected _createFileWatcher (): FileWatcher {
return new FileWatcher();
}

addFileToWatches (filename) {
this.fileWatcher.addFile(this, filename);
protected _createKeyboardObserver (): LiveModeKeyboardEventObserver {
return new LiveModeKeyboardEventObserver();
}

_initFileWatching (files) {
private _initFileWatching (files: string[]): void {
files.forEach(file => this.addFileToWatches(file));
}

_setRunning () {
this.running = true;
private _setRunning (): void {
this.running = true;
this.restarting = false;
this.stopping = false;
this.stopping = false;
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/live/interfaces.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/live/keyboard-observer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from 'process';
import { emitKeypressEvents, Key } from 'readline';
import { pull } from 'lodash';
import { LiveModeController } from './interfaces';
import LiveModeController from './controller';

const LOCK_KEY_PRESS_TIMEOUT = 1000;

Expand Down
2 changes: 1 addition & 1 deletion src/live/test-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class LiveModeRunner extends Runner {
stop () {
return super.stop()
.then(() => {
return this.controller._exit();
return this.controller.exit();
});
}

Expand Down
4 changes: 2 additions & 2 deletions test/functional/fixtures/live/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ if (config.useLocalBrowsers && !config.useHeadlessBrowsers) {

helper.emitter.once('tests-completed', () => {
setTimeout(() => {
runner.controller._restart()
runner.controller.restart()
.then(() => {
runner.exit();
});
Expand Down Expand Up @@ -131,7 +131,7 @@ if (config.useLocalBrowsers && !config.useHeadlessBrowsers) {

helper.emitter.once('tests-completed', () => {
setTimeout(() => {
runner.controller._restart().then(() => {
runner.controller.restart().then(() => {
expect(Object.keys(helper.data).length).eql(2);
expect(helper.data[0]).eql(true);
expect(helper.data[1]).eql(true);
Expand Down
28 changes: 14 additions & 14 deletions test/server/live-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('TestCafe Live', function () {
it('rerun', function () {
return runTests(testFileWithSingleTestPath)
.then(() => {
return runner.controller._restart()
return runner.controller.restart()
.then(() => {
expect(runner.runCount).eql(2);
});
Expand Down Expand Up @@ -247,7 +247,7 @@ describe('TestCafe Live', function () {
runner.src(testFileWithMultipleTestsPath);
})
.then(() => {
return runner.controller._restart();
return runner.controller.restart();
})
.then(() => {
expect(runner.runCount).eql(2);
Expand All @@ -268,7 +268,7 @@ describe('TestCafe Live', function () {
.then(() => {
runner.src(testFileWithSyntaxErrorPath);

return runner.controller._restart();
return runner.controller.restart();
})
.then(() => {
expect(errors.length).eql(1);
Expand All @@ -279,7 +279,7 @@ describe('TestCafe Live', function () {
runner.clearSources();
runner.src(testFileWithSingleTestPath);

return runner.controller._restart();
return runner.controller.restart();
})
.then(() => {
expect(runner.runCount).eql(3);
Expand All @@ -293,7 +293,7 @@ describe('TestCafe Live', function () {
runner.clearSources();
runner.src(testFileWithSyntaxErrorPath);

return runner.controller._restart();
return runner.controller.restart();
})
.then(() => {
expect(runner.runCount).eql(4);
Expand Down Expand Up @@ -400,7 +400,7 @@ describe('TestCafe Live', function () {

expect(runner.runCount).eql(1);

return runner.controller._restart();
return runner.controller.restart();
})
.then(() => {
expect(runner.runCount).eql(2);
Expand All @@ -412,12 +412,12 @@ describe('TestCafe Live', function () {
.then(() => {
expect(runner.runCount).eql(1);

runner.controller._restart();
runner.controller.restart();

expect(runner.controller.restarting).eql(false);
expect(runner.controller.running).eql(true);

runner.controller._restart();
runner.controller.restart();

expect(runner.controller.restarting).eql(true);
expect(runner.controller.running).eql(true);
Expand All @@ -430,9 +430,9 @@ describe('TestCafe Live', function () {
return runTests(testFileWithSingleTestPath, { runTimeout: 100 })
.then(() => {
expect(runner.runCount).eql(1);
runner.controller._restart();
runner.controller.restart();

return runner.controller._stop();
return runner.controller.stop();
})
.then(() => {
expect(runner.runCount).eql(1);
Expand All @@ -446,16 +446,16 @@ describe('TestCafe Live', function () {
.then(() => {
expect(runner.runCount).eql(1);

runner.controller._toggleWatching();
runner.controller.toggleWatching();

return runner.controller._restart();
return runner.controller.restart();
})
.then(() => {
expect(runner.runCount).eql(1);

runner.controller._toggleWatching();
runner.controller.toggleWatching();

return runner.controller._restart();
return runner.controller.restart();
})
.then(() => {
expect(runner.runCount).eql(2);
Expand Down