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

Feature/workspace/extension folder #2265

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
4 changes: 4 additions & 0 deletions clients/cobol-lsp-vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
"command": "cobol-lsp.snippets.insertSnippets",
"title": "Insert COBOL Snippet",
"category": "Snippets"
},
{
"command": "cobol-lsp.open.copybook.internalfolder",
"title": "Open Copybooks Internal Folder"
}
],
"languages": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,31 @@

import * as path from "path";
import { clearCache } from "../../commands/ClearCopybookCacheCommand";
import * as vscode from "vscode";

jest.mock("vscode", () => ({
Uri: {
parse: jest.fn().mockReturnValue(path.join("tmp-ws", ".c4z", ".copybooks")),
file: jest.fn().mockReturnValue({
fsPath: path.join(__dirname, ".zowe"),
with: jest
.fn()
.mockImplementation(
(change: {
scheme?: string;
authority?: string;
path?: string;
query?: string;
fragment?: string;
}) => {
return {
path: change.path,
fsPath: change.path,
with: jest.fn().mockReturnValue({ path: change.path }),
};
},
),
}),
},
window: {
setStatusBarMessage: jest.fn().mockResolvedValue(true),
Expand Down Expand Up @@ -68,7 +89,7 @@ describe("Tests downloaded copybook cache clear", () => {
it("checks running command multiple times doesn't produce error", () => {
expect(() => {
for (let index = 0; index < 3; index++) {
clearCache();
clearCache(vscode.Uri.file("/storagePath"));
}
}).not.toThrowError();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jest.mock("../../services/copybook/CopybookDownloadService");
Utils.getZoweExplorerAPI = jest.fn();

const copybookDownloadService: CopybookDownloadService =
new CopybookDownloadService();
new CopybookDownloadService("/storagePath");
const copybook: string = "cobyBookTest";
const progName: string = "progNameTest";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ jest.mock("../services/copybook/CopybookDownloadService");
jest.mock("../commands/ClearCopybookCacheCommand");

jest.mock("../services/Settings", () => ({
createFileWithGivenPath: jest.fn(),
initializeSettings: jest.fn(),
SettingsService: {
serverRuntime: jest
Expand Down Expand Up @@ -87,20 +86,23 @@ jest.mock("vscode", () => ({
.fn()
.mockReturnValue("onDidChangeConfiguration"),
workspaceFolders: [{ uri: { fsPath: "ws-path" } } as any],
fs: {
createDirectory: jest.fn(),
},
},
Uri: {
file: jest.fn().mockReturnValue("workspaceFolder2"),
},
}));

jest.mock("vscode-languageclient", () => ({
LanguageClient: jest.fn(),
}));
jest.mock("../services/reporter/TelemetryService");
jest.mock("../services/copybook/CopybookMessageHandler", () => ({
resolveCopybookHandler: jest.fn(),
downloadCopybookHandler: jest.fn(),
}));

const context: any = {
subscriptions: [],
globalStorageUri: { fsPath: "/storagePath" },
};

beforeEach(() => {
Expand All @@ -117,7 +119,7 @@ describe("Check plugin extension for cobol starts successfully.", () => {
"Extension activation event was triggered",
);

expect(vscode.commands.registerCommand).toBeCalledTimes(9);
expect(vscode.commands.registerCommand).toBeCalledTimes(10);

expect(fetchCopybookCommand).toHaveBeenCalled();
expect(gotoCopybookSettings).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ jest.mock("vscode", () => ({
},
Uri: {
file: jest.fn().mockReturnValue({
fsPath: "path",
fsPath: "/storagePath",
}),
},
RelativePattern: jest.fn().mockReturnValue(undefined),
slavek-kucera marked this conversation as resolved.
Show resolved Hide resolved
}));
jest.mock("vscode-languageclient/node", () => ({
LanguageClient: jest.fn(),
Expand All @@ -68,7 +69,10 @@ beforeEach(() => {
const SERVER_STOPPED_MSG = "server stopped";
describe("LanguageClientService positive scenario", () => {
beforeEach(() => {
languageClientService = new LanguageClientService(jest.fn() as any);
languageClientService = new LanguageClientService(
jest.fn() as any,
vscode.Uri.file("/storagePath"),
);
new JavaCheck().isJavaInstalled = jest.fn().mockResolvedValue(true);
vscode.workspace.getConfiguration(expect.any(String)).get = jest
.fn()
Expand Down Expand Up @@ -236,7 +240,10 @@ describe("LanguageClientService negative scenario.", () => {
test("LSP port not defined and jar path doesn't exists", async () => {
(fs.existsSync as any) = jest.fn().mockReturnValue(false);
try {
await new LanguageClientService(jest.fn() as any).checkPrerequisites();
await new LanguageClientService(
jest.fn() as any,
vscode.Uri.file("/storagePath"),
).checkPrerequisites();
} catch (error: any) {
expect(error.toString()).toBe("Error: LSP server for cobol not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,14 @@
import * as fs from "fs-extra";
import * as path from "path";
import * as vscode from "vscode";
import { C4Z_FOLDER, GITIGNORE_FILE } from "../../constants";
import {
createFileWithGivenPath,
SettingsService,
} from "../../services/Settings";
import { SettingsService } from "../../services/Settings";
import { SettingsUtils } from "../../services/util/SettingsUtils";

const fsPath = "tmp-ws";
let wsPath: string;
let c4zPath: string;
let filePath: string;

beforeAll(() => {
(vscode.workspace.workspaceFolders as any) = [
{ uri: { fsPath, path: fsPath } } as any,
{ uri: { fsPath: makefsPath(fsPath), path: makePath(fsPath) } } as any,
];
const firstWorkspaceFolder = vscode.workspace.workspaceFolders![0];
if (!firstWorkspaceFolder) return;
wsPath = path.join(firstWorkspaceFolder.uri.fsPath);
c4zPath = path.join(wsPath, C4Z_FOLDER);
filePath = path.join(c4zPath, GITIGNORE_FILE);
});

afterAll(() => {
if (fs.existsSync(wsPath)) {
fs.remove(wsPath);
}
});

jest.mock("vscode", () => ({
Expand All @@ -54,40 +35,36 @@ jest.mock("vscode", () => ({
workspace: {},
}));

describe(".gitignore file in .c4z folder tests", () => {
it("Create .gitignore file if not exists", () => {
createFileWithGivenPath(C4Z_FOLDER, GITIGNORE_FILE, "/**");

expect(fs.existsSync(wsPath)).toEqual(true);
expect(fs.existsSync(c4zPath)).toEqual(true);
expect(fs.existsSync(filePath)).toEqual(true);
});

it("Modify .gitignore file if exists", () => {
const pattern = "srs/*\n.sds/*";
createFileWithGivenPath(C4Z_FOLDER, GITIGNORE_FILE, pattern);
const found = fs
.readFileSync(filePath)
.toString()
.split("\n")
.filter((e) => e.trim().length > 0)
.map((e) => e.trim())
.indexOf(pattern);

expect(found).toBeGreaterThanOrEqual(-1);
});

it("workspace not exist", () => {
(vscode.workspace.workspaceFolders as any) = [];
const createFile = jest.fn();
createFileWithGivenPath(C4Z_FOLDER, GITIGNORE_FILE, "/**");

expect(createFile).toHaveBeenCalledTimes(0);
expect(vscode.workspace.workspaceFolders![0]).toBe(undefined);
});
});
function makefsPath(p: string): string {
return path.join(process.platform == "win32" ? "a:" : "", p);
}
function makePath(p: string): string {
return (process.platform == "win32" ? "/a:" : "") + p;
}

describe("SettingsService evaluate variables", () => {
beforeAll(() => {
(vscode.workspace.workspaceFolders as vscode.WorkspaceFolder[]) = [
{
uri: {
path: makePath("/tmp-ws"),
scheme: "",
authority: "",
query: "",
fragment: "",
fsPath: makefsPath("/tmp-ws"),
with: function (): vscode.Uri {
throw new Error("Function not implemented.");
},
toJSON: function () {
throw new Error("Function not implemented.");
},
},
name: "workspace",
index: 0,
},
];
});
test("Evaluate fileBasenameNoExtension", () => {
vscode.workspace.getConfiguration = jest.fn().mockReturnValue({
get: jest.fn().mockReturnValue(["copybook/${fileBasenameNoExtension}"]),
Expand All @@ -96,7 +73,7 @@ describe("SettingsService evaluate variables", () => {
"file:///program",
"COBOL",
);
expect(paths[0]).toEqual("copybook/program");
expect(paths[0]).toEqual(makefsPath("/tmp-ws/copybook/program"));
});

test("Evaluate fileBasenameNoExtension", () => {
Expand All @@ -107,18 +84,19 @@ describe("SettingsService evaluate variables", () => {
"file:///program.cbl",
"COBOL",
);
expect(paths[0]).toEqual("copybook/program");
expect(paths[0]).toEqual(makefsPath("/tmp-ws/copybook/program"));
});

test("Evaluate fileBasenameNoExtension with extension and dots", () => {
vscode.workspace.getConfiguration = jest.fn().mockReturnValue({
get: jest.fn().mockReturnValue(["copybook/${fileBasenameNoExtension}"]),
});

const paths = SettingsService.getCopybookLocalPath(
"file:///program.file.cbl",
"COBOL",
);
expect(paths[0]).toEqual("copybook/program.file");
expect(paths[0]).toEqual(makefsPath("/tmp-ws/copybook/program.file"));
});

test("Get local settings for a dialect", () => {
Expand Down Expand Up @@ -156,7 +134,6 @@ test("getWorkspaceFoldersPath return an array of paths", () => {
const paths = SettingsUtils.getWorkspaceFoldersPath();
expect(paths).toStrictEqual(["/ws-vscode"]);
});

test("json validation", () => {
expect(SettingsUtils.isValidJSON(undefined)).toBeFalsy();
expect(SettingsUtils.isValidJSON("{}")).toBeTruthy();
Expand Down Expand Up @@ -240,3 +217,26 @@ describe("SettingsService returns correct Copybook Configuration Values", () =>
expect(configuredValue[0]).toBe("configured-cobol-settings");
});
});
describe("SettingsService prepares local search folders", () => {
test("returns all paths are transformed into absolutes", () => {
const paths = [makefsPath("/absolute"), "relative"];
expect(
SettingsService.prepareLocalSearchFolders(paths, [
makefsPath("/workspacePath"),
]),
).toEqual([makefsPath("/absolute"), makefsPath("/workspacePath/relative")]);
});
test("all workspace paths concatanated into relative paths", () => {
const paths = [makefsPath("/absolute"), "relative"];
expect(
SettingsService.prepareLocalSearchFolders(paths, [
makefsPath("/workspacePath"),
makefsPath("/workspacePath2"),
]),
).toEqual([
makefsPath("/absolute"),
makefsPath("/workspacePath/relative"),
makefsPath("/workspacePath2/relative"),
]);
});
});
Loading
Loading