From 97fe2058c6efdb9c348ff4baf5b8d9f517bcfc3d Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Fri, 21 Aug 2020 12:19:23 -0700 Subject: [PATCH 1/5] test: add failing stub and test for building the failure message --- .../invalid-hunk-handler/index.ts | 19 ++++++ test/invalid-hunks.ts | 61 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/github-handler/comment-handler/invalid-hunk-handler/index.ts create mode 100644 test/invalid-hunks.ts diff --git a/src/github-handler/comment-handler/invalid-hunk-handler/index.ts b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts new file mode 100644 index 00000000..309cbbe5 --- /dev/null +++ b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts @@ -0,0 +1,19 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import {Hunk} from '../../../types'; + +export function buildErrorMessage(invalidHunks: Map): string { + return 'FIXME'; +} diff --git a/test/invalid-hunks.ts b/test/invalid-hunks.ts new file mode 100644 index 00000000..3302f616 --- /dev/null +++ b/test/invalid-hunks.ts @@ -0,0 +1,61 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import {expect} from 'chai'; +import {describe, it, before} from 'mocha'; +import {setup} from './util'; +import {buildErrorMessage} from '../src/github-handler/comment-handler/invalid-hunk-handler'; + +before(() => { + setup(); +}); + +describe('buildErrorMessage', () => { + it('should handle an empty list of failures', () => { + const invalidHunks = new Map(); + const expectedMessage = ''; + + const errorMessage = buildErrorMessage(invalidHunks); + expect(errorMessage).to.be.equal(expectedMessage); + }); + + it('should handle multiple file entries', () => { + const invalidHunks = new Map(); + invalidHunks.set('foo.txt', [ + {oldStart: 1, oldEnd: 2, newStart: 1, newEnd: 2}, + ]); + invalidHunks.set('bar.txt', [ + {oldStart: 3, oldEnd: 4, newStart: 3, newEnd: 4}, + ]); + const expectedMessage = `Some suggestions could not be made: +* foo.txt +* bar.txt`; + + const errorMessage = buildErrorMessage(invalidHunks); + expect(errorMessage).to.be.equal(expectedMessage); + }); + + it('should handle multiple entries for a file', () => { + const invalidHunks = new Map(); + invalidHunks.set('foo.txt', [ + {oldStart: 1, oldEnd: 2, newStart: 1, newEnd: 2}, + {oldStart: 3, oldEnd: 4, newStart: 3, newEnd: 4}, + ]); + const expectedMessage = `Some suggestions could not be made: + * foo.txt`; + + const errorMessage = buildErrorMessage(invalidHunks); + expect(errorMessage).to.be.equal(expectedMessage); + }); +}); From db0cc8d8b59b8be82823801a9c60ebd465d50298 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Fri, 21 Aug 2020 12:31:41 -0700 Subject: [PATCH 2/5] fix: implement message building --- .../comment-handler/invalid-hunk-handler/index.ts | 14 +++++++++++++- test/invalid-hunks.ts | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/github-handler/comment-handler/invalid-hunk-handler/index.ts b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts index 309cbbe5..87b2b286 100644 --- a/src/github-handler/comment-handler/invalid-hunk-handler/index.ts +++ b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts @@ -14,6 +14,18 @@ import {Hunk} from '../../../types'; +function fileErrorMessage(filename: string, hunks: Hunk[]): string { + return `* ${filename}`; +} + export function buildErrorMessage(invalidHunks: Map): string { - return 'FIXME'; + if (invalidHunks.size === 0) { + return ''; + } + return ( + 'Some suggestions could not be made:\n' + + Array.from(invalidHunks, ([filename, hunks]) => + fileErrorMessage(filename, hunks) + ).join('\n') + ); } diff --git a/test/invalid-hunks.ts b/test/invalid-hunks.ts index 3302f616..c05d1a9e 100644 --- a/test/invalid-hunks.ts +++ b/test/invalid-hunks.ts @@ -53,7 +53,7 @@ describe('buildErrorMessage', () => { {oldStart: 3, oldEnd: 4, newStart: 3, newEnd: 4}, ]); const expectedMessage = `Some suggestions could not be made: - * foo.txt`; +* foo.txt`; const errorMessage = buildErrorMessage(invalidHunks); expect(errorMessage).to.be.equal(expectedMessage); From b922677d2a44c5269977d2b3f86f0ca854182041 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Fri, 21 Aug 2020 13:51:11 -0700 Subject: [PATCH 3/5] fix: use original line numbers in error message --- .../comment-handler/invalid-hunk-handler/index.ts | 6 +++++- test/invalid-hunks.ts | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/github-handler/comment-handler/invalid-hunk-handler/index.ts b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts index 87b2b286..24c78952 100644 --- a/src/github-handler/comment-handler/invalid-hunk-handler/index.ts +++ b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts @@ -14,8 +14,12 @@ import {Hunk} from '../../../types'; +function hunkErrorMessage(hunk: Hunk): string { + return ` * lines ${hunk.oldStart}-${hunk.oldEnd}`; +} + function fileErrorMessage(filename: string, hunks: Hunk[]): string { - return `* ${filename}`; + return `* ${filename}\n` + hunks.map(hunkErrorMessage).join('\n'); } export function buildErrorMessage(invalidHunks: Map): string { diff --git a/test/invalid-hunks.ts b/test/invalid-hunks.ts index c05d1a9e..5202a8c5 100644 --- a/test/invalid-hunks.ts +++ b/test/invalid-hunks.ts @@ -40,7 +40,9 @@ describe('buildErrorMessage', () => { ]); const expectedMessage = `Some suggestions could not be made: * foo.txt -* bar.txt`; + * lines 1-2 +* bar.txt + * lines 3-4`; const errorMessage = buildErrorMessage(invalidHunks); expect(errorMessage).to.be.equal(expectedMessage); @@ -53,7 +55,9 @@ describe('buildErrorMessage', () => { {oldStart: 3, oldEnd: 4, newStart: 3, newEnd: 4}, ]); const expectedMessage = `Some suggestions could not be made: -* foo.txt`; +* foo.txt + * lines 1-2 + * lines 3-4`; const errorMessage = buildErrorMessage(invalidHunks); expect(errorMessage).to.be.equal(expectedMessage); From aab08a841c691da089b822f67c205dfbd01bf6ca Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Fri, 21 Aug 2020 13:59:55 -0700 Subject: [PATCH 4/5] docs: add docstring --- .../comment-handler/invalid-hunk-handler/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/github-handler/comment-handler/invalid-hunk-handler/index.ts b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts index 24c78952..49ca5fe5 100644 --- a/src/github-handler/comment-handler/invalid-hunk-handler/index.ts +++ b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts @@ -22,6 +22,10 @@ function fileErrorMessage(filename: string, hunks: Hunk[]): string { return `* ${filename}\n` + hunks.map(hunkErrorMessage).join('\n'); } +/** + * Build an error message based on invalid hunks. + * @param invalidHunks a map of filename to hunks that are not suggestable + */ export function buildErrorMessage(invalidHunks: Map): string { if (invalidHunks.size === 0) { return ''; From b1403e6386142d15e9ea33f29bb4b4c94eedff28 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Fri, 21 Aug 2020 14:20:14 -0700 Subject: [PATCH 5/5] docs: add note about empty input returning empty string --- src/github-handler/comment-handler/invalid-hunk-handler/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/github-handler/comment-handler/invalid-hunk-handler/index.ts b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts index 49ca5fe5..f61b8b1d 100644 --- a/src/github-handler/comment-handler/invalid-hunk-handler/index.ts +++ b/src/github-handler/comment-handler/invalid-hunk-handler/index.ts @@ -24,6 +24,7 @@ function fileErrorMessage(filename: string, hunks: Hunk[]): string { /** * Build an error message based on invalid hunks. + * Returns an empty string if the provided hunks are empty. * @param invalidHunks a map of filename to hunks that are not suggestable */ export function buildErrorMessage(invalidHunks: Map): string {