Skip to content

Commit

Permalink
refactor: update folder strucutre (#11930)
Browse files Browse the repository at this point in the history
* refactor: update folder structure for progressHandler and telemetry

* refactor: update handlers folder strucuture

* test: update ut

* test: fix ut
  • Loading branch information
yiqing-zhao authored Jul 3, 2024
1 parent 382da7e commit b164389
Show file tree
Hide file tree
Showing 33 changed files with 445 additions and 219 deletions.
1 change: 0 additions & 1 deletion packages/vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,6 @@
"test-watch": "tsc -watch -p ./",
"pretest": "npm run lint && npm run check-format && npm run test-compile",
"test:unit": "rimraf out && rimraf coverage && npm run compile && nyc mocha --config .mocharc.json \"test/**/*.test.ts\"",
"test:manifest": "rimraf out && rimraf coverage && npm run compile && nyc mocha --config .mocharc.json \"test/handlers/manifestHandlers.test.ts\"",
"test:integration": "echo 'to be implementd'",
"test:e2e": "echo 'to be implementd'",
"check-format": "prettier --list-different --config .prettierrc.js --ignore-path .prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode-extension/src/debug/depsChecker/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import M365TokenInstance from "../../commonlib/m365Login";
import { ExtensionErrors, ExtensionSource } from "../../error/error";
import { VS_CODE_UI } from "../../qm/vsc_ui";
import { tools, workspaceUri } from "../../globalVariables";
import { checkCopilotCallback } from "../../handlers/checkAccessCallback";
import { ProgressHandler } from "../../progressHandler";
import { checkCopilotCallback } from "../../handlers/accounts/checkAccessCallback";
import { ProgressHandler } from "../progressHandler";
import { ExtTelemetry } from "../../telemetry/extTelemetry";
import { TelemetryEvent, TelemetryProperty } from "../../telemetry/extTelemetryEvents";
import { getDefaultString, localize } from "../../utils/localizeUtils";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as util from "util";
import { ProgressLocation, window } from "vscode";

import { IProgressHandler, ok } from "@microsoft/teamsfx-api";
import { localize } from "./utils/localizeUtils";
import { localize } from "../utils/localizeUtils";

export class ProgressHandler implements IProgressHandler {
private totalSteps: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import VsCodeLogInstance from "../../commonlib/log";
import { ExtensionErrors, ExtensionSource } from "../../error/error";
import * as globalVariables from "../../globalVariables";
import { ProgressHandler } from "../../progressHandler";
import { ProgressHandler } from "../progressHandler";
import {
TelemetryEvent,
TelemetryProperty,
Expand Down
13 changes: 8 additions & 5 deletions packages/vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ import {
azureAccountSignOutHelpHandler,
cmpAccountsHandler,
createAccountHandler,
} from "./handlers/accountHandlers";
} from "./handlers/accounts/accountHandlers";
import { activate as activateHandlers } from "./handlers/activate";
import { autoOpenProjectHandler } from "./handlers/autoOpenProjectHandler";
import { checkCopilotCallback, checkSideloadingCallback } from "./handlers/checkAccessCallback";
import { checkCopilotAccessHandler } from "./handlers/checkCopilotAccess";
import {
checkCopilotCallback,
checkSideloadingCallback,
} from "./handlers/accounts/checkAccessCallback";
import { checkCopilotAccessHandler } from "./handlers/accounts/checkCopilotAccess";
import { manageCollaboratorHandler } from "./handlers/collaboratorHandlers";
import {
openFolderHandler,
Expand Down Expand Up @@ -158,9 +161,9 @@ import { openReadMeHandler } from "./handlers/readmeHandlers";
import {
refreshCopilotCallback,
refreshSideloadingCallback,
} from "./handlers/refreshAccessHandlers";
} from "./handlers/accounts/refreshAccessHandlers";
import { showOutputChannelHandler } from "./handlers/showOutputChannel";
import { signinAzureCallback, signinM365Callback } from "./handlers/signinAccountHandlers";
import { signinAzureCallback, signinM365Callback } from "./handlers/accounts/signinAccountHandlers";
import { openTutorialHandler, selectTutorialsHandler } from "./handlers/tutorialHandlers";
import {
createProjectFromWalkthroughHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import { QuickPickItem, window } from "vscode";
import { FxError, OptionItem, Result, SingleSelectConfig, ok } from "@microsoft/teamsfx-api";
import { Correlator, AppStudioScopes } from "@microsoft/teamsfx-core";
import { ExtTelemetry } from "../telemetry/extTelemetry";
import { AccountType, TelemetryEvent, TelemetryProperty } from "../telemetry/extTelemetryEvents";
import { signInAzure, signOutAzure, signInM365, signOutM365 } from "../utils/accountUtils";
import { localize } from "../utils/localizeUtils";
import { getTriggerFromProperty } from "../utils/telemetryUtils";
import azureAccountManager from "../commonlib/azureLogin";
import M365TokenInstance from "../commonlib/m365Login";
import { VS_CODE_UI } from "../qm/vsc_ui";
import { ExtTelemetry } from "../../telemetry/extTelemetry";
import { AccountType, TelemetryEvent, TelemetryProperty } from "../../telemetry/extTelemetryEvents";
import { signInAzure, signOutAzure, signInM365, signOutM365 } from "../../utils/accountUtils";
import { localize } from "../../utils/localizeUtils";
import { getTriggerFromProperty } from "../../utils/telemetryUtils";
import azureAccountManager from "../../commonlib/azureLogin";
import M365TokenInstance from "../../commonlib/m365Login";
import { VS_CODE_UI } from "../../qm/vsc_ui";

export interface VscQuickPickItem extends QuickPickItem {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
// Licensed under the MIT license.

import { Result, FxError, ok } from "@microsoft/teamsfx-api";
import { localize } from "../utils/localizeUtils";
import { VS_CODE_UI } from "../qm/vsc_ui";
import { ExtTelemetry } from "../telemetry/extTelemetry";
import { localize } from "../../utils/localizeUtils";
import { VS_CODE_UI } from "../../qm/vsc_ui";
import { ExtTelemetry } from "../../telemetry/extTelemetry";
import {
TelemetryEvent,
TelemetryProperty,
TelemetryTriggerFrom,
} from "../telemetry/extTelemetryEvents";
import { WebviewPanel } from "../controls/webviewPanel";
import { PanelType } from "../controls/PanelType";
} from "../../telemetry/extTelemetryEvents";
import { WebviewPanel } from "../../controls/webviewPanel";
import { PanelType } from "../../controls/PanelType";

export async function checkCopilotCallback(args?: any[]): Promise<Result<null, FxError>> {
VS_CODE_UI.showMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
// Licensed under the MIT license.

import * as vscode from "vscode";
import M365TokenInstance from "../commonlib/m365Login";
import { signedIn } from "../commonlib/common/constant";
import { localize } from "../utils/localizeUtils";
import VsCodeLogInstance from "../commonlib/log";
import M365TokenInstance from "../../commonlib/m365Login";
import { signedIn } from "../../commonlib/common/constant";
import { localize } from "../../utils/localizeUtils";
import VsCodeLogInstance from "../../commonlib/log";
import { FxError, Result, err, ok } from "@microsoft/teamsfx-api";
import {
AppStudioScopes,
MosServiceScope,
PackageService,
SummaryConstant,
} from "@microsoft/teamsfx-core";
import { wrapError } from "../error/common";
import { signInM365 } from "../utils/accountUtils";
import { wrapError } from "../../error/common";
import { signInM365 } from "../../utils/accountUtils";

export async function checkCopilotAccessHandler(): Promise<Result<null, FxError>> {
// check m365 login status, if not logged in, pop up a message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import { Result, FxError, ok } from "@microsoft/teamsfx-api";
import { AppStudioScopes } from "@microsoft/teamsfx-core";
import accountTreeViewProviderInstance from "../treeview/account/accountTreeViewProvider";
import M365TokenInstance from "../commonlib/m365Login";
import accountTreeViewProviderInstance from "../../treeview/account/accountTreeViewProvider";
import M365TokenInstance from "../../commonlib/m365Login";

export async function refreshSideloadingCallback(args?: any[]): Promise<Result<null, FxError>> {
const status = await M365TokenInstance.getStatus({ scopes: AppStudioScopes });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

import { Result, FxError, ok, err } from "@microsoft/teamsfx-api";
import { AppStudioScopes, isUserCancelError } from "@microsoft/teamsfx-core";
import { tools } from "../globalVariables";
import { ExtTelemetry } from "../telemetry/extTelemetry";
import { AccountType, TelemetryEvent, TelemetryProperty } from "../telemetry/extTelemetryEvents";
import { AzureAccountNode } from "../treeview/account/azureNode";
import { AccountItemStatus } from "../treeview/account/common";
import { M365AccountNode } from "../treeview/account/m365Node";
import { getTriggerFromProperty } from "../utils/telemetryUtils";
import envTreeProviderInstance from "../treeview/environmentTreeViewProvider";
import azureAccountManager from "../commonlib/azureLogin";
import { tools } from "../../globalVariables";
import { ExtTelemetry } from "../../telemetry/extTelemetry";
import { AccountType, TelemetryEvent, TelemetryProperty } from "../../telemetry/extTelemetryEvents";
import { AzureAccountNode } from "../../treeview/account/azureNode";
import { AccountItemStatus } from "../../treeview/account/common";
import { M365AccountNode } from "../../treeview/account/m365Node";
import { getTriggerFromProperty } from "../../utils/telemetryUtils";
import envTreeProviderInstance from "../../treeview/environmentTreeViewProvider";
import azureAccountManager from "../../commonlib/azureLogin";

export async function signinM365Callback(...args: unknown[]): Promise<Result<null, FxError>> {
let node: M365AccountNode | undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/telemetry/extTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
globalStateUpdate,
} from "@microsoft/teamsfx-core";
import * as extensionPackage from "../../package.json";
import { VSCodeTelemetryReporter } from "../commonlib/telemetry";
import { VSCodeTelemetryReporter } from "./vscodeTelemetryReporter";
import * as globalVariables from "../globalVariables";
import { getProjectId } from "../utils/telemetryUtils";
import { TelemetryComponentType, TelemetryEvent, TelemetryProperty } from "./extTelemetryEvents";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TelemetryReporter, ConfigFolderName } from "@microsoft/teamsfx-api";
import { anonymizeFilePaths } from "../utils/fileSystemUtils";
import { isFeatureFlagEnabled, FeatureFlags, getAllFeatureFlags } from "../featureFlags";
import { getPackageVersion } from "../utils/telemetryUtils";
import { TelemetryProperty } from "../telemetry/extTelemetryEvents";
import { TelemetryProperty } from "./extTelemetryEvents";
import { Correlator, getProjectMetadata } from "@microsoft/teamsfx-core";
import { configure, getLogger, Logger } from "log4js";
import { workspaceUri } from "../globalVariables";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as vscode from "vscode";

import { getSideloadingStatus } from "@microsoft/teamsfx-core";
import { checkSideloadingCallback } from "../../handlers/checkAccessCallback";
import { checkSideloadingCallback } from "../../handlers/accounts/checkAccessCallback";
import { TelemetryTriggerFrom } from "../../telemetry/extTelemetryEvents";
import { localize } from "../../utils/localizeUtils";
import { DynamicNode } from "../dynamicNode";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,56 @@
import * as chai from "chai";
import * as spies from "chai-spies";
import { Stage, UserError } from "@microsoft/teamsfx-api";
import { ExtTelemetry } from "../../src/telemetry/extTelemetry";
import { maskSecret, telemetryUtils } from "@microsoft/teamsfx-core";
import * as globalState from "@microsoft/teamsfx-core/build/common/globalState";
import * as chai from "chai";
import * as fs from "fs-extra";
import * as sinon from "sinon";
import { Uri } from "vscode";
import * as globalVariables from "../../src/globalVariables";
import * as telemetryModule from "../../src/telemetry/extTelemetry";
import { ExtTelemetry } from "../../src/telemetry/extTelemetry";
import { TelemetryEvent } from "../../src/telemetry/extTelemetryEvents";
import sinon = require("sinon");
import * as vscTelemetryUtils from "../../src/utils/telemetryUtils";
import * as fs from "fs-extra";
import * as globalVariables from "../../src/globalVariables";
import { Uri } from "vscode";
import * as globalState from "@microsoft/teamsfx-core/build/common/globalState";
import { telemetryUtils, maskSecret } from "@microsoft/teamsfx-core";

chai.use(spies);
const spy = chai.spy;

const reporterSpy = spy.interface({
sendTelemetryErrorEvent(
eventName: string,
properties?: { [p: string]: string },
measurements?: { [p: string]: number },
errorProps?: string[]
): void {},
sendTelemetryEvent(
eventName: string,
properties?: { [p: string]: string },
measurements?: { [p: string]: number }
): void {},
sendTelemetryException(
error: Error,
properties?: { [p: string]: string },
measurements?: { [p: string]: number }
): void {},
});
import { MockTelemetryReporter } from "../mocks/mockTools";

describe("ExtTelemetry", () => {
afterEach(() => {
// Restore the default sandbox here
sinon.restore();
});
chai.util.addProperty(ExtTelemetry, "reporter", () => {});
let sendTelemetryErrorEventSpy: sinon.SinonSpy<
[
eventName: string,
properties?: { [key: string]: string } | undefined,
measurements?: { [key: string]: number } | undefined,
errorProps?: string[] | undefined
],
void
>;
let sendTelemetryEventSpy: sinon.SinonSpy<
[
eventName: string,
properties?: { [key: string]: string } | undefined,
measurements?: { [key: string]: number } | undefined
],
void
>;
let sendTelemetryExceptionSpy: sinon.SinonSpy<
[
error: Error,
properties?: { [key: string]: string } | undefined,
measurements?: { [key: string]: number } | undefined
],
void
>;

describe("setHasSentTelemetry", () => {
it("query-expfeature", () => {
const eventName = "query-expfeature";
ExtTelemetry.hasSentTelemetry = false;
ExtTelemetry.setHasSentTelemetry(eventName);
chai.expect(ExtTelemetry.hasSentTelemetry).equals(false);
});

it("other-event", () => {
const eventName = "other-event";
ExtTelemetry.hasSentTelemetry = false;
ExtTelemetry.setHasSentTelemetry(eventName);
chai.expect(ExtTelemetry.hasSentTelemetry).equals(true);
});
Expand Down Expand Up @@ -97,9 +100,14 @@ describe("ExtTelemetry", () => {

describe("Send Telemetry", () => {
const sandbox = sinon.createSandbox();
const reporterStub = new MockTelemetryReporter();

beforeEach(() => {
chai.util.addProperty(ExtTelemetry, "reporter", () => reporterSpy);
chai.util.addProperty(ExtTelemetry, "settingsVersion", () => "1.0.0");
sendTelemetryErrorEventSpy = sandbox.spy(reporterStub, "sendTelemetryErrorEvent");
sendTelemetryEventSpy = sandbox.spy(reporterStub, "sendTelemetryEvent");
sendTelemetryExceptionSpy = sandbox.spy(reporterStub, "sendTelemetryException");
sandbox.stub(ExtTelemetry, "reporter").value(reporterStub);
sandbox.stub(ExtTelemetry, "settingsVersion").value("1.0.0");
sandbox.stub(fs, "pathExistsSync").returns(false);
sandbox.stub(globalVariables, "workspaceUri").value(Uri.file("test"));
sandbox.stub(globalVariables, "isSPFxProject").value(false);
Expand All @@ -117,7 +125,8 @@ describe("ExtTelemetry", () => {
{ numericMeasure: 123 }
);

chai.expect(reporterSpy.sendTelemetryEvent).to.have.been.called.with(
sinon.assert.calledOnceWithMatch(
sendTelemetryEventSpy,
"sampleEvent",
{
stringProp: "some string",
Expand Down Expand Up @@ -145,7 +154,8 @@ describe("ExtTelemetry", () => {
["errorProps"]
);

chai.expect(reporterSpy.sendTelemetryErrorEvent).to.have.been.called.with(
sinon.assert.calledOnceWithMatch(
sendTelemetryErrorEventSpy,
"sampleEvent",
{
stringProp: "some string",
Expand Down Expand Up @@ -177,7 +187,8 @@ describe("ExtTelemetry", () => {
{ numericMeasure: 123 }
);

chai.expect(reporterSpy.sendTelemetryException).to.have.been.called.with(
sinon.assert.calledOnceWithMatch(
sendTelemetryExceptionSpy,
error,
{
stringProp: "some string",
Expand Down Expand Up @@ -223,6 +234,9 @@ describe("ExtTelemetry", () => {
});

it("sendCachedTelemetryEventsAsync", async () => {
const reporterStub = new MockTelemetryReporter();
sendTelemetryEventSpy = sandbox.spy(reporterStub, "sendTelemetryEvent");
sandbox.stub(ExtTelemetry, "reporter").value(reporterStub);
const timestamp = new Date().toISOString();
const telemetryEvents = {
eventName: "deactivate",
Expand All @@ -235,11 +249,10 @@ describe("ExtTelemetry", () => {
const telemetryData = JSON.stringify(telemetryEvents);
sandbox.stub(globalState, "globalStateGet").callsFake(async () => telemetryData);
sandbox.stub(globalState, "globalStateUpdate");
chai.util.addProperty(ExtTelemetry, "reporter", () => reporterSpy);

await ExtTelemetry.sendCachedTelemetryEventsAsync();

chai.expect(reporterSpy.sendTelemetryEvent).to.have.been.called.with("deactivate", {
sinon.assert.calledOnceWithMatch(sendTelemetryEventSpy, "deactivate", {
"correlation-id": "correlation-id",
"project-id": "project-id",
timestamp: timestamp,
Expand Down
Loading

0 comments on commit b164389

Please sign in to comment.