Skip to content

Commit

Permalink
test: use virtual filesystem for eslint-plugin tests (#1323)
Browse files Browse the repository at this point in the history
## Proposed change

<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. List any
dependencies that is required for this change. -->

## Related issues

- 🐛 Fixes #(issue)
- 🚀 Feature #(issue)

<!-- Please make sure to follow the contributing guidelines on
https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md -->
  • Loading branch information
fpaul-1A authored Feb 6, 2024
2 parents d07067e + 961c227 commit e4093c7
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ module.exports = {
'@nx/dependency-checks': ['error', {
'buildTargets': ['build', 'build-builders', 'compile', 'test'],
'checkObsoleteDependencies': false,
'ignoredDependencies': ['ora']
'checkVersionMismatches': false,
'ignoredDependencies': ['ora', '@o3r/test-helpers']
}]
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/@o3r/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@nx/eslint-plugin": "~17.3.0",
"@nx/jest": "~17.3.0",
"@o3r/build-helpers": "workspace:^",
"@o3r/test-helpers": "workspace:^",
"@stylistic/eslint-plugin-ts": "^1.5.4",
"@types/jest": "~29.5.2",
"@types/node": "^20.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as path from 'node:path';
import * as fs from 'node:fs';
import { cleanVirtualFileSystem, useVirtualFileSystem } from '@o3r/test-helpers';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import * as path from 'node:path';

const virtualFileSystem = useVirtualFileSystem();
import jsonDependencyVersionsHarmonize from './json-dependency-versions-harmonize';

const ruleTester = new TSESLint.RuleTester({
Expand All @@ -27,23 +29,22 @@ const packageJson2 = {
myOtherDep: '~2.0.0'
}
};
const fakeFolder = path.resolve('/fake-folder');
const packageToLint = path.join(fakeFolder, 'local', 'packages', 'my-package', 'package.json');

const packageToLint = path.resolve(__dirname, 'local', 'packages', 'my-package', 'package.json');

beforeAll(() => {
fs.mkdirSync(path.resolve(__dirname, 'local'));
fs.writeFileSync(path.resolve(__dirname, 'local', 'package.json'), JSON.stringify(packageJsonWorkspace));
beforeAll(async () => {
await virtualFileSystem.promises.mkdir(path.join(fakeFolder, 'local'), {recursive: true});
await virtualFileSystem.promises.writeFile(path.join(fakeFolder, 'local', 'package.json'), JSON.stringify(packageJsonWorkspace));

fs.mkdirSync(path.resolve(__dirname, 'local', 'packages'));
fs.mkdirSync(path.resolve(__dirname, 'local', 'packages', 'my-package'));
fs.mkdirSync(path.resolve(__dirname, 'local', 'packages', 'my-package-2'));
fs.writeFileSync(path.resolve(__dirname, 'local', 'packages', 'my-package-2', 'package.json'), JSON.stringify(packageJson2));
await virtualFileSystem.promises.mkdir(path.join(fakeFolder, 'local', 'packages', 'my-package'), {recursive: true});
await virtualFileSystem.promises.mkdir(path.join(fakeFolder, 'local', 'packages', 'my-package-2'), {recursive: true});
await virtualFileSystem.promises.writeFile(path.join(fakeFolder, 'local', 'packages', 'my-package-2', 'package.json'), JSON.stringify(packageJson2));

fs.writeFileSync(packageToLint, '{}');
await virtualFileSystem.promises.writeFile(packageToLint, '{}');
});

afterAll(() => {
fs.rmSync(path.resolve(__dirname, 'local'), {recursive: true, force: true});
cleanVirtualFileSystem();
});

ruleTester.run('json-dependency-versions-harmonize', jsonDependencyVersionsHarmonize, {
Expand All @@ -68,7 +69,7 @@ ruleTester.run('json-dependency-versions-harmonize', jsonDependencyVersionsHarmo
messageId: 'error',
data: {
depName: 'myDep',
packageJsonFile: path.resolve(__dirname, 'local', 'package.json'),
packageJsonFile: path.join(fakeFolder, 'local', 'package.json'),
version: '^2.0.0'
},
suggestions: [
Expand All @@ -92,7 +93,7 @@ ruleTester.run('json-dependency-versions-harmonize', jsonDependencyVersionsHarmo
messageId: 'error',
data: {
depName: 'myDep',
packageJsonFile: path.resolve(__dirname, 'local', 'package.json'),
packageJsonFile: path.join(fakeFolder, 'local', 'package.json'),
version: '^2.0.0'
}
}
Expand All @@ -108,7 +109,7 @@ ruleTester.run('json-dependency-versions-harmonize', jsonDependencyVersionsHarmo
messageId: 'error',
data: {
depName: 'myOtherDep',
packageJsonFile: path.resolve(__dirname, 'local', 'packages', 'my-package-2', 'package.json'),
packageJsonFile: path.join(fakeFolder, 'local', 'packages', 'my-package-2', 'package.json'),
version: '~2.0.0'
}
}
Expand All @@ -124,7 +125,7 @@ ruleTester.run('json-dependency-versions-harmonize', jsonDependencyVersionsHarmo
messageId: 'error',
data: {
depName: 'myOtherDep',
packageJsonFile: path.resolve(__dirname, 'local', 'packages', 'my-package-2', 'package.json'),
packageJsonFile: path.join(fakeFolder, 'local', 'packages', 'my-package-2', 'package.json'),
version: '~2.0.0'
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as semver from 'semver';
import type { PackageJson } from 'type-fest';
import * as fs from 'node:fs';
import { existsSync, readFileSync } from 'node:fs';
import { dirname, normalize, posix , resolve } from 'node:path';
import { sync as globbySync } from 'globby';
Expand Down Expand Up @@ -46,7 +47,7 @@ export const findWorkspacePackageJsons = (directory: string, rootDir?: string):
}
const packagePaths = globbySync(
(Array.isArray(content.workspaces) ? content.workspaces : content.workspaces.packages || []).map((f) => posix.join(f, 'package.json')),
{ cwd: directory, onlyFiles: false, absolute: true }
{ cwd: directory, onlyFiles: false, absolute: true, fs }
);
const isPackageWorkspace = packagePaths.some((workspacePath) => normalize(workspacePath) === rootDir);
const getPackages = () => ([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as path from 'node:path';
import * as fs from 'node:fs';
import { cleanVirtualFileSystem, useVirtualFileSystem } from '@o3r/test-helpers';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import * as path from 'node:path';

const virtualFileSystem = useVirtualFileSystem();
import noFolderImportForModule from './no-folder-import-for-module';

const ruleTester = new TSESLint.RuleTester({
Expand All @@ -10,17 +12,16 @@ const ruleTester = new TSESLint.RuleTester({
sourceType: 'module'
}
});
const fakeFolder = path.resolve('/fake-folder');

beforeAll(() => {
fs.mkdirSync(path.resolve(__dirname, 'local'));
fs.mkdirSync(path.resolve(__dirname, 'empty-local'));
fs.writeFileSync(path.resolve(__dirname, 'local', 'index.ts'), 'export default {};');
beforeAll(async () => {
await virtualFileSystem.promises.mkdir(path.join(fakeFolder, 'local'), {recursive: true});
await virtualFileSystem.promises.mkdir(path.join(fakeFolder, 'empty-local'), {recursive: true});
await virtualFileSystem.promises.writeFile(path.join(fakeFolder, 'local', 'index.ts'), 'export default {};');
});

afterAll(() => {
fs.unlinkSync(path.resolve(__dirname, 'local', 'index.ts'));
fs.rmdirSync(path.resolve(__dirname, 'local'));
fs.rmdirSync(path.resolve(__dirname, 'empty-local'));
cleanVirtualFileSystem();
});

ruleTester.run('no-folder-import-for-module', noFolderImportForModule, {
Expand All @@ -33,7 +34,7 @@ ruleTester.run('no-folder-import-for-module', noFolderImportForModule, {
],
invalid: [
{
filename: path.resolve(__dirname, 'test.ts'),
filename: path.join(fakeFolder, 'test.ts'),
output: 'import {myImportModule} from "./local/index";',
code: 'import {myImportModule} from "./local";',
errors: [
Expand All @@ -45,7 +46,7 @@ ruleTester.run('no-folder-import-for-module', noFolderImportForModule, {
]
},
{
filename: path.resolve(__dirname, 'test.ts'),
filename: path.join(fakeFolder, 'test.ts'),
output: 'import {randomImport, myImportModule} from "./local/index";',
code: 'import {randomImport, myImportModule} from "./local";',
errors: [
Expand All @@ -57,7 +58,7 @@ ruleTester.run('no-folder-import-for-module', noFolderImportForModule, {
]
},
{
filename: path.resolve(__dirname, 'test.ts'),
filename: path.join(fakeFolder, 'test.ts'),
code: 'import {myImportModule} from "./empty-local";',
errors: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as path from 'node:path';
import * as fs from 'node:fs';
import { cleanVirtualFileSystem, useVirtualFileSystem } from '@o3r/test-helpers';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import * as path from 'node:path';

const virtualFileSystem = useVirtualFileSystem();
import yamlDependencyVersionsHarmonize from './yarnrc-package-extensions-harmonize';

const ruleTester = new TSESLint.RuleTester({
Expand Down Expand Up @@ -59,20 +61,20 @@ packageExtensions:
`;


const packageToLint = path.resolve(__dirname, 'local', '.yarnrc.yml');
const fakeFolder = path.resolve('/fake-folder');
const packageToLint = path.join(fakeFolder, 'local', '.yarnrc.yml');

beforeAll(() => {
fs.mkdirSync(path.resolve(__dirname, 'local'));
fs.writeFileSync(path.resolve(__dirname, 'local', 'package.json'), JSON.stringify(packageJsonWorkspace));
beforeAll(async () => {
await virtualFileSystem.promises.mkdir(path.join(fakeFolder, 'local'), {recursive: true});
await virtualFileSystem.promises.writeFile(path.join(fakeFolder, 'local', 'package.json'), JSON.stringify(packageJsonWorkspace));

fs.mkdirSync(path.resolve(__dirname, 'local', 'packages'));
fs.mkdirSync(path.resolve(__dirname, 'local', 'packages', 'my-package'));
fs.mkdirSync(path.resolve(__dirname, 'local', 'packages', 'my-package-2'));
fs.writeFileSync(path.resolve(__dirname, 'local', 'packages', 'my-package-2', 'package.json'), JSON.stringify(packageJson2));
await virtualFileSystem.promises.mkdir(path.join(fakeFolder, 'local', 'packages', 'my-package'), {recursive: true});
await virtualFileSystem.promises.mkdir(path.join(fakeFolder, 'local', 'packages', 'my-package-2'), {recursive: true});
await virtualFileSystem.promises.writeFile(path.join(fakeFolder, 'local', 'packages', 'my-package-2', 'package.json'), JSON.stringify(packageJson2));
});

afterAll(() => {
fs.rmSync(path.resolve(__dirname, 'local'), {recursive: true, force: true});
cleanVirtualFileSystem();
});

ruleTester.run('json-dependency-versions-harmonize', yamlDependencyVersionsHarmonize, {
Expand All @@ -89,7 +91,7 @@ ruleTester.run('json-dependency-versions-harmonize', yamlDependencyVersionsHarmo
messageId: 'error',
data: {
depName: 'myDep',
packageJsonFile: path.resolve(__dirname, 'local', 'package.json'),
packageJsonFile: path.join(fakeFolder, 'local', 'package.json'),
version: '^2.0.0'
},
suggestions: [
Expand Down Expand Up @@ -119,7 +121,7 @@ packageExtensions:
messageId: 'error',
data: {
depName: 'myDep',
packageJsonFile: path.resolve(__dirname, 'local', 'package.json'),
packageJsonFile: path.join(fakeFolder, 'local', 'package.json'),
version: '^2.0.0'
},
suggestions: [
Expand Down
4 changes: 4 additions & 0 deletions packages/@o3r/eslint-plugin/testing/jest.config.ut.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module.exports = {
...getJestProjectConfig(rootDir, false),
displayName: require('../package.json').name,
rootDir,
fakeTimers: {
enableGlobally: true,
advanceTimers: true
},
testPathIgnorePatterns: [
'<rootDir>/.*/templates/.*',
'<rootDir>/builders/.*',
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7492,6 +7492,7 @@ __metadata:
"@nx/eslint-plugin": "npm:~17.3.0"
"@nx/jest": "npm:~17.3.0"
"@o3r/build-helpers": "workspace:^"
"@o3r/test-helpers": "workspace:^"
"@stylistic/eslint-plugin-ts": "npm:^1.5.4"
"@types/jest": "npm:~29.5.2"
"@types/node": "npm:^20.0.0"
Expand Down

0 comments on commit e4093c7

Please sign in to comment.