Skip to content

Commit 4e5fc32

Browse files
authored
Merge pull request #773 from ap891843/feature/supportRelativepathCpyBook
feat: Support copybooks outside the current workspace #755
2 parents 5817335 + 0d3baa6 commit 4e5fc32

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

clients/cobol-lsp-vscode-extension/src/__tests__/CopybookResolveURITest.spec.ts

+29-14
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414
import * as fs from "fs-extra";
1515
import * as path from "path";
1616
import * as vscode from "vscode";
17-
import {CopybookURI} from "../services/copybook/CopybookURI";
18-
import {ProfileService} from "../services/ProfileService";
19-
import {SettingsUtils} from "../services/util/SettingsUtils";
20-
import {ZoweApi} from "../services/ZoweApi";
17+
import { CopybookURI } from "../services/copybook/CopybookURI";
18+
import { ProfileService } from "../services/ProfileService";
19+
import { SettingsUtils } from "../services/util/SettingsUtils";
20+
import { ZoweApi } from "../services/ZoweApi";
2121
import * as fsUtils from "../services/util/FSUtils";
22-
import {COPYBOOK_EXT_ARRAY} from "../constants";
22+
import { COPYBOOK_EXT_ARRAY } from "../constants";
2323

2424
const zoweApi: ZoweApi = new ZoweApi();
2525
const profileService: ProfileService = new ProfileService(zoweApi);
2626
const copybookURI: CopybookURI = new CopybookURI(profileService);
2727
const copybookName: string = "NSTCOPY1";
2828
const copybookNameWithExtension: string = "NSTCOPY2.CPY";
2929
const CPY_FOLDER_NAME = ".cobcopy";
30+
const RELATIVE_CPY_FOLDER_NAME = "../relativeCobcopy";
3031
const folderPath = path.join(__dirname, CPY_FOLDER_NAME);
3132
SettingsUtils.getWorkspacesURI = jest.fn().mockReturnValue(["file://" + __dirname]);
3233
profileService.resolveProfile = jest.fn().mockReturnValue("PRF");
@@ -35,7 +36,7 @@ vscode.workspace.getConfiguration = jest.fn().mockReturnValue({
3536
});
3637

