Skip to content

Commit

Permalink
Don't trigger profile conversion if only meta files exist
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Johnson <[email protected]>
  • Loading branch information
t1m0thyj committed Nov 5, 2024
1 parent 04fdf7e commit b141946
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 51 deletions.
28 changes: 24 additions & 4 deletions packages/imperative/src/config/__tests__/ConfigUtils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import * as jsonfile from "jsonfile";
import * as glob from "fast-glob";
import { ConfigUtils } from "../../config/src/ConfigUtils";
import { CredentialManagerFactory } from "../../security";
import { ImperativeConfig } from "../../utilities";
Expand Down Expand Up @@ -132,10 +133,29 @@ describe("Config Utils", () => {
})
} as any);

const fsExistsSyncSpy = jest.spyOn(fs, "existsSync").mockReturnValueOnce(false);
const globSyncSpy = jest.spyOn(glob, "sync").mockReturnValueOnce([]);

expect(ConfigUtils.onlyV1ProfilesExist).toBe(false);
expect(fsExistsSyncSpy).toHaveBeenCalledTimes(1);
expect(globSyncSpy).toHaveBeenCalledTimes(1);
});

it("should return false when only V1 profile meta files exist", () => {
jest.spyOn(ImperativeConfig, "instance", "get").mockReturnValue({
config: {
exists: false
},
cliHome: "/fake/cli/home/dir",
loadedConfig: jest.fn(() => {
return {
envVariablePrefix: "Fake_cli_prefix"
};
})
} as any);

const globSyncSpy = jest.spyOn(glob, "sync").mockReturnValueOnce(["profiles/zosmf/zosmf_meta.yaml"]);

expect(ConfigUtils.onlyV1ProfilesExist).toBe(false);
expect(globSyncSpy).toHaveBeenCalledTimes(1);
});

it("should return true when only V1 profiles exist", () => {
Expand All @@ -151,10 +171,10 @@ describe("Config Utils", () => {
})
} as any);

const fsExistsSyncSpy = jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
const globSyncSpy = jest.spyOn(glob, "sync").mockReturnValueOnce(["profiles/zosmf/lpar1.yaml"]);

expect(ConfigUtils.onlyV1ProfilesExist).toBe(true);
expect(fsExistsSyncSpy).toHaveBeenCalledTimes(1);
expect(globSyncSpy).toHaveBeenCalledTimes(1);
});
});

Expand Down
41 changes: 18 additions & 23 deletions packages/imperative/src/config/src/ConfigUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
*
*/

import { homedir as osHomedir } from "os";
import { normalize as pathNormalize, join as pathJoin } from "path";
import { existsSync as fsExistsSync } from "fs";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as glob from "fast-glob";
import * as jsonfile from "jsonfile";

