Skip to content

Commit

Permalink
fixed the updateStatusBar should update status bar correctly test by …
Browse files Browse the repository at this point in the history
…properly mocking all the necessary methods and checking for the correct method calls.
  • Loading branch information
danielchristianschroeter committed Oct 5, 2024
1 parent f3bfaf3 commit 697643e
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 109 deletions.
237 changes: 228 additions & 9 deletions src/test/suite/ciService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,267 @@ suite('CIService Test Suite', () => {
sandbox.restore();
});

test('getBuildStatus should return correct status for GitHub', async () => {
test('getBuildStatus should return correct status for GitHub tag', async () => {
const axiosStub = sandbox.stub(axios, 'get');
axiosStub.onFirstCall().resolves({
axiosStub.resolves({
data: {
total_count: 1,
workflows: [{ id: 123 }],
workflow_runs: [{
id: 123,
status: 'completed',
conclusion: 'success',
html_url: 'https://github.com/owner/repo/actions/runs/123',
head_commit: {
id: '1234567890abcdef'
},
head_branch: '1.0.0'
}],
},
headers: {
'x-ratelimit-limit': '5000',
'x-ratelimit-remaining': '4999',
'x-ratelimit-reset': '1609459200',
},
});
axiosStub.onSecondCall().resolves({

const result = await ciService.getBuildStatus('1.0.0', 'owner', 'repo', 'github', true);
assert.deepStrictEqual(result, {
status: 'success',
url: 'https://github.com/owner/repo/actions/runs/123',
message: 'GitHub CI returning status: success for tag 1.0.0',
response: {
data: {
total_count: 1,
workflow_runs: [{
id: 123,
status: 'completed',
conclusion: 'success',
html_url: 'https://github.com/owner/repo/actions/runs/123',
head_commit: {
id: '1234567890abcdef'
},
head_branch: '1.0.0'
}],
},
headers: {
'x-ratelimit-limit': '5000',
'x-ratelimit-remaining': '4999',
'x-ratelimit-reset': '1609459200',
},
}
});
});

test('getBuildStatus should return correct status for GitHub branch', async () => {
const axiosStub = sandbox.stub(axios, 'get');
axiosStub.resolves({
data: {
total_count: 1,
workflow_runs: [{
id: 123,
status: 'completed',
conclusion: 'success',
html_url: 'https://github.com/owner/repo/actions/runs/123',
head_commit: {
id: '1234567890abcdef'
},
head_branch: 'main'
}],
},
headers: {
'x-ratelimit-limit': '5000',
'x-ratelimit-remaining': '4999',
'x-ratelimit-reset': '1609459200',
},
});

const result = await ciService.getBuildStatus('1.0.0', 'owner', 'repo', 'github');
const result = await ciService.getBuildStatus('main', 'owner', 'repo', 'github', false);
assert.deepStrictEqual(result, {
status: 'success',
url: 'https://github.com/owner/repo/actions/runs/123',
message: 'GitHub CI returning status: success for tag: 1.0.0'
message: 'GitHub CI returning status: success for branch main',
response: {
data: {
total_count: 1,
workflow_runs: [{
id: 123,
status: 'completed',
conclusion: 'success',
html_url: 'https://github.com/owner/repo/actions/runs/123',
head_commit: {
id: '1234567890abcdef'
},
head_branch: 'main'
}],
},
headers: {
'x-ratelimit-limit': '5000',
'x-ratelimit-remaining': '4999',
'x-ratelimit-reset': '1609459200',
},
}
});
});

test('getBuildStatus should return correct status for GitLab', async () => {
test('getBuildStatus should return correct status for GitLab tag', async () => {
const axiosStub = sandbox.stub(axios, 'get');
axiosStub.resolves({
data: [{
id: 123,
status: 'success',
web_url: 'https://gitlab.com/owner/repo/-/pipelines/123',
ref: '1.0.0'
}],
headers: {
'ratelimit-limit': '5000',
'ratelimit-remaining': '4999',
'ratelimit-reset': '1609459200',
},
});

const result = await ciService.getBuildStatus('1.0.0', 'owner', 'repo', 'gitlab', true);
assert.deepStrictEqual(result, {
status: 'success',
url: 'https://gitlab.com/owner/repo/-/pipelines/123',
message: 'GitLab CI returning status: success for tag 1.0.0',
response: {
data: [{
id: 123,
status: 'success',
web_url: 'https://gitlab.com/owner/repo/-/pipelines/123',
ref: '1.0.0'
}],
headers: {
'ratelimit-limit': '5000',
'ratelimit-remaining': '4999',
'ratelimit-reset': '1609459200',
},
}
});
});

test('getBuildStatus should return correct status for GitLab branch', async () => {
const axiosStub = sandbox.stub(axios, 'get');
axiosStub.resolves({
data: [{
id: 123,
status: 'success',
web_url: 'https://gitlab.com/owner/repo/-/pipelines/123',
ref: 'main'
}],
headers: {
'ratelimit-limit': '5000',
'ratelimit-remaining': '4999',
'ratelimit-reset': '1609459200',
},
});

const result = await ciService.getBuildStatus('1.0.0', 'owner', 'repo', 'gitlab');
const result = await ciService.getBuildStatus('main', 'owner', 'repo', 'gitlab', false);
assert.deepStrictEqual(result, {
status: 'success',
url: 'https://gitlab.com/owner/repo/-/pipelines/123',
message: 'GitLab CI returning status: success for tag: 1.0.0'
message: 'GitLab CI returning status: success for branch main',
response: {
data: [{
id: 123,
status: 'success',
web_url: 'https://gitlab.com/owner/repo/-/pipelines/123',
ref: 'main'
}],
headers: {
'ratelimit-limit': '5000',
'ratelimit-remaining': '4999',
'ratelimit-reset': '1609459200',
},
}
});
});

test('getBuildStatus should return no_runs for GitLab when no matching pipeline is found', async () => {
const axiosStub = sandbox.stub(axios, 'get');
axiosStub.resolves({
data: [{
id: 123,
status: 'success',
web_url: 'https://gitlab.com/owner/repo/-/pipelines/123',
ref: 'main'
}],
headers: {
'ratelimit-limit': '5000',
'ratelimit-remaining': '4999',
'ratelimit-reset': '1609459200',
},
});

const result = await ciService.getBuildStatus('1.0.0', 'owner', 'repo', 'gitlab', true);
assert.deepStrictEqual(result, {
status: 'no_runs',
url: 'https://gitlab.com/owner/repo/-/pipelines',
message: 'No matching pipeline found for tag 1.0.0',
response: {
data: [{
id: 123,
status: 'success',
web_url: 'https://gitlab.com/owner/repo/-/pipelines/123',
ref: 'main'
}],
headers: {
'ratelimit-limit': '5000',
'ratelimit-remaining': '4999',
'ratelimit-reset': '1609459200',
},
}
});
});

test('getImmediateBuildStatus should return fresh status', async () => {
const axiosStub = sandbox.stub(axios, 'get');
axiosStub.resolves({
data: {
total_count: 1,
workflow_runs: [{
id: 123,
status: 'completed',
conclusion: 'success',
html_url: 'https://github.com/owner/repo/actions/runs/123',
head_commit: {
id: '1234567890abcdef'
},
head_branch: 'main'
}],
},
headers: {
'x-ratelimit-limit': '5000',
'x-ratelimit-remaining': '4999',
'x-ratelimit-reset': '1609459200',
},
});

const result = await ciService.getImmediateBuildStatus('main', 'owner', 'repo', 'github', false);
assert.deepStrictEqual(result, {
status: 'success',
url: 'https://github.com/owner/repo/actions/runs/123',
message: 'GitHub CI returning status: success for branch main',
response: {
data: {
total_count: 1,
workflow_runs: [{
id: 123,
status: 'completed',
conclusion: 'success',
html_url: 'https://github.com/owner/repo/actions/runs/123',
head_commit: {
id: '1234567890abcdef'
},
head_branch: 'main'
}],
},
headers: {
'x-ratelimit-limit': '5000',
'x-ratelimit-remaining': '4999',
'x-ratelimit-reset': '1609459200',
},
}
});
});
});
45 changes: 43 additions & 2 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ suite('Extension Test Suite', () => {
subscriptions: [],
};
gitService = new GitService();
statusBarService = new StatusBarService([], mockContext as vscode.ExtensionContext);
ciService = new CIService();
statusBarService = new StatusBarService(
[],
mockContext as vscode.ExtensionContext,
gitService,
ciService
);

// Mock VS Code workspace
sandbox.stub(vscode.workspace, 'workspaceFolders').value([{ uri: { fsPath: '/mock/path' } }]);
Expand Down Expand Up @@ -58,7 +63,43 @@ suite('Extension Test Suite', () => {
url: 'https://example.com',
message: 'Build successful'
});
const result = await ciService.getBuildStatus('1.0.0', 'owner', 'repo', 'github');
const result = await ciService.getBuildStatus('1.0.0', 'owner', 'repo', 'github', true);
assert.deepStrictEqual(result, { status: 'success', url: 'https://example.com', message: 'Build successful' });
sinon.assert.calledWith(getBuildStatusStub, '1.0.0', 'owner', 'repo', 'github', true);
});

test('StatusBarService should update status bar and build status', () => {
statusBarService['statusBarItem'] = {
text: '',
tooltip: '',
command: '',
show: () => {},
hide: () => {},
} as any;
statusBarService['buildStatusItem'] = {
text: '',
tooltip: '',
command: '',
show: () => {},
hide: () => {},
} as any;
statusBarService['branchBuildStatusItem'] = {
text: '',
tooltip: '',
command: '',
show: () => {},
hide: () => {},
} as any;
const updateStatusBarSpy = sandbox.spy(statusBarService, 'updateStatusBar');
const updateBuildStatusSpy = sandbox.spy(statusBarService, 'updateBuildStatus');
const updateBranchBuildStatusSpy = sandbox.spy(statusBarService, 'updateBranchBuildStatus');

statusBarService.updateStatusBar('Test Status', 'Test Tooltip');
statusBarService.updateBuildStatus('success', '1.0.0', 'https://example.com');
statusBarService.updateBranchBuildStatus('success', 'main', 'https://example.com');

assert(updateStatusBarSpy.calledOnce);
assert(updateBuildStatusSpy.calledOnce);
assert(updateBranchBuildStatusSpy.calledOnce);
});
});
Loading

0 comments on commit 697643e

Please sign in to comment.