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

fix: install generator to a valid and absolute location #543

Merged
merged 1 commit into from
Apr 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
},
"ApplicationWizard.installationLocation": {
"type": "string",
"markdownDescription": "Install generators in a specified location. If not set, global location is used."
"markdownDescription": "Set the default folder in which generators will be installed. The path to this folder must be valid and absolute. If there is no path defined, the global location will be used."
},
"ApplicationWizard.autoUpdate": {
"type": "boolean",
Expand Down
4 changes: 3 additions & 1 deletion packages/backend/src/exploregens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IChildLogger } from "@vscode-logging/logger";
import { IRpc } from "@sap-devx/webview-rpc/out.ext/rpc-common";
import * as util from "util";
import * as path from "path";
import * as fs from "fs";
import messages from "./exploreGensMessages";
import * as envUtils from "./env/utils";

Expand All @@ -18,7 +19,8 @@ export enum GenState {

export class ExploreGens {
public static getInstallationLocation(wsConfig: any) {
return _.trim(wsConfig.get(ExploreGens.INSTALLATION_LOCATION));
const location = _.trim(wsConfig.get(ExploreGens.INSTALLATION_LOCATION));
return fs.existsSync(location) ? location : undefined;
}

private static readonly INSTALLATION_LOCATION =
Expand Down
72 changes: 44 additions & 28 deletions packages/backend/test/exploregens.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as mocha from "mocha";
import { expect } from "chai";
import * as sinon from "sinon";
import { SinonSandbox, SinonMock, createSandbox } from "sinon";
import * as _ from "lodash";
import * as path from "path";
import * as fs from "fs";
import { IChildLogger } from "@vscode-logging/logger";
import {
IRpc,
Expand All @@ -13,7 +14,7 @@ import * as npmFetch from "npm-registry-fetch";
import { mockVscode } from "./mockUtil";
import messages from "../src/exploreGensMessages";
import Environment = require("yeoman-environment");
import * as envutils from "../src/env/utils";
import * as envUtils from "../src/env/utils";

const testYoEnv = {
lookup: () => true,
Expand Down Expand Up @@ -62,21 +63,22 @@ import { ExploreGens, GenState } from "../src/exploregens";
import { fail } from "assert";

describe("exploregens unit test", () => {
let sandbox: any;
let rpcMock: any;
let workspaceConfigMock: any;
let exploreGensMock: any;
let loggerMock: any;
let npmFetchMock: any;
let vscodeWindowMock: any;
let vscodeCommandsMock: any;
let vscodeWorkspaceMock: any;
let statusBarMessageMock: any;
let globalStateMock: any;
let processMock: any;
let yoEnvMock: any;
let testYoEnvMock: any;
let envUtilsMock: any;
let sandbox: SinonSandbox;
let rpcMock: SinonMock;
let fsMock: SinonMock;
let workspaceConfigMock: SinonMock;
let exploreGensMock: SinonMock;
let loggerMock: SinonMock;
let npmFetchMock: SinonMock;
let vscodeWindowMock: SinonMock;
let vscodeCommandsMock: SinonMock;
let vscodeWorkspaceMock: SinonMock;
let statusBarMessageMock: SinonMock;
let globalStateMock: SinonMock;
let processMock: SinonMock;
let yoEnvMock: SinonMock;
let testYoEnvMock: SinonMock;
let envUtilsMock: SinonMock;

class TestRpc implements IRpc {
public timeout: number;
Expand Down Expand Up @@ -134,7 +136,7 @@ describe("exploregens unit test", () => {
exploregens.init(rpc);

before(() => {
sandbox = sinon.createSandbox();
sandbox = createSandbox();
});

after(() => {
Expand All @@ -143,6 +145,7 @@ describe("exploregens unit test", () => {

beforeEach(() => {
rpcMock = sandbox.mock(rpc);
fsMock = sandbox.mock(fs);
workspaceConfigMock = sandbox.mock(config);
loggerMock = sandbox.mock(childLogger);
exploreGensMock = sandbox.mock(exploregens);
Expand All @@ -155,11 +158,12 @@ describe("exploregens unit test", () => {
yoEnvMock = sandbox.mock(Environment);
testYoEnvMock = sandbox.mock(testYoEnv);
vscodeCommandsMock = sandbox.mock(testVscode.commands);
envUtilsMock = sandbox.mock(envutils);
envUtilsMock = sandbox.mock(envUtils);
});

afterEach(() => {
rpcMock.verify();
fsMock.verify();
workspaceConfigMock.verify();
loggerMock.verify();
exploreGensMock.verify();
Expand Down Expand Up @@ -249,13 +253,14 @@ describe("exploregens unit test", () => {
});

const customLocation = path.join("home", "user", "projects");
fsMock.expects("existsSync").withExactArgs(customLocation).returns(true);
workspaceConfigMock
.expects("get")
.withExactArgs(ExploreGens["INSTALLATION_LOCATION"])
.returns(customLocation);
yoEnvMock.expects("createEnv").returns(testYoEnv);
testYoEnvMock.expects("lookup").withArgs({
npmPaths: [path.join(customLocation, envutils.NODE_MODULES)],
npmPaths: [path.join(customLocation, envUtils.NODE_MODULES)],
});
exploregens["init"](rpc);
});
Expand Down Expand Up @@ -295,8 +300,7 @@ describe("exploregens unit test", () => {

workspaceConfigMock
.expects("get")
.withExactArgs(ExploreGens["INSTALLATION_LOCATION"])
.returns();
.withExactArgs(ExploreGens["INSTALLATION_LOCATION"]);
yoEnvMock.expects("createEnv").returns(testYoEnv);
exploregens["init"](rpc);
});
Expand Down Expand Up @@ -467,29 +471,42 @@ describe("exploregens unit test", () => {
it("location undefined", () => {
workspaceConfigMock
.expects("get")
.withExactArgs(ExploreGens["INSTALLATION_LOCATION"])
.returns();
.withExactArgs(ExploreGens["INSTALLATION_LOCATION"]);
const res = exploregens["getGeneratorsLocationParams"]();
expect(res).to.be.equal("-g");
});

it("location is a valid string", () => {
const customLocation = TESTVALUE;
workspaceConfigMock
.expects("get")
.withExactArgs(ExploreGens["INSTALLATION_LOCATION"])
.returns(TESTVALUE);
.returns(customLocation);
fsMock.expects("existsSync").withExactArgs(customLocation).returns(true);
const res = exploregens["getGeneratorsLocationParams"]();
expect(res).to.be.equal(`--prefix ${TESTVALUE}`);
});

it("location is a string with unnecessary spaces", () => {
const customLocation = ` ${TESTVALUE} `;
workspaceConfigMock
.expects("get")
.withExactArgs(ExploreGens["INSTALLATION_LOCATION"])
.returns(` ${TESTVALUE} `);
.returns(customLocation);
fsMock.expects("existsSync").withExactArgs(TESTVALUE).returns(true);
const res = exploregens["getGeneratorsLocationParams"]();
expect(res).to.be.deep.equal(`--prefix ${TESTVALUE}`);
});

it("location starts with tild ~", () => {
const location = "~/notExistLocation";
workspaceConfigMock
.expects("get")
.withExactArgs(ExploreGens["INSTALLATION_LOCATION"])
.returns(location);
const res = exploregens["getGeneratorsLocationParams"]();
expect(res).to.be.equal("-g");
});
});

it("exec", async () => {
Expand All @@ -514,8 +531,7 @@ describe("exploregens unit test", () => {
it("recommended array is undefined", () => {
workspaceConfigMock
.expects("get")
.withExactArgs(exploregens["SEARCH_QUERY"])
.returns();
.withExactArgs(exploregens["SEARCH_QUERY"]);
const res = exploregens["getRecommendedQuery"]();
expect(res).to.have.lengthOf(0);
});
Expand Down