|
1 | 1 | import { dirname, join } from 'path';
|
2 | 2 | import { fileURLToPath } from 'url';
|
3 |
| -import { expect } from 'vitest'; |
4 |
| -import { configMiddleware } from './config-middleware'; |
| 3 | +import { SpyInstance, afterEach, beforeEach, describe, expect } from 'vitest'; |
| 4 | +import { CoreConfig } from '@code-pushup/models'; |
| 5 | +import { |
| 6 | + configMiddleware, |
| 7 | + filterCategoryByOnlyPluginsOption, |
| 8 | + filterPluginsByOnlyPluginsOption, |
| 9 | + validateOnlyPluginsOption, |
| 10 | +} from './config-middleware'; |
5 | 11 |
|
6 | 12 | const __dirname = dirname(fileURLToPath(import.meta.url));
|
7 | 13 | const withDirName = (path: string) => join(__dirname, path);
|
@@ -52,3 +58,110 @@ describe('applyConfigMiddleware', () => {
|
52 | 58 | expect(error?.message).toContain(defaultConfigPath);
|
53 | 59 | });
|
54 | 60 | });
|
| 61 | + |
| 62 | +describe('filterPluginsByOnlyPluginsOption', () => { |
| 63 | + it('should return all plugins if no onlyPlugins option', async () => { |
| 64 | + const plugins = [ |
| 65 | + { slug: 'plugin1' }, |
| 66 | + { slug: 'plugin2' }, |
| 67 | + { slug: 'plugin3' }, |
| 68 | + ]; |
| 69 | + const filtered = filterPluginsByOnlyPluginsOption( |
| 70 | + plugins as CoreConfig['plugins'], |
| 71 | + {}, |
| 72 | + ); |
| 73 | + expect(filtered).toEqual(plugins); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return only plugins with matching slugs', () => { |
| 77 | + const plugins = [ |
| 78 | + { slug: 'plugin1' }, |
| 79 | + { slug: 'plugin2' }, |
| 80 | + { slug: 'plugin3' }, |
| 81 | + ]; |
| 82 | + const filtered = filterPluginsByOnlyPluginsOption( |
| 83 | + plugins as CoreConfig['plugins'], |
| 84 | + { |
| 85 | + onlyPlugins: ['plugin1', 'plugin3'], |
| 86 | + }, |
| 87 | + ); |
| 88 | + expect(filtered).toEqual([{ slug: 'plugin1' }, { slug: 'plugin3' }]); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +// without the `no-secrets` rule, this would be flagged as a security issue |
| 93 | +// eslint-disable-next-line no-secrets/no-secrets |
| 94 | +describe('filterCategoryByOnlyPluginsOption', () => { |
| 95 | + let logSpy: SpyInstance; |
| 96 | + beforeEach(() => { |
| 97 | + logSpy = vi.spyOn(console, 'log'); |
| 98 | + }); |
| 99 | + |
| 100 | + afterEach(() => { |
| 101 | + logSpy.mockRestore(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should return all categories if no onlyPlugins option', () => { |
| 105 | + const categories = [ |
| 106 | + { refs: [{ slug: 'plugin1' }, { slug: 'plugin2' }] }, |
| 107 | + { refs: [{ slug: 'plugin3' }] }, |
| 108 | + ]; |
| 109 | + const filtered = filterCategoryByOnlyPluginsOption( |
| 110 | + categories as CoreConfig['categories'], |
| 111 | + {}, |
| 112 | + ); |
| 113 | + expect(filtered).toEqual(categories); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should return only categories with matching slugs', () => { |
| 117 | + const categories = [ |
| 118 | + { refs: [{ slug: 'plugin1' }, { slug: 'plugin2' }] }, |
| 119 | + { refs: [{ slug: 'plugin3' }] }, |
| 120 | + ]; |
| 121 | + const filtered = filterCategoryByOnlyPluginsOption( |
| 122 | + categories as CoreConfig['categories'], |
| 123 | + { |
| 124 | + onlyPlugins: ['plugin1', 'plugin3'], |
| 125 | + }, |
| 126 | + ); |
| 127 | + expect(filtered).toEqual([{ refs: [{ slug: 'plugin3' }] }]); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should log if category is ignored', () => { |
| 131 | + const categories = [ |
| 132 | + { title: 'category1', refs: [{ slug: 'plugin1' }, { slug: 'plugin2' }] }, |
| 133 | + { title: 'category2', refs: [{ slug: 'plugin3' }] }, |
| 134 | + ]; |
| 135 | + filterCategoryByOnlyPluginsOption(categories as CoreConfig['categories'], { |
| 136 | + onlyPlugins: ['plugin1', 'plugin3'], |
| 137 | + }); |
| 138 | + expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('"category1"')); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe('validateOnlyPluginsOption', () => { |
| 143 | + let logSpy: SpyInstance; |
| 144 | + beforeEach(() => { |
| 145 | + logSpy = vi.spyOn(console, 'log'); |
| 146 | + }); |
| 147 | + |
| 148 | + afterEach(() => { |
| 149 | + logSpy.mockRestore(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should log if onlyPlugins option contains non-existing plugin', () => { |
| 153 | + const plugins = [{ slug: 'plugin1' }, { slug: 'plugin2' }]; |
| 154 | + validateOnlyPluginsOption(plugins as CoreConfig['plugins'], { |
| 155 | + onlyPlugins: ['plugin1', 'plugin3'], |
| 156 | + }); |
| 157 | + expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('"plugin3"')); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should not log if onlyPlugins option contains existing plugin', () => { |
| 161 | + const plugins = [{ slug: 'plugin1' }, { slug: 'plugin2' }]; |
| 162 | + validateOnlyPluginsOption(plugins as CoreConfig['plugins'], { |
| 163 | + onlyPlugins: ['plugin1'], |
| 164 | + }); |
| 165 | + expect(logSpy).not.toHaveBeenCalled(); |
| 166 | + }); |
| 167 | +}); |
0 commit comments