From 8f6856d6e9c6a327d04f4f3394c809e28cab4419 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Thu, 25 Apr 2024 12:34:39 -0600 Subject: [PATCH] Extract some code from checkstyle action.ts for (hopefully) easier debugging Signed-off-by: Taylor Smock --- actions/checkstyle/dist/index.js | 47 ++++------- actions/checkstyle/src/action.ts | 84 +------------------ .../{action.test.ts => checkstyle.test.ts} | 7 +- actions/checkstyle/src/checkstyle.ts | 82 ++++++++++++++++++ package-lock.json | 3 +- 5 files changed, 110 insertions(+), 113 deletions(-) rename actions/checkstyle/src/{action.test.ts => checkstyle.test.ts} (84%) create mode 100644 actions/checkstyle/src/checkstyle.ts diff --git a/actions/checkstyle/dist/index.js b/actions/checkstyle/dist/index.js index 3322023..66a5dde 100644 --- a/actions/checkstyle/dist/index.js +++ b/actions/checkstyle/dist/index.js @@ -28909,23 +28909,6 @@ module.exports = parseParams /******/ } /******/ /************************************************************************/ -/******/ /* webpack/runtime/define property getters */ -/******/ (() => { -/******/ // define getter functions for harmony exports -/******/ __nccwpck_require__.d = (exports, definition) => { -/******/ for(var key in definition) { -/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) { -/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); -/******/ } -/******/ } -/******/ }; -/******/ })(); -/******/ -/******/ /* webpack/runtime/hasOwnProperty shorthand */ -/******/ (() => { -/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) -/******/ })(); -/******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports @@ -28949,11 +28932,6 @@ var __webpack_exports__ = {}; // ESM COMPAT FLAG __nccwpck_require__.r(__webpack_exports__); -// EXPORTS -__nccwpck_require__.d(__webpack_exports__, { - "parseData": () => (/* binding */ action_parseData) -}); - // EXTERNAL MODULE: ../../node_modules/@actions/core/lib/core.js var core = __nccwpck_require__(7117); // EXTERNAL MODULE: external "fs" @@ -29029,13 +29007,13 @@ run().catch((err) => (0,core.setFailed)(err)); // EXTERNAL MODULE: external "path" var external_path_ = __nccwpck_require__(1017); -;// CONCATENATED MODULE: ./src/action.ts +;// CONCATENATED MODULE: ./src/checkstyle.ts -function action_parseFile(trim, fileData) { +function checkstyle_parseFile(trim, fileData) { let subFile = fileData["@_name"].split(external_path_.sep); let currentlyTrimmed = 0; while (!(0,external_fs_.existsSync)((0,external_path_.join)(...subFile))) { @@ -29066,7 +29044,7 @@ function action_parseFile(trim, fileData) { } return problems; } -function action_parseData(trim, data) { +function checkstyle_parseData(trim, data) { const alwaysArray = ["checkstyle.file", "checkstyle.file.error"]; const parser = new fxp.XMLParser({ ignoreAttributes: false, @@ -29078,13 +29056,11 @@ function action_parseData(trim, data) { const files = parsed["checkstyle"]["file"]; let problems = []; for (const file of files) { - problems = problems.concat(action_parseFile(trim, file)); + problems = problems.concat(checkstyle_parseFile(trim, file)); } return problems; } -async function action_run() { - const checkstyleFile = (0,core.getInput)("file"); - const pathTrim = (0,core.getInput)("pathTrim"); +function checkstyle_run(pathTrim, checkstyleFile) { let trim; if (pathTrim === "") { trim = process.cwd().split(external_path_.sep).length; @@ -29094,10 +29070,19 @@ async function action_run() { } const data = (0,external_fs_.readFileSync)(checkstyleFile); (0,core.debug)(data.toString()); - logProblems(action_parseData(trim, data)); + logProblems(checkstyle_parseData(trim, data)); +} + +;// CONCATENATED MODULE: ./src/action.ts + + +async function action_run() { + const checkstyleFile = (0,core.getInput)("file"); + const pathTrim = (0,core.getInput)("pathTrim"); + checkstyle_run(checkstyleFile, pathTrim); } action_run().catch((err) => { - console.log(err); + console.log(err.stack); (0,core.setFailed)(err); }); diff --git a/actions/checkstyle/src/action.ts b/actions/checkstyle/src/action.ts index bb2fd0a..872ba7f 100644 --- a/actions/checkstyle/src/action.ts +++ b/actions/checkstyle/src/action.ts @@ -1,89 +1,13 @@ -import { debug, getInput, setFailed } from "@actions/core"; -import { readFileSync, existsSync } from "fs"; -import { XMLParser } from "fast-xml-parser"; -import { Problem } from "pmd"; -import { logProblems } from "pmd"; -import { sep, join } from "path"; - -interface Violations { - "@_source": string; - "@_message": string; - "@_severity": string; - "@_column": string; - "@_line": string; -} - -interface FileData { - "@_name": string; - error?: Array; -} - -function parseFile(trim: number, fileData: FileData): Problem[] { - let subFile = fileData["@_name"].split(sep); - let currentlyTrimmed = 0; - while (!existsSync(join(...subFile))) { - subFile = subFile.slice(1); - currentlyTrimmed += 1; - if (currentlyTrimmed >= trim) { - break; - } - } - const file = join(...subFile); - const violations = fileData["error"]; - const problems: Problem[] = []; - if ( - violations !== undefined && - violations !== null && - violations.length !== 0 - ) { - for (const violation of violations) { - const title = violation["@_source"]; - problems.push({ - file: file, - title: title, - startColumn: parseInt(violation["@_column"]), - endColumn: parseInt(violation["@_column"]), - startLine: parseInt(violation["@_line"]), - endLine: parseInt(violation["@_line"]), - info: violation["@_message"], - }); - } - } - return problems; -} - -export function parseData(trim: number, data: string | Buffer): Problem[] { - const alwaysArray = ["checkstyle.file", "checkstyle.file.error"]; - const parser = new XMLParser({ - ignoreAttributes: false, - isArray: (name, jpath) => { - return alwaysArray.indexOf(jpath) >= 0; - }, - }); - const parsed = parser.parse(data); - const files = parsed["checkstyle"]["file"]; - let problems: Problem[] = []; - for (const file of files) { - problems = problems.concat(parseFile(trim, file)); - } - return problems; -} +import { getInput, setFailed } from "@actions/core"; +import { run as realRun } from "./checkstyle"; async function run(): Promise { const checkstyleFile = getInput("file"); const pathTrim = getInput("pathTrim"); - let trim: number; - if (pathTrim === "") { - trim = process.cwd().split(sep).length; - } else { - trim = parseInt(pathTrim); - } - const data = readFileSync(checkstyleFile); - debug(data.toString()); - logProblems(parseData(trim, data)); + realRun(checkstyleFile, pathTrim); } run().catch((err) => { - console.log(err); + console.log(err.stack); setFailed(err); }); diff --git a/actions/checkstyle/src/action.test.ts b/actions/checkstyle/src/checkstyle.test.ts similarity index 84% rename from actions/checkstyle/src/action.test.ts rename to actions/checkstyle/src/checkstyle.test.ts index 3a2fdee..4dc4cf7 100644 --- a/actions/checkstyle/src/action.test.ts +++ b/actions/checkstyle/src/checkstyle.test.ts @@ -1,4 +1,4 @@ -import { parseData } from "./action"; +import { parseData, run as actionRun } from "./checkstyle"; import { describe, expect, test } from "@jest/globals"; import { expectProblem } from "pmd/src/problem.test"; @@ -30,4 +30,9 @@ describe("Test action/parseData", () => { problems[0], ); }); + test("JOSM Checkstyle XML, single file", () => { + expect(() => + actionRun("", "/Users/tsmock/workspace/josm/core/checkstyle-josm.xml"), + ).not.toThrow(); + }); }); diff --git a/actions/checkstyle/src/checkstyle.ts b/actions/checkstyle/src/checkstyle.ts new file mode 100644 index 0000000..b1245d7 --- /dev/null +++ b/actions/checkstyle/src/checkstyle.ts @@ -0,0 +1,82 @@ +import { debug } from "@actions/core"; +import { readFileSync, existsSync } from "fs"; +import { XMLParser } from "fast-xml-parser"; +import { Problem } from "pmd"; +import { logProblems } from "pmd"; +import { sep, join } from "path"; + +interface Violations { + "@_source": string; + "@_message": string; + "@_severity": string; + "@_column": string; + "@_line": string; +} + +interface FileData { + "@_name": string; + error?: Array; +} + +function parseFile(trim: number, fileData: FileData): Problem[] { + let subFile = fileData["@_name"].split(sep); + let currentlyTrimmed = 0; + while (!existsSync(join(...subFile))) { + subFile = subFile.slice(1); + currentlyTrimmed += 1; + if (currentlyTrimmed >= trim) { + break; + } + } + const file = join(...subFile); + const violations = fileData["error"]; + const problems: Problem[] = []; + if ( + violations !== undefined && + violations !== null && + violations.length !== 0 + ) { + for (const violation of violations) { + const title = violation["@_source"]; + problems.push({ + file: file, + title: title, + startColumn: parseInt(violation["@_column"]), + endColumn: parseInt(violation["@_column"]), + startLine: parseInt(violation["@_line"]), + endLine: parseInt(violation["@_line"]), + info: violation["@_message"], + }); + } + } + return problems; +} + +export function parseData(trim: number, data: string | Buffer): Problem[] { + const alwaysArray = ["checkstyle.file", "checkstyle.file.error"]; + const parser = new XMLParser({ + ignoreAttributes: false, + isArray: (name, jpath) => { + return alwaysArray.indexOf(jpath) >= 0; + }, + }); + const parsed = parser.parse(data); + const files = parsed["checkstyle"]["file"]; + let problems: Problem[] = []; + for (const file of files) { + problems = problems.concat(parseFile(trim, file)); + } + return problems; +} + +export function run(pathTrim: string, checkstyleFile: string) { + let trim: number; + if (pathTrim === "") { + trim = process.cwd().split(sep).length; + } else { + trim = parseInt(pathTrim); + } + const data = readFileSync(checkstyleFile); + debug(data.toString()); + logProblems(parseData(trim, data)); +} diff --git a/package-lock.json b/package-lock.json index 9d69fec..68f2a87 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,8 @@ "license": "GPL-2.0-or-later", "dependencies": { "@actions/core": "^1.10.1", - "fast-xml-parser": "^4.3.5" + "fast-xml-parser": "^4.3.5", + "pmd": "file:../pmd" }, "devDependencies": { "@jest/globals": "^29.7.0",