Skip to content

Commit

Permalink
Add Sonar scan properties and remove old system test files
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
  • Loading branch information
t1m0thyj committed Oct 7, 2024
1 parent 785a7ee commit 28109f7
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 203 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ out
lib
!packages/eslint-plugin-zowe-explorer/**/lib
logs
testProfileData.ts
results
*.log
npm-shrinkwrap.json
Expand Down
4 changes: 4 additions & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sonar.sources=packages/**/src
sonar.tests=packages/**/__tests__
sonar.test.inclusions=**/*.test.ts,**/*.steps.ts
sonar.cpd.exclusions=packages/**/__tests__
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,180 @@ describe("ProfilesUtils unit tests", () => {
});
});

describe("initializeZoweFolder.2", () => {
it("should create directories and files that do not exist", async () => {
const blockMocks = createBlockMocks();
blockMocks.mockGetDirectValue.mockReturnValue(true);
blockMocks.mockExistsSync.mockReturnValue(false);
jest.spyOn(fs, "readFileSync").mockReturnValue(Buffer.from(JSON.stringify({ overrides: { credentialManager: "@zowe/cli" } }), "utf-8"));
const createFileSpy = jest.spyOn(ProfilesUtils, "writeOverridesFile");
await ProfilesUtils.initializeZoweFolder();
expect(ProfilesUtils.PROFILE_SECURITY).toBe(Constants.ZOWE_CLI_SCM);
expect(blockMocks.mockMkdirSync).toHaveBeenCalledTimes(2);
expect(createFileSpy).toHaveBeenCalledTimes(1);
});

it("should skip creating directories and files that already exist", async () => {
const blockMocks = createBlockMocks();
jest.spyOn(ProfilesUtils, "getCredentialManagerOverride").mockReturnValue("@zowe/cli");
blockMocks.mockGetDirectValue.mockReturnValue("@zowe/cli");
blockMocks.mockExistsSync.mockReturnValue(true);
const fileJson = blockMocks.mockFileRead;
jest.spyOn(fs, "readFileSync").mockReturnValue(Buffer.from(JSON.stringify({ overrides: { credentialManager: "@zowe/cli" } }), "utf-8"));
blockMocks.mockReadFileSync.mockReturnValueOnce(JSON.stringify(fileJson, null, 2));
await ProfilesUtils.initializeZoweFolder();
expect(ProfilesUtils.PROFILE_SECURITY).toBe("@zowe/cli");
expect(blockMocks.mockMkdirSync).toHaveBeenCalledTimes(0);
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledTimes(0);
});
});

describe("writeOverridesFile.2", () => {
it("should have file exist", () => {
const blockMocks = createBlockMocks();
blockMocks.mockReadFileSync.mockReturnValueOnce(
JSON.stringify({ overrides: { CredentialManager: "@zowe/cli", testValue: true } }, null, 2)
);
ProfilesUtils.writeOverridesFile();
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledTimes(0);
});

it("should return and have no change to the existing file if PROFILE_SECURITY matches file", () => {
const blockMocks = createBlockMocks();
const fileJson = blockMocks.mockFileRead;
blockMocks.mockReadFileSync.mockReturnValueOnce(JSON.stringify(fileJson, null, 2));
ProfilesUtils.writeOverridesFile();
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledTimes(0);
});

it("should add credential manager overrides object to existing object", () => {
const blockMocks = createBlockMocks();
const fileJson = {
test: null,
};
blockMocks.mockReadFileSync.mockReturnValueOnce(JSON.stringify(fileJson, null, 2));
const mergedJson = { ...blockMocks.mockFileRead, ...fileJson };
const mergedString = JSON.stringify(mergedJson, null, 2);
ProfilesUtils.writeOverridesFile();
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledTimes(1);
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledWith(blockMocks.zoweDir, mergedString, { encoding: "utf-8", flag: "w" });
});

it("should have not exist and create default file", () => {
const blockMocks = createBlockMocks();
Object.defineProperty(fs, "readFileSync", {
value: jest.fn().mockImplementationOnce(() => {
throw new Error("ENOENT");
}),
configurable: true,
});
const loggerSpy = jest.spyOn(ZoweLogger, "debug");
const content = JSON.stringify(blockMocks.mockFileRead, null, 2);
ProfilesUtils.writeOverridesFile();
expect(loggerSpy).toHaveBeenCalledWith("Reading imperative.json failed. Will try to create file.");
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledWith(blockMocks.zoweDir, content, { encoding: "utf-8", flag: "w" });
expect(blockMocks.mockWriteFileSync).not.toThrow();
});

it("should re-create file if overrides file contains invalid JSON", () => {
const blockMocks = createBlockMocks();
const fileJson = {
test: null,
};
blockMocks.mockReadFileSync.mockReturnValueOnce(JSON.stringify(fileJson, null, 2).slice(1));
const writeFileSpy = jest.spyOn(fs, "writeFileSync");
expect(ProfilesUtils.writeOverridesFile).not.toThrow();
expect(writeFileSpy).toHaveBeenCalled();
});
});

describe("initializeZoweFolder.3", () => {
it("should create directories and files that do not exist", async () => {
const blockMocks = createBlockMocks();
blockMocks.mockGetDirectValue.mockReturnValue(true);
blockMocks.mockExistsSync.mockReturnValue(false);
jest.spyOn(fs, "readFileSync").mockReturnValue(Buffer.from(JSON.stringify({ overrides: { credentialManager: "@zowe/cli" } }), "utf-8"));
const createFileSpy = jest.spyOn(ProfilesUtils, "writeOverridesFile");
await ProfilesUtils.initializeZoweFolder();
expect(ProfilesUtils.PROFILE_SECURITY).toBe(Constants.ZOWE_CLI_SCM);
expect(blockMocks.mockMkdirSync).toHaveBeenCalledTimes(2);
expect(createFileSpy).toHaveBeenCalledTimes(1);
});

it("should skip creating directories and files that already exist", async () => {
const blockMocks = createBlockMocks();
jest.spyOn(ProfilesUtils, "getCredentialManagerOverride").mockReturnValue("@zowe/cli");
blockMocks.mockGetDirectValue.mockReturnValue("@zowe/cli");
blockMocks.mockExistsSync.mockReturnValue(true);
const fileJson = blockMocks.mockFileRead;
jest.spyOn(fs, "readFileSync").mockReturnValue(Buffer.from(JSON.stringify({ overrides: { credentialManager: "@zowe/cli" } }), "utf-8"));
blockMocks.mockReadFileSync.mockReturnValueOnce(JSON.stringify(fileJson, null, 2));
await ProfilesUtils.initializeZoweFolder();
expect(ProfilesUtils.PROFILE_SECURITY).toBe("@zowe/cli");
expect(blockMocks.mockMkdirSync).toHaveBeenCalledTimes(0);
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledTimes(0);
});
});