import { CredentialManagerFactory } from "../../security/src/CredentialManagerFactory";
Expand All @@ -32,7 +33,7 @@ export class ConfigUtils {
* @returns {string} - Returns the Zowe home directory
*/
public static getZoweDir(): string {
const defaultHome = pathJoin(osHomedir(), ".zowe");
const defaultHome = path.join(os.homedir(), ".zowe");
if (ImperativeConfig.instance.loadedConfig?.defaultHome !== defaultHome) {
ImperativeConfig.instance.loadedConfig = {
name: "zowe",
Expand All @@ -52,8 +53,8 @@ export class ConfigUtils {
*/
public static readExtendersJson(): IExtendersJsonOpts {
const cliHome = ImperativeConfig.instance.loadedConfig != null ? ImperativeConfig.instance.cliHome : ConfigUtils.getZoweDir();
const extenderJsonPath = pathJoin(cliHome, "extenders.json");
if (!fsExistsSync(extenderJsonPath)) {
const extenderJsonPath = path.join(cliHome, "extenders.json");
if (!fs.existsSync(extenderJsonPath)) {
jsonfile.writeFileSync(extenderJsonPath, {
profileTypes: {}
}, { spaces: 4 });
Expand All @@ -70,7 +71,7 @@ export class ConfigUtils {
*/
public static writeExtendersJson(obj: IExtendersJsonOpts): boolean {
try {
const extenderJsonPath = pathJoin(ConfigUtils.getZoweDir(), "extenders.json");
const extenderJsonPath = path.join(ConfigUtils.getZoweDir(), "extenders.json");
jsonfile.writeFileSync(extenderJsonPath, obj, { spaces: 4 });
} catch (err) {
return false;
Expand Down Expand Up @@ -144,20 +145,14 @@ export class ConfigUtils {
return false;
}

let v1ZosmfProfileFileNm: string;
try {
v1ZosmfProfileFileNm = pathNormalize(ImperativeConfig.instance.cliHome + "/profiles/zosmf/zosmf_meta.yaml");
} catch (_thrownErr) {
// We failed to get the CLI home directory. So, we definitely have no V1 profiles.
return false;
}

if (fsExistsSync(v1ZosmfProfileFileNm)) {
// we found V1 profiles
return true;
}

return false;
// look for V1 profiles in the CLI home directory
const v1ProfilePaths = glob.sync("profiles/**/*.yaml", { cwd: ImperativeConfig.instance.cliHome })
.filter(filename => {
// ignore meta yaml files
const { dir, name } = path.parse(filename);
return name !== path.basename(dir) + "_meta";
});
return v1ProfilePaths.length > 0;
}

/**
Expand Down Expand Up @@ -190,10 +185,10 @@ export class ConfigUtils {
const envVarNm = envVarPrefix + EnvironmentalVariableSettings.CLI_HOME_SUFFIX;
if (process.env[envVarNm] === undefined) {
// use OS home directory
homeDir = pathJoin(osHomedir(), "." + appName.toLowerCase());
homeDir = path.join(os.homedir(), "." + appName.toLowerCase());
} else {
// use the available environment variable
homeDir = pathNormalize(process.env[envVarNm]);
homeDir = path.normalize(process.env[envVarNm]);
}
ImperativeConfig.instance.loadedConfig = {
name: appName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1730,14 +1730,10 @@ describe("ConnectionPropsForSessCfg tests", () => {

it("should state that you have no zowe config file", async () => {
// Pretend that we do not have a zowe config.
Object.defineProperty(ImperativeConfig.instance, "config", {
configurable: true,
get: jest.fn(() => {
return {
exists: false,
};
}),
});
jest.spyOn(ImperativeConfig, "instance", "get").mockReturnValue({
config: { exists: false },
cliHome: "/fake/cli/home/dir",
} as any);

// call the function that we want to test
await getValuesCallBack(["hostname"]);
Expand All @@ -1755,14 +1751,9 @@ describe("ConnectionPropsForSessCfg tests", () => {

it("should state that V1 profiles are not supported", async () => {
// Pretend that we do not have a zowe config.
Object.defineProperty(ImperativeConfig.instance, "config", {
configurable: true,
get: jest.fn(() => {
return {
exists: false,
};
}),
});
jest.spyOn(ImperativeConfig, "instance", "get").mockReturnValue({
config: { exists: false },
} as any);

/* Pretend that we only have V1 profiles.
* onlyV1ProfilesExist is a getter property, so mock the property.
Expand Down Expand Up @@ -1790,14 +1781,9 @@ describe("ConnectionPropsForSessCfg tests", () => {

it("should state that connection properties are missing from config", async () => {
// Pretend that we have a zowe config.
Object.defineProperty(ImperativeConfig.instance, "config", {
configurable: true,
get: jest.fn(() => {
return {
exists: true,
};
}),
});
jest.spyOn(ImperativeConfig, "instance", "get").mockReturnValue({
config: { exists: true },
} as any);

/* Pretend that we do not have any V1 profiles.
* onlyV1ProfilesExist is a getter property, so mock the property.
Expand Down

0 comments on commit b141946

Please sign in to comment.