From 5ee879fe1547697ea95a609ede567f1ffefbf47b Mon Sep 17 00:00:00 2001 From: Arseniy Terekhin Date: Sun, 24 Mar 2024 13:19:13 +0300 Subject: [PATCH] refactor: separate revert test suite into its own file --- src/test/suite/commandSuites.ts | 132 ----------------------------- src/test/suite/extension.test.ts | 2 +- src/test/suite/revertSuite.ts | 138 +++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 133 deletions(-) create mode 100644 src/test/suite/revertSuite.ts diff --git a/src/test/suite/commandSuites.ts b/src/test/suite/commandSuites.ts index 04440a6..7c74b02 100644 --- a/src/test/suite/commandSuites.ts +++ b/src/test/suite/commandSuites.ts @@ -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 { @@ -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 { - 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'; diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index 312ef09..2405883 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -1,7 +1,6 @@ import { before, afterEach, Suite } from 'mocha'; import * as sinon from 'sinon'; import { - RevertSuite, TagSuite, StatusSuite, CleanSuite, @@ -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(); diff --git a/src/test/suite/revertSuite.ts b/src/test/suite/revertSuite.ts new file mode 100644 index 0000000..52fd049 --- /dev/null +++ b/src/test/suite/revertSuite.ts @@ -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 { + 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'] + ); + }); +}