Skip to content

Commit

Permalink
fix: validate stop-on-error (#8467)
Browse files Browse the repository at this point in the history
* fix: validate stop-on-error
  • Loading branch information
nliu-ms authored Apr 21, 2023
1 parent aa80cf0 commit 369be92
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 15 deletions.
5 changes: 3 additions & 2 deletions packages/fx-core/resource/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -955,9 +955,10 @@
"driver.teamsApp.validate.skip": "%s action is currently skipped, will be updated in the future version.",
"driver.teamsApp.invalidParameter": "Following parameter is missing or invalid for %s action: %s.",
"driver.teamsApp.validate.invalidParameter": "Parameter %s should be provided for action %s",
"driver.teamsApp.validate.result": "Teams Toolkit has completed checking your app package against validation rules. %s failed, %s passed. Check [Output panel](%s) for details.",
"driver.teamsApp.validate.result": "Teams Toolkit has completed checking your app package against validation rules. %s.",
"driver.teamsApp.validate.result.display": "Teams Toolkit has completed checking your app package against validation rules. %s. Check [Output panel](command:fx-extension.showOutputChannel) for details.",
"error.teamsApp.validate.apiFailed": "Teams app package validation failed due to %s",
"error.teamsApp.validate.apiFailed.display": "Teams app pacakge validation failed. Check [Output panel](%s) for details.",
"error.teamsApp.validate.apiFailed.display": "Teams app pacakge validation failed. Check [Output panel](command:fx-extension.showOutputChannel) for details.",
"error.teamsApp.AppIdNotExistError": "Teams app with ID %s does not exist in Teams Developer Portal.",
"error.teamsApp.InvalidAppIdError": "Teams app ID %s is invalid, must be a GUID.",
"driver.botFramework.description": "creates or updates the bot registration on dev.botframework.com",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class ValidateAppPackageDriver implements StepDriver {
`${validationResult.warnings.length} warning` +
(validationResult.warnings.length > 1 ? "s" : "") +
", ",
color: Colors.BRIGHT_RED,
color: Colors.BRIGHT_YELLOW,
});
merge(context.telemetryProperties, {
[TelemetryPropertyKey.validationWarnings]: validationResult.warnings
Expand Down Expand Up @@ -177,6 +177,17 @@ export class ValidateAppPackageDriver implements StepDriver {
});
});
context.ui?.showMessage("info", outputMessage, false);
if (validationResult.errors.length > 0) {
const message = `Teams Toolkit has completed checking your app package against validation rules. ${validationResult.errors.length} failed.`;
return err(
AppStudioResultFactory.UserError(AppStudioError.ValidationFailedError.name, [
message,
message,
])
);
} else {
return ok(new Map());
}
} else {
// logs in output window
const errors = validationResult.errors
Expand Down Expand Up @@ -240,12 +251,25 @@ export class ValidateAppPackageDriver implements StepDriver {
context.logProvider?.info(`${outputMessage}\n${errors}\n${warnings}\n${notes}`, true);

if (args.showMessage) {
// For non-lifecycle commands, just show the message
const message = getLocalizedString(
"driver.teamsApp.validate.result",
summaryStr.join(", "),
"command:fx-extension.showOutputChannel"
"driver.teamsApp.validate.result.display",
summaryStr.join(", ")
);
context.ui?.showMessage("info", message, false);
} else {
// For lifecycle like provision, stop-on-error
if (validationResult.errors.length > 0) {
return err(
AppStudioResultFactory.UserError(AppStudioError.ValidationFailedError.name, [
getDefaultString("driver.teamsApp.validate.result", summaryStr.join(", ")),
getLocalizedString(
"driver.teamsApp.validate.result.display",
summaryStr.join(", ")
),
])
);
}
}
}
} catch (e: any) {
Expand All @@ -254,10 +278,7 @@ export class ValidateAppPackageDriver implements StepDriver {
);
context.ui?.showMessage(
"warn",
getLocalizedString(
"error.teamsApp.validate.apiFailed.display",
"command:fx-extension.showOutputChannel"
),
getLocalizedString("error.teamsApp.validate.apiFailed.display"),
false
);
}
Expand Down
65 changes: 60 additions & 5 deletions packages/fx-core/tests/component/driver/teamsApp/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe("teamsApp/validateAppPackage", async () => {
chai.assert(result.isOk());
});

it("validate app package - no passed", async () => {
it("validate app package - stop-on-error", async () => {
sinon.stub(AppStudioClient, "partnerCenterAppPackageValidation").resolves({
errors: [
{
Expand Down Expand Up @@ -322,16 +322,16 @@ describe("teamsApp/validateAppPackage", async () => {

const args: ValidateAppPackageArgs = {
appPackagePath: "fakePath",
showMessage: true,
showMessage: false,
};
let result = await teamsAppDriver.run(args, mockedDriverContext);
chai.assert(result.isOk());
chai.assert(result.isErr());

result = await teamsAppDriver.run(args, contextWithoutUI);
chai.assert(result.isOk());
chai.assert(result.isErr());
});

it("happy path - cli", async () => {
it("errors - cli", async () => {
sinon.stub(AppStudioClient, "partnerCenterAppPackageValidation").resolves({
errors: [
{
Expand Down Expand Up @@ -392,6 +392,61 @@ describe("teamsApp/validateAppPackage", async () => {
platform: Platform.CLI,
};

const result = await teamsAppDriver.run(args, mockedCliDriverContext);
chai.assert(result.isErr());
});

it("happy path - cli", async () => {
sinon.stub(AppStudioClient, "partnerCenterAppPackageValidation").resolves({
errors: [],
status: "Rejected",
warnings: [
{
id: "fakeId",
content: "Valid domains cannot contain a hosting site with a wildcard.",
filePath: "",
helpUrl: "https://docs.microsoft.com",
shortCodeNumber: 123,
validationCategory: "domain",
title: "valid domain",
},
],
notes: [
{
id: "fakeId",
content: "Schema URL is present.",
title: "schema",
},
],
addInDetails: {
displayName: "fake name",
developerName: "fake name",
version: "1.14.1",
manifestVersion: "1.14.1",
},
});
sinon.stub(fs, "pathExists").resolves(true);
// sinon.stub(fs, "readFile").resolves(Buffer.from(""));
sinon.stub(fs, "readFile").callsFake(async () => {
const zip = new AdmZip();
zip.addFile(Constants.MANIFEST_FILE, Buffer.from(JSON.stringify(new TeamsAppManifest())));
zip.addFile("color.png", new Buffer(""));
zip.addFile("outlie.png", new Buffer(""));

const archivedFile = zip.toBuffer();
return archivedFile;
});
sinon.stub(metadataUtil, "parseManifest");

const args: ValidateAppPackageArgs = {
appPackagePath: "fakePath",
};

const mockedCliDriverContext = {
...mockedDriverContext,
platform: Platform.CLI,
};

const result = await teamsAppDriver.run(args, mockedCliDriverContext);
chai.assert(result.isOk());
});
Expand Down

0 comments on commit 369be92

Please sign in to comment.