Skip to content

Commit

Permalink
chore: Add config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadashy committed Jul 15, 2024
1 parent 3e61086 commit 0d62855
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 50 deletions.
106 changes: 56 additions & 50 deletions tests/config/configLoader.test.ts
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');
});
});
});
31 changes: 31 additions & 0 deletions tests/config/configValidator.test.ts
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);
});
});

0 comments on commit 0d62855

Please sign in to comment.