Skip to content

Commit

Permalink
fix tests and formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashley-taylor committed Jul 7, 2020
1 parent 21c6cc0 commit cdb4f1a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 63 deletions.
15 changes: 7 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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;
129 changes: 74 additions & 55 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit cdb4f1a

Please sign in to comment.