Skip to content

Commit

Permalink
feat: build failure message from invalid hunks (#90)
Browse files Browse the repository at this point in the history
* test: add failing stub and test for building the failure message

* fix: implement message building

* fix: use original line numbers in error message

* docs: add docstring

* docs: add note about empty input returning empty string
  • Loading branch information
chingor13 committed Aug 21, 2020
1 parent 5cc566e commit ecdb615
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/github-handler/comment-handler/invalid-hunk-handler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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';

function hunkErrorMessage(hunk: Hunk): string {
return ` * lines ${hunk.oldStart}-${hunk.oldEnd}`;
}

function fileErrorMessage(filename: string, hunks: Hunk[]): string {
return `* ${filename}\n` + hunks.map(hunkErrorMessage).join('\n');
}

/**
* 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, Hunk[]>): string {
if (invalidHunks.size === 0) {
return '';
}
return (
'Some suggestions could not be made:\n' +
Array.from(invalidHunks, ([filename, hunks]) =>
fileErrorMessage(filename, hunks)
).join('\n')
);
}
65 changes: 65 additions & 0 deletions test/invalid-hunks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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
* lines 1-2
* bar.txt
* lines 3-4`;

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
* lines 1-2
* lines 3-4`;

const errorMessage = buildErrorMessage(invalidHunks);
expect(errorMessage).to.be.equal(expectedMessage);
});
});

0 comments on commit ecdb615

Please sign in to comment.