Skip to content

Commit

Permalink
feat(core): add affected projects by test suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
EladBezalel committed Dec 10, 2023
1 parent 161c86c commit a7eb41b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
73 changes: 72 additions & 1 deletion libs/core/src/true-affected.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,43 @@ describe('trueAffected', () => {
expect(affected).toEqual(['proj1', 'proj3']);
});

it('should ignore files that are not in projects files', async () => {
it('should include test files by suffixes', async () => {
jest.spyOn(git, 'getChangedFiles').mockReturnValue([
{
filePath: 'proj1/index._test_.ts',
changedLines: [6],
},
]);

const affected = await trueAffected({
cwd,
base: 'main',
rootTsConfig: 'tsconfig.json',
projects: [
{
name: 'proj1',
sourceRoot: 'proj1/',
tsConfig: 'proj1/tsconfig.json',
},
{
name: 'proj2',
sourceRoot: 'proj2/',
tsConfig: 'proj2/tsconfig.json',
},
{
name: 'proj3',
sourceRoot: 'proj3/',
tsConfig: 'proj3/tsconfig.json',
implicitDependencies: ['proj1'],
},
],
testSuffixes: ['_test_'],
});

expect(affected).toEqual(['proj1', 'proj3']);
});

it('should default test files suffixes to spec and test', async () => {
jest.spyOn(git, 'getChangedFiles').mockReturnValue([
{
filePath: 'proj1/index.spec.ts',
Expand Down Expand Up @@ -197,6 +233,41 @@ describe('trueAffected', () => {
],
});

expect(affected).toEqual(['proj1', 'proj3']);
});

it('should ignore files that are not in projects files', async () => {
jest.spyOn(git, 'getChangedFiles').mockReturnValue([
{
filePath: 'proj1/README.md',
changedLines: [6],
},
]);

const affected = await trueAffected({
cwd,
base: 'main',
rootTsConfig: 'tsconfig.json',
projects: [
{
name: 'proj1',
sourceRoot: 'proj1/',
tsConfig: 'proj1/tsconfig.json',
},
{
name: 'proj2',
sourceRoot: 'proj2/',
tsConfig: 'proj2/tsconfig.json',
},
{
name: 'proj3',
sourceRoot: 'proj3/',
tsConfig: 'proj3/tsconfig.json',
implicitDependencies: ['proj1'],
},
],
});

expect(affected).toEqual([]);
});

Expand Down
10 changes: 9 additions & 1 deletion libs/core/src/true-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const trueAffected = async ({
base = 'origin/main',
projects,
includeFiles = [],
testSuffixes = ['spec', 'test'],
}: TrueAffected) => {
const project = new Project({
compilerOptions: {
Expand Down Expand Up @@ -88,7 +89,14 @@ export const trueAffected = async ({

const changedFiles = [...sourceChangedFiles, ...nonSourceChangedFiles];

const affectedPackages = new Set<string>();
const changedTestFilesPackages = changedFiles
.filter(({ filePath }) =>
new RegExp(`.*\\.(${testSuffixes.join('|')})\\.(ts|js)x?$`).test(filePath)
)
.map(({ filePath }) => getPackageNameByPath(filePath, projects))
.filter((v): v is string => v != null);

const affectedPackages = new Set<string>(changedTestFilesPackages);
const visitedIdentifiers = new Map<string, string[]>();

const findReferencesLibs = (node: Node<ts.Node>) => {
Expand Down
1 change: 1 addition & 0 deletions libs/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export interface TrueAffected {
base?: string;
projects: TrueAffectedProject[];
includeFiles?: string[];
testSuffixes?: string[];
}

0 comments on commit a7eb41b

Please sign in to comment.