Skip to content

Commit

Permalink
coverage, messages and output folder path bug fixes (#195)
Browse files Browse the repository at this point in the history
* coverage and messages fixes

* path bug fix
  • Loading branch information
slavik-lvovsky authored Mar 22, 2020
1 parent d044def commit fcf0f57
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 61 deletions.
10 changes: 5 additions & 5 deletions backend/.nycrc.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"require": ["ts-node/register/transpile-only"],
"include": ["src/**/*.ts"],
"exclude": ["src/logger/*.ts"],
"exclude": ["src/logger/*.ts", "src/webSocketServer/*.ts"],
"reporter": ["lcov", "text"],
"extension": [".ts"],
"all": true,
"temp-dir": "./reports/.nyc_output",
"report-dir": "./reports/coverage",
"check-coverage": true,
"branches": 51.5,
"lines": 56.2,
"functions": 43.3,
"statements": 56.1
"branches": 54.2,
"lines": 63.6,
"functions": 54.9,
"statements": 63.4
}
51 changes: 24 additions & 27 deletions backend/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,32 +147,31 @@ export class YeomanUIPanel {
this.rpc = new RpcExtension(this.panel.webview);
const outputChannel: YouiLog = new OutputChannelLog();
const vscodeYouiEvents: YouiEvents = new VSCodeYouiEvents(this.rpc, this.panel);
const outputFolder = _.get(vscode, "workspace.workspaceFolders[0].uri.path");
const outputFolder = _.get(vscode, "workspace.workspaceFolders[0].uri.fsPath");
this.yeomanui = new YeomanUI(this.rpc, vscodeYouiEvents, outputChannel, this.logger, YeomanUIPanel.genFilter, outputFolder);
this.yeomanui.registerCustomQuestionEventHandler("file-browser", "getFilePath", this.showOpenFileDialog.bind(this));
this.yeomanui.registerCustomQuestionEventHandler("folder-browser", "getPath", this.showOpenFolderDialog.bind(this));

// Set the webview's initial html content
this._update();

// Set the context (yeoman-ui is focused)
vscode.commands.executeCommand('setContext', 'yeomanUI.Focused', this.panel.active);

// Listen for when the panel is disposed
// This happens when the user closes the panel or when the panel is closed programatically
this.panel.onDidDispose(() => this.dispose(), null, this.disposables);

// Update the content based on view changes
this.panel.onDidChangeViewState(
e => {
if (this.panel.visible) {
this._update();
}
vscode.commands.executeCommand('setContext', 'yeomanUI.Focused', this.panel.active);
},
null,
this.disposables
);
this._update().then(() => {
// Set the context (yeoman-ui is focused)
vscode.commands.executeCommand('setContext', 'yeomanUI.Focused', this.panel.active);

// Listen for when the panel is disposed
// This happens when the user closes the panel or when the panel is closed programatically
this.panel.onDidDispose(() => this.dispose(), null, this.disposables);
const that = this;
// Update the content based on view changes
this.panel.onDidChangeViewState(async () => {
if (that.panel.visible) {
await that._update();
}
vscode.commands.executeCommand('setContext', 'yeomanUI.Focused', that.panel.active);
},
null,
this.disposables
);
});
}

