Skip to content

Commit

Permalink
refactor: separate revert test suite into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
senyai committed Mar 24, 2024
1 parent 5bc7e45 commit 5ee879f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 133 deletions.
132 changes: 0 additions & 132 deletions src/test/suite/commandSuites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import * as fs from 'fs/promises';
import { OpenedRepository, ResourceStatus } from '../../openedRepository';
import { Suite, Func, Test } from 'mocha';
import { toFossilUri } from '../../uri';
import { FossilResourceGroup } from '../../resourceGroups';

declare module 'mocha' {
interface TestFunction {
Expand All @@ -36,137 +35,6 @@ test.if = function (condition: boolean, title: string, fn: Func): Test {
}
};

export function RevertSuite(this: Suite): void {
test('Single source', async () => {
const url = await add(
'revert_me.txt',
'Some original text\n',
'add revert_me.txt'
);
await fs.writeFile(url.fsPath, 'something new');

const repository = getRepository();
await repository.updateModelState();
const resource = repository.workingGroup.getResource(url);
assert.ok(resource);

const showWarningMessage: sinon.SinonStub = this.ctx.sandbox.stub(
window,
'showWarningMessage'
);
showWarningMessage.onFirstCall().resolves('&&Discard Changes');

await commands.executeCommand('fossil.revert', resource);
const newContext = await fs.readFile(url.fsPath);
assert.equal(newContext.toString('utf-8'), 'Some original text\n');
});

test('Dialog has no typos', async () => {
const repository = getRepository();
const rootUri = workspace.workspaceFolders![0].uri;
const fake_status = [];
const execStub = getExecStub(this.ctx.sandbox);
const fileUris: Uri[] = [];
for (const filename of 'abcdefghijklmn') {
const fileUri = Uri.joinPath(rootUri, 'added', filename);
fake_status.push(`EDITED added/${filename}`);
fileUris.push(fileUri);
}
const statusCall = fakeFossilStatus(execStub, fake_status.join('\n'));
await repository.updateModelState();
sinon.assert.calledOnce(statusCall);
const resources = fileUris.map(uri => {
const resource = repository.workingGroup.getResource(uri);
assert.ok(resource);
return resource;
});
const swm: sinon.SinonStub = this.ctx.sandbox.stub(
window,
'showWarningMessage'
);
await commands.executeCommand('fossil.revert', ...resources);
assert.ok(
swm.firstCall.calledWith(
'Are you sure you want to discard changes to 14 files?\n\n • a\n • b\n • c\n • d\n • e\n • f\n • g\n • h\nand 6 others'
)
);
await commands.executeCommand(
'fossil.revert',
...resources.slice(0, 3)
);
assert.ok(
swm.secondCall.calledWith(
'Are you sure you want to discard changes to 3 files?\n\n • a\n • b\n • c'
)
);
});

test('Revert (Nothing)', async () => {
await commands.executeCommand('fossil.revert');
});

async function revertAllTest(
sandbox: sinon.SinonSandbox,
groups: FossilResourceGroup[],
message: string,
files: string[]
): Promise<void> {
const swm: sinon.SinonStub = sandbox.stub(window, 'showWarningMessage');
swm.onFirstCall().resolves('&&Discard Changes');

const repository = getRepository();
const execStub = getExecStub(sandbox);
const statusStub = fakeFossilStatus(
execStub,
'EDITED a.txt\nEDITED b.txt\nCONFLICT c.txt\nCONFLICT d.txt'
);
const revertStub = execStub
.withArgs(sinon.match.array.startsWith(['revert']))
.resolves();
await repository.updateModelState();
sinon.assert.calledOnce(statusStub);
await commands.executeCommand('fossil.revertAll', ...groups);
sinon.assert.calledOnceWithExactly(
swm,
message,
{ modal: true },
'&&Discard Changes'
);
sinon.assert.calledOnceWithExactly(revertStub, ['revert', ...files]);
}

test('Revert all (no groups)', async () => {
await revertAllTest(
this.ctx.sandbox,
[],
'Are you sure you want to discard changes in ' +
'"Changes" and "Unresolved Conflicts" group?',
['a.txt', 'b.txt', 'c.txt', 'd.txt']
);
});

test('Revert all (changes group)', async () => {
const repository = getRepository();
await revertAllTest(
this.ctx.sandbox,
[repository.workingGroup],
'Are you sure you want to discard changes in "Changes" group?',
['a.txt', 'b.txt']
);
});

test('Revert all (conflict group)', async () => {
const repository = getRepository();
await revertAllTest(
this.ctx.sandbox,
[repository.conflictGroup],
'Are you sure you want to discard changes ' +
'in "Unresolved Conflicts" group?',
['c.txt', 'd.txt']
);
});
}

export function StatusSuite(this: Suite): void {
test('Missing is visible in Source Control panel', async () => {
const filename = 'smiviscp.txt';
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { before, afterEach, Suite } from 'mocha';
import * as sinon from 'sinon';
import {
RevertSuite,
TagSuite,
StatusSuite,
CleanSuite,
Expand All @@ -18,6 +17,7 @@ import { QualityOfLifeSuite as QualityOfLifeSuite } from './qualityOfLifeSuite';
import { PatchSuite, StageSuite, StashSuite, UpdateSuite } from './stateSuite';
import { RenameSuite } from './renameSuite';
import { BranchSuite } from './branchSuite';
import { RevertSuite } from './revertSuite';

suite('Fossil.OpenedRepo', function (this: Suite) {
this.ctx.sandbox = sinon.createSandbox();
Expand Down
138 changes: 138 additions & 0 deletions src/test/suite/revertSuite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { Uri, window, workspace, commands } from 'vscode';
import * as sinon from 'sinon';
import { add, fakeFossilStatus, getExecStub, getRepository } from './common';
import * as assert from 'assert/strict';
import * as fs from 'fs/promises';
import { Suite } from 'mocha';
import { FossilResourceGroup } from '../../resourceGroups';

export function RevertSuite(this: Suite): void {
test('Single source', async () => {
const url = await add(
'revert_me.txt',
'Some original text\n',
'add revert_me.txt'
);
await fs.writeFile(url.fsPath, 'something new');

const repository = getRepository();
await repository.updateModelState();
const resource = repository.workingGroup.getResource(url);
assert.ok(resource);

const showWarningMessage: sinon.SinonStub = this.ctx.sandbox.stub(
window,
'showWarningMessage'
);
showWarningMessage.onFirstCall().resolves('&&Discard Changes');

await commands.executeCommand('fossil.revert', resource);
const newContext = await fs.readFile(url.fsPath);
assert.equal(newContext.toString('utf-8'), 'Some original text\n');
});

test('Dialog has no typos', async () => {
const repository = getRepository();
const rootUri = workspace.workspaceFolders![0].uri;
const fake_status = [];
const execStub = getExecStub(this.ctx.sandbox);
const fileUris: Uri[] = [];
for (const filename of 'abcdefghijklmn') {
const fileUri = Uri.joinPath(rootUri, 'added', filename);
fake_status.push(`EDITED added/${filename}`);
fileUris.push(fileUri);
}
const statusCall = fakeFossilStatus(execStub, fake_status.join('\n'));
await repository.updateModelState();
sinon.assert.calledOnce(statusCall);
const resources = fileUris.map(uri => {
const resource = repository.workingGroup.getResource(uri);
assert.ok(resource);
return resource;
});
const swm: sinon.SinonStub = this.ctx.sandbox.stub(
window,
'showWarningMessage'
);
await commands.executeCommand('fossil.revert', ...resources);
assert.ok(
swm.firstCall.calledWith(
'Are you sure you want to discard changes to 14 files?\n\n • a\n • b\n • c\n • d\n • e\n • f\n • g\n • h\nand 6 others'
)
);
await commands.executeCommand(
'fossil.revert',
...resources.slice(0, 3)
);
assert.ok(
swm.secondCall.calledWith(
'Are you sure you want to discard changes to 3 files?\n\n • a\n • b\n • c'
)
);
});

test('Revert (Nothing)', async () => {
await commands.executeCommand('fossil.revert');
});

async function revertAllTest(
sandbox: sinon.SinonSandbox,
groups: FossilResourceGroup[],
message: string,
files: string[]
): Promise<void> {
const swm: sinon.SinonStub = sandbox.stub(window, 'showWarningMessage');
swm.onFirstCall().resolves('&&Discard Changes');

const repository = getRepository();
const execStub = getExecStub(sandbox);
const statusStub = fakeFossilStatus(
execStub,
'EDITED a.txt\nEDITED b.txt\nCONFLICT c.txt\nCONFLICT d.txt'
);
const revertStub = execStub
.withArgs(sinon.match.array.startsWith(['revert']))
.resolves();
await repository.updateModelState();
sinon.assert.calledOnce(statusStub);
await commands.executeCommand('fossil.revertAll', ...groups);
sinon.assert.calledOnceWithExactly(
swm,
message,
{ modal: true },
'&&Discard Changes'
);
sinon.assert.calledOnceWithExactly(revertStub, ['revert', ...files]);
}

test('Revert all (no groups)', async () => {
await revertAllTest(
this.ctx.sandbox,
[],
'Are you sure you want to discard changes in ' +
'"Changes" and "Unresolved Conflicts" group?',
['a.txt', 'b.txt', 'c.txt', 'd.txt']
);
});

test('Revert all (changes group)', async () => {
const repository = getRepository();
await revertAllTest(
this.ctx.sandbox,
[repository.workingGroup],
'Are you sure you want to discard changes in "Changes" group?',
['a.txt', 'b.txt']
);
});

test('Revert all (conflict group)', async () => {
const repository = getRepository();
await revertAllTest(
this.ctx.sandbox,
[repository.conflictGroup],
'Are you sure you want to discard changes ' +
'in "Unresolved Conflicts" group?',
['c.txt', 'd.txt']
);
});
}

0 comments on commit 5ee879f

Please sign in to comment.