Skip to content

Commit

Permalink
fix: tweak fitness function rule for mocha tests (#20887)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronfigueiredo authored Sep 18, 2023
1 parent bec1ab0 commit 62c7353
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
19 changes: 18 additions & 1 deletion development/fitness-functions/common/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ function filterDiffByFilePath(diff: string, regex: string): string {
return filteredDiff;
}

// This function returns all lines that are additions to files that are being
// modified but that previously already existed. Example:
// diff --git a/test.js b/test.js
// index 0000000000..872e0d8293
// --- /dev/null
// +++ b/test.js
// @@ -0,0 +1 @@
// +new line change to a previously existing file
function filterDiffLineAdditions(diff: string): string {
const diffLines = diff.split('\n');

Expand All @@ -44,6 +52,15 @@ function filterDiffLineAdditions(diff: string): string {
return diffAdditionLines.join('/n');
}

// This function returns all lines that are additions to new files that are being
// created. Example:
// diff --git a/test.js b/test.js
// new file mode 100644
// index 0000000000..872e0d8293
// --- /dev/null
// +++ b/test.js
// @@ -0,0 +1 @@
// +new line change as the new file is created
function filterDiffFileCreations(diff: string): string {
// split by `diff --git` and remove the first element which is empty
const diffBlocks = diff.split(`diff --git`).slice(1);
Expand Down Expand Up @@ -91,7 +108,7 @@ function hasNumberOfCodeBlocksIncreased(

export {
filterDiffByFilePath,
filterDiffLineAdditions,
filterDiffFileCreations,
filterDiffLineAdditions,
hasNumberOfCodeBlocksIncreased,
};
23 changes: 21 additions & 2 deletions development/fitness-functions/rules/sinon-assert-syntax.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { generateModifyFilesDiff } from '../common/test-data';
import {
generateCreateFileDiff,
generateModifyFilesDiff,
} from '../common/test-data';
import { preventSinonAssertSyntax } from './sinon-assert-syntax';

describe('preventSinonAssertSyntax()', (): void => {
Expand All @@ -10,7 +13,7 @@ describe('preventSinonAssertSyntax()', (): void => {
expect(hasRulePassed).toBe(true);
});

it('should not pass when receiving a diff with one of the blocked expressions', (): void => {
it('should pass when receiving a diff with an existing file with one of the blocked expressions', (): void => {
const infringingExpression = 'assert.equal';
const testDiff = [
generateModifyFilesDiff('new-file.ts', 'foo', 'bar'),
Expand All @@ -24,6 +27,22 @@ describe('preventSinonAssertSyntax()', (): void => {

const hasRulePassed = preventSinonAssertSyntax(testDiff);

expect(hasRulePassed).toBe(true);
});

it('should not pass when receiving a diff with a new file with one of the blocked expressions', (): void => {
const infringingExpression = 'assert.equal';
const testDiff = [
generateModifyFilesDiff('new-file.ts', 'foo', 'bar'),
generateCreateFileDiff('old-file.js', 'pong'),
generateCreateFileDiff(
'test.js',
`yada yada ${infringingExpression} yada yada`,
),
].join('');

const hasRulePassed = preventSinonAssertSyntax(testDiff);

expect(hasRulePassed).toBe(false);
});
});
4 changes: 2 additions & 2 deletions development/fitness-functions/rules/sinon-assert-syntax.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EXCLUDE_E2E_TESTS_REGEX } from '../common/constants';
import {
filterDiffLineAdditions,
filterDiffByFilePath,
filterDiffFileCreations,
hasNumberOfCodeBlocksIncreased,
} from '../common/shared';

Expand All @@ -16,7 +16,7 @@ const codeBlocks = [

function preventSinonAssertSyntax(diff: string): boolean {
const diffByFilePath = filterDiffByFilePath(diff, EXCLUDE_E2E_TESTS_REGEX);
const diffAdditions = filterDiffLineAdditions(diffByFilePath);
const diffAdditions = filterDiffFileCreations(diffByFilePath);
const hashmap = hasNumberOfCodeBlocksIncreased(diffAdditions, codeBlocks);

const haveOccurencesOfAtLeastOneCodeBlockIncreased =
Expand Down

0 comments on commit 62c7353

Please sign in to comment.