public dispose() {
Expand All @@ -193,10 +192,8 @@ export class YeomanUIPanel {
return this.rpc ? this.rpc.invoke("setMessages", [messages]) : Promise.resolve();
}

private _update() {

// TODO: don't use sync
let indexHtml: string = fsextra.readFileSync(path.join(YeomanUIPanel.getMediaPath(this.extensionPath), 'index.html'), "utf8");
private async _update() {
let indexHtml: string = await fsextra.readFile(path.join(YeomanUIPanel.getMediaPath(this.extensionPath), 'index.html'), "utf8");
if (indexHtml) {
// Local path to main script run in the webview
const scriptPathOnDisk = vscode.Uri.file(path.join(YeomanUIPanel.getMediaPath(this.extensionPath), path.sep));
Expand All @@ -211,9 +208,9 @@ export class YeomanUIPanel {
const uiMessages = _.assign({}, backendMessages, _.get(YeomanUIPanel, "messages", {}));
this.panel.title = _.get(uiMessages, "panel_title");

this.setMessages(uiMessages);

this.panel.webview.html = indexHtml;

await this.setMessages(uiMessages);
}
}

Expand Down
36 changes: 16 additions & 20 deletions backend/src/yeomanui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ export class YeomanUI {
"Some quick example text of the generator description. This is a long text so that the example will look good.";
private static YEOMAN_PNG = "yeoman.png";
private static isWin32 = (process.platform === 'win32');
private static cwd: string = path.join(os.homedir(), 'projects');
private static NODE_MODULES = 'node_modules';
private static readonly PROJECTS: string = path.join(os.homedir(), 'projects');
private static readonly NODE_MODULES = 'node_modules';

private static funcReplacer(key: any, value: any) {
return _.isFunction(value) ? "__Function" : value;
}

private cwd: string;
private rpc: IRpc;
private youiEvents: YouiEvents;
private outputChannel: YouiLog;
Expand All @@ -61,7 +62,7 @@ export class YeomanUI {
private genFilter: GeneratorFilter;
private customQuestionEventHandlers: Map<string, Map<string, Function>>;

constructor(rpc: IRpc, youiEvents: YouiEvents, outputChannel: YouiLog, logger: IChildLogger, genFilter?: GeneratorFilter, outputPath?: string) {
constructor(rpc: IRpc, youiEvents: YouiEvents, outputChannel: YouiLog, logger: IChildLogger, genFilter?: GeneratorFilter, outputPath: string = YeomanUI.PROJECTS) {
this.rpc = rpc;
if (!this.rpc) {
throw new Error("rpc must be set");
Expand Down Expand Up @@ -133,7 +134,7 @@ export class YeomanUI {
// see issue: https://github.com/yeoman/environment/issues/55
// process.chdir() doesn't work after environment has been created
try {
await fsextra.mkdirs(YeomanUI.cwd);
await fsextra.mkdirs(this.getCwd());
const env: Environment = Environment.createEnv(undefined, {}, this.youiAdapter);
const meta: Environment.GeneratorMeta = this.getGenMetadata(generatorName);
// TODO: support sub-generators
Expand All @@ -151,7 +152,7 @@ export class YeomanUI {

this.promptCount = 0;
this.gen = (gen as Generator);
this.gen.destinationRoot(YeomanUI.cwd);
this.gen.destinationRoot(this.getCwd());
/* Generator.run() returns promise. Sending a callback is deprecated:
https://yeoman.github.io/generator/Generator.html#run
... but .d.ts hasn't been updated for a while:
Expand Down Expand Up @@ -219,6 +220,14 @@ export class YeomanUI {
this.outputChannel.log(message);
}

public setCwd(cwd: string) {
this.cwd = (cwd || YeomanUI.PROJECTS);
}

public getCwd(): string {
return this.cwd;
}

public async showPrompt(questions: Environment.Adapter.Questions<any>): Promise<inquirer.Answers> {
this.currentQuestions = questions;

Expand Down Expand Up @@ -253,15 +262,15 @@ export class YeomanUI {
private getEnv(): Environment.Options {
const env: Environment.Options = Environment.createEnv();
const envGetNpmPaths: () => any = env.getNpmPaths;
const that = this;
env.getNpmPaths = function (localOnly:boolean = false) {
// Start with the local paths derived by cwd in vscode
// (as opposed to cwd of the plugin host process which is what is used by yeoman/environment)
// Walk up the CWD and add `node_modules/` folder lookup on each level
const parts: string[] = YeomanUI.cwd.split(path.sep);
const parts: string[] = that.getCwd().split(path.sep);
const localPaths = _.map(parts, (part, index) => {
const resrpath = path.join(...parts.slice(0, index + 1), YeomanUI.NODE_MODULES);
return YeomanUI.isWin32 ? resrpath : path.join(path.sep, resrpath);

});
const defaultPaths = envGetNpmPaths.call(this, localOnly);

Expand Down Expand Up @@ -416,17 +425,4 @@ export class YeomanUI {
}
}
}

public setCwd(cwd: string){
if(cwd){
YeomanUI.cwd=cwd;
}
else{
YeomanUI.cwd=path.join(os.homedir(), 'projects');
}
}

public getCwd():string {
return YeomanUI.cwd;
}
}
13 changes: 7 additions & 6 deletions backend/tests/yeomanui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ describe('yeomanui unit test', () => {
});

describe("getEnv", () => {
const yeomanUiInstance: YeomanUI = new YeomanUI(rpc, youiEvents, logger, testLogger);
const yeomanUiInstance: YeomanUI = new YeomanUI(rpc, youiEvents, logger, testLogger, null, path.join("root/project/folder"));
const testEnv = yeomanUiInstance["getEnv"]();
const nodemodules = YeomanUI["NODE_MODULES"];
testEnv.getNpmPaths = (localOnly: boolean = false): string[] => {
Expand All @@ -514,13 +514,12 @@ describe('yeomanui unit test', () => {
};

beforeEach(() => {
YeomanUI["cwd"] = path.join("root/project/folder");
yeomanEnvMock.expects("createEnv").returns(testEnv);
});

it("env.getNpmPaths - localOnly is false, isWin32 is true", () => {
YeomanUI["isWin32"] = true;
const env = yeomanUi["getEnv"]();
const env = yeomanUiInstance["getEnv"]();
const res = env.getNpmPaths();
expect(res).to.have.lengthOf(7);
expect(res).to.include(path.join("root", nodemodules));
Expand All @@ -534,7 +533,7 @@ describe('yeomanui unit test', () => {

it("env.getNpmPaths - localOnly is true, isWin32 is true", () => {
YeomanUI["isWin32"] = true;
const env = yeomanUi["getEnv"]();
const env = yeomanUiInstance["getEnv"]();
const res = env.getNpmPaths(true);
expect(res).to.have.lengthOf(5);
expect(res).to.include(path.join("root", nodemodules));
Expand All @@ -546,7 +545,7 @@ describe('yeomanui unit test', () => {

it("env.getNpmPaths - localOnly is false, isWin32 is false", () => {
YeomanUI["isWin32"] = false;
const env = yeomanUi["getEnv"]();
const env = yeomanUiInstance["getEnv"]();
const res = env.getNpmPaths();
expect(res).to.have.lengthOf(7);
expect(res).to.include(path.join(path.sep, "root", nodemodules));
Expand All @@ -560,7 +559,7 @@ describe('yeomanui unit test', () => {

it("env.getNpmPaths - localOnly is true, isWin32 is false", () => {
YeomanUI["isWin32"] = false;
const env = yeomanUi["getEnv"]();
const env = yeomanUiInstance["getEnv"]();
const res = env.getNpmPaths(true);
expect(res).to.have.lengthOf(5);
expect(res).to.include(path.join(path.sep, "root", nodemodules));
Expand Down Expand Up @@ -627,6 +626,7 @@ describe('yeomanui unit test', () => {
yeomanUiInstance.registerCustomQuestionEventHandler("questionType", "testEvent", testEventFunction);
yeomanUiInstance["currentQuestions"] = [{name:"question1", guiType: "questionType"}];
const response = await yeomanUiInstance.evaluateMethod(null, "question1", "testEvent");
// tslint:disable-next-line: no-unused-expression
expect(response).to.be.true;
});

Expand All @@ -643,6 +643,7 @@ describe('yeomanui unit test', () => {
it("no questions", async () => {
const yeomanUiInstance: YeomanUI = new YeomanUI(rpc, youiEvents, logger, testLogger);
const response = await yeomanUiInstance.evaluateMethod(null, "question1", "method1");
// tslint:disable-next-line: no-unused-expression
expect(response).to.be.undefined;
});

Expand Down
6 changes: 3 additions & 3 deletions frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ module.exports = {
],
coverageThreshold: {
"global": {
"branches": 95.8,
"branches": 95.9,
"functions": 100,
"lines": 98.1,
"statements": 98.1
"lines": 98.2,
"statements": 98.2
}
}
}

0 comments on commit fcf0f57

Please sign in to comment.