From 0b5f020b74e6deaa0c008ef99b62b22e384c7ab3 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 16 Sep 2018 16:11:52 +0200 Subject: [PATCH] fix(test): Tests fixed on windows --- src/execHelper.ts | 8 ++-- src/extension.ts | 69 ++++++++++++++++++++++++++++++++- src/extensionState.ts | 58 --------------------------- src/test/execHelper.test.ts | 10 +++-- src/test/extension.test.ts | 10 ++--- src/test/extension_calc.test.ts | 30 +++++++------- src/test/status.test.ts | 8 ++-- src/test/vasm.test.ts | 8 ++-- src/vasm.ts | 12 +++--- 9 files changed, 113 insertions(+), 100 deletions(-) delete mode 100644 src/extensionState.ts diff --git a/src/execHelper.ts b/src/execHelper.ts index df23b15f..b9b3bab0 100644 --- a/src/execHelper.ts +++ b/src/execHelper.ts @@ -1,7 +1,7 @@ import * as cp from 'child_process'; import * as vscode from 'vscode'; import * as path from 'path'; -import { ExtensionState } from './extensionState'; +import { ExtensionState } from './extension'; export class ICheckResult { file: string = ""; @@ -27,7 +27,7 @@ export class ExecutorHelper { * @param parser Parser for the output */ runTool(args: string[], cwd: string | null, severity: string, useStdErr: boolean, cmd: string, env: any, printUnexpectedOutput: boolean, parser: ExecutorParser | null, token?: vscode.CancellationToken): Promise { - let outputChannel = ExtensionState.getInstance().getStatusManager().outputChannel; + let outputChannel = ExtensionState.getCurrent().getStatusManager().outputChannel; let p: cp.ChildProcess; if (token) { token.onCancellationRequested(() => { @@ -160,8 +160,8 @@ export class ExecutorHelper { diagnosticMap.forEach((diagMap, file) => { const fileUri = vscode.Uri.parse(file); - let warningDiagnosticCollection = ExtensionState.getInstance().getWarningDiagnosticCollection(); - let errorDiagnosticCollection = ExtensionState.getInstance().getErrorDiagnosticCollection(); + let warningDiagnosticCollection = ExtensionState.getCurrent().getWarningDiagnosticCollection(); + let errorDiagnosticCollection = ExtensionState.getCurrent().getErrorDiagnosticCollection(); if (diagnosticSeverity === undefined || diagnosticSeverity === vscode.DiagnosticSeverity.Error) { const newErrors = diagMap.get(vscode.DiagnosticSeverity.Error); let existingWarnings = warningDiagnosticCollection.get(fileUri); diff --git a/src/extension.ts b/src/extension.ts index 15165ee7..7678869d 100755 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,7 +1,6 @@ // The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below import * as vscode from 'vscode'; -import { ExtensionState } from './extensionState'; import { M68kFormatter } from './formatter'; import { M68kHoverProvider } from './hover'; import { M86kColorProvider } from './color'; @@ -10,6 +9,10 @@ import { VASMController } from './vasm'; import { FsUAEDebugSession } from './fsUAEDebug'; import * as Net from 'net'; import { RunFsUAENoDebugSession } from './runFsUAENoDebug'; +import { Calc } from './calc'; +import { VASMCompiler } from './vasm'; +import { StatusManager } from "./status"; +import { Disassembler } from './disassemble'; import { M68kDefinitionProvider } from './definitionProvider'; @@ -23,10 +26,65 @@ export const AMIGA_ASM_MODE: vscode.DocumentFilter = { language: 'm68k', scheme: */ const EMBED_DEBUG_ADAPTER = true; +export class ExtensionState { + private compiler: VASMCompiler | undefined; + private errorDiagnosticCollection: vscode.DiagnosticCollection | undefined; + private warningDiagnosticCollection: vscode.DiagnosticCollection | undefined; + private statusManager: StatusManager | undefined; + private calc: Calc | undefined; + private disassembler: Disassembler | undefined; + public getErrorDiagnosticCollection(): vscode.DiagnosticCollection { + if (this.errorDiagnosticCollection === undefined) { + this.errorDiagnosticCollection = vscode.languages.createDiagnosticCollection('m68k-error'); + } + return this.errorDiagnosticCollection; + } + public getWarningDiagnosticCollection(): vscode.DiagnosticCollection { + if (this.warningDiagnosticCollection === undefined) { + this.warningDiagnosticCollection = vscode.languages.createDiagnosticCollection('m68k-warning'); + } + return this.warningDiagnosticCollection; + } + public getStatusManager(): StatusManager { + if (this.statusManager === undefined) { + this.statusManager = new StatusManager(); + } + return this.statusManager; + } + public getCalc(): Calc { + if (this.calc === undefined) { + this.calc = new Calc(); + } + return this.calc; + } + public getCompiler(): VASMCompiler { + if (this.compiler === undefined) { + this.compiler = new VASMCompiler(); + } + return this.compiler; + } + public getDisassembler(): Disassembler { + if (this.disassembler === undefined) { + this.disassembler = new Disassembler(); + } + return this.disassembler; + } + public static getCurrent(): ExtensionState { + // activate the extension + let ext = vscode.extensions.getExtension('prb28.amiga-assembly'); + if (ext) { + return ext.exports.getState(); + } + return new ExtensionState(); + } +} + +const state = new ExtensionState(); + // this method is called when your extension is activated // your extension is activated the very first time the command is executed export function activate(context: vscode.ExtensionContext) { - let state = ExtensionState.getInstance(); + context.globalState.update('state', state); // Preparing the status manager let statusManager = state.getStatusManager(); statusManager.showStatus("Build", 'amiga-assembly.build-vasm-workspace', "Build Workspace"); @@ -143,6 +201,13 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('fs-uae-run', runProvider)); context.subscriptions.push(runProvider); statusManager.outputChannel.appendLine("------> done"); + + let api = { + getState(): ExtensionState { + return state; + } + }; + return api; } export function deactivate() { diff --git a/src/extensionState.ts b/src/extensionState.ts deleted file mode 100644 index d791b6c0..00000000 --- a/src/extensionState.ts +++ /dev/null @@ -1,58 +0,0 @@ - -import * as vscode from 'vscode'; -import { Calc } from './calc'; -import { VASMCompiler } from './vasm'; -import { StatusManager } from "./status"; -import { Disassembler } from './disassemble'; - -export class ExtensionState { - private static instance: ExtensionState; - private compiler: VASMCompiler | undefined; - private errorDiagnosticCollection: vscode.DiagnosticCollection | undefined; - private warningDiagnosticCollection: vscode.DiagnosticCollection | undefined; - private statusManager: StatusManager | undefined; - private calc: Calc | undefined; - private disassembler: Disassembler | undefined; - public static getInstance() { - if (!ExtensionState.instance) { - ExtensionState.instance = new ExtensionState(); - } - return ExtensionState.instance; - } - public getErrorDiagnosticCollection(): vscode.DiagnosticCollection { - if (this.errorDiagnosticCollection === undefined) { - this.errorDiagnosticCollection = vscode.languages.createDiagnosticCollection('m68k-error'); - } - return this.errorDiagnosticCollection; - } - public getWarningDiagnosticCollection(): vscode.DiagnosticCollection { - if (this.warningDiagnosticCollection === undefined) { - this.warningDiagnosticCollection = vscode.languages.createDiagnosticCollection('m68k-warning'); - } - return this.warningDiagnosticCollection; - } - public getStatusManager(): StatusManager { - if (this.statusManager === undefined) { - this.statusManager = new StatusManager(); - } - return this.statusManager; - } - public getCalc(): Calc { - if (this.calc === undefined) { - this.calc = new Calc(); - } - return this.calc; - } - public getCompiler(): VASMCompiler { - if (this.compiler === undefined) { - this.compiler = new VASMCompiler(); - } - return this.compiler; - } - public getDisassembler(): Disassembler { - if (this.disassembler === undefined) { - this.disassembler = new Disassembler(); - } - return this.disassembler; - } -} \ No newline at end of file diff --git a/src/test/execHelper.test.ts b/src/test/execHelper.test.ts index a34abd27..de17f4aa 100644 --- a/src/test/execHelper.test.ts +++ b/src/test/execHelper.test.ts @@ -3,7 +3,7 @@ import * as vscode from 'vscode'; import * as cp from 'child_process'; import { reset, capture, spy, verify, anyString, instance, when, anything, mock, resetCalls } from 'ts-mockito/lib/ts-mockito'; import { ExecutorHelper, ExecutorParser, ICheckResult } from '../execHelper'; -import { ExtensionState } from '../extensionState'; +import { ExtensionState } from '../extension'; import { DummyTextDocument } from './dummy'; describe("Executor Tests", function () { @@ -11,7 +11,7 @@ describe("Executor Tests", function () { before(() => { // Opening file to activate the extension const newFile = vscode.Uri.parse("untitled://./exe.s"); - return vscode.window.showTextDocument(newFile).then(() => { spiedOutputChannel = spy(ExtensionState.getInstance().getStatusManager().outputChannel); }); + return vscode.window.showTextDocument(newFile).then(() => { spiedOutputChannel = spy(ExtensionState.getCurrent().getStatusManager().outputChannel); }); }); it("Should execute a command and parse stdout", async () => { resetCalls(spiedOutputChannel); @@ -63,8 +63,8 @@ describe("Executor Tests", function () { }); describe("Diagnostics handle", () => { let ex: ExecutorHelper; - let errorDiagnosticCollection = ExtensionState.getInstance().getErrorDiagnosticCollection(); - let warningDiagnosticCollection = ExtensionState.getInstance().getWarningDiagnosticCollection(); + let errorDiagnosticCollection: vscode.DiagnosticCollection; + let warningDiagnosticCollection: vscode.DiagnosticCollection; let spiedErrorDiagnosticCollection: vscode.DiagnosticCollection; let spiedWarningDiagnosticCollection: vscode.DiagnosticCollection; let error: ICheckResult; @@ -72,6 +72,8 @@ describe("Executor Tests", function () { let errors: Array; let document: DummyTextDocument; beforeEach(() => { + errorDiagnosticCollection = ExtensionState.getCurrent().getErrorDiagnosticCollection(); + warningDiagnosticCollection = ExtensionState.getCurrent().getWarningDiagnosticCollection(); errorDiagnosticCollection.clear(); warningDiagnosticCollection.clear(); ex = new ExecutorHelper(); diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index 2a4f6bd2..7a13e750 100755 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -9,7 +9,7 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as fs from 'fs'; import { spy, verify, when, anything, resetCalls, mock, instance } from 'ts-mockito/lib/ts-mockito'; -import { ExtensionState } from '../extensionState'; +import { ExtensionState } from '../extension'; import { Capstone } from '../capstone'; // Defines a Mocha test suite to group tests of similar kind together @@ -65,7 +65,7 @@ describe("Global Extension Tests", function () { }); describe("Build commands", function () { it("Should build the workspace on command", async () => { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let spiedCompiler = spy(state.getCompiler()); let spiedStatus = spy(state.getStatusManager()); when(spiedCompiler.buildWorkspace()).thenCall(() => { return Promise.resolve(); }); @@ -84,7 +84,7 @@ describe("Global Extension Tests", function () { verify(spiedStatus.onError("nope")).once(); }); it("Should build the current document on command", async () => { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let spiedCompiler = spy(state.getCompiler()); let spiedStatus = spy(state.getStatusManager()); when(spiedCompiler.buildCurrentEditorFile()).thenCall(() => { return Promise.resolve(); }); @@ -102,7 +102,7 @@ describe("Global Extension Tests", function () { verify(spiedStatus.onError("nope")).once(); }); it("Should clean the current workspace on command", async () => { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let spiedCompiler = spy(state.getCompiler()); let spiedStatus = spy(state.getStatusManager()); when(spiedCompiler.cleanWorkspace()).thenCall(() => { return Promise.resolve(); }); @@ -122,7 +122,7 @@ describe("Global Extension Tests", function () { }); describe("Disassemble command", function () { it("Should disassemble a file", async () => { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let spiedDisassembler = spy(state.getDisassembler()); let mockedCapstone = mock(Capstone); let capstone = instance(mockedCapstone); diff --git a/src/test/extension_calc.test.ts b/src/test/extension_calc.test.ts index eef085f0..1e898537 100755 --- a/src/test/extension_calc.test.ts +++ b/src/test/extension_calc.test.ts @@ -7,11 +7,12 @@ import { expect } from 'chai'; import * as vscode from 'vscode'; import { spy, verify, anyString, capture, when, anything } from 'ts-mockito/lib/ts-mockito'; -import { ExtensionState } from '../extensionState'; +import { ExtensionState } from '../extension'; // Defines a Mocha test suite to group tests of similar kind together describe("Global Extension Tests", function () { - describe("Calc commands", function () { + context("Calc commands", function () { + let state: ExtensionState | undefined; before(async () => { const newFile = vscode.Uri.parse("untitled://./myfile.s"); await vscode.workspace.openTextDocument(newFile).then(async document => { @@ -39,22 +40,25 @@ describe("Global Extension Tests", function () { } else { expect.fail("Editor not available"); } + state = ExtensionState.getCurrent(); }); - it("Should evaluate the selection in the status bar", () => { + it("Should evaluate the selection in the status bar", async () => { this.timeout(60000); - let calc = ExtensionState.getInstance().getCalc(); - // Get the satus value - // tslint:disable-next-line:no-unused-expression - expect(calc).to.not.be.undefined; - let sb = calc.getStatusBar(); - // tslint:disable-next-line:no-unused-expression - expect(sb).to.not.be.undefined; - if (sb) { - expect(sb.text).to.be.equal("3+2=#5/$5/%101"); + if (state) { + let calc = state.getCalc(); + // Get the satus value + // tslint:disable-next-line:no-unused-expression + expect(calc).to.not.be.undefined; + let sb = calc.getStatusBar(); + // tslint:disable-next-line:no-unused-expression + expect(sb).to.not.be.undefined; + if (sb) { + expect(sb.text).to.be.equal("3+2=#5/$5/%101"); + } } }); it("Should hide the status bar if it it not evaluable", async () => { - let calc = ExtensionState.getInstance().getCalc(); + let calc = ExtensionState.getCurrent().getCalc(); let sb = calc.getStatusBar(); // tslint:disable-next-line:no-unused-expression expect(sb).to.not.be.undefined; diff --git a/src/test/status.test.ts b/src/test/status.test.ts index e70dab95..768076c8 100755 --- a/src/test/status.test.ts +++ b/src/test/status.test.ts @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { ExtensionState } from '../extensionState'; +import { ExtensionState } from '../extension'; import { spy, verify, anyString, when } from 'ts-mockito/lib/ts-mockito'; import * as vscode from 'vscode'; @@ -11,7 +11,7 @@ describe("Status Tests", function () { return vscode.window.showTextDocument(newFile); }); it("Should status show on command", () => { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let statusManager = state.getStatusManager(); statusManager.diagnosticsStatusBarItem.show(); // status is shown @@ -48,7 +48,7 @@ describe("Status Tests", function () { }); it("Should show react to an error", () => { let spiedWindow = spy(vscode.window); - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let statusManager = state.getStatusManager(); expect(statusManager.statusBarEntry).not.to.be.null; if (statusManager.statusBarEntry) { @@ -60,7 +60,7 @@ describe("Status Tests", function () { } }); it("Should show react to a success", () => { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let statusManager = state.getStatusManager(); expect(statusManager.statusBarEntry).not.to.be.null; if (statusManager.statusBarEntry) { diff --git a/src/test/vasm.test.ts b/src/test/vasm.test.ts index e36a5278..726a98b7 100755 --- a/src/test/vasm.test.ts +++ b/src/test/vasm.test.ts @@ -5,7 +5,7 @@ import { capture, spy, verify, anyString, when, anything, resetCalls, reset } fr import { VASMCompiler, VASMParser, VASMController } from '../vasm'; import { ExecutorHelper, ICheckResult } from '../execHelper'; import { DummyTextDocument } from './dummy'; -import { ExtensionState } from '../extensionState'; +import { ExtensionState } from '../extension'; import { VLINKLinker } from '../vlink'; describe("VASM Tests", function () { @@ -164,7 +164,7 @@ describe("VASM Tests", function () { }); it("Should clean the workspace", async function () { let spiedWorkspace = spy(vscode.workspace); - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let spiedOutputChannel = spy(state.getStatusManager().outputChannel); when(spiedCompiler.unlink(anything())).thenCall(() => { }); let file1 = vscode.Uri.parse("file:///build/file1.o"); @@ -177,7 +177,7 @@ describe("VASM Tests", function () { }); }); it("Should get an error when cleaning the workspace", async function () { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let spiedOutputChannel = spy(state.getStatusManager().outputChannel); spiedCompiler = spy(compiler); when(spiedCompiler.getWorkspaceRootDir()).thenReturn(vscode.Uri.parse("file:///workdir")); @@ -209,7 +209,7 @@ describe("VASM Tests", function () { it("Should build the current document on save", async () => { let compiler = new VASMCompiler(); const spiedCompiler = spy(compiler); - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); const spiedStatus = spy(state.getStatusManager()); let controller = new VASMController(compiler); let document = new DummyTextDocument(); diff --git a/src/vasm.ts b/src/vasm.ts index a8f0029d..c275f2b5 100755 --- a/src/vasm.ts +++ b/src/vasm.ts @@ -1,6 +1,6 @@ import { window, workspace, Disposable, DiagnosticSeverity, TextDocument, Uri } from "vscode"; import { ExecutorParser, ICheckResult, ExecutorHelper } from "./execHelper"; -import { ExtensionState } from './extensionState'; +import { ExtensionState } from './extension'; import { VLINKLinker } from './vlink'; import * as fs from "fs"; import * as path from "path"; @@ -87,7 +87,7 @@ export class VASMCompiler { public buildWorkspace(): Promise { return new Promise((resolve, reject) => { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let warningDiagnosticCollection = state.getWarningDiagnosticCollection(); let errorDiagnosticCollection = state.getErrorDiagnosticCollection(); errorDiagnosticCollection.clear(); @@ -115,7 +115,7 @@ export class VASMCompiler { */ public cleanWorkspace(): Promise { return new Promise((resolve, reject) => { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let warningDiagnosticCollection = state.getWarningDiagnosticCollection(); let errorDiagnosticCollection = state.getErrorDiagnosticCollection(); let statusManager = state.getStatusManager(); @@ -180,7 +180,7 @@ export class VASMCompiler { } else { // The linker is not mandatory // show a warning in the output - ExtensionState.getInstance().getStatusManager().outputChannel.append("Warning : the linker vlink is not configured"); + ExtensionState.getCurrent().getStatusManager().outputChannel.append("Warning : the linker vlink is not configured"); return resolve(); } }).catch(err => { return reject(new Error(err)); }); @@ -205,7 +205,7 @@ export class VASMCompiler { let conf: any = configuration.get('vasm'); if (this.mayCompile(conf)) { return this.mkdirSync(buildDir.fsPath).then(() => { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let warningDiagnosticCollection = state.getWarningDiagnosticCollection(); let errorDiagnosticCollection = state.getErrorDiagnosticCollection(); let vasmExecutableName: string = conf.file; @@ -309,7 +309,7 @@ export class VASMController { } onSaveDocument(document: TextDocument) { - let state = ExtensionState.getInstance(); + let state = ExtensionState.getCurrent(); let statusManager = state.getStatusManager(); if (document.languageId !== 'm68k') { return;