Skip to content

Commit

Permalink
test: add "Branch change is reflected in status bar" test
Browse files Browse the repository at this point in the history
  • Loading branch information
senyai committed Nov 26, 2024
1 parent 4153e64 commit c5ad642
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
81 changes: 81 additions & 0 deletions src/test/suite/commandSuites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import {
add,
assertGroups,
cleanupFossil,
fakeExecutionResult,
fakeFossilBranch,
fakeFossilChanges,
fakeFossilStatus,
fakeRawExecutionResult,
fakeStatusResult,
getExecStub,
getRawExecStub,
getRepository,
statusBarCommands,
} from './common';
import * as assert from 'assert/strict';
import * as fs from 'fs/promises';
Expand Down Expand Up @@ -251,7 +254,85 @@ export function StatusSuite(this: Suite): void {
sinon.assert.calledOnce(status);
sinon.assert.calledOnce(branch);
sinon.assert.calledOnce(changes);

// reset everything, not leaving 'refresh' as current branch
branch.resolves(fakeExecutionResult({ stdout: 'trunk' }));
changes.resolves(
fakeExecutionResult({ stdout: 'changes: None. Already up-to-date' })
);
status.resolves(fakeStatusResult(''));
await commands.executeCommand('fossil.refresh');
assertGroups(getRepository(), {});
});

test('Branch change is reflected in status bar', async () => {
// 1. Check current branch name
const branchCommandBefore = statusBarCommands()[0];
assert.equal(branchCommandBefore.title, '$(git-branch) trunk');

// 2. Create branch
const branchName = 'statusbar1';
const cib = this.ctx.sandbox.stub(window, 'createInputBox');
cib.onFirstCall().callsFake(() => {
const inputBox: vscode.InputBox = cib.wrappedMethod();
const stub = sinon.stub(inputBox);
stub.show.callsFake(() => {
stub.value = branchName;
const onDidAccept = stub.onDidAccept.getCall(0).args[0];
onDidAccept();
});
return stub;
});

const execStub = getExecStub(this.ctx.sandbox);
fakeFossilStatus(execStub, '\n'); // ensure branch doesn't get '+'
const branchCreation = execStub.withArgs([
'branch',
'new',
branchName,
'current',
]);
await commands.executeCommand('fossil.branch');
sinon.assert.calledOnce(branchCreation);

// 3. Change the branch
const branchSwitch = execStub.withArgs(['update', branchName]);
const sqp = this.ctx.sandbox.stub(window, 'showQuickPick');
sqp.onFirstCall().callsFake(items => {
assert.ok(items instanceof Array);
const item = items.find(
item => item.label == `$(git-branch) ${branchName}`
);
assert.ok(item);
assert.equal(item.description, '');
assert.equal(item.detail, undefined);
return Promise.resolve(item);
});
await commands.executeCommand('fossil.branchChange');
sinon.assert.calledOnce(sqp);
sinon.assert.calledOnce(branchSwitch);

// 4. Check branch name is changed
const branchCommandAfter = statusBarCommands()[0];
assert.equal(branchCommandAfter.title, `$(git-branch) ${branchName}`);

// 5. Change branch back to 'trunk'
sqp.onSecondCall().callsFake(items => {
assert.ok(items instanceof Array);
const item = items.find(
item => item.label == '$(git-branch) trunk'
);
assert.ok(item);
assert.equal(item.label, `$(git-branch) trunk`);
assert.equal(item.description, '');
assert.equal(item.detail, undefined);
return Promise.resolve(item);
});
await commands.executeCommand('fossil.branchChange');
sinon.assert.calledTwice(sqp);
const branchCommandLast = statusBarCommands()[0];
assert.equal(branchCommandLast.title, `$(git-branch) trunk`);
}).timeout(20000);
}

export function TagSuite(this: Suite): void {
Expand Down
12 changes: 8 additions & 4 deletions src/test/suite/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,19 @@ export function fakeRawExecutionResult({
};
}

export function fakeFossilStatus(execStub: ExecStub, status: string): ExecStub {
export function fakeStatusResult(status: string): ExecResult {
const args: FossilArgs = ['status', '--differ', '--merge'];
const header =
'checkout: 0000000000000000000000000000000000000000 2023-05-26 12:43:56 UTC\n' +
'parent: 0000000000000000000000000000000000000001 2023-05-26 12:43:56 UTC\n' +
'tags: trunk, this is a test, custom tag\n';
const args: FossilArgs = ['status', '--differ', '--merge'];
return fakeExecutionResult({ stdout: header + status, args });
}

export function fakeFossilStatus(execStub: ExecStub, status: string): ExecStub {
return execStub
.withArgs(args)
.resolves(fakeExecutionResult({ stdout: header + status, args }));
.withArgs(['status', '--differ', '--merge'])
.resolves(fakeStatusResult(status));
}

export function fakeFossilBranch(
Expand Down

0 comments on commit c5ad642

Please sign in to comment.