Skip to content

Commit

Permalink
Updating it with regex plus adding a test
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrunkat committed Jun 27, 2023
1 parent 4bab93c commit 4729912
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
17 changes: 10 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isPullRequestTested = exports.getLinkedEpics = exports.getLinkedIssue = exports.fail = exports.ensureCorrectLinkingAndEstimates = exports.addTeamLabel = exports.getTeamLabelName = exports.fillCurrentMilestone = exports.assignPrCreator = exports.findCurrentTeamMilestone = exports.findUsersTeamName = void 0;
exports.isPullRequestTested = exports.isTestFilePath = exports.getLinkedEpics = exports.getLinkedIssue = exports.fail = exports.ensureCorrectLinkingAndEstimates = exports.addTeamLabel = exports.getTeamLabelName = exports.fillCurrentMilestone = exports.assignPrCreator = exports.findCurrentTeamMilestone = exports.findUsersTeamName = void 0;
const axios_1 = __importDefault(__nccwpck_require__(8757));
const core = __importStar(__nccwpck_require__(2186));
const consts_1 = __nccwpck_require__(4831);
Expand Down Expand Up @@ -316,6 +316,14 @@ function getLinkedEpics(timelineItems) {
}
exports.getLinkedEpics = getLinkedEpics;
;
function isTestFilePath(filePath) {
const testFileNameRegex = /(\.|_|\w)*tests?(\.|_|\w)*\.\w{2,3}$/;
return filePath.includes('/test/')
|| filePath.includes('/tests/')
|| testFileNameRegex.test(filePath);
}
exports.isTestFilePath = isTestFilePath;
;
/**
* Fetches a list of changed files and mark those that contain changes in test files.
*/
Expand All @@ -326,12 +334,7 @@ async function isPullRequestTested(octokit, pullRequest) {
pull_number: pullRequest.number,
});
const filePaths = files.data.map((file) => file.filename);
const testFilePaths = filePaths.filter((filePath) => {
return filePath.includes('/test/')
|| filePath.includes('/tests/')
|| filePath.endsWith('.test.js')
|| filePath.endsWith('.test.ts');
});
const testFilePaths = filePaths.filter((filePath) => isTestFilePath(filePath));
console.log(`${testFilePaths.length} test files found`);
console.log(`- ${testFilePaths.join('\n- ')}`);
return testFilePaths.length > 0;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

30 changes: 28 additions & 2 deletions src/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getOctokit } from '@actions/github';
// import { getOctokit } from '@actions/github';
import { components } from '@octokit/openapi-types/types.d';
import {
findCurrentTeamMilestone,
Expand All @@ -7,7 +7,7 @@ import {
getLinkedIssue,
getLinkedEpics,
ZenhubTimelineItem,
isPullRequestTested,
isTestFilePath,
} from './helpers';

type Milestone = components['schemas']['milestone'];
Expand Down Expand Up @@ -126,6 +126,32 @@ describe('ZenHub events extractors', () => {
});
});

describe('isTestFilePath', () => {
test('works with filenames', () => {
expect(isTestFilePath('/dasdasd.test.js')).toBe(true);
expect(isTestFilePath('asdasdlddd.ahoj.ss')).toBe(false);
expect(isTestFilePath('bla.test.py')).toBe(true);
expect(isTestFilePath('some-dir/another/test.py')).toBe(true);
expect(isTestFilePath('asds/test/test.js')).toBe(true);
expect(isTestFilePath('inte')).toBe(false);
expect(isTestFilePath('bla.tests.py')).toBe(true);
expect(isTestFilePath('testk.py')).toBe(true);
expect(isTestFilePath('asds/test/test.js')).toBe(true);
expect(isTestFilePath('ahoj.mjs')).toBe(false);
expect(isTestFilePath('ahoj/test.mjs')).toBe(true);
expect(isTestFilePath('ahoj/zdar/tests.py')).toBe(true);
expect(isTestFilePath('my.tests.mjs')).toBe(true);
expect(isTestFilePath('ahoj/test_basic.py')).toBe(true);
expect(isTestFilePath('simething/jknkjnkj/js')).toBe(false);
});

test('works with directories', () => {
expect(isTestFilePath('something/test/something')).toBe(true);
expect(isTestFilePath('something/tests/something')).toBe(true);
expect(isTestFilePath('something/non-test/something')).toBe(false);
});
});

// mtrunkat: I use these to test the action locally.
/*
describe('ensureCorrectLinkingAndEstimates', () => {
Expand Down
15 changes: 9 additions & 6 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ export function getLinkedEpics(timelineItems: ZenhubTimelineItem[]): ZenhubIssue
return [...connectedEpics.values()];
};

export function isTestFilePath(filePath: string): boolean {
const testFileNameRegex = /(\.|_|\w)*tests?(\.|_|\w)*\.\w{2,3}$/;

return filePath.includes('/test/')
|| filePath.includes('/tests/')
|| testFileNameRegex.test(filePath);
};

/**
* Fetches a list of changed files and mark those that contain changes in test files.
*/
Expand All @@ -329,12 +337,7 @@ export async function isPullRequestTested(octokit: OctokitType, pullRequest: Pul
pull_number: pullRequest.number,
});
const filePaths = files.data.map((file) => file.filename);
const testFileNameRegex = /(\.|_|\w)*tests?(\.|_|\w)*\.\w{2,3}$/;
const testFilePaths = filePaths.filter((filePath) => {
return filePath.includes('/test/')
|| filePath.includes('/tests/')
|| testFileNameRegex.test(filePath);
});
const testFilePaths = filePaths.filter((filePath) => isTestFilePath(filePath));

console.log(`${testFilePaths.length} test files found`);
console.log(`- ${testFilePaths.join('\n- ')}`);
Expand Down

0 comments on commit 4729912

Please sign in to comment.