-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: build failure message from invalid hunks (#90)
* 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
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/github-handler/comment-handler/invalid-hunk-handler/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |