Skip to content

chore(ama-sdk-schematics): ignore coverage for generated files #3091

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion apps/showcase/testing/jest.config.ut.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ module.exports = {
...createCjsPreset(getTsJestBaseConfig()),
...getOtterJestBaseConfig(rootDir),
...getJestUnitTestConfig(),
setupFilesAfterEnv: ['<rootDir>/testing/setup-jest.ts']
setupFilesAfterEnv: ['<rootDir>/testing/setup-jest.ts'],
coveragePathIgnorePatterns: [
'<rootDir>/dev-resources/',
'<rootDir>/training-assets/'
]
};
5 changes: 0 additions & 5 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ ignore:
- 'apps/@*/*/build'

# Generated files
- 'apps/showcase/dev-resources'
- 'apps/showcase/training-assets'
- 'packages/@o3r-training/showcase-sdk/src/api'
- 'packages/@o3r-training/showcase-sdk/src/models/base'
- 'packages/@o3r-training/showcase-sdk/src/spec'
- 'tools/github-actions/*/packaged-action'

# Templates
Expand Down
5 changes: 5 additions & 0 deletions packages/@ama-sdk/schematics/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
"version": "12.1.3",
"description": "Updates of @ama-sdk/schematics to v12.1.3",
"factory": "./schematics/ng-update/index#updateV12_1_3"
},
"migration-v12_3": {
"version": "12.3.0-prerelease.0",
"description": "Updates of @ama-sdk/schematics to v12.3.*",
"factory": "./schematics/ng-update/index#updateV12_3"
}
}
}
8 changes: 6 additions & 2 deletions packages/@ama-sdk/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"type-fest": {
"optional": true
},
"typescript": {
"optional": true
}
},
"peerDependencies": {
Expand All @@ -71,7 +74,8 @@
"@openapitools/openapi-generator-cli": "^2.15.0",
"openapi-types": "^12.0.0",
"ts-node": "~10.9.2",
"type-fest": "^4.30.1"
"type-fest": "^4.30.1",
"typescript": "^5.5.4"
},
"dependencies": {
"@angular-devkit/core": "~19.2.0",
Expand Down Expand Up @@ -146,7 +150,7 @@
"@swc/cli": "~0.7.0",
"@swc/core": "~1.11.0",
"@swc/helpers": "~0.5.0",
"@typescript-eslint/eslint-plugin": "~8.30.0",
"@typescript-eslint/eslint-plugin": "~8.31.0",
"jest-junit": "~16.0.0",
"lint-staged": "^15.0.0",
"minimist": "^1.2.6",
Expand Down
15 changes: 15 additions & 0 deletions packages/@ama-sdk/schematics/schematics/ng-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
updateV11_0 as tsUpdateV11_0,
updateV11_4 as tsUpdateV11_4,
updateV12_1_3 as tsUpdateV12_1_3,
updateV12_3 as tsUpdateV12_3,
} from './typescript';

/**
Expand Down Expand Up @@ -96,3 +97,17 @@ export function updateV12_1_3(): Rule {
return tree;
};
}

/**
* update of Otter library V12.3
*/
// eslint-disable-next-line @typescript-eslint/naming-convention -- function name contains the version
export function updateV12_3(): Rule {
return (tree, context) => {
if (isTypescriptSdk(tree)) {
return tsUpdateV12_3()(tree, context);
}

return tree;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
import {
clearPackageJsonExports,
} from './v12.1/clean-packagejson-exports';
import {
updateJestConfigCoveragePathIgnorePatterns,
} from './v12.3/coverage-ignore';

/**
* update of Otter library V10.0
Expand Down Expand Up @@ -100,3 +103,15 @@ export function updateV12_1_3(): Rule {

return chain(updateRules);
}

/**
* Update of Ama-sdk library V12.3
*/
// eslint-disable-next-line @typescript-eslint/naming-convention -- function name contains the version
export function updateV12_3(): Rule {
const updateRules: Rule[] = [
updateJestConfigCoveragePathIgnorePatterns
];

return chain(updateRules);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
readFileSync,
} from 'node:fs';
import {
resolve,
} from 'node:path';
import {
Tree,
} from '@angular-devkit/schematics';
import {
updateJestConfigCoveragePathIgnorePatterns,
} from './coverage-ignore';

describe('updateJestConfigCoveragePathIgnorePatterns', () => {
const mock = readFileSync(resolve(__dirname, '..', '..', '..', '..', 'testing', 'mocks', 'jest-config', 'mock.jest.config.js'), { encoding: 'utf8' });
const resultContent = readFileSync(resolve(__dirname, '..', '..', '..', '..', 'testing', 'mocks', 'jest-config', 'result.jest.config.js'), { encoding: 'utf8' });

beforeEach(() => {
jest.resetModules();
});

test('should not touch other files', async () => {
const tree = Tree.empty();
tree.create('other.js', mock);
await updateJestConfigCoveragePathIgnorePatterns(tree, { logger: { info: jest.fn() } } as any);
expect(tree.readText('other.js')).toBe(mock);
});

test('should update jest.config.js', async () => {
const tree = Tree.empty();
tree.create('jest.config.js', mock);
const context = { logger: { info: jest.fn(), warn: jest.fn() } } as any;
await updateJestConfigCoveragePathIgnorePatterns(tree, context);
expect(context.logger.warn).not.toHaveBeenCalled();
expect(tree.readText('jest.config.js')).toBe(resultContent);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type {
Rule,
} from '@angular-devkit/schematics';
import * as ts from 'typescript';

const coveragePathIgnorePatterns = [
'<rootDir>/src/api/**/*.ts',
'<rootDir>/src/models/base/**/*.ts',
'<rootDir>/src/spec/api-mock.ts',
'<rootDir>/src/spec/operation-adapter.ts'
];

/**
* Update Jest config to ignore coverage of generated path
* @param tree
* @param context
*/
export const updateJestConfigCoveragePathIgnorePatterns: Rule = (tree, context) => {
const filePath = 'jest.config.js';
if (!tree.exists(filePath)) {
context.logger.info(`No ${filePath} found, the coverage ignore pattern won't be added.`);
return;
}

const tsSourceFile = ts.createSourceFile(
filePath,
tree.readText(filePath),
ts.ScriptTarget.Latest,
true
);

const result = ts.transform(tsSourceFile, [
(ctx) => (rootNode) => {
const { factory } = ctx;
return ts.visitNode(rootNode, (node) => {
return ts.visitEachChild(node, (statement) => {
if (
ts.isExpressionStatement(statement)
&& ts.isBinaryExpression(statement.expression)
&& /module\.exports/.test(statement.expression.left.getText(rootNode))
&& ts.isObjectLiteralExpression(statement.expression.right)
&& !statement.expression.right.properties.some((prop) => ts.isPropertyAssignment(prop) && prop.name.getText(rootNode) === 'coveragePathIgnorePatterns')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we try to complete missing files if the coveragePathIgnorePatterns is already defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that discovering intersection between globs is a bit too much :D.

) {
return factory.updateExpressionStatement(
statement,
factory.updateBinaryExpression(statement.expression,
statement.expression.left,
statement.expression.operatorToken,
factory.updateObjectLiteralExpression(statement.expression.right, [
...statement.expression.right.properties,
factory.createPropertyAssignment('coveragePathIgnorePatterns', factory.createArrayLiteralExpression(
coveragePathIgnorePatterns.map((pattern) => factory.createStringLiteral(pattern, true)),
true
))
])
)
);
}
}, ctx);
}) as ts.SourceFile;
}
]);

const transformedSourceFile = result.transformed[0];
const printer = ts.createPrinter({
removeComments: false,
newLine: ts.NewLineKind.LineFeed
});

const content = printer.printFile(transformedSourceFile)
.split('\n')
// migrate from 4 spaces to 2 spaces indent
.map((line) => {
const match = line.match(/^ */);
const numberSpaces = match ? match[0].length : 0;
const newNumber = (numberSpaces - numberSpaces % 2) / 2;
return line.replace(/^ */, Array.from({ length: newNumber }).fill(' ').join(''));
})
.join('\n');

tree.overwrite(filePath, content);
return tree;
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ module.exports = {
stringifyContentPathRegex: '\\.html$',
}
]
}
},
coveragePathIgnorePatterns: [
'<rootDir>/src/api/**/*.ts',
'<rootDir>/src/models/base/**/*.ts',
'<rootDir>/src/spec/api-mock.ts',
'<rootDir>/src/spec/operation-adapter.ts'
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
displayName: require('./package.json').name,
preset: 'ts-jest',
rootDir: '.',
roots: ['<rootDir>/src/'],
reporters: [
'default',
['jest-junit', { outputDirectory: '<rootDir>/dist-test', suiteName: '<% if (projectName) { %>@<%=projectName%>/<% } %><%=projectPackageName%> unit tests' }]
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
displayName: require('./package.json').name,
preset: 'ts-jest',
rootDir: '.',
roots: ['<rootDir>/src/'],
reporters: [
'default',
['jest-junit', { outputDirectory: '<rootDir>/dist-test', suiteName: '<% if (projectName) { %>@<%=projectName%>/<% } %><%=projectPackageName%> unit tests' }]
],
coveragePathIgnorePatterns: [
'<rootDir>/src/api/**/*.ts',
'<rootDir>/src/models/base/**/*.ts',
'<rootDir>/src/spec/api-mock.ts',
'<rootDir>/src/spec/operation-adapter.ts'
]
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ const rootDir = path.join(__dirname, '..');
module.exports = {
...createDefaultPreset(getTsJestBaseConfig()),
...getOtterJestBaseConfig(rootDir),
...getJestUnitTestConfig()
...getJestUnitTestConfig(),
coveragePathIgnorePatterns: [
'<rootDir>/src/api/**/*.ts',
'<rootDir>/src/models/base/**/*.ts',
'<rootDir>/src/spec/api-mock.ts',
'<rootDir>/src/spec/operation-adapter.ts'
]
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ const rootDir = path.join(__dirname, '..');
module.exports = {
...createDefaultPreset(getTsJestBaseConfig()),
...getOtterJestBaseConfig(rootDir),
...getJestUnitTestConfig()
...getJestUnitTestConfig(),
coveragePathIgnorePatterns: [
'<rootDir>/src/api/**/*.ts',
'<rootDir>/src/models/base/**/*.ts',
'<rootDir>/src/spec/api-mock.ts',
'<rootDir>/src/spec/operation-adapter.ts'
]
};
6 changes: 3 additions & 3 deletions packages/@o3r/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@
"@o3r/store-sync": "workspace:^",
"@stylistic/eslint-plugin": "~3.1.0",
"@types/jest": "~29.5.2",
"@typescript-eslint/eslint-plugin": "~8.30.0",
"@typescript-eslint/parser": "~8.30.0",
"@typescript-eslint/eslint-plugin": "~8.31.0",
"@typescript-eslint/parser": "~8.31.0",
"angular-eslint": "~19.3.0",
"cpy-cli": "^5.0.0",
"eslint": "~9.25.0",
Expand All @@ -190,7 +190,7 @@
"jest-preset-angular": "~14.5.0",
"jsonc-eslint-parser": "~2.4.0",
"nx": "~20.8.0",
"typescript-eslint": "~8.30.0",
"typescript-eslint": "~8.31.0",
"zone.js": "~0.15.0"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions packages/@o3r/eslint-config-otter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
"typescript": "^5.5.4"
},
"generatorDependencies": {
"@typescript-eslint/eslint-plugin": "~8.30.0",
"@typescript-eslint/parser": "~8.30.0",
"@typescript-eslint/utils": "~8.30.0",
"@typescript-eslint/eslint-plugin": "~8.31.0",
"@typescript-eslint/parser": "~8.31.0",
"@typescript-eslint/utils": "~8.31.0",
"eslint": "~9.25.0",
"eslint-plugin-jsdoc": "~50.6.0",
"eslint-plugin-unicorn": "~56.0.0"
Expand Down
3 changes: 3 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ __metadata:
openapi-types: ^12.0.0
ts-node: ~10.9.2
type-fest: ^4.30.1
typescript: ^5.5.4
peerDependenciesMeta:
"@ama-sdk/core":
optional: true
Expand All @@ -896,6 +897,8 @@ __metadata:
optional: true
type-fest:
optional: true
typescript:
optional: true
bin:
amasdk-clear-index: ./dist/cli/clear-index.cjs
amasdk-files-pack: ./dist/cli/files-pack.cjs
Expand Down