Skip to content

Commit

Permalink
Fixes #52 sourcePath without trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Feb 19, 2022
1 parent 0abbef9 commit b2cb5ed
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ function extractFilenames(allFiles, page, sourcePath) {
core.debug(` ${i}: ${allFiles[i].status} ${allFiles[i].filename}`);
}
}
// add trailing slash
sourcePath = sourcePath !== '.' ? path.normalize(`${sourcePath}/`) : sourcePath;
const filenames = allFiles
.filter(f => f.status === 'added' || f.status === 'changed' || f.status === 'modified')
.map(f => path.normalize(f.filename))
Expand Down
38 changes: 38 additions & 0 deletions tests/data/compare-files-page1-issue52.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"url": "https://api.github.com/repos/pmd/pmd-github-action-tests/compare/44c1557134c7dbaf46ecdf796fb871c8df8989e4...8a7a25638d8ca5207cc824dea9571325b243c6a1",
"status": "ahead",
"ahead_by": 2,
"behind_by": 0,
"total_commits": 2,
"commits": [],
"files": [
{
"sha": "879c25e370bb62b991c95adf0e4becf220d394fb",
"filename": "src/main/java/AvoidCatchingThrowableSample.java",
"status": "modified",
"additions": 2,
"deletions": 1,
"changes": 3,
"blob_url": "https://github.com/pmd/pmd-github-action-tests/blob/cfe417bd477804d0dd4cd9a6fd56e0bee5235b3f/src/main/java/AvoidCatchingThrowableSample.java",
"raw_url": "https://github.com/pmd/pmd-github-action-tests/raw/cfe417bd477804d0dd4cd9a6fd56e0bee5235b3f/src/main/java/AvoidCatchingThrowableSample.java",
"contents_url": "https://api.github.com/repos/pmd/pmd-github-action-tests/contents/src/main/java/AvoidCatchingThrowableSample.java?ref=cfe417bd477804d0dd4cd9a6fd56e0bee5235b3f",
"patch": "@@ -1,9 +1,10 @@\n public class AvoidCatchingThrowableSample {\n+ \n public void bar() {\n try {\n // do something\n } catch (Throwable th) { // should not catch Throwable\n th.printStackTrace();\n }\n }\n-}\n\\ No newline at end of file\n+}"
},
{
"filename": "src/main/java/DeletedFile.java",
"status": "removed"
},
{
"filename": "src/main/java2/NewFile.java",
"status": "added"
},
{
"filename": "src/main/java/ChangedFile.java",
"status": "changed"
},
{
"filename": "README.md",
"status": "modified"
}
]
}
54 changes: 54 additions & 0 deletions tests/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,60 @@ describe('pmd-github-action-util', function () {
expect(fileList).toStrictEqual(['src/main/java/AvoidCatchingThrowableSample.java', 'src/main/java/NewFile.java', 'src/main/java/ChangedFile.java']
.map(f => path.normalize(f)));
})

test('sourcePath is applied correctly for analyzeModifiedFiles - issue #52', async () => {
// disable ACTIONS_STEP_DEBUG
delete process.env['RUNNER_DEBUG'];
process.env['GITHUB_REPOSITORY'] = 'pmd/pmd-github-action-tests'
process.env['GITHUB_EVENT_NAME'] = 'push';
process.env['GITHUB_EVENT_PATH'] = __dirname + '/data/push-event-data.json';
for (let page = 1; page <= 10; page++) {
nock('https://api.github.com')
.get(`/repos/pmd/pmd-github-action-tests/compare/44c1557134c7dbaf46ecdf796fb871c8df8989e4...8a7a25638d8ca5207cc824dea9571325b243c6a1?per_page=30&page=${page}`)
.replyWithFile(200, __dirname + '/data/compare-files-page1-issue52.json', {
'Content-Type': 'application/json',
});
}
let fileList = await util.determineModifiedFiles('my_test_token', path.normalize('src/main/java'));
expect(fileList).toStrictEqual(['src/main/java/AvoidCatchingThrowableSample.java', 'src/main/java/ChangedFile.java']
.map(f => path.normalize(f)));
})

test('sourcePath with trailing slash is applied correctly for analyzeModifiedFiles - issue #52', async () => {
// disable ACTIONS_STEP_DEBUG
delete process.env['RUNNER_DEBUG'];
process.env['GITHUB_REPOSITORY'] = 'pmd/pmd-github-action-tests'
process.env['GITHUB_EVENT_NAME'] = 'push';
process.env['GITHUB_EVENT_PATH'] = __dirname + '/data/push-event-data.json';
for (let page = 1; page <= 10; page++) {
nock('https://api.github.com')
.get(`/repos/pmd/pmd-github-action-tests/compare/44c1557134c7dbaf46ecdf796fb871c8df8989e4...8a7a25638d8ca5207cc824dea9571325b243c6a1?per_page=30&page=${page}`)
.replyWithFile(200, __dirname + '/data/compare-files-page1-issue52.json', {
'Content-Type': 'application/json',
});
}
let fileList = await util.determineModifiedFiles('my_test_token', path.normalize('src/main/java/'));
expect(fileList).toStrictEqual(['src/main/java/AvoidCatchingThrowableSample.java', 'src/main/java/ChangedFile.java']
.map(f => path.normalize(f)));
})

test('sourcePath with current dir is applied correctly for analyzeModifiedFiles - issue #52', async () => {
// disable ACTIONS_STEP_DEBUG
delete process.env['RUNNER_DEBUG'];
process.env['GITHUB_REPOSITORY'] = 'pmd/pmd-github-action-tests'
process.env['GITHUB_EVENT_NAME'] = 'push';
process.env['GITHUB_EVENT_PATH'] = __dirname + '/data/push-event-data.json';
for (let page = 1; page <= 10; page++) {
nock('https://api.github.com')
.get(`/repos/pmd/pmd-github-action-tests/compare/44c1557134c7dbaf46ecdf796fb871c8df8989e4...8a7a25638d8ca5207cc824dea9571325b243c6a1?per_page=30&page=${page}`)
.replyWithFile(200, __dirname + '/data/compare-files-page1-issue52.json', {
'Content-Type': 'application/json',
});
}
let fileList = await util.determineModifiedFiles('my_test_token', path.normalize('.'));
expect(fileList).toStrictEqual(['src/main/java/AvoidCatchingThrowableSample.java', 'src/main/java2/NewFile.java', 'src/main/java/ChangedFile.java', 'README.md']
.map(f => path.normalize(f)));
})
});

function setGlobal(key, value) {
Expand Down

0 comments on commit b2cb5ed

Please sign in to comment.