Add tests for more Git state scenarios

This commit is contained in:
Koen Vlaswinkel
2023-11-16 16:49:02 +01:00
parent 779faa324c
commit 5db1f76c55

View File

@@ -25,12 +25,16 @@ describe("findGitHubRepositoryForWorkspace", () => {
name: "main", name: "main",
upstream: { upstream: {
name: "origin", name: "origin",
remote: "origin", remote: "fork",
}, },
}, },
remotes: [ remotes: [
{ {
name: "origin", name: "origin",
fetchUrl: "https://github.com/codeql/test-incorrect.git",
},
{
name: "fork",
fetchUrl: "https://github.com/codeql/test.git", fetchUrl: "https://github.com/codeql/test.git",
}, },
], ],
@@ -60,7 +64,9 @@ describe("findGitHubRepositoryForWorkspace", () => {
getExtensionSpy.mockReturnValue( getExtensionSpy.mockReturnValue(
mockedObject<Extension<GitExtension>>({ mockedObject<Extension<GitExtension>>({
isActive: true,
exports: { exports: {
enabled: true,
getAPI: getAPISpy, getAPI: getAPISpy,
}, },
}), }),
@@ -85,11 +91,61 @@ describe("findGitHubRepositoryForWorkspace", () => {
it("returns an error", async () => { it("returns an error", async () => {
expect(await findGitHubRepositoryForWorkspace()).toEqual( expect(await findGitHubRepositoryForWorkspace()).toEqual(
ValueResult.fail(["Git extension is not installed or initialized"]), ValueResult.fail(["Git extension not found"]),
); );
}); });
}); });
describe("when the git extension is not activated", () => {
const activate = jest.fn();
beforeEach(() => {
getExtensionSpy.mockReturnValue(
mockedObject<Extension<GitExtension>>({
isActive: false,
activate,
exports: {
enabled: true,
getAPI: getAPISpy,
},
}),
);
});
it("returns the GitHub repository name with owner", async () => {
expect(await findGitHubRepositoryForWorkspace()).toEqual(
ValueResult.ok({
owner: "codeql",
name: "test",
}),
);
expect(activate).toHaveBeenCalledTimes(1);
});
});
describe("when the git extension is disabled by the setting", () => {
beforeEach(() => {
getExtensionSpy.mockReturnValue(
mockedObject<Extension<GitExtension>>({
isActive: true,
exports: {
enabled: false,
getAPI: getAPISpy,
},
}),
);
});
it("returns an error", async () => {
expect(await findGitHubRepositoryForWorkspace()).toEqual(
ValueResult.fail(["Git extension is not enabled"]),
);
expect(getAPISpy).not.toHaveBeenCalled();
});
});
describe("when the git extension is not yet initialized", () => { describe("when the git extension is not yet initialized", () => {
beforeEach(() => { beforeEach(() => {
const onDidChangeState = jest.fn(); const onDidChangeState = jest.fn();
@@ -149,7 +205,48 @@ describe("findGitHubRepositoryForWorkspace", () => {
}); });
}); });
describe("when the current branch does not have a remote", () => { describe("when the current branch does not have a remote but origin remote exists", () => {
beforeEach(() => {
mockGitExtensionAPI = mockedObject<GitExtensionAPI>({
state: "initialized",
repositories: [
{
...repositories[0],
state: {
...repositories[0].state,
HEAD: {
...repositories[0].state.HEAD,
upstream: undefined,
},
remotes: [
{
name: "upstream",
fetchUrl: "https://github.com/github/codeql-incorrect.git",
},
{
name: "origin",
fetchUrl: "https://github.com/github/codeql.git",
},
],
},
},
],
});
getAPISpy.mockReturnValue(mockGitExtensionAPI);
});
it("returns the GitHub repository name with owner", async () => {
expect(await findGitHubRepositoryForWorkspace()).toEqual(
ValueResult.ok({
owner: "github",
name: "codeql",
}),
);
});
});
describe("when the current branch does not have a remote and no origin remote", () => {
beforeEach(() => { beforeEach(() => {
mockGitExtensionAPI = mockedObject<GitExtensionAPI>({ mockGitExtensionAPI = mockedObject<GitExtensionAPI>({
state: "initialized", state: "initialized",
@@ -167,6 +264,10 @@ describe("findGitHubRepositoryForWorkspace", () => {
name: "upstream", name: "upstream",
fetchUrl: "https://github.com/github/codeql.git", fetchUrl: "https://github.com/github/codeql.git",
}, },
{
name: "fork",
fetchUrl: "https://github.com/github/codeql-incorrect.git",
},
], ],
}, },
}, },