3738
// file utils
38-
function createFile(filename: string): string {
39+
function createFile(filename: string, folderPath: string): string {
3940
fs.writeFileSync(path.join(folderPath, filename), "Some dummy content", err => {
4041
if (err) {
4142
return null;
@@ -46,13 +47,17 @@ function createFile(filename: string): string {
4647

4748
function createDirectory(targetPath: string) {
4849

49-
fs.mkdirSync(targetPath, {recursive: true});
50+
fs.mkdirSync(targetPath, { recursive: true });
5051
}
5152

5253
function removeFolder(pathFile: string) {
5354
fs.remove(pathFile);
5455
}
5556

57+
function removeNonEmptyFolder(targetPath: string) {
58+
fs.rmdirSync(targetPath, { recursive: true });
59+
}
60+
5661
function buildResultArrayFrom(settingsMockValue: string[], profileName: string): number {
5762
vscode.workspace.getConfiguration = jest.fn().mockReturnValue({
5863
get: jest.fn().mockReturnValue(settingsMockValue),
@@ -65,8 +70,8 @@ beforeEach(() => {
6570
});
6671
beforeAll(() => {
6772
createDirectory(folderPath);
68-
createFile(copybookName);
69-
createFile(copybookNameWithExtension);
73+
createFile(copybookName, folderPath);
74+
createFile(copybookNameWithExtension, folderPath);
7075
});
7176
afterAll(() => {
7277
return removeFolder(folderPath);
@@ -95,6 +100,16 @@ describe("Resolve local copybook present in one or more folders specified by the
95100
test("Given a copybook with extension on filesystem, the uri is correctly returned", () => {
96101
expect(fsUtils.searchInWorkspace("NSTCOPY2", [CPY_FOLDER_NAME], COPYBOOK_EXT_ARRAY)).toBeDefined();
97102
});
103+
test("Given a valid relative path for copybook with extension on filesystem, the uri is correctly returned", () => {
104+
const dir = path.join(__dirname, RELATIVE_CPY_FOLDER_NAME);
105+
createDirectory(dir);
106+
createFile(copybookNameWithExtension, dir);
107+
expect(fsUtils.searchInWorkspace("NSTCOPY2", [RELATIVE_CPY_FOLDER_NAME], COPYBOOK_EXT_ARRAY)).toBeDefined();
108+
removeNonEmptyFolder(dir);
109+
});
110+
test("Given a valid absolute path for copybook with extension on filesystem, the uri is correctly returned", () => {
111+
expect(fsUtils.searchInWorkspace("NSTCOPY2", [path.normalize(folderPath)], COPYBOOK_EXT_ARRAY)).toBeDefined();
112+
});
98113
});
99114
describe("With invalid input parameters, the list of URI that represent copybook downloaded are not generated", () => {
100115
test("given a profile but no dataset, the result list returned is empty", () => {
@@ -136,9 +151,9 @@ describe("Prioritize search criteria for copybooks test suite", () => {
136151
});
137152
test("With both local and dsn references defined in the settings.json, the search is applied on local resources" +
138153
"first", async () => {
139-
provideMockValueForLocalAndDSN(CPY_FOLDER_NAME, "");
140-
const uri: string = await copybookURI.resolveCopybookURI(copybookName, "PRGNAME");
141-
expect(uri).not.toBe("");
142-
expect(spySearchInWorkspace).toBeCalledTimes(1);
143-
});
154+
provideMockValueForLocalAndDSN(CPY_FOLDER_NAME, "");
155+
const uri: string = await copybookURI.resolveCopybookURI(copybookName, "PRGNAME");
156+
expect(uri).not.toBe("");
157+
expect(spySearchInWorkspace).toBeCalledTimes(1);
158+
});
144159
});

clients/cobol-lsp-vscode-extension/src/services/util/FSUtils.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
* Broadcom, Inc. - initial API and implementation
1313
*/
1414

15-
import {URL} from "url";
15+
import * as urlUtil from "url";
1616
import * as path from "path";
17-
import {readdirSync, existsSync} from "fs";
18-
import {SettingsUtils} from "./SettingsUtils";
17+
import { readdirSync, existsSync } from "fs";
18+
import { SettingsUtils } from "./SettingsUtils";
1919
import * as fs from "fs";
2020

2121
/**
@@ -27,19 +27,19 @@ import * as fs from "fs";
2727
* this URI really exists on FS.
2828
*
2929
*/
30-
export function getURIFrom(folder: string, entityName: string, extensions?: string[]): URL {
30+
export function getURIFrom(folder: string, entityName: string, extensions?: string[]): urlUtil.URL {
3131
if (!extensions) {
32-
const url = new URL(path.join(folder, entityName));
32+
const url = new urlUtil.URL(path.join(folder, entityName));
3333
if (existsSync(url)) {
3434
return url;
3535
}
3636
} else {
37-
const fileList = readdirSync(new URL(folder));
37+
const fileList = readdirSync(new urlUtil.URL(folder));
3838
for (const extension of extensions) {
3939
const copybookFileWithExtension = (entityName + extension).toUpperCase();
4040
const found = fileList.find(filename => filename.toUpperCase() === copybookFileWithExtension.toUpperCase())
4141
if (found) {
42-
return new URL(path.join(folder, found));
42+
return new urlUtil.URL(path.join(folder, found));
4343
}
4444
}
4545
}
@@ -50,9 +50,11 @@ export function getURIFrom(folder: string, entityName: string, extensions?: stri
5050
* @param resource represent the file to search within the workspace folder list
5151
* @return an URI representation of the file or undefined if not found
5252
*/
53-
export function getURIFromResource(resource: string): URL {
53+
export function getURIFromResource(resource: string): urlUtil.URL {
5454
for (const workspaceFolder of SettingsUtils.getWorkspacesURI()) {
55-
const uri: URL = new URL(path.join(workspaceFolder, resource));
55+
const uri: urlUtil.URL = (path.resolve(resource) === path.normalize(resource))
56+
? urlUtil.pathToFileURL(resource) :
57+
new urlUtil.URL(path.normalize(path.join(workspaceFolder, resource)));
5658
if (fs.existsSync(uri)) {
5759
return uri;
5860
}
@@ -70,10 +72,10 @@ export function searchInWorkspace(entityName: string, targetFolders: string[], e
7072
if (targetFolders) {
7173
const localFolderList: string[] = targetFolders
7274
.map(getURIFromResource)
73-
.filter((url: URL) => url !== undefined)
74-
.map((url: URL) => url.href);
75+
.filter((url: urlUtil.URL) => url !== undefined)
76+
.map((url: urlUtil.URL) => url.href);
7577
for (const folder of localFolderList) {
76-
let uri: URL = getURIFrom(folder, entityName);
78+
let uri: urlUtil.URL = getURIFrom(folder, entityName);
7779
if (!uri) {
7880
uri = getURIFrom(folder, entityName, extensions);
7981
}

0 commit comments

Comments
 (0)