From d562ebbf25599e4806224fef8ec26804d1f9d74f Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Sun, 8 Apr 2018 23:26:39 +1000 Subject: [PATCH 1/9] ref #44776 Wrote a test for Repository#getCommit --- extensions/git/src/test/git.test.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/extensions/git/src/test/git.test.ts b/extensions/git/src/test/git.test.ts index 09661eebc9c..39c9f0a7e9f 100644 --- a/extensions/git/src/test/git.test.ts +++ b/extensions/git/src/test/git.test.ts @@ -6,8 +6,9 @@ 'use strict'; import 'mocha'; -import { GitStatusParser, parseGitmodules } from '../git'; +import { Git, GitStatusParser, Repository, parseGitmodules } from '../git'; import * as assert from 'assert'; +import * as sinon from 'sinon'; suite('git', () => { suite('GitStatusParser', () => { @@ -175,4 +176,22 @@ suite('git', () => { ]); }); }); + + suite('Repository', () => { + test('get commit', async () => { + const spawnOption = {}; + const gitOutput = `52c293a05038d865604c2284aa8698bd087915a1 +This is a commit message.`; + const git = sinon.createStubInstance(Git); + git.exec = sinon.stub() + .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%B', 'REF'], spawnOption) + .returns(Promise.resolve({stdout: gitOutput})); + const repository = new Repository(git, 'REPOSITORY_ROOT'); + + assert.deepEqual(await repository.getCommit('REF'), { + hash: '52c293a05038d865604c2284aa8698bd087915a1', + message: 'This is a commit message.' + }); + }); + }); }); \ No newline at end of file From 3fdf0bcc56dadadb82b50b8ef9632fd20379079f Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Sun, 8 Apr 2018 23:41:43 +1000 Subject: [PATCH 2/9] ref #44776 Get previous commits as part of commit info --- extensions/git/src/git.ts | 7 ++++--- extensions/git/src/test/git.test.ts | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index dc4829808b4..b2c111a46fe 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -481,6 +481,7 @@ export class Git { export interface Commit { hash: string; message: string; + previousHashes: string[]; } export class GitStatusParser { @@ -1287,14 +1288,14 @@ export class Repository { } async getCommit(ref: string): Promise { - const result = await this.run(['show', '-s', '--format=%H\n%B', ref]); - const match = /^([0-9a-f]{40})\n([^]*)$/m.exec(result.stdout.trim()); + const result = await this.run(['show', '-s', '--format=%H\n%P\n%B', ref]); + const match = /^([0-9a-f]{40})\n(.*)\n([^]*)$/m.exec(result.stdout.trim()); if (!match) { return Promise.reject('bad commit format'); } - return { hash: match[1], message: match[2] }; + return { hash: match[1], message: match[3], previousHashes: [match[2]] }; } async updateSubmodules(paths: string[]): Promise { diff --git a/extensions/git/src/test/git.test.ts b/extensions/git/src/test/git.test.ts index 39c9f0a7e9f..af4a24c349b 100644 --- a/extensions/git/src/test/git.test.ts +++ b/extensions/git/src/test/git.test.ts @@ -181,16 +181,18 @@ suite('git', () => { test('get commit', async () => { const spawnOption = {}; const gitOutput = `52c293a05038d865604c2284aa8698bd087915a1 +8e5a374372b8393906c7e380dbb09349c5385554 This is a commit message.`; const git = sinon.createStubInstance(Git); git.exec = sinon.stub() - .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%B', 'REF'], spawnOption) + .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_SINGLE_PARENT'], spawnOption) .returns(Promise.resolve({stdout: gitOutput})); const repository = new Repository(git, 'REPOSITORY_ROOT'); - assert.deepEqual(await repository.getCommit('REF'), { + assert.deepEqual(await repository.getCommit('REF_SINGLE_PARENT'), { hash: '52c293a05038d865604c2284aa8698bd087915a1', - message: 'This is a commit message.' + message: 'This is a commit message.', + previousHashes: ['8e5a374372b8393906c7e380dbb09349c5385554'] }); }); }); From ac63779dcaac39c2ab175988e71afaed31622d08 Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Sun, 8 Apr 2018 23:52:25 +1000 Subject: [PATCH 3/9] ref #44776 Cater for the cases of multiple/no previous hashes --- extensions/git/src/git.ts | 3 ++- extensions/git/src/test/git.test.ts | 40 +++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index b2c111a46fe..3b22f956538 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -1295,7 +1295,8 @@ export class Repository { return Promise.reject('bad commit format'); } - return { hash: match[1], message: match[3], previousHashes: [match[2]] }; + const previousHashes = match[2] ? match[2].split(' ') : []; + return { hash: match[1], message: match[3], previousHashes }; } async updateSubmodules(paths: string[]): Promise { diff --git a/extensions/git/src/test/git.test.ts b/extensions/git/src/test/git.test.ts index af4a24c349b..aff9b1f353f 100644 --- a/extensions/git/src/test/git.test.ts +++ b/extensions/git/src/test/git.test.ts @@ -178,22 +178,46 @@ suite('git', () => { }); suite('Repository', () => { - test('get commit', async () => { - const spawnOption = {}; - const gitOutput = `52c293a05038d865604c2284aa8698bd087915a1 + const spawnOption = {}; + const GIT_OUTPUT_SINGLE_PARENT = `52c293a05038d865604c2284aa8698bd087915a1 8e5a374372b8393906c7e380dbb09349c5385554 This is a commit message.`; - const git = sinon.createStubInstance(Git); - git.exec = sinon.stub() - .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_SINGLE_PARENT'], spawnOption) - .returns(Promise.resolve({stdout: gitOutput})); - const repository = new Repository(git, 'REPOSITORY_ROOT'); + const GIT_OUTPUT_MULTIPLE_PARENTS = `52c293a05038d865604c2284aa8698bd087915a1 +8e5a374372b8393906c7e380dbb09349c5385554 df27d8c75b129ab9b178b386077da2822101b217 +This is a commit message.`; + const GIT_OUTPUT_NO_PARENTS = `52c293a05038d865604c2284aa8698bd087915a1 +This is a commit message.`; + + const git = sinon.createStubInstance(Git); + git.exec = sinon.stub(); + git.exec + .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_SINGLE_PARENT'], spawnOption) + .returns(Promise.resolve({stdout: GIT_OUTPUT_SINGLE_PARENT})); + git.exec + .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_MULTIPLE_PARENTS'], spawnOption) + .returns(Promise.resolve({stdout: GIT_OUTPUT_MULTIPLE_PARENTS})); + git.exec + .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_NO_PARENTS'], spawnOption) + .returns(Promise.resolve({stdout: GIT_OUTPUT_NO_PARENTS})); + const repository = new Repository(git, 'REPOSITORY_ROOT'); + + test('get commit', async () => { assert.deepEqual(await repository.getCommit('REF_SINGLE_PARENT'), { hash: '52c293a05038d865604c2284aa8698bd087915a1', message: 'This is a commit message.', previousHashes: ['8e5a374372b8393906c7e380dbb09349c5385554'] }); }); + + test('multiple previous commits', async () => { + const commit = await repository.getCommit('REF_MULTIPLE_PARENTS'); + assert.deepEqual(commit.previousHashes, ['8e5a374372b8393906c7e380dbb09349c5385554', 'df27d8c75b129ab9b178b386077da2822101b217']); + }); + + test('no previous commits', async () => { + const commit = await repository.getCommit('REF_NO_PARENTS'); + assert.deepEqual(commit.previousHashes, []); + }); }); }); \ No newline at end of file From 963d337689650ea8e8adc848103a5ab79fe32944 Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Mon, 9 Apr 2018 00:06:28 +1000 Subject: [PATCH 4/9] ref #44776 Refactoring test --- extensions/git/src/test/git.test.ts | 30 +++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/extensions/git/src/test/git.test.ts b/extensions/git/src/test/git.test.ts index aff9b1f353f..ceab42b490c 100644 --- a/extensions/git/src/test/git.test.ts +++ b/extensions/git/src/test/git.test.ts @@ -190,16 +190,18 @@ This is a commit message.`; This is a commit message.`; const git = sinon.createStubInstance(Git); - git.exec = sinon.stub(); - git.exec - .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_SINGLE_PARENT'], spawnOption) - .returns(Promise.resolve({stdout: GIT_OUTPUT_SINGLE_PARENT})); - git.exec - .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_MULTIPLE_PARENTS'], spawnOption) - .returns(Promise.resolve({stdout: GIT_OUTPUT_MULTIPLE_PARENTS})); - git.exec - .withArgs('REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_NO_PARENTS'], spawnOption) - .returns(Promise.resolve({stdout: GIT_OUTPUT_NO_PARENTS})); + git.exec = stub([ + { + withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_SINGLE_PARENT'], spawnOption], + returns: GIT_OUTPUT_SINGLE_PARENT + }, { + withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_MULTIPLE_PARENTS'], spawnOption], + returns: GIT_OUTPUT_MULTIPLE_PARENTS + }, { + withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_NO_PARENTS'], spawnOption], + returns: GIT_OUTPUT_NO_PARENTS + } + ]); const repository = new Repository(git, 'REPOSITORY_ROOT'); test('get commit', async () => { @@ -219,5 +221,13 @@ This is a commit message.`; const commit = await repository.getCommit('REF_NO_PARENTS'); assert.deepEqual(commit.previousHashes, []); }); + + function stub(argOutputPairs: {withArgs: any[], returns: string}[]): sinon.SinonStub { + const stub = sinon.stub(); + argOutputPairs.forEach(({withArgs, returns}) => { + stub.withArgs(...withArgs).returns(Promise.resolve({stdout: returns})); + }); + return stub; + } }); }); \ No newline at end of file From 91414f62cfa5e260099a778b7a5c1097a562082f Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Mon, 9 Apr 2018 22:02:39 +1000 Subject: [PATCH 5/9] ref #44776 Introduced `deleteRef` method on git.Repository --- extensions/git/src/git.ts | 5 ++ extensions/git/src/test/git.test.ts | 92 +++++++++++++++++------------ 2 files changed, 58 insertions(+), 39 deletions(-) diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 3b22f956538..96c70dae28e 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -895,6 +895,11 @@ export class Repository { await this.run(args); } + async deleteRef(ref: string): Promise { + const args = ['update-ref', '-d', ref]; + await this.run(args); + } + async merge(ref: string): Promise { const args = ['merge', ref]; diff --git a/extensions/git/src/test/git.test.ts b/extensions/git/src/test/git.test.ts index ceab42b490c..375e787b823 100644 --- a/extensions/git/src/test/git.test.ts +++ b/extensions/git/src/test/git.test.ts @@ -179,55 +179,69 @@ suite('git', () => { suite('Repository', () => { const spawnOption = {}; - const GIT_OUTPUT_SINGLE_PARENT = `52c293a05038d865604c2284aa8698bd087915a1 + + suite('getCommit', () => { + const GIT_OUTPUT_SINGLE_PARENT = `52c293a05038d865604c2284aa8698bd087915a1 8e5a374372b8393906c7e380dbb09349c5385554 This is a commit message.`; - const GIT_OUTPUT_MULTIPLE_PARENTS = `52c293a05038d865604c2284aa8698bd087915a1 + const GIT_OUTPUT_MULTIPLE_PARENTS = `52c293a05038d865604c2284aa8698bd087915a1 8e5a374372b8393906c7e380dbb09349c5385554 df27d8c75b129ab9b178b386077da2822101b217 This is a commit message.`; - const GIT_OUTPUT_NO_PARENTS = `52c293a05038d865604c2284aa8698bd087915a1 + const GIT_OUTPUT_NO_PARENTS = `52c293a05038d865604c2284aa8698bd087915a1 This is a commit message.`; - const git = sinon.createStubInstance(Git); - git.exec = stub([ - { - withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_SINGLE_PARENT'], spawnOption], - returns: GIT_OUTPUT_SINGLE_PARENT - }, { - withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_MULTIPLE_PARENTS'], spawnOption], - returns: GIT_OUTPUT_MULTIPLE_PARENTS - }, { - withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_NO_PARENTS'], spawnOption], - returns: GIT_OUTPUT_NO_PARENTS + const git = sinon.createStubInstance(Git); + git.exec = stub([ + { + withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_SINGLE_PARENT'], spawnOption], + returns: GIT_OUTPUT_SINGLE_PARENT + }, { + withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_MULTIPLE_PARENTS'], spawnOption], + returns: GIT_OUTPUT_MULTIPLE_PARENTS + }, { + withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_NO_PARENTS'], spawnOption], + returns: GIT_OUTPUT_NO_PARENTS + } + ]); + const repository = new Repository(git, 'REPOSITORY_ROOT'); + + test('get commit', async () => { + assert.deepEqual(await repository.getCommit('REF_SINGLE_PARENT'), { + hash: '52c293a05038d865604c2284aa8698bd087915a1', + message: 'This is a commit message.', + previousHashes: ['8e5a374372b8393906c7e380dbb09349c5385554'] + }); + }); + + test('multiple previous commits', async () => { + const commit = await repository.getCommit('REF_MULTIPLE_PARENTS'); + assert.deepEqual(commit.previousHashes, ['8e5a374372b8393906c7e380dbb09349c5385554', 'df27d8c75b129ab9b178b386077da2822101b217']); + }); + + test('no previous commits', async () => { + const commit = await repository.getCommit('REF_NO_PARENTS'); + assert.deepEqual(commit.previousHashes, []); + }); + + function stub(argOutputPairs: {withArgs: any[], returns: string}[]): sinon.SinonStub { + const stub = sinon.stub(); + argOutputPairs.forEach(({withArgs, returns}) => { + stub.withArgs(...withArgs).returns(Promise.resolve({stdout: returns})); + }); + return stub; } - ]); - const repository = new Repository(git, 'REPOSITORY_ROOT'); + }); - test('get commit', async () => { - assert.deepEqual(await repository.getCommit('REF_SINGLE_PARENT'), { - hash: '52c293a05038d865604c2284aa8698bd087915a1', - message: 'This is a commit message.', - previousHashes: ['8e5a374372b8393906c7e380dbb09349c5385554'] + suite('deleteRef', () => { + const git = sinon.createStubInstance(Git); + git.exec = sinon.spy(); + const repository = new Repository(git, 'REPOSITORY_ROOT'); + + test('delete ref', async () => { + await repository.deleteRef('REF_TO_BE_DELETED'); + assert.deepEqual(git.exec.args, [['REPOSITORY_ROOT', ['update-ref', '-d', 'REF_TO_BE_DELETED'], spawnOption]]); }); }); - - test('multiple previous commits', async () => { - const commit = await repository.getCommit('REF_MULTIPLE_PARENTS'); - assert.deepEqual(commit.previousHashes, ['8e5a374372b8393906c7e380dbb09349c5385554', 'df27d8c75b129ab9b178b386077da2822101b217']); - }); - - test('no previous commits', async () => { - const commit = await repository.getCommit('REF_NO_PARENTS'); - assert.deepEqual(commit.previousHashes, []); - }); - - function stub(argOutputPairs: {withArgs: any[], returns: string}[]): sinon.SinonStub { - const stub = sinon.stub(); - argOutputPairs.forEach(({withArgs, returns}) => { - stub.withArgs(...withArgs).returns(Promise.resolve({stdout: returns})); - }); - return stub; - } }); }); \ No newline at end of file From 81b3b44413a09ed2c0103d1db34084850cd1134f Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Mon, 9 Apr 2018 22:06:07 +1000 Subject: [PATCH 6/9] ref #44776 Added DeleteRef operation --- extensions/git/src/repository.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index cca9f563060..b70df19cc79 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -292,6 +292,7 @@ export enum Operation { GetCommitTemplate = 'GetCommitTemplate', DeleteBranch = 'DeleteBranch', RenameBranch = 'RenameBranch', + DeleteRef = 'DeleteRef', Merge = 'Merge', Ignore = 'Ignore', Tag = 'Tag', @@ -722,6 +723,10 @@ export class Repository implements Disposable { await this.run(Operation.Reset, () => this.repository.reset(treeish, hard)); } + async deleteRef(ref: string): Promise { + await this.run(Operation.DeleteRef, () => this.repository.deleteRef(ref)); + } + @throttle async fetch(): Promise { await this.run(Operation.Fetch, () => this.repository.fetch()); From 278842c987856558adfd505e601cf761c261d4f7 Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Mon, 9 Apr 2018 22:07:59 +1000 Subject: [PATCH 7/9] ref #44776 deleteRef instead of reset if commit has no parents --- extensions/git/src/commands.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index e37383c4932..ccb441dded2 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -1123,7 +1123,12 @@ export class CommandCenter { } const commit = await repository.getCommit('HEAD'); - await repository.reset('HEAD~'); + if (commit.previousHashes.length > 0) { + await repository.reset('HEAD~'); + } else { + await repository.deleteRef('HEAD'); + await this.unstageAll(repository); + } repository.inputBox.value = commit.message; } From 6b7a644082f4aa17265558fc11c23261fbc4b89e Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Wed, 11 Apr 2018 20:02:20 +1000 Subject: [PATCH 8/9] ref #44776 Only test the logic of parsing `git show` output * Instead of testing it through `getCommit` as we don't use stub in test --- extensions/git/src/git.ts | 19 ++++---- extensions/git/src/test/git.test.ts | 72 +++++++++++------------------ 2 files changed, 39 insertions(+), 52 deletions(-) diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 96c70dae28e..c13078b4053 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -612,6 +612,16 @@ export function parseGitmodules(raw: string): Submodule[] { return result; } +export function parseGitCommit(raw: string): Commit | null { + const match = /^([0-9a-f]{40})\n(.*)\n([^]*)$/m.exec(raw.trim()); + if (!match) { + return null; + } + + const previousHashes = match[2] ? match[2].split(' ') : []; + return { hash: match[1], message: match[3], previousHashes }; +} + export interface DiffOptions { cached?: boolean; } @@ -1294,14 +1304,7 @@ export class Repository { async getCommit(ref: string): Promise { const result = await this.run(['show', '-s', '--format=%H\n%P\n%B', ref]); - const match = /^([0-9a-f]{40})\n(.*)\n([^]*)$/m.exec(result.stdout.trim()); - - if (!match) { - return Promise.reject('bad commit format'); - } - - const previousHashes = match[2] ? match[2].split(' ') : []; - return { hash: match[1], message: match[3], previousHashes }; + return parseGitCommit(result.stdout) || Promise.reject('bad commit format'); } async updateSubmodules(paths: string[]): Promise { diff --git a/extensions/git/src/test/git.test.ts b/extensions/git/src/test/git.test.ts index 375e787b823..1e28ac305f1 100644 --- a/extensions/git/src/test/git.test.ts +++ b/extensions/git/src/test/git.test.ts @@ -6,7 +6,7 @@ 'use strict'; import 'mocha'; -import { Git, GitStatusParser, Repository, parseGitmodules } from '../git'; +import { Git, GitStatusParser, Repository, parseGitCommit, parseGitmodules } from '../git'; import * as assert from 'assert'; import * as sinon from 'sinon'; @@ -177,63 +177,47 @@ suite('git', () => { }); }); - suite('Repository', () => { - const spawnOption = {}; - - suite('getCommit', () => { + suite('parseGitCommit', () => { + test('single previous commit', () => { const GIT_OUTPUT_SINGLE_PARENT = `52c293a05038d865604c2284aa8698bd087915a1 8e5a374372b8393906c7e380dbb09349c5385554 This is a commit message.`; + + assert.deepEqual(parseGitCommit(GIT_OUTPUT_SINGLE_PARENT), { + hash: '52c293a05038d865604c2284aa8698bd087915a1', + message: 'This is a commit message.', + previousHashes: ['8e5a374372b8393906c7e380dbb09349c5385554'] + }); + }); + + test('multiple previous commits', () => { const GIT_OUTPUT_MULTIPLE_PARENTS = `52c293a05038d865604c2284aa8698bd087915a1 8e5a374372b8393906c7e380dbb09349c5385554 df27d8c75b129ab9b178b386077da2822101b217 This is a commit message.`; + + assert.deepEqual(parseGitCommit(GIT_OUTPUT_MULTIPLE_PARENTS), { + hash: '52c293a05038d865604c2284aa8698bd087915a1', + message: 'This is a commit message.', + previousHashes: ['8e5a374372b8393906c7e380dbb09349c5385554', 'df27d8c75b129ab9b178b386077da2822101b217'] + }); + }); + + test('no previous commits', async () => { const GIT_OUTPUT_NO_PARENTS = `52c293a05038d865604c2284aa8698bd087915a1 This is a commit message.`; - const git = sinon.createStubInstance(Git); - git.exec = stub([ - { - withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_SINGLE_PARENT'], spawnOption], - returns: GIT_OUTPUT_SINGLE_PARENT - }, { - withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_MULTIPLE_PARENTS'], spawnOption], - returns: GIT_OUTPUT_MULTIPLE_PARENTS - }, { - withArgs: ['REPOSITORY_ROOT', ['show', '-s', '--format=%H\n%P\n%B', 'REF_NO_PARENTS'], spawnOption], - returns: GIT_OUTPUT_NO_PARENTS - } - ]); - const repository = new Repository(git, 'REPOSITORY_ROOT'); - - test('get commit', async () => { - assert.deepEqual(await repository.getCommit('REF_SINGLE_PARENT'), { - hash: '52c293a05038d865604c2284aa8698bd087915a1', - message: 'This is a commit message.', - previousHashes: ['8e5a374372b8393906c7e380dbb09349c5385554'] - }); + assert.deepEqual(parseGitCommit(GIT_OUTPUT_NO_PARENTS), { + hash: '52c293a05038d865604c2284aa8698bd087915a1', + message: 'This is a commit message.', + previousHashes: [] }); - - test('multiple previous commits', async () => { - const commit = await repository.getCommit('REF_MULTIPLE_PARENTS'); - assert.deepEqual(commit.previousHashes, ['8e5a374372b8393906c7e380dbb09349c5385554', 'df27d8c75b129ab9b178b386077da2822101b217']); - }); - - test('no previous commits', async () => { - const commit = await repository.getCommit('REF_NO_PARENTS'); - assert.deepEqual(commit.previousHashes, []); - }); - - function stub(argOutputPairs: {withArgs: any[], returns: string}[]): sinon.SinonStub { - const stub = sinon.stub(); - argOutputPairs.forEach(({withArgs, returns}) => { - stub.withArgs(...withArgs).returns(Promise.resolve({stdout: returns})); - }); - return stub; - } }); + }); + suite('Repository', () => { suite('deleteRef', () => { + const spawnOption = {}; const git = sinon.createStubInstance(Git); git.exec = sinon.spy(); const repository = new Repository(git, 'REPOSITORY_ROOT'); From a02823851b27227a3252498e278d1eb1a4a2171a Mon Sep 17 00:00:00 2001 From: Ryuichi Inagaki Date: Wed, 11 Apr 2018 20:03:39 +1000 Subject: [PATCH 9/9] ref #44776 Removed the test for deleteRef method * as we don't use stub in test --- extensions/git/src/test/git.test.ts | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/extensions/git/src/test/git.test.ts b/extensions/git/src/test/git.test.ts index 1e28ac305f1..a1c5e196cfa 100644 --- a/extensions/git/src/test/git.test.ts +++ b/extensions/git/src/test/git.test.ts @@ -6,9 +6,8 @@ 'use strict'; import 'mocha'; -import { Git, GitStatusParser, Repository, parseGitCommit, parseGitmodules } from '../git'; +import { GitStatusParser, parseGitCommit, parseGitmodules } from '../git'; import * as assert from 'assert'; -import * as sinon from 'sinon'; suite('git', () => { suite('GitStatusParser', () => { @@ -214,18 +213,4 @@ This is a commit message.`; }); }); }); - - suite('Repository', () => { - suite('deleteRef', () => { - const spawnOption = {}; - const git = sinon.createStubInstance(Git); - git.exec = sinon.spy(); - const repository = new Repository(git, 'REPOSITORY_ROOT'); - - test('delete ref', async () => { - await repository.deleteRef('REF_TO_BE_DELETED'); - assert.deepEqual(git.exec.args, [['REPOSITORY_ROOT', ['update-ref', '-d', 'REF_TO_BE_DELETED'], spawnOption]]); - }); - }); - }); }); \ No newline at end of file