-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,67 @@ | ||
import { expect, test, describe } from 'vitest'; | ||
import { mergeConfigs } from '../../src/config/configLoader.js'; | ||
import { RepopackConfigCli, RepopackConfigFile } from '../../src/types/index.js'; | ||
import { defaultConfig } from '../../src/config/defaultConfig.js'; | ||
import { expect, test, describe, vi, beforeEach } from 'vitest'; | ||
import { loadFileConfig, mergeConfigs } from '../../src/config/configLoader.js'; | ||
import { RepopackConfigFile, RepopackConfigCli } from '../../src/types/index.js'; | ||
import * as fs from 'fs/promises'; | ||
import { Stats } from 'fs'; | ||
|
||
vi.mock('fs/promises'); | ||
vi.mock('../../src/utils/logger', () => ({ | ||
logger: { | ||
trace: vi.fn(), | ||
note: vi.fn(), | ||
}, | ||
})); | ||
|
||
describe('configLoader', () => { | ||
test('mergeConfigs should correctly merge configs', () => { | ||
const fileConfig: RepopackConfigFile = { | ||
output: { | ||
filePath: 'file-output.txt', | ||
headerText: 'File header', | ||
}, | ||
ignore: { | ||
useDefaultPatterns: true, | ||
customPatterns: ['file-ignore'], | ||
}, | ||
}; | ||
|
||
const cliConfig: RepopackConfigCli = { | ||
output: { | ||
filePath: 'cli-output.txt', | ||
}, | ||
ignore: { | ||
useDefaultPatterns: true, | ||
customPatterns: ['cli-ignore'], | ||
}, | ||
}; | ||
|
||
const mergedConfig = mergeConfigs(fileConfig, cliConfig); | ||
|
||
expect(mergedConfig).toEqual({ | ||
output: { | ||
filePath: 'cli-output.txt', | ||
headerText: 'File header', | ||
}, | ||
ignore: { | ||
useDefaultPatterns: true, | ||
customPatterns: ['file-ignore', 'cli-ignore'], | ||
}, | ||
}); | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
test('mergeConfigs should use default values when not provided', () => { | ||
const mergedConfig = mergeConfigs({}, {}); | ||
describe('loadFileConfig', () => { | ||
test('should load and parse a valid config file', async () => { | ||
const mockConfig = { | ||
output: { filePath: 'test-output.txt' }, | ||
ignore: { useDefaultPatterns: true }, | ||
}; | ||
vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(mockConfig)); | ||
vi.mocked(fs.stat).mockResolvedValue({ isFile: () => true } as Stats); | ||
|
||
const result = await loadFileConfig('test-config.json'); | ||
expect(result).toEqual(mockConfig); | ||
}); | ||
|
||
test('should return an empty object if no config file is found', async () => { | ||
vi.mocked(fs.stat).mockRejectedValue(new Error('File not found')); | ||
|
||
expect(mergedConfig).toEqual(defaultConfig); | ||
const result = await loadFileConfig(null); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
test('should throw an error for invalid JSON', async () => { | ||
vi.mocked(fs.readFile).mockResolvedValue('invalid json'); | ||
vi.mocked(fs.stat).mockResolvedValue({ isFile: () => true } as Stats); | ||
|
||
await expect(loadFileConfig('test-config.json')).rejects.toThrow('Invalid JSON'); | ||
}); | ||
}); | ||
|
||
test('mergeConfigs should override default headerText', () => { | ||
const fileConfig: RepopackConfigFile = { | ||
output: { | ||
filePath: 'file-output.txt', | ||
headerText: 'Custom header', | ||
}, | ||
}; | ||
describe('mergeConfigs', () => { | ||
test('should correctly merge configs', () => { | ||
const fileConfig: RepopackConfigFile = { | ||
output: { filePath: 'file-output.txt' }, | ||
ignore: { useDefaultPatterns: true, customPatterns: ['file-ignore'] }, | ||
}; | ||
const cliConfig: RepopackConfigCli = { | ||
output: { filePath: 'cli-output.txt' }, | ||
ignore: { customPatterns: ['cli-ignore'] }, | ||
}; | ||
|
||
const mergedConfig = mergeConfigs(fileConfig, {}); | ||
const result = mergeConfigs(fileConfig, cliConfig); | ||
|
||
expect(mergedConfig.output.headerText).toBe('Custom header'); | ||
expect(result.output.filePath).toBe('cli-output.txt'); | ||
expect(result.ignore.useDefaultPatterns).toBe(true); | ||
expect(result.ignore.customPatterns).toContain('file-ignore'); | ||
expect(result.ignore.customPatterns).toContain('cli-ignore'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { expect, test, describe } from 'vitest'; | ||
import { validateConfig, RepopackConfigValidationError } from '../../src/config/configValidator.js'; | ||
|
||
describe('configValidator', () => { | ||
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(RepopackConfigValidationError); | ||
}); | ||
|
||
test('should throw for invalid output.filePath', () => { | ||
const invalidConfig = { output: { filePath: 123 } }; | ||
expect(() => validateConfig(invalidConfig)).toThrow(RepopackConfigValidationError); | ||
}); | ||
|
||
test('should throw for invalid ignore.useDefaultPatterns', () => { | ||
const invalidConfig = { ignore: { useDefaultPatterns: 'true' } }; | ||
expect(() => validateConfig(invalidConfig)).toThrow(RepopackConfigValidationError); | ||
}); | ||
|
||
test('should throw for invalid ignore.customPatterns', () => { | ||
const invalidConfig = { ignore: { customPatterns: 'not an array' } }; | ||
expect(() => validateConfig(invalidConfig)).toThrow(RepopackConfigValidationError); | ||
}); | ||
}); |