Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into feat/interface
  • Loading branch information
TwitchBronBron committed May 29, 2021
2 parents 47e2533 + 903296c commit 9118175
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 219 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0



## [0.39.2] - 2021-05-28
### Changed
- upgraded to [roku-deploy@3.4.0](https://github.com/rokucommunity/roku-deploy/blob/master/CHANGELOG.md#340---2021-05-28) which brings significant zip speed improvements



## [0.39.1] - 2021-05-24
[0.39.1]: https://github.com/rokucommunity/brighterscript/compare/v0.39.0...v0.39.1
### Changed
- re-export `CodeActionKind` so plugins don't need to import from vscode-brightscript-language directly.
### Fixed
- code action for "replace" tasks bug
- include missing information in the CodeAction construction



## [0.39.0] - 2021-05-18
[0.39.0]: https://github.com/rokucommunity/brighterscript/compare/v0.38.2...v0.39.0
### Added
Expand Down Expand Up @@ -1126,3 +1142,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[0.37.3]: https://github.com/rokucommunity/brighterscript/compare/v0.37.2...v0.37.3
[0.37.4]: https://github.com/rokucommunity/brighterscript/compare/v0.37.3...v0.37.4
[0.38.0]: https://github.com/rokucommunity/brighterscript/compare/v0.37.4...v0.38.0
[0.39.0]: https://github.com/rokucommunity/brighterscript/compare/v0.38.0...v0.39.0
[0.39.1]: https://github.com/rokucommunity/brighterscript/compare/v0.39.0...v0.39.1
[0.39.2]: https://github.com/rokucommunity/brighterscript/compare/v0.39.1...v0.39.2
278 changes: 69 additions & 209 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brighterscript",
"version": "0.39.0",
"version": "0.39.2",
"description": "A superset of Roku's BrightScript language.",
"scripts": {
"preversion": "npm run build && npm run lint && npm run test",
Expand Down Expand Up @@ -128,7 +128,7 @@
"moment": "^2.23.0",
"p-settle": "^2.1.0",
"parse-ms": "^2.1.0",
"roku-deploy": "^3.2.4",
"roku-deploy": "^3.4.0",
"serialize-error": "^7.0.1",
"source-map": "^0.7.3",
"vscode-languageserver": "7.0.0",
Expand Down
26 changes: 22 additions & 4 deletions src/CodeActionUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CodeActionKind, Diagnostic, Position, Range, WorkspaceEdit } from 'vscode-languageserver';
import { CodeAction, TextEdit } from 'vscode-languageserver';
import type { Diagnostic, Position, Range, WorkspaceEdit } from 'vscode-languageserver';
import { CodeActionKind, CodeAction, TextEdit } from 'vscode-languageserver';
import { URI } from 'vscode-uri';

export class CodeActionUtil {
Expand All @@ -20,13 +20,31 @@ export class CodeActionUtil {
TextEdit.insert(change.position, change.newText)
);
} else if (change.type === 'replace') {
TextEdit.replace(change.range, change.newText);
edit.changes[uri].push(
TextEdit.replace(change.range, change.newText)
);
}
}
return CodeAction.create(obj.title, edit);
const action = CodeAction.create(obj.title, edit, obj.kind);
action.isPreferred = obj.isPreferred;
action.diagnostics = this.serializableDiagnostics(obj.diagnostics);
return action;
}

public serializableDiagnostics(diagnostics: Diagnostic[]) {
return diagnostics?.map(({ range, severity, code, source, message, relatedInformation }) => ({
range: range,
severity: severity,
source: source,
code: code,
message: message,
relatedInformation: relatedInformation
}));
}
}

export { CodeActionKind };