describe("writeOverridesFile.3", () => {
it("should have file exist", () => {
const blockMocks = createBlockMocks();
blockMocks.mockReadFileSync.mockReturnValueOnce(
JSON.stringify({ overrides: { CredentialManager: "@zowe/cli", testValue: true } }, null, 2)
);
ProfilesUtils.writeOverridesFile();
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledTimes(0);
});

it("should return and have no change to the existing file if PROFILE_SECURITY matches file", () => {
const blockMocks = createBlockMocks();
const fileJson = blockMocks.mockFileRead;
blockMocks.mockReadFileSync.mockReturnValueOnce(JSON.stringify(fileJson, null, 2));
ProfilesUtils.writeOverridesFile();
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledTimes(0);
});

it("should add credential manager overrides object to existing object", () => {
const blockMocks = createBlockMocks();
const fileJson = {
test: null,
};
blockMocks.mockReadFileSync.mockReturnValueOnce(JSON.stringify(fileJson, null, 2));
const mergedJson = { ...blockMocks.mockFileRead, ...fileJson };
const mergedString = JSON.stringify(mergedJson, null, 2);
ProfilesUtils.writeOverridesFile();
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledTimes(1);
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledWith(blockMocks.zoweDir, mergedString, { encoding: "utf-8", flag: "w" });
});

it("should have not exist and create default file", () => {
const blockMocks = createBlockMocks();
Object.defineProperty(fs, "readFileSync", {
value: jest.fn().mockImplementationOnce(() => {
throw new Error("ENOENT");
}),
configurable: true,
});
const loggerSpy = jest.spyOn(ZoweLogger, "debug");
const content = JSON.stringify(blockMocks.mockFileRead, null, 2);
ProfilesUtils.writeOverridesFile();
expect(loggerSpy).toHaveBeenCalledWith("Reading imperative.json failed. Will try to create file.");
expect(blockMocks.mockWriteFileSync).toHaveBeenCalledWith(blockMocks.zoweDir, content, { encoding: "utf-8", flag: "w" });
expect(blockMocks.mockWriteFileSync).not.toThrow();
});

it("should re-create file if overrides file contains invalid JSON", () => {
const blockMocks = createBlockMocks();
const fileJson = {
test: null,
};
blockMocks.mockReadFileSync.mockReturnValueOnce(JSON.stringify(fileJson, null, 2).slice(1));
const writeFileSpy = jest.spyOn(fs, "writeFileSync");
expect(ProfilesUtils.writeOverridesFile).not.toThrow();
expect(writeFileSpy).toHaveBeenCalled();
});
});

describe("initializeZoweProfiles", () => {
it("should successfully initialize Zowe folder and read config from disk", async () => {
const initZoweFolderSpy = jest.spyOn(ProfilesUtils, "initializeZoweFolder");
Expand Down
4 changes: 1 addition & 3 deletions packages/zowe-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1793,10 +1793,8 @@
"fresh-clone": "pnpm clean:bundle && pnpm clean:dependencies",
"madge": "madge -c --no-color --no-spinner --exclude __mocks__ --extensions js,ts src/",
"pretty": "prettier --write .",
"createTestProfileData": "tsx ./scripts/createTestProfileData.ts",
"createDemoNodes": "tsx ./scripts/createDemoNodes.ts",
"generateLocalization": "pnpm dlx @vscode/l10n-dev export --o ./l10n ./src && node ./scripts/generatePoeditorJson.js",
"copy-secrets": "tsx ./scripts/getSecretsPrebuilds.ts",
"copy-secrets": "node ./scripts/getSecretsPrebuilds.js",
"strip-l10n": "node ./scripts/stripL10nComments.js"
},
"engines": {
Expand Down
5 changes: 0 additions & 5 deletions packages/zowe-explorer/scripts/createDemoNodes.ts

This file was deleted.

26 changes: 0 additions & 26 deletions packages/zowe-explorer/scripts/createTestProfileData.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copySync } from "fs-extra";
import { join, resolve } from "path";
const { copySync } = require("fs-extra");
const { join, resolve } = require("path");
const secretsPkgDir = resolve(require.resolve("@zowe/secrets-for-zowe-sdk"), "..", "..");
copySync(join(secretsPkgDir, "prebuilds"), resolve(__dirname, "..", "prebuilds"));
copySync(join(secretsPkgDir, "prebuilds"), resolve(__dirname, "..", "prebuilds"));
4 changes: 1 addition & 3 deletions packages/zowe-explorer/tsconfig-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"__tests__/__unit__",
"__tests__/__integration__/bdd/step_definitions/**/*.ts",
// Needed for integration tests to work:
"__tests__/__integration__/tdd",
"resources/testProfileData.example.ts",
"resources/testProfileData.ts"
"__tests__/__integration__/tdd"
]
}
Loading

0 comments on commit 28109f7

Please sign in to comment.