|
| 1 | +const core = require('@actions/core'); |
| 2 | +const path = require('path'); |
| 3 | +const sarif = require('../lib/sarif'); |
| 4 | +const annotations = require('../lib/annotations'); |
| 5 | + |
| 6 | +core.error = jest.fn(); |
| 7 | +core.warning = jest.fn(); |
| 8 | +core.notice = jest.fn(); |
| 9 | +process.cwd = jest.fn(); |
| 10 | + |
| 11 | +describe('pmd-github-action-annotations', function () { |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + core.error.mockClear(); |
| 15 | + core.warning.mockClear(); |
| 16 | + core.notice.mockClear(); |
| 17 | + process.cwd.mockClear(); |
| 18 | + process.cwd.mockReturnValue('/folder'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('can create annotation', () => { |
| 22 | + const report = sarif.loadReport(path.join(__dirname, 'data', 'pmd-report.sarif')); |
| 23 | + |
| 24 | + annotations.processSarifReport(report); |
| 25 | + |
| 26 | + expect(core.notice).toHaveBeenCalledTimes(1); |
| 27 | + expect(core.notice).toHaveBeenCalledWith(`Detects when a local variable is declared and/or assigned but not used. |
| 28 | +Second line. |
| 29 | + Third line with additional indentation. |
| 30 | + Fourth line with less indentation. |
| 31 | +
|
| 32 | +UnusedLocalVariable (Priority: 5, Ruleset: Best Practices) |
| 33 | +https://pmd.github.io/pmd-6.40.0/pmd_rules_apex_bestpractices.html#unusedlocalvariable`, { |
| 34 | + title: 'Variable \'x\' defined but not used', |
| 35 | + file: '/home/andreas/PMD/source/pmd-github-action-test/src/classes/UnusedLocalVariableSample.cls', |
| 36 | + startLine: 3, |
| 37 | + endLine: 3 |
| 38 | + }); |
| 39 | + expect(core.error).not.toHaveBeenCalled(); |
| 40 | + expect(core.warning).not.toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('can deal with null report', () => { |
| 44 | + annotations.processSarifReport(null); |
| 45 | + expect(core.notice).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('can deal with error, warning and notice', () => { |
| 49 | + const report = sarif.loadReport(path.join(__dirname, 'data', 'pmd-report-priorities.sarif')); |
| 50 | + annotations.processSarifReport(report); |
| 51 | + |
| 52 | + expect(core.error).toHaveBeenCalledTimes(2); |
| 53 | + expect(core.error).toHaveBeenNthCalledWith(1, 'Full description for High Prio Rule\n\n0 - high prio rule (Priority: 1, Ruleset: sample ruleset)\nhttps://pmd.github.io/latest/ruleHighPrio', { title: 'High Prio Rule', file: 'file1.txt', startLine: 4, endLine: 5 }); |
| 54 | + expect(core.error).toHaveBeenNthCalledWith(2, 'Full description for Medium High Prio Rule\n\n1 - medium high prio rule (Priority: 2, Ruleset: sample ruleset)\nhttps://pmd.github.io/latest/ruleMediumHighPrio', { title: 'Medium High Prio Rule', file: 'dir/file2.txt', startLine: 5 }); |
| 55 | + expect(core.warning).toHaveBeenCalledTimes(2); |
| 56 | + expect(core.warning).toHaveBeenNthCalledWith(1, 'Full description for Medium Prio Rule\n\n2 - medium prio rule (Priority: 3, Ruleset: sample ruleset)\nhttps://pmd.github.io/latest/ruleMediumPrio', { title: 'Medium Prio Rule', file: 'file3.txt', startLine: 6 }); |
| 57 | + expect(core.warning).toHaveBeenNthCalledWith(2, 'Full description for Medium Low Prio Rule\n\n3 - medium low prio rule (Priority: 4, Ruleset: sample ruleset)\nhttps://pmd.github.io/latest/ruleMediumLowPrio', { title: 'Medium Low Prio Rule', file: 'file4.txt', startLine: 7 }); |
| 58 | + expect(core.notice).toHaveBeenCalledTimes(2); |
| 59 | + expect(core.notice).toHaveBeenNthCalledWith(1, 'Full description for Low Prio Rule\n\n4 - low prio rule (Priority: 5, Ruleset: sample ruleset)\nhttps://pmd.github.io/latest/ruleLowPrio', { title: 'Low Prio Rule', file: 'file5.txt', startLine: 8 }); |
| 60 | + expect(core.notice).toHaveBeenNthCalledWith(2, 'Full description for Low Prio Rule\n\n4 - low prio rule (Priority: 5, Ruleset: sample ruleset)\nhttps://pmd.github.io/latest/ruleLowPrio', { title: 'Low Prio Rule', file: 'file6.txt', startLine: 9 }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments