Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test function name): get function name from config #50

Merged
merged 7 commits into from
Feb 24, 2019
25 changes: 16 additions & 9 deletions lib/tests-from-pull-request.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require('lodash');
const createFixture = require('./create-fixture');

module.exports = getTestsFromPR;
Expand All @@ -7,11 +8,14 @@ function getTestsFromPR(context) {

const repo = context.repo();
const { pullRequests } = context.github;
let testFunctionName;

return getPullRequestFiles()
return getTestFunctionName(context)
.then((functionName) => testFunctionName = functionName)
.then(() => getPullRequestFiles())
.then((files) => files.map((file) => file.patch))
.then((patches) => patches.join('\n'))
.then((prPatch) => getTestNames(prPatch))
.then((prPatch) => getTestNames(prPatch, testFunctionName))
.then((testNames) => {
context.log.debug({ data: testNames }, 'test names from pull request');

Expand All @@ -28,29 +32,32 @@ function getTestsFromPR(context) {
}
}

function getTestNames(prPatch) {
function getTestNames(prPatch, testFunctionName) {
const testNames = [];
const parsedPatch = prPatch.split('\n');

parsedPatch.forEach((line) => {
if (line.startsWith('+')) {
line = removePrefixAndTrim(line);
const testName = getTestName(line);
const testName = getTestName(line, testFunctionName);

testName ? testNames.push(testName) : null;
}
});

return testNames;
}

function removePrefixAndTrim(line) {
return line.slice(1).trim();
}

function getTestName(line) {
const getTestNameRegex = /it\((.*),/;
function getTestFunctionName(context) {
return context.config('config.yml')
.then((config) => _.get(config, 'tdd1tTestFunctionName', 'it'));
}

function getTestName(line, testFunctionName) {
const getTestNameRegex = new RegExp(`${testFunctionName}\\((.*),`);
const regexTestResult = getTestNameRegex.exec(line);

return regexTestResult && regexTestResult[1].trim().slice(1,-1);
return regexTestResult && regexTestResult[1].trim().slice(1, -1);
}