Skip to content

Commit

Permalink
Merge pull request #10013 from OfficeDev/aochengwang/fix-auto-update
Browse files Browse the repository at this point in the history
fix: corner cases for npm view command
  • Loading branch information
a1exwang authored Nov 3, 2023
2 parents ac51cf0 + d6ac5e8 commit 8dfdd50
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,14 @@ export class TestToolChecker implements DepsChecker {
"version",
"--json"
);
const versionList: string[] = JSON.parse(result);
// when there are one result, it will return string
// when there are multiple results, it will return array of strings
let versionList: string[] | string = JSON.parse(result);
if (typeof versionList === "string") {
versionList = [versionList];
}
if (!Array.isArray(versionList)) {
// do update if npm returned invalid result
return true;
}
return versionList.filter((v) => semver.gt(v, latestInstalledVersion)).length > 0;
Expand Down
88 changes: 88 additions & 0 deletions packages/fx-core/tests/common/deps-checker/testToolChecker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface EnvironmentInfo {
testToolVersionBeforeInstall?: string;
testToolVersionAfterInstall?: string;
installSuccess?: boolean;
npmViewResult?: string;
}

// output
Expand Down Expand Up @@ -99,6 +100,11 @@ function mockEnvironment(sandbox: sinon.SinonSandbox, info: EnvironmentInfo): En
} else {
throw new Error("failed to npm install");
}
} else if (command === "npm" && args.includes("view")) {
if (info.npmViewResult === undefined) {
throw new Error("fail to npm view");
}
return info.npmViewResult;
}
throw new Error("Command not mocked");
});
Expand Down Expand Up @@ -579,6 +585,88 @@ describe("Test Tool Checker Test", () => {
expect(npmInstalled).to.be.true;
expect(checkedUpdate).to.be.true;
});
it("Already installed, symlink created, needs to check update but has multiple recent versions, should use latest", async () => {
const checker = new TestToolChecker();
const symlinkDir = "symlinkDir";
const versionRange = "~1.2.3";
let npmInstalled = false;
let checkedUpdate = false;
const homePortableDir = path.join(homePortablesDir, "1.2.3");
const homePortableExec = path.join(homePortableDir, "node_modules", ".bin", "teamsapptester");
sandbox.stub(fileHelper, "rename").resolves();
sandbox.stub(fileHelper, "createSymlink").resolves();
mockfs({
[path.join(projectPath, "devTools", ".testTool.installInfo.json")]: "",
[homePortableExec]: "",
});
let installedVersion = "1.2.3";
sandbox
.stub(cpUtils, "executeCommand")
.callsFake(async (_cwd, _logger, _options, command, ...args) => {
if (args.includes("--version")) {
if (command === "node") return "v18.16.1";
if (command === "npm") return "9.7.0";
return installedVersion;
} else if (args.includes("install")) {
installedVersion = "1.2.4";
npmInstalled = true;
} else if (args.includes("view")) {
checkedUpdate = true;
return '"1.2.3"';
}
return "";
});
// Act
const status = await checker.resolve({ projectPath, symlinkDir, versionRange });
// Assert
expect(status.isInstalled).to.be.true;
expect(status.details.binFolders).not.empty;
expect(status.error).to.be.undefined;
expect(status.details.installVersion).to.eq("1.2.3");
expect(npmInstalled).to.be.false;
expect(checkedUpdate).to.be.true;
});
it("Already installed, symlink created, needs to check update but has multiple recent versions, should use latest", async () => {
const checker = new TestToolChecker();
const symlinkDir = "symlinkDir";
const versionRange = "~1.2.3";
let npmInstalled = false;
let checkedUpdate = false;
const homePortableDir = path.join(homePortablesDir, "1.2.3");
const homePortableExec = path.join(homePortableDir, "node_modules", ".bin", "teamsapptester");
sandbox.stub(fileHelper, "rename").resolves();
sandbox.stub(fileHelper, "createSymlink").resolves();
mockfs({
[path.join(projectPath, "devTools", ".testTool.installInfo.json")]: "",
[homePortableExec]: "",
});
let installedVersion = "1.2.3";
sandbox
.stub(cpUtils, "executeCommand")
.callsFake(async (_cwd, _logger, _options, command, ...args) => {
if (args.includes("--version")) {
if (command === "node") return "v18.16.1";
if (command === "npm") return "9.7.0";
return installedVersion;
} else if (args.includes("install")) {
installedVersion = "1.2.5";
npmInstalled = true;
} else if (args.includes("view")) {
checkedUpdate = true;
return '["1.2.4", "1.2.5"]';
}
return "";
});
// Act
const status = await checker.resolve({ projectPath, symlinkDir, versionRange });
// Assert
expect(status.isInstalled).to.be.true;
expect(status.details.binFolders).not.empty;
expect(status.error).to.be.undefined;
expect(status.details.installVersion).to.eq("1.2.5");
expect(npmInstalled).to.be.true;
expect(checkedUpdate).to.be.true;
});
it("Already installed, symlink created, needs to check update but update failed", async () => {
const checker = new TestToolChecker();
const symlinkDir = "symlinkDir";
Expand Down

0 comments on commit 8dfdd50

Please sign in to comment.