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(core): add affected projects by test suffixes #25

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions libs/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ const affected = await trueAffected({

### **Options**

| Option | Type | Description | Default |
| -------------- | ----------- | ------------------------------------------------------------ | ------------- |
| `rootTsConfig` | `string` | The path to the root tsconfig file | |
| `projects` | `Project[]` | An array of projects to check | |
| `cwd` | `string` | The current working directory | |
| `base` | `string` | The base branch to compare against | `origin/main` |
| `includeFiles` | `string[]` | Glob patterns to include (relative to projects' source root) | |
| Option | Type | Description | Default |
| -------------- | ----------------------- | ------------------------------------------------------------ | ----------------- |
| `rootTsConfig` | `string` | The path to the root tsconfig file | |
| `projects` | `Project[]` | An array of projects to check | |
| `cwd` | `string` | The current working directory | |
| `base` | `string` | The base branch to compare against | `origin/main` |
| `include` | `(string \| Regexp)[]` | Glob patterns to include (relative to projects' source root) | spec & test files |

> `rootTsConfig` - The path to the root tsconfig file, should include the `paths` prop with all projects mapping so `ts-morph` can find the references.

Expand Down
14 changes: 5 additions & 9 deletions libs/core/src/true-affected.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe('trueAffected', () => {
it('should ignore files that are not in projects files', async () => {
jest.spyOn(git, 'getChangedFiles').mockReturnValue([
{
filePath: 'proj1/index.spec.ts',
filePath: 'proj1/README.md',
changedLines: [6],
},
]);
Expand Down Expand Up @@ -310,16 +310,12 @@ describe('trueAffected', () => {

it.each([
['regular path', ['package.json'], ['proj3']],
['glob path', ['**/package.json'], ['proj3']],
[
'multiple paths',
['package.json', '**/jest.config.js'],
['proj2', 'proj3'],
],
['regexp path', [/\.spec\.ts$/], ['proj1']],
['multiple paths', ['package.json', /\.spec\.ts$/], ['proj1', 'proj3']],
])('should include files with %s', async (title, filePatterns, expected) => {
jest.spyOn(git, 'getChangedFiles').mockReturnValue([
{
filePath: 'proj2/jest.config.js',
filePath: 'proj1/test.spec.ts',
changedLines: [1],
},
{
Expand Down Expand Up @@ -349,7 +345,7 @@ describe('trueAffected', () => {
tsConfig: 'proj3/tsconfig.json',
},
],
includeFiles: filePatterns,
include: filePatterns,
});

expect(affected).toEqual(expected);
Expand Down
39 changes: 25 additions & 14 deletions libs/core/src/true-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ const ignoredRootNodeTypes = [
SyntaxKind.IfStatement,
];

export const DEFAULT_INCLUDE_TEST_FILES = /\.(spec|test)\.(ts|js)x?/;

export const trueAffected = async ({
cwd,
rootTsConfig,
base = 'origin/main',
projects,
includeFiles = [],
include = [DEFAULT_INCLUDE_TEST_FILES],
}: TrueAffected) => {
const project = new Project({
compilerOptions: {
Expand Down Expand Up @@ -57,26 +59,21 @@ export const trueAffected = async ({
join(resolve(cwd, sourceRoot), '**/*.{ts,js}')
);
}

project.addSourceFilesAtPaths(
includeFiles.map((path) => `${resolve(cwd, sourceRoot)}/${path}`)
);
}
);

const sourceChangedFiles: GetChangedFiles[] = getChangedFiles({
const changedFiles = getChangedFiles({
base,
cwd,
}).filter(
});

const sourceChangedFiles: GetChangedFiles[] = changedFiles.filter(
({ filePath }) => project.getSourceFile(resolve(cwd, filePath)) != null
);

const ignoredPaths = ['./node_modules', './dist', './.git'];

const nonSourceChangedFiles: GetChangedFiles[] = getChangedFiles({
base,
cwd,
})
const nonSourceChangedFiles: GetChangedFiles[] = changedFiles
.filter(
({ filePath }) =>
!filePath.match(/.*\.(ts|js)x?$/g) &&
Expand All @@ -86,9 +83,23 @@ export const trueAffected = async ({
findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths)
);

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

const changedIncludedFilesPackages = changedFiles
.filter(({ filePath }) =>
include.some((file) =>
typeof file === 'string'
? filePath.endsWith(file)
: filePath.match(file)
)
)
.map(({ filePath }) => getPackageNameByPath(filePath, projects))
.filter((v): v is string => v != null);

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

const findReferencesLibs = (node: Node<ts.Node>) => {
Expand Down Expand Up @@ -126,7 +137,7 @@ export const trueAffected = async ({
});
};

changedFiles.forEach(({ filePath, changedLines }) => {
filteredChangedFiles.forEach(({ filePath, changedLines }) => {
const sourceFile = project.getSourceFile(resolve(cwd, filePath));

if (sourceFile == null) return;
Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export interface TrueAffected {
rootTsConfig?: string;
base?: string;
projects: TrueAffectedProject[];
includeFiles?: string[];
include?: (string | RegExp)[];
}
2 changes: 1 addition & 1 deletion libs/nx/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('cli', () => {
rootTsConfig: 'tsconfig.base.json',
base: 'origin/main',
projects: [],
includeFiles: [],
include: [],
});
});

Expand Down
4 changes: 2 additions & 2 deletions libs/nx/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawn } from 'node:child_process';
import { CommandModule } from 'yargs';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';
import { trueAffected } from '@traf/core';
import { DEFAULT_INCLUDE_TEST_FILES, trueAffected } from '@traf/core';
import { getNxTrueAffectedProjects } from './nx';

const color = '#ff0083';
Expand Down Expand Up @@ -43,7 +43,7 @@ export const affectedAction = async ({
rootTsConfig: tsConfigFilePath,
base,
projects,
includeFiles,
include: [...includeFiles, DEFAULT_INCLUDE_TEST_FILES],
});

if (json) {
Expand Down