diff --git a/index.js b/index.js index 7deb0955..43dec7f5 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,7 @@ const path = require("path"); testFunction = async (testcase) => { if (testcase.failure) { if (annotations.length < numFailures) { - let {filePath, line} = await findTestLocation(file, testcase); + let { filePath, line } = await findTestLocation(file, testcase); annotations.push({ path: filePath, start_line: line, @@ -54,7 +54,7 @@ const path = require("path"); for (const testcase of testsuite.testcase) { await testFunction(testcase); } - } else if(testsuite.testcase){ + } else if (testsuite.testcase) { //single test await testFunction(testsuite.testcase); } @@ -94,7 +94,7 @@ const path = require("path"); ); if (!checkRun) { console.log( - "Junit tests result passed but can not identify test suite." + "Junit tests result passed but can not identify github check run id." ); console.log( "Can happen when performing a pull request from a forked repository." @@ -134,9 +134,7 @@ const path = require("path"); * @returns {Promise<{line: number, filePath: string}>} the line and the file of the failing test method. */ async function findTestLocation(testReportFile, testcase) { - const klass = testcase.$.classname - .replace(/$.*/g, "") - .replace(/\./g, "/"); + const klass = testcase.$.classname.replace(/$.*/g, "").replace(/\./g, "/"); // Search in src directories because some files having the same name of the class may have been // generated in the build folder. @@ -147,7 +145,8 @@ async function findTestLocation(testReportFile, testcase) { let bestFilePath; let bestRelativePathLength = -1; for await (const candidateFile of filePaths.globGenerator()) { - let candidateRelativeLength = path.relative(testReportFile, candidateFile).length; + let candidateRelativeLength = path.relative(testReportFile, candidateFile) + .length; if (!bestFilePath || candidateRelativeLength < bestRelativePathLength) { bestFilePath = candidateFile; @@ -172,7 +171,7 @@ async function findTestLocation(testReportFile, testcase) { //fall back so see something bestFilePath = `${klass}`; } - return {filePath: bestFilePath, line}; + return { filePath: bestFilePath, line }; } module.exports.findTestLocation = findTestLocation; diff --git a/index.test.js b/index.test.js index f1cba6f2..215b0458 100644 --- a/index.test.js +++ b/index.test.js @@ -1,78 +1,97 @@ -const index = require('./index'); +const index = require("./index"); const path = require("path"); const fs = require("fs").promises; -describe('find test location', () => { - let testReportFile; - let testCase; - - describe('given single module archetype', () => { - beforeAll(async () => { - testReportFile = resolve("target/surefire-reports/TEST-dummy.xml"); - testCase = { - classname: "org.dummy.ClassTest", - name: "methodTest" - }; - - await addFile( - 'src/main/java/org/dummy/ClassTest.java', - 'package org.dummy;\n' + - 'class ClassTest {\n' + - 'void methodTest() { }\n' + - '}'); - - await addFile('src/main/java/org/dummy2/ClassTest.java', '/* empty */'); - }); - - afterAll(clearFiles); - - it('should find path of the class', async () => { - let {filePath, line} = await index.findTestLocation(testReportFile, testCase); +describe("find test location", () => { + let testReportFile; + let testCase; + + describe("given single module archetype", () => { + beforeAll(async () => { + testReportFile = resolve("target/surefire-reports/TEST-dummy.xml"); + testCase = { + classname: "org.dummy.ClassTest", + name: "methodTest", + }; + + await addFile( + "src/main/java/org/dummy/ClassTest.java", + "package org.dummy;\n" + + "class ClassTest {\n" + + "void methodTest() { }\n" + + "}" + ); + + await addFile("src/main/java/org/dummy2/ClassTest.java", "/* empty */"); + }); - expect(filePath).toBe(resolve('src/main/java/org/dummy/ClassTest.java')); - }); + afterAll(clearFiles); - it('should find line of the method', async () => { - let {filePath, line} = await index.findTestLocation(testReportFile, testCase); + it("should find path of the class", async () => { + let { filePath, line } = await index.findTestLocation( + testReportFile, + testCase + ); - expect(line).toBe(3); - }); + expect(filePath).toBe(resolve("src/main/java/org/dummy/ClassTest.java")); }); - describe('given multiple gradle modules', () => { - beforeAll(async () => { - testReportFile = resolve("very_long_module1/build/test-results/test/TEST-dummy.xml"); - testCase = { - classname: "org.dummy.ClassTest", - name: "methodTest" - }; + it("should find line of the method", async () => { + let { filePath, line } = await index.findTestLocation( + testReportFile, + testCase + ); - await addFile('src/main/java/org/dummy/ClassTest.java', ''); - await addFile('very_long_module1/src/main/java/org/dummy/ClassTest.java', ''); - await addFile('module2/src/main/java/org/dummy/ClassTest.java', ''); - }); + expect(line).toBe(3); + }); + }); + + describe("given multiple gradle modules", () => { + beforeAll(async () => { + testReportFile = resolve( + "very_long_module1/build/test-results/test/TEST-dummy.xml" + ); + testCase = { + $: { + classname: "org.dummy.ClassTest", + name: "methodTest", + }, + }; + + await addFile("src/main/java/org/dummy/ClassTest.java", ""); + await addFile( + "very_long_module1/src/main/java/org/dummy/ClassTest.java", + "" + ); + await addFile("module2/src/main/java/org/dummy/ClassTest.java", ""); + }); - afterAll(clearFiles); + afterAll(clearFiles); - it('should find path of the class in the good module', async () => { - let {filePath, line} = await index.findTestLocation(testReportFile, testCase); + it("should find path of the class in the good module", async () => { + let { filePath, line } = await index.findTestLocation( + testReportFile, + testCase + ); - expect(filePath).toBe(resolve('very_long_module1/src/main/java/org/dummy/ClassTest.java')); - }); + expect(filePath).toBe( + resolve("very_long_module1/src/main/java/org/dummy/ClassTest.java") + ); }); + }); }); async function addFile(filePath, content) { - filePath = 'tmp/' + filePath; - let dirname = path.dirname(filePath); - await fs.mkdir(dirname, { recursive: true }); - await fs.writeFile(filePath, content); + filePath = "tmp/" + filePath; + let dirname = path.dirname(filePath); + await fs.mkdir(dirname, { recursive: true }); + await fs.writeFile(filePath, content); } async function clearFiles() { - await fs.rmdir('tmp', { recursive: true }); + await fs.rmdir("tmp", { recursive: true }); } function resolve(filePath) { - return path.resolve('tmp/' + filePath); + return path.resolve("tmp/" + filePath); }