From 961c227a51d9de00c22f500b889eebe168b48c53 Mon Sep 17 00:00:00 2001 From: Florian PAUL Date: Tue, 6 Feb 2024 15:12:29 +0100 Subject: [PATCH] test: use virtual filesystem for eslint-plugin tests --- .eslintrc.js | 3 +- packages/@o3r/eslint-plugin/package.json | 1 + ...json-dependency-versions-harmonize.spec.ts | 35 ++++++++++--------- .../version-harmonize.ts | 3 +- .../no-folder-import-for-module.spec.ts | 25 ++++++------- ...arnrc-package-extensions-harmonize.spec.ts | 28 ++++++++------- .../eslint-plugin/testing/jest.config.ut.js | 4 +++ yarn.lock | 1 + 8 files changed, 56 insertions(+), 44 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index d2ced9073a..b13c930e25 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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'] }] } }, diff --git a/packages/@o3r/eslint-plugin/package.json b/packages/@o3r/eslint-plugin/package.json index 598be2874a..0af399477a 100644 --- a/packages/@o3r/eslint-plugin/package.json +++ b/packages/@o3r/eslint-plugin/package.json @@ -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", diff --git a/packages/@o3r/eslint-plugin/src/rules/json/json-dependency-versions-harmonize/json-dependency-versions-harmonize.spec.ts b/packages/@o3r/eslint-plugin/src/rules/json/json-dependency-versions-harmonize/json-dependency-versions-harmonize.spec.ts index bc5a347872..8b5d8c456b 100644 --- a/packages/@o3r/eslint-plugin/src/rules/json/json-dependency-versions-harmonize/json-dependency-versions-harmonize.spec.ts +++ b/packages/@o3r/eslint-plugin/src/rules/json/json-dependency-versions-harmonize/json-dependency-versions-harmonize.spec.ts @@ -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({ @@ -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, { @@ -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: [ @@ -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' } } @@ -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' } } @@ -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' } } diff --git a/packages/@o3r/eslint-plugin/src/rules/json/json-dependency-versions-harmonize/version-harmonize.ts b/packages/@o3r/eslint-plugin/src/rules/json/json-dependency-versions-harmonize/version-harmonize.ts index 04ced03ced..9e0fe83fd2 100644 --- a/packages/@o3r/eslint-plugin/src/rules/json/json-dependency-versions-harmonize/version-harmonize.ts +++ b/packages/@o3r/eslint-plugin/src/rules/json/json-dependency-versions-harmonize/version-harmonize.ts @@ -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'; @@ -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 = () => ([ diff --git a/packages/@o3r/eslint-plugin/src/rules/typescript/no-folder-import-for-module/no-folder-import-for-module.spec.ts b/packages/@o3r/eslint-plugin/src/rules/typescript/no-folder-import-for-module/no-folder-import-for-module.spec.ts index 1c11852556..8086d50618 100644 --- a/packages/@o3r/eslint-plugin/src/rules/typescript/no-folder-import-for-module/no-folder-import-for-module.spec.ts +++ b/packages/@o3r/eslint-plugin/src/rules/typescript/no-folder-import-for-module/no-folder-import-for-module.spec.ts @@ -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({ @@ -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, { @@ -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: [ @@ -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: [ @@ -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: [ { diff --git a/packages/@o3r/eslint-plugin/src/rules/yaml/yarnrc-package-extensions-harmonize/yarnrc-package-extensions-harmonize.spec.ts b/packages/@o3r/eslint-plugin/src/rules/yaml/yarnrc-package-extensions-harmonize/yarnrc-package-extensions-harmonize.spec.ts index e5d398a524..1492ef9f07 100644 --- a/packages/@o3r/eslint-plugin/src/rules/yaml/yarnrc-package-extensions-harmonize/yarnrc-package-extensions-harmonize.spec.ts +++ b/packages/@o3r/eslint-plugin/src/rules/yaml/yarnrc-package-extensions-harmonize/yarnrc-package-extensions-harmonize.spec.ts @@ -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({ @@ -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, { @@ -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: [ @@ -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: [ diff --git a/packages/@o3r/eslint-plugin/testing/jest.config.ut.js b/packages/@o3r/eslint-plugin/testing/jest.config.ut.js index 5eb96361ac..1d202c4c96 100644 --- a/packages/@o3r/eslint-plugin/testing/jest.config.ut.js +++ b/packages/@o3r/eslint-plugin/testing/jest.config.ut.js @@ -7,6 +7,10 @@ module.exports = { ...getJestProjectConfig(rootDir, false), displayName: require('../package.json').name, rootDir, + fakeTimers: { + enableGlobally: true, + advanceTimers: true + }, testPathIgnorePatterns: [ '/.*/templates/.*', '/builders/.*', diff --git a/yarn.lock b/yarn.lock index 2676911eec..65bad05094 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"