Skip to content

Commit

Permalink
Merge pull requets #22 from adangel:windows
Browse files Browse the repository at this point in the history
Call pmd.bat under win32 #22

* pr-22:
  Call pmd.bat under win32
  • Loading branch information
adangel committed Dec 10, 2021
2 parents 3cd2913 + b033830 commit 8141cb4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const github = require('@actions/github');
const tc = require('@actions/tool-cache');
const exec = require('@actions/exec');
const semver = require('semver');
const os = require('os');

const downloadPmd = async function(version, token) {
let pmdVersion = version;
Expand All @@ -24,9 +25,12 @@ const downloadPmd = async function(version, token) {
}

const executePmd = async function(pmdInfo, sourcePath, ruleset, reportFormat, reportFile) {
const execOutput = await exec.getExecOutput(`${pmdInfo.path}/bin/run.sh`,
let pmdExecutable = '/bin/run.sh pmd';
if (os.platform() === 'win32') {
pmdExecutable = '\\bin\\pmd.bat';
}
const execOutput = await exec.getExecOutput(`${pmdInfo.path}${pmdExecutable}`,
[
'pmd',
useNewArgsFormat(pmdInfo.version) ? '--no-cache' : '-no-cache',
'-d', sourcePath,
'-f', reportFormat,
Expand Down
41 changes: 38 additions & 3 deletions tests/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const nock = require('nock');
const io = require('@actions/io');
const os = require('os');
const fs = require('fs');
const exec = require('@actions/exec');
const util = require('../lib/util');

const cachePath = path.join(__dirname, 'CACHE')
Expand All @@ -13,19 +14,26 @@ process.env['RUNNER_TEMP'] = tempPath
process.env['RUNNER_TOOL_CACHE'] = cachePath

describe('pmd-github-action-util', function () {
beforeAll(function () {
let platformMock;
let execMock;

beforeAll(function() {
setGlobal('TEST_DOWNLOAD_TOOL_RETRY_MIN_SECONDS', 0)
setGlobal('TEST_DOWNLOAD_TOOL_RETRY_MAX_SECONDS', 0)
})

beforeEach(async function () {
platformMock = jest.spyOn(os, 'platform');
execMock = jest.spyOn(exec, 'getExecOutput');
await io.rmRF(cachePath)
await io.rmRF(tempPath)
await io.mkdirP(cachePath)
await io.mkdirP(tempPath)
})

afterEach(function () {
platformMock.mockRestore();
execMock.mockRestore();
})

afterAll(async function () {
Expand Down Expand Up @@ -136,11 +144,38 @@ describe('pmd-github-action-util', function () {
.reply(503, 'Test Internal Server Error');

expect(() => util.downloadPmd('latest', 'my_test_token')).rejects.toThrow();
});
})

it('failure while executing PMD', async () => {
expect(() => util.executePmd({ path: 'non-existing-pmd-path' }, '.', 'ruleset.xml', 'sarif', 'pmd-report.sarif')).rejects.toThrow();
});
})

it('can execute PMD win32', async () => {
platformMock.mockReturnValueOnce('win32');
execMock.mockReturnValueOnce({ exitCode: 0, stdout: '', stderr: '' });
nock('https://api.github.com')
.get('/repos/pmd/pmd/releases/latest')
.replyWithFile(200, __dirname + '/data/releases-latest.json', {
'Content-Type': 'application/json',
})
nock('https://github.com')
.get('/pmd/pmd/releases/download/pmd_releases/6.40.0/pmd-bin-6.40.0.zip')
.replyWithFile(200, __dirname + '/data/pmd-bin-6.40.0.zip')

const pmdInfo = await util.downloadPmd('latest', 'my_test_token');
await util.executePmd(pmdInfo, '.', 'ruleset.xml', 'sarif', 'pmd-report.sarif');

expect(execMock).toBeCalledWith(`${pmdInfo.path}\\bin\\pmd.bat`, [
'-no-cache',
'-d', '.',
'-f', 'sarif',
'-R', 'ruleset.xml',
'-r', 'pmd-report.sarif',
],
{
ignoreReturnCode: true
});
})
});

function setGlobal(key, value) {
Expand Down

0 comments on commit 8141cb4

Please sign in to comment.