From 3af0bf38d4a1be20762dd29c2c159e46d7b51810 Mon Sep 17 00:00:00 2001 From: Kazuki Yamada Date: Sun, 10 Nov 2024 22:26:31 +0900 Subject: [PATCH] feat(config): Implement Zod for robust configuration validation --- package-lock.json | 13 +- package.json | 3 +- src/cli/actions/defaultAction.ts | 2 +- src/cli/actions/initAction.ts | 2 +- src/cli/cliPrint.ts | 2 +- src/cli/cliRun.ts | 2 +- src/config/configLoad.ts | 29 ++-- src/config/configSchema.ts | 81 ++++++++++ src/config/configTypes.ts | 57 ------- src/config/configValidate.ts | 72 --------- src/config/defaultConfig.ts | 2 +- src/core/file/fileProcess.ts | 2 +- src/core/file/fileSearch.ts | 2 +- src/core/output/outputGenerate.ts | 2 +- src/core/output/outputGeneratorTypes.ts | 2 +- src/core/output/outputStyleDecorate.ts | 2 +- src/core/packager.ts | 2 +- src/index.ts | 2 +- src/shared/errorHandle.ts | 17 ++ tests/config/configLoad.test.ts | 26 ++- tests/config/configSchema.test.ts | 198 +++++++++++++++++++++++ tests/config/configValidate.test.ts | 51 ------ tests/integration-tests/packager.test.ts | 2 +- tests/testing/testUtils.ts | 2 +- 24 files changed, 367 insertions(+), 208 deletions(-) create mode 100644 src/config/configSchema.ts delete mode 100644 src/config/configTypes.ts delete mode 100644 src/config/configValidate.ts create mode 100644 tests/config/configSchema.test.ts delete mode 100644 tests/config/configValidate.test.ts diff --git a/package-lock.json b/package-lock.json index 0b10e38..17d6a28 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,8 @@ "p-map": "^7.0.2", "picocolors": "^1.1.1", "strip-comments": "^2.0.1", - "tiktoken": "^1.0.17" + "tiktoken": "^1.0.17", + "zod": "^3.23.8" }, "bin": { "repomix": "bin/repomix.cjs" @@ -426,6 +427,7 @@ }, "node_modules/@clack/prompts/node_modules/is-unicode-supported": { "version": "1.3.0", + "extraneous": true, "inBundle": true, "license": "MIT", "engines": { @@ -4273,6 +4275,15 @@ "engines": { "node": ">=8" } + }, + "node_modules/zod": { + "version": "3.23.8", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz", + "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } } } } diff --git a/package.json b/package.json index 2e88d14..f7fe045 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,8 @@ "p-map": "^7.0.2", "picocolors": "^1.1.1", "strip-comments": "^2.0.1", - "tiktoken": "^1.0.17" + "tiktoken": "^1.0.17", + "zod": "^3.23.8" }, "devDependencies": { "@biomejs/biome": "^1.9.4", diff --git a/src/cli/actions/defaultAction.ts b/src/cli/actions/defaultAction.ts index 313c46b..29f83ad 100644 --- a/src/cli/actions/defaultAction.ts +++ b/src/cli/actions/defaultAction.ts @@ -5,7 +5,7 @@ import type { RepomixConfigFile, RepomixConfigMerged, RepomixOutputStyle, -} from '../../config/configTypes.js'; +} from '../../config/configSchema.js'; import { type PackResult, pack } from '../../core/packager.js'; import { logger } from '../../shared/logger.js'; import { printCompletion, printSecurityCheck, printSummary, printTopFiles } from '../cliPrint.js'; diff --git a/src/cli/actions/initAction.ts b/src/cli/actions/initAction.ts index b095197..5b6b4c4 100644 --- a/src/cli/actions/initAction.ts +++ b/src/cli/actions/initAction.ts @@ -2,7 +2,7 @@ import fs from 'node:fs/promises'; import path from 'node:path'; import * as prompts from '@clack/prompts'; import pc from 'picocolors'; -import type { RepomixConfigFile, RepomixOutputStyle } from '../../config/configTypes.js'; +import type { RepomixConfigFile, RepomixOutputStyle } from '../../config/configSchema.js'; import { defaultConfig, defaultFilePathMap } from '../../config/defaultConfig.js'; import { getGlobalDirectory } from '../../config/globalDirectory.js'; import { logger } from '../../shared/logger.js'; diff --git a/src/cli/cliPrint.ts b/src/cli/cliPrint.ts index bd38e2c..68f7bed 100644 --- a/src/cli/cliPrint.ts +++ b/src/cli/cliPrint.ts @@ -1,6 +1,6 @@ import path from 'node:path'; import pc from 'picocolors'; -import type { RepomixConfigMerged } from '../config/configTypes.js'; +import type { RepomixConfigMerged } from '../config/configSchema.js'; import type { SuspiciousFileResult } from '../core/security/securityCheck.js'; import { logger } from '../shared/logger.js'; diff --git a/src/cli/cliRun.ts b/src/cli/cliRun.ts index 3a363e2..c55e9e0 100644 --- a/src/cli/cliRun.ts +++ b/src/cli/cliRun.ts @@ -1,7 +1,7 @@ import process from 'node:process'; import { type OptionValues, program } from 'commander'; import pc from 'picocolors'; -import type { RepomixOutputStyle } from '../config/configTypes.js'; +import type { RepomixOutputStyle } from '../config/configSchema.js'; import { getVersion } from '../core/file/packageJsonParse.js'; import { handleError } from '../shared/errorHandle.js'; import { logger } from '../shared/logger.js'; diff --git a/src/config/configLoad.ts b/src/config/configLoad.ts index 7903e4e..00a4dba 100644 --- a/src/config/configLoad.ts +++ b/src/config/configLoad.ts @@ -1,9 +1,14 @@ import * as fs from 'node:fs/promises'; import path from 'node:path'; -import { RepomixError } from '../shared/errorHandle.js'; +import { RepomixError, rethrowValidationErrorIfZodError } from '../shared/errorHandle.js'; import { logger } from '../shared/logger.js'; -import type { RepomixConfigCli, RepomixConfigFile, RepomixConfigMerged } from './configTypes.js'; -import { RepomixConfigValidationError, validateConfig } from './configValidate.js'; +import { + type RepomixConfigCli, + type RepomixConfigFile, + type RepomixConfigMerged, + repomixConfigFileSchema, + repomixConfigMergedSchema, +} from './configSchema.js'; import { defaultConfig, defaultFilePathMap } from './defaultConfig.js'; import { getGlobalDirectory } from './globalDirectory.js'; @@ -25,7 +30,6 @@ export const loadFileConfig = async (rootDir: string, argConfigPath: string | nu logger.trace('Loading local config from:', fullPath); - // Check local file existence const isLocalFileExists = await fs .stat(fullPath) .then((stats) => stats.isFile()) @@ -36,7 +40,6 @@ export const loadFileConfig = async (rootDir: string, argConfigPath: string | nu } if (useDefaultConfig) { - // Try to load global config const globalConfigPath = getGlobalConfigPath(); logger.trace('Loading global config from:', globalConfigPath); @@ -61,12 +64,9 @@ const loadAndValidateConfig = async (filePath: string): Promise; + +export type RepomixConfigDefault = z.infer; + +export type RepomixConfigFile = z.infer; + +export type RepomixConfigCli = z.infer; + +export type RepomixConfigMerged = z.infer; diff --git a/src/config/configTypes.ts b/src/config/configTypes.ts deleted file mode 100644 index 99ef70e..0000000 --- a/src/config/configTypes.ts +++ /dev/null @@ -1,57 +0,0 @@ -export type RepomixOutputStyle = 'plain' | 'xml' | 'markdown'; - -interface RepomixConfigBase { - output?: { - filePath?: string; - style?: RepomixOutputStyle; - headerText?: string; - instructionFilePath?: string; - removeComments?: boolean; - removeEmptyLines?: boolean; - topFilesLength?: number; - showLineNumbers?: boolean; - copyToClipboard?: boolean; - }; - include?: string[]; - ignore?: { - useGitignore?: boolean; - useDefaultPatterns?: boolean; - customPatterns?: string[]; - }; - security?: { - enableSecurityCheck?: boolean; - }; -} - -export type RepomixConfigDefault = RepomixConfigBase & { - output: { - filePath: string; - style: RepomixOutputStyle; - headerText?: string; - instructionFilePath?: string; - removeComments: boolean; - removeEmptyLines: boolean; - topFilesLength: number; - showLineNumbers: boolean; - copyToClipboard: boolean; - }; - include: string[]; - ignore: { - useGitignore: boolean; - useDefaultPatterns: boolean; - customPatterns?: string[]; - }; - security: { - enableSecurityCheck: boolean; - }; -}; - -export type RepomixConfigFile = RepomixConfigBase; - -export type RepomixConfigCli = RepomixConfigBase; - -export type RepomixConfigMerged = RepomixConfigDefault & - RepomixConfigFile & - RepomixConfigCli & { - cwd: string; - }; diff --git a/src/config/configValidate.ts b/src/config/configValidate.ts deleted file mode 100644 index 0afc7f7..0000000 --- a/src/config/configValidate.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { RepomixError } from '../shared/errorHandle.js'; -import type { RepomixConfigFile } from './configTypes.js'; - -export class RepomixConfigValidationError extends RepomixError { - constructor(message: string) { - super(message); - this.name = 'RepomixConfigValidationError'; - } -} - -export function validateConfig(config: unknown): asserts config is RepomixConfigFile { - if (typeof config !== 'object' || config == null) { - throw new RepomixConfigValidationError('Configuration must be an object'); - } - - const { output, ignore, security } = config as Partial; - - // Validate output - if (output !== undefined) { - if (typeof output !== 'object' || output == null) { - throw new RepomixConfigValidationError('output must be an object'); - } - - const { filePath, headerText, style } = output; - if (filePath !== undefined && typeof filePath !== 'string') { - throw new RepomixConfigValidationError('output.filePath must be a string'); - } - if (headerText !== undefined && typeof headerText !== 'string') { - throw new RepomixConfigValidationError('output.headerText must be a string'); - } - if (style !== undefined) { - if (typeof style !== 'string') { - throw new RepomixConfigValidationError('output.style must be a string'); - } - if (style !== 'plain' && style !== 'xml' && style !== 'markdown') { - throw new RepomixConfigValidationError('output.style must be either "plain", "xml" or "markdown"'); - } - } - } - - // Validate ignore - if (ignore !== undefined) { - if (typeof ignore !== 'object' || ignore == null) { - throw new RepomixConfigValidationError('ignore must be an object'); - } - - const { useDefaultPatterns, customPatterns } = ignore; - if (useDefaultPatterns !== undefined && typeof useDefaultPatterns !== 'boolean') { - throw new RepomixConfigValidationError('ignore.useDefaultPatterns must be a boolean'); - } - if (customPatterns !== undefined) { - if (!Array.isArray(customPatterns)) { - throw new RepomixConfigValidationError('ignore.customPatterns must be an array'); - } - if (!customPatterns.every((pattern) => typeof pattern === 'string')) { - throw new RepomixConfigValidationError('All items in ignore.customPatterns must be strings'); - } - } - } - - // Validate security - if (security !== undefined) { - if (typeof security !== 'object' || security == null) { - throw new RepomixConfigValidationError('security must be an object'); - } - - const { enableSecurityCheck } = security; - if (enableSecurityCheck !== undefined && typeof enableSecurityCheck !== 'boolean') { - throw new RepomixConfigValidationError('security.enableSecurityCheck must be a boolean'); - } - } -} diff --git a/src/config/defaultConfig.ts b/src/config/defaultConfig.ts index 1c83999..65857a6 100644 --- a/src/config/defaultConfig.ts +++ b/src/config/defaultConfig.ts @@ -1,4 +1,4 @@ -import type { RepomixConfigDefault, RepomixOutputStyle } from './configTypes.js'; +import type { RepomixConfigDefault, RepomixOutputStyle } from './configSchema.js'; export const defaultFilePathMap: Record = { plain: 'repomix-output.txt', diff --git a/src/core/file/fileProcess.ts b/src/core/file/fileProcess.ts index fe87ee1..d6a6992 100644 --- a/src/core/file/fileProcess.ts +++ b/src/core/file/fileProcess.ts @@ -1,5 +1,5 @@ import pMap from 'p-map'; -import type { RepomixConfigMerged } from '../../config/configTypes.js'; +import type { RepomixConfigMerged } from '../../config/configSchema.js'; import { getProcessConcurrency } from '../../shared/processConcurrency.js'; import { getFileManipulator } from './fileManipulate.js'; import type { ProcessedFile, RawFile } from './fileTypes.js'; diff --git a/src/core/file/fileSearch.ts b/src/core/file/fileSearch.ts index 60deb32..6ae8a25 100644 --- a/src/core/file/fileSearch.ts +++ b/src/core/file/fileSearch.ts @@ -1,5 +1,5 @@ import { globby } from 'globby'; -import type { RepomixConfigMerged } from '../../config/configTypes.js'; +import type { RepomixConfigMerged } from '../../config/configSchema.js'; import { defaultIgnoreList } from '../../config/defaultIgnore.js'; import { logger } from '../../shared/logger.js'; import { sortPaths } from './filePathSort.js'; diff --git a/src/core/output/outputGenerate.ts b/src/core/output/outputGenerate.ts index c0b16e8..9064024 100644 --- a/src/core/output/outputGenerate.ts +++ b/src/core/output/outputGenerate.ts @@ -1,7 +1,7 @@ import fs from 'node:fs/promises'; import path from 'node:path'; import Handlebars from 'handlebars'; -import type { RepomixConfigMerged } from '../../config/configTypes.js'; +import type { RepomixConfigMerged } from '../../config/configSchema.js'; import { RepomixError } from '../../shared/errorHandle.js'; import { generateTreeString } from '../file/fileTreeGenerate.js'; import type { ProcessedFile } from '../file/fileTypes.js'; diff --git a/src/core/output/outputGeneratorTypes.ts b/src/core/output/outputGeneratorTypes.ts index dac9f15..5ff261c 100644 --- a/src/core/output/outputGeneratorTypes.ts +++ b/src/core/output/outputGeneratorTypes.ts @@ -1,4 +1,4 @@ -import type { RepomixConfigMerged } from '../../config/configTypes.js'; +import type { RepomixConfigMerged } from '../../config/configSchema.js'; import type { ProcessedFile } from '../file/fileTypes.js'; export interface OutputGeneratorContext { diff --git a/src/core/output/outputStyleDecorate.ts b/src/core/output/outputStyleDecorate.ts index bdbbc55..5d05638 100644 --- a/src/core/output/outputStyleDecorate.ts +++ b/src/core/output/outputStyleDecorate.ts @@ -1,4 +1,4 @@ -import type { RepomixConfigMerged } from '../../config/configTypes.js'; +import type { RepomixConfigMerged } from '../../config/configSchema.js'; export const generateHeader = (generationDate: string): string => { return ` diff --git a/src/core/packager.ts b/src/core/packager.ts index eecc236..95ae2e1 100644 --- a/src/core/packager.ts +++ b/src/core/packager.ts @@ -4,7 +4,7 @@ import { setTimeout } from 'node:timers/promises'; import clipboard from 'clipboardy'; import pMap from 'p-map'; import pc from 'picocolors'; -import type { RepomixConfigMerged } from '../config/configTypes.js'; +import type { RepomixConfigMerged } from '../config/configSchema.js'; import { logger } from '../shared/logger.js'; import { getProcessConcurrency } from '../shared/processConcurrency.js'; import type { RepomixProgressCallback } from '../shared/types.js'; diff --git a/src/index.ts b/src/index.ts index 5940380..926d27e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ export { pack } from './core/packager.js'; -export type { RepomixConfigFile as RepomixConfig } from './config/configTypes.js'; +export type { RepomixConfigFile as RepomixConfig } from './config/configSchema.js'; export { run as cli } from './cli/cliRun.js'; diff --git a/src/shared/errorHandle.ts b/src/shared/errorHandle.ts index 940a156..0be93a8 100644 --- a/src/shared/errorHandle.ts +++ b/src/shared/errorHandle.ts @@ -1,4 +1,5 @@ import { logger } from './logger.js'; +import { z } from 'zod'; export class RepomixError extends Error { constructor(message: string) { @@ -7,6 +8,13 @@ export class RepomixError extends Error { } } +export class RepomixConfigValidationError extends RepomixError { + constructor(message: string) { + super(message); + this.name = 'RepomixConfigValidationError'; + } +} + export const handleError = (error: unknown): void => { if (error instanceof RepomixError) { logger.error(`Error: ${error.message}`); @@ -19,3 +27,12 @@ export const handleError = (error: unknown): void => { logger.info('For more help, please visit: https://github.com/yamadashy/repomix/issues'); }; + +export const rethrowValidationErrorIfZodError = (error: unknown, message: string): void => { + if (error instanceof z.ZodError) { + const zodErrorText = error.errors.map((err) => `${err.path.join('.')}: ${err.message}`).join(', '); + throw new RepomixConfigValidationError( + `${message}\n ${zodErrorText}\n Please check the config file and try again.`, + ); + } +}; diff --git a/tests/config/configLoad.test.ts b/tests/config/configLoad.test.ts index ec41a84..0516e2a 100644 --- a/tests/config/configLoad.test.ts +++ b/tests/config/configLoad.test.ts @@ -4,9 +4,10 @@ import path from 'node:path'; import process from 'node:process'; import { beforeEach, describe, expect, test, vi } from 'vitest'; import { loadFileConfig, mergeConfigs } from '../../src/config/configLoad.js'; -import type { RepomixConfigCli, RepomixConfigFile } from '../../src/config/configTypes.js'; +import type { RepomixConfigCli, RepomixConfigFile } from '../../src/config/configSchema.js'; import { getGlobalDirectory } from '../../src/config/globalDirectory.js'; import { logger } from '../../src/shared/logger.js'; +import { RepomixConfigValidationError } from '../../src/shared/errorHandle.js'; vi.mock('node:fs/promises'); vi.mock('../../src/shared/logger', () => ({ @@ -38,6 +39,17 @@ describe('configLoad', () => { expect(result).toEqual(mockConfig); }); + test('should throw RepomixConfigValidationError for invalid config', async () => { + const invalidConfig = { + output: { filePath: 123, style: 'invalid' }, // Invalid filePath type and invalid style + ignore: { useDefaultPatterns: 'not a boolean' }, // Invalid type + }; + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(invalidConfig)); + vi.mocked(fs.stat).mockResolvedValue({ isFile: () => true } as Stats); + + await expect(loadFileConfig(process.cwd(), 'test-config.json')).rejects.toThrow(RepomixConfigValidationError); + }); + test('should load global config when local config is not found', async () => { const mockGlobalConfig = { output: { filePath: 'global-output.txt' }, @@ -91,5 +103,17 @@ describe('configLoad', () => { expect(result.ignore.customPatterns).toContain('file-ignore'); expect(result.ignore.customPatterns).toContain('cli-ignore'); }); + + test('should throw RepomixConfigValidationError for invalid merged config', () => { + const fileConfig: RepomixConfigFile = { + output: { filePath: 'file-output.txt', style: 'plain' }, + }; + const cliConfig: RepomixConfigCli = { + // @ts-ignore + output: { style: 'invalid' }, // Invalid style + }; + + expect(() => mergeConfigs(process.cwd(), fileConfig, cliConfig)).toThrow(RepomixConfigValidationError); + }); }); }); diff --git a/tests/config/configSchema.test.ts b/tests/config/configSchema.test.ts new file mode 100644 index 0000000..48acc00 --- /dev/null +++ b/tests/config/configSchema.test.ts @@ -0,0 +1,198 @@ +import { describe, expect, it } from 'vitest'; +import { z } from 'zod'; +import { + repomixConfigBaseSchema, + repomixConfigCliSchema, + repomixConfigDefaultSchema, + repomixConfigFileSchema, + repomixConfigMergedSchema, + repomixOutputStyleSchema, +} from '../../src/config/configSchema.js'; + +describe('configSchema', () => { + describe('repomixOutputStyleSchema', () => { + it('should accept valid output styles', () => { + expect(repomixOutputStyleSchema.parse('plain')).toBe('plain'); + expect(repomixOutputStyleSchema.parse('xml')).toBe('xml'); + }); + + it('should reject invalid output styles', () => { + expect(() => repomixOutputStyleSchema.parse('invalid')).toThrow(z.ZodError); + }); + }); + + describe('repomixConfigBaseSchema', () => { + it('should accept valid base config', () => { + const validConfig = { + output: { + filePath: 'output.txt', + style: 'plain', + removeComments: true, + }, + include: ['**/*.js'], + ignore: { + useGitignore: true, + customPatterns: ['node_modules'], + }, + security: { + enableSecurityCheck: true, + }, + }; + expect(repomixConfigBaseSchema.parse(validConfig)).toEqual(validConfig); + }); + + it('should accept empty object', () => { + expect(repomixConfigBaseSchema.parse({})).toEqual({}); + }); + + it('should reject invalid types', () => { + const invalidConfig = { + output: { + filePath: 123, // Should be string + style: 'invalid', // Should be 'plain' or 'xml' + }, + include: 'not-an-array', // Should be an array + }; + expect(() => repomixConfigBaseSchema.parse(invalidConfig)).toThrow(z.ZodError); + }); + }); + + describe('repomixConfigDefaultSchema', () => { + it('should accept valid default config', () => { + const validConfig = { + output: { + filePath: 'output.txt', + style: 'plain', + removeComments: false, + removeEmptyLines: false, + topFilesLength: 5, + showLineNumbers: false, + copyToClipboard: true, + }, + include: [], + ignore: { + useGitignore: true, + useDefaultPatterns: true, + }, + security: { + enableSecurityCheck: true, + }, + }; + expect(repomixConfigDefaultSchema.parse(validConfig)).toEqual(validConfig); + }); + + it('should reject incomplete config', () => { + const incompleteConfig = { + output: { + filePath: 'output.txt', + // Missing required fields + }, + }; + expect(() => repomixConfigDefaultSchema.parse(incompleteConfig)).toThrow(z.ZodError); + }); + }); + + describe('repomixConfigFileSchema', () => { + it('should accept valid file config', () => { + const validConfig = { + output: { + filePath: 'custom-output.txt', + style: 'xml', + }, + ignore: { + customPatterns: ['*.log'], + }, + }; + expect(repomixConfigFileSchema.parse(validConfig)).toEqual(validConfig); + }); + + it('should accept partial config', () => { + const partialConfig = { + output: { + filePath: 'partial-output.txt', + }, + }; + expect(repomixConfigFileSchema.parse(partialConfig)).toEqual(partialConfig); + }); + }); + + describe('repomixConfigCliSchema', () => { + it('should accept valid CLI config', () => { + const validConfig = { + output: { + filePath: 'cli-output.txt', + showLineNumbers: true, + }, + include: ['src/**/*.ts'], + }; + expect(repomixConfigCliSchema.parse(validConfig)).toEqual(validConfig); + }); + + it('should reject invalid CLI options', () => { + const invalidConfig = { + output: { + filePath: 123, // Should be string + }, + }; + expect(() => repomixConfigCliSchema.parse(invalidConfig)).toThrow(z.ZodError); + }); + }); + + describe('repomixConfigMergedSchema', () => { + it('should accept valid merged config', () => { + const validConfig = { + cwd: '/path/to/project', + output: { + filePath: 'merged-output.txt', + style: 'plain', + removeComments: true, + removeEmptyLines: false, + topFilesLength: 10, + showLineNumbers: true, + copyToClipboard: false, + }, + include: ['**/*.js', '**/*.ts'], + ignore: { + useGitignore: true, + useDefaultPatterns: true, + customPatterns: ['*.log'], + }, + security: { + enableSecurityCheck: true, + }, + }; + expect(repomixConfigMergedSchema.parse(validConfig)).toEqual(validConfig); + }); + + it('should reject merged config missing required fields', () => { + const invalidConfig = { + cwd: '/path/to/project', + // Missing required output field + }; + expect(() => repomixConfigMergedSchema.parse(invalidConfig)).toThrow(z.ZodError); + }); + + it('should reject merged config with invalid types', () => { + const invalidConfig = { + cwd: '/path/to/project', + output: { + filePath: 'output.txt', + style: 'plain', + removeComments: 'not-a-boolean', // Should be boolean + removeEmptyLines: false, + topFilesLength: '5', // Should be number + showLineNumbers: false, + }, + include: ['**/*.js'], + ignore: { + useGitignore: true, + useDefaultPatterns: true, + }, + security: { + enableSecurityCheck: true, + }, + }; + expect(() => repomixConfigMergedSchema.parse(invalidConfig)).toThrow(z.ZodError); + }); + }); +}); diff --git a/tests/config/configValidate.test.ts b/tests/config/configValidate.test.ts deleted file mode 100644 index 67f7583..0000000 --- a/tests/config/configValidate.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { describe, expect, test } from 'vitest'; -import { RepomixConfigValidationError, validateConfig } from '../../src/config/configValidate.js'; - -describe('configValidate', () => { - test('should pass for a valid config', () => { - const validConfig = { - output: { filePath: 'test.txt', headerText: 'Test Header' }, - ignore: { useDefaultPatterns: true, customPatterns: ['*.log'] }, - }; - expect(() => validateConfig(validConfig)).not.toThrow(); - }); - - test('should throw for non-object config', () => { - expect(() => validateConfig('not an object')).toThrow(RepomixConfigValidationError); - }); - - test('should throw for invalid output.filePath', () => { - const invalidConfig = { output: { filePath: 123 } }; - expect(() => validateConfig(invalidConfig)).toThrow(RepomixConfigValidationError); - }); - - test('should throw for invalid ignore.useDefaultPatterns', () => { - const invalidConfig = { ignore: { useDefaultPatterns: 'true' } }; - expect(() => validateConfig(invalidConfig)).toThrow(RepomixConfigValidationError); - }); - - test('should throw for invalid ignore.customPatterns', () => { - const invalidConfig = { ignore: { customPatterns: 'not an array' } }; - expect(() => validateConfig(invalidConfig)).toThrow(RepomixConfigValidationError); - }); - - test('should pass for a valid config with output style', () => { - const validConfig = { - output: { filePath: 'test.txt', style: 'xml' }, - ignore: { useDefaultPatterns: true }, - }; - expect(() => validateConfig(validConfig)).not.toThrow(); - }); - - test('should throw for invalid output.style type', () => { - const invalidConfig = { output: { style: 123 } }; - expect(() => validateConfig(invalidConfig)).toThrow(RepomixConfigValidationError); - expect(() => validateConfig(invalidConfig)).toThrow('output.style must be a string'); - }); - - test('should throw for invalid output.style value', () => { - const invalidConfig = { output: { style: 'invalid' } }; - expect(() => validateConfig(invalidConfig)).toThrow(RepomixConfigValidationError); - expect(() => validateConfig(invalidConfig)).toThrow('output.style must be either "plain", "xml" or "markdown"'); - }); -}); diff --git a/tests/integration-tests/packager.test.ts b/tests/integration-tests/packager.test.ts index 4730ffb..e361f14 100644 --- a/tests/integration-tests/packager.test.ts +++ b/tests/integration-tests/packager.test.ts @@ -4,7 +4,7 @@ import path from 'node:path'; import process from 'node:process'; import { afterEach, beforeEach, describe, expect, test } from 'vitest'; import { loadFileConfig, mergeConfigs } from '../../src/config/configLoad.js'; -import type { RepomixConfigFile, RepomixConfigMerged, RepomixOutputStyle } from '../../src/config/configTypes.js'; +import type { RepomixConfigFile, RepomixConfigMerged, RepomixOutputStyle } from '../../src/config/configSchema.js'; import { pack } from '../../src/core/packager.js'; import { isWindows } from '../testing/testUtils.js'; diff --git a/tests/testing/testUtils.ts b/tests/testing/testUtils.ts index ffdb62a..9652966 100644 --- a/tests/testing/testUtils.ts +++ b/tests/testing/testUtils.ts @@ -1,6 +1,6 @@ import os from 'node:os'; import process from 'node:process'; -import type { RepomixConfigMerged } from '../../src/config/configTypes.js'; +import type { RepomixConfigMerged } from '../../src/config/configSchema.js'; import { defaultConfig } from '../../src/config/defaultConfig.js'; type DeepPartial = {