|
| 1 | +import path from "path"; |
| 2 | +import { RuleTester } from "eslint"; |
| 3 | +import { describe, it } from "vitest"; |
| 4 | +import tsParser from "@typescript-eslint/parser"; |
| 5 | +import rule from "./no-config-imports.js"; |
| 6 | + |
| 7 | +const ROOT = process.cwd(); |
| 8 | +const resolve = (p) => path.resolve(ROOT, p); |
| 9 | +const CONFIG_FILE_PATH = resolve("src/common/config.js"); |
| 10 | +const ALLOWED_CONFIG_VALUE_IMPORT_FILES = [resolve("src/index.ts"), resolve("src/resources/common/config.ts")]; |
| 11 | + |
| 12 | +const ruleTester = new RuleTester({ |
| 13 | + languageOptions: { |
| 14 | + parser: tsParser, |
| 15 | + parserOptions: { ecmaVersion: 2022, sourceType: "module" }, |
| 16 | + }, |
| 17 | +}); |
| 18 | + |
| 19 | +describe("no-config-imports", () => { |
| 20 | + it("should not report any violations", () => { |
| 21 | + ruleTester.run("no-config-imports", rule, { |
| 22 | + valid: [ |
| 23 | + { |
| 24 | + filename: resolve("src/some/module.ts"), |
| 25 | + code: 'import type { UserConfig } from "../common/config.js";\n', |
| 26 | + options: [{ configFilePath: CONFIG_FILE_PATH, allowedFiles: ALLOWED_CONFIG_VALUE_IMPORT_FILES }], |
| 27 | + }, |
| 28 | + { |
| 29 | + filename: resolve("src/some/module.ts"), |
| 30 | + code: 'import { something } from "../common/logger.js";\n', |
| 31 | + options: [{ configFilePath: CONFIG_FILE_PATH, allowedFiles: ALLOWED_CONFIG_VALUE_IMPORT_FILES }], |
| 32 | + }, |
| 33 | + { |
| 34 | + filename: resolve("src/some/module.ts"), |
| 35 | + code: 'import type * as Cfg from "../common/config.js";\n', |
| 36 | + options: [{ configFilePath: CONFIG_FILE_PATH, allowedFiles: ALLOWED_CONFIG_VALUE_IMPORT_FILES }], |
| 37 | + }, |
| 38 | + { |
| 39 | + filename: ALLOWED_CONFIG_VALUE_IMPORT_FILES[1], |
| 40 | + code: 'import { driverOptions } from "../common/config.js";\n', |
| 41 | + options: [{ configFilePath: CONFIG_FILE_PATH, allowedFiles: ALLOWED_CONFIG_VALUE_IMPORT_FILES }], |
| 42 | + }, |
| 43 | + ], |
| 44 | + invalid: [], |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should report rule violations", () => { |
| 49 | + ruleTester.run("no-config-imports", rule, { |
| 50 | + valid: [], |
| 51 | + invalid: [ |
| 52 | + { |
| 53 | + filename: resolve("src/another/module.ts"), |
| 54 | + code: 'import { driverOptions } from "../common/config.js";\n', |
| 55 | + options: [{ configFilePath: CONFIG_FILE_PATH, allowedFiles: ALLOWED_CONFIG_VALUE_IMPORT_FILES }], |
| 56 | + errors: [{ messageId: "noConfigImports" }], |
| 57 | + }, |
| 58 | + { |
| 59 | + filename: resolve("src/another/module.ts"), |
| 60 | + code: 'import configDefault from "../common/config.js";\n', |
| 61 | + options: [{ configFilePath: CONFIG_FILE_PATH, allowedFiles: ALLOWED_CONFIG_VALUE_IMPORT_FILES }], |
| 62 | + errors: [{ messageId: "noConfigImports" }], |
| 63 | + }, |
| 64 | + { |
| 65 | + filename: resolve("src/another/module.ts"), |
| 66 | + code: 'import * as cfg from "../common/config.js";\n', |
| 67 | + options: [{ configFilePath: CONFIG_FILE_PATH, allowedFiles: ALLOWED_CONFIG_VALUE_IMPORT_FILES }], |
| 68 | + errors: [{ messageId: "noConfigImports" }], |
| 69 | + }, |
| 70 | + { |
| 71 | + filename: resolve("src/another/module.ts"), |
| 72 | + code: 'import { type UserConfig, driverOptions } from "../common/config.js";\n', |
| 73 | + options: [{ configFilePath: CONFIG_FILE_PATH, allowedFiles: ALLOWED_CONFIG_VALUE_IMPORT_FILES }], |
| 74 | + errors: [{ messageId: "noConfigImports" }], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
0 commit comments