export interface CodeActionShorthand {
title: string;
diagnostics?: Diagnostic[];
Expand Down
147 changes: 147 additions & 0 deletions src/Logger.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { expect } from 'chai';
import { Logger, LogLevel, noop } from './Logger';
import chalk from 'chalk';
import { createSandbox } from 'sinon';
const sinon = createSandbox();

describe('Logger', () => {
let logger: Logger;

beforeEach(() => {
logger = new Logger(LogLevel.trace);
sinon.restore();
//disable chalk colors for testing
sinon.stub(chalk, 'grey').callsFake((arg) => arg as any);
});

it('noop does nothing', () => {
noop();
});

it('loglevel setter converts string to enum', () => {
(logger as any).logLevel = 'error';
expect(logger.logLevel).to.eql(LogLevel.error);
(logger as any).logLevel = 'info';
expect(logger.logLevel).to.eql(LogLevel.info);
});

it('uses LogLevel.log by default', () => {
logger = new Logger();
expect(logger.logLevel).to.eql(LogLevel.log);
});

describe('log methods call correct error type', () => {
it('error', () => {
const stub = sinon.stub(logger as any, 'writeToLog').callsFake(() => { });
logger.error();
expect(stub.getCalls()[0].args[0]).to.eql(console.error);
});

it('warn', () => {
const stub = sinon.stub(logger as any, 'writeToLog').callsFake(() => { });
logger.warn();
expect(stub.getCalls()[0].args[0]).to.eql(console.warn);
});

it('log', () => {
const stub = sinon.stub(logger as any, 'writeToLog').callsFake(() => { });
logger.log();
expect(stub.getCalls()[0].args[0]).to.eql(console.log);
});

it('info', () => {
const stub = sinon.stub(logger as any, 'writeToLog').callsFake(() => { });
logger.info();
expect(stub.getCalls()[0].args[0]).to.eql(console.info);
});

it('debug', () => {
const stub = sinon.stub(logger as any, 'writeToLog').callsFake(() => { });
logger.debug();
expect(stub.getCalls()[0].args[0]).to.eql(console.debug);
});

it('trace', () => {
const stub = sinon.stub(logger as any, 'writeToLog').callsFake(() => { });
logger.trace();
expect(stub.getCalls()[0].args[0]).to.eql(console.trace);
});
});

it('skips all errors on error level', () => {
logger.logLevel = LogLevel.off;
const stub = sinon.stub(logger as any, 'writeToLog').callsFake(() => { });
logger.trace();
logger.debug();
logger.info();
logger.log();
logger.warn();
logger.error();

expect(
stub.getCalls().map(x => x.args[0])
).to.eql([]);
});

it('does not skip when log level is high enough', () => {
logger.logLevel = LogLevel.trace;
const stub = sinon.stub(logger as any, 'writeToLog').callsFake(() => { });
logger.trace();
logger.debug();
logger.info();
logger.log();
logger.warn();
logger.error();

expect(
stub.getCalls().map(x => x.args[0])
).to.eql([
console.trace,
console.debug,
console.info,
console.log,
console.warn,
console.error
]);
});

describe('time', () => {
it('calls action even if logLevel is wrong', () => {
logger.logLevel = LogLevel.error;
const spy = sinon.spy();
logger.time(LogLevel.info, null, spy);
expect(spy.called).to.be.true;
});

it('runs timer when loglevel is right', () => {
logger.logLevel = LogLevel.log;
const spy = sinon.spy();
logger.time(LogLevel.log, null, spy);
expect(spy.called).to.be.true;
});

it('returns value', () => {
logger.logLevel = LogLevel.log;
const spy = sinon.spy(() => {
return true;
});
expect(
logger.time(LogLevel.log, null, spy)
).to.be.true;
expect(spy.called).to.be.true;
});

it('gives callable pause and resume functions even when not running timer', () => {
logger.time(LogLevel.info, null, (pause, resume) => {
pause();
resume();
});
});

it('waits for and returns a promise when a promise is returned from the action', () => {
expect(logger.time(LogLevel.info, ['message'], () => {
return Promise.resolve();
})).to.be.instanceof(Promise);
});
});
});
7 changes: 4 additions & 3 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class Logger {
let logLevelString = LogLevel[logLevel];

//write the initial log
this[logLevelString](...messages);
this[logLevelString](...messages ?? []);
this.indent += ' ';

stopwatch.start();
Expand All @@ -135,7 +135,7 @@ export class Logger {
//return a function to call when the timer is complete
let done = () => {
this.indent = this.indent.substring(2);
this[logLevelString](...messages, `finished. (${chalk.blue(stopwatch.getDurationText())})`);
this[logLevelString](...messages ?? [], `finished. (${chalk.blue(stopwatch.getDurationText())})`);
};

//if this is a promise, wait for it to resolve and then return the original result
Expand All @@ -154,11 +154,12 @@ export class Logger {
}
}

function noop() {
export function noop() {

}

export enum LogLevel {
off = 0,
error = 1,
warn = 2,
log = 3,
Expand Down
3 changes: 3 additions & 0 deletions src/ProgramBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ export class ProgramBuilder {
await this.logger.time(LogLevel.log, [`Creating package at ${this.options.outFile}`], async () => {
await rokuDeploy.zipPackage({
...this.options,
logLevel: this.options.logLevel as LogLevel,
outDir: util.getOutDir(this.options),
outFile: path.basename(this.options.outFile)
});
Expand All @@ -374,6 +375,7 @@ export class ProgramBuilder {
let options = util.cwdWork(this.options.cwd, () => {
return rokuDeploy.getOptions({
...this.options,
logLevel: this.options.logLevel as LogLevel,
outDir: util.getOutDir(this.options),
outFile: path.basename(this.options.outFile)
});
Expand Down Expand Up @@ -417,6 +419,7 @@ export class ProgramBuilder {
await this.logger.time(LogLevel.log, ['Deploying package to', this.options.host], async () => {
await rokuDeploy.publish({
...this.options,
logLevel: this.options.logLevel as LogLevel,
outDir: util.getOutDir(this.options),
outFile: path.basename(this.options.outFile)
});
Expand Down
55 changes: 54 additions & 1 deletion src/Stopwatch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,60 @@
import { Stopwatch } from './Stopwatch';
import { expect } from 'chai';
import { Stopwatch } from './Stopwatch';
import { util } from './util';

describe('Stopwatch', () => {
let stopwatch: Stopwatch;
beforeEach(() => {
stopwatch = new Stopwatch();
});

it('constructs', () => {
stopwatch = new Stopwatch();
});

it('starts', () => {
expect(stopwatch['startTime']).to.not.exist;
stopwatch.start();
expect(stopwatch['startTime']).to.exist;
});

it('resets', () => {
stopwatch.start();
expect(stopwatch['startTime']).to.exist;
stopwatch.reset();
expect(stopwatch['startTime']).to.not.exist;
});

it('stops', async () => {
stopwatch.start();
expect(stopwatch['startTime']).to.exist;
await util.sleep(3);
stopwatch.stop();
expect(stopwatch['startTime']).to.not.exist;
expect(stopwatch['totalMilliseconds']).to.be.gte(2);
});

it('stop multiple times has no effect', () => {
stopwatch.start();
stopwatch.stop();
stopwatch.stop();
});

it('breaks out hours, minutes, and seconds', () => {
stopwatch['totalMilliseconds'] = (17 * 60 * 1000) + (43 * 1000) + 30;
expect(stopwatch.getDurationText()).to.eql('17m43s30.0ms');
});

it('returns only seconds and milliseconds', () => {
stopwatch['totalMilliseconds'] = (43 * 1000) + 30;
expect(stopwatch.getDurationText()).to.eql('43s30.0ms');
});

it('returns only milliseconds', () => {
stopwatch['totalMilliseconds'] = 30;
expect(stopwatch.getDurationText()).to.eql('30.0ms');
});

it('works for single run', async () => {
let stopwatch = new Stopwatch();
stopwatch.start();
Expand Down

0 comments on commit 9118175

Please sign in to comment.