|
| 1 | +import { applyLanguageConfigFallbacks } from '../scripts/generate-config.js'; |
| 2 | + |
| 3 | +describe('generate-config', () => { |
| 4 | + describe('applyLanguageConfigFallbacks', () => { |
| 5 | + test('returns inputs as-is when no language specified', () => { |
| 6 | + const inputs = { |
| 7 | + repo: 'owner/repo', |
| 8 | + language: '', |
| 9 | + buildMode: 'none', |
| 10 | + buildCommand: '', |
| 11 | + version: '2.0.0', |
| 12 | + distribution: 'ubuntu-latest', |
| 13 | + }; |
| 14 | + const config = { |
| 15 | + languages_config: [], |
| 16 | + }; |
| 17 | + |
| 18 | + const result = applyLanguageConfigFallbacks(inputs, config); |
| 19 | + |
| 20 | + expect(result).toEqual(inputs); |
| 21 | + }); |
| 22 | + |
| 23 | + test('returns inputs as-is when language config not found', () => { |
| 24 | + const inputs = { |
| 25 | + repo: 'owner/repo', |
| 26 | + language: 'java-kotlin', |
| 27 | + buildMode: 'manual', |
| 28 | + buildCommand: 'mvn compile', |
| 29 | + version: '2.0.0', |
| 30 | + distribution: 'ubuntu-latest', |
| 31 | + }; |
| 32 | + const config = { |
| 33 | + languages_config: [ |
| 34 | + { |
| 35 | + language: 'javascript', |
| 36 | + build_mode: 'none', |
| 37 | + }, |
| 38 | + ], |
| 39 | + }; |
| 40 | + |
| 41 | + const result = applyLanguageConfigFallbacks(inputs, config); |
| 42 | + |
| 43 | + expect(result).toEqual(inputs); |
| 44 | + }); |
| 45 | + |
| 46 | + test('preserves input values even when config has fallbacks', () => { |
| 47 | + const inputs = { |
| 48 | + repo: 'owner/repo', |
| 49 | + language: 'java-kotlin', |
| 50 | + buildMode: 'manual', |
| 51 | + buildCommand: 'mvn compile', |
| 52 | + version: '2.0.0', |
| 53 | + distribution: 'ubuntu-latest', |
| 54 | + }; |
| 55 | + const config = { |
| 56 | + languages_config: [ |
| 57 | + { |
| 58 | + language: 'java-kotlin', |
| 59 | + build_mode: 'autobuild', |
| 60 | + build_command: 'gradle build', |
| 61 | + version: '1.0.0', |
| 62 | + distribution: 'debian-latest', |
| 63 | + }, |
| 64 | + ], |
| 65 | + }; |
| 66 | + |
| 67 | + const result = applyLanguageConfigFallbacks(inputs, config); |
| 68 | + |
| 69 | + // Inputs should take precedence |
| 70 | + expect(result.buildMode).toBe('manual'); |
| 71 | + expect(result.buildCommand).toBe('mvn compile'); |
| 72 | + expect(result.version).toBe('2.0.0'); |
| 73 | + expect(result.distribution).toBe('ubuntu-latest'); |
| 74 | + }); |
| 75 | + |
| 76 | + test('applies config fallbacks for missing input values', () => { |
| 77 | + const inputs = { |
| 78 | + repo: 'owner/repo', |
| 79 | + language: 'java-kotlin', |
| 80 | + buildMode: undefined, |
| 81 | + buildCommand: undefined, |
| 82 | + version: undefined, |
| 83 | + distribution: undefined, |
| 84 | + }; |
| 85 | + const config = { |
| 86 | + languages_config: [ |
| 87 | + { |
| 88 | + language: 'java-kotlin', |
| 89 | + build_mode: 'autobuild', |
| 90 | + build_command: 'gradle build', |
| 91 | + version: '2.5.0', |
| 92 | + distribution: 'ubuntu-latest', |
| 93 | + }, |
| 94 | + ], |
| 95 | + }; |
| 96 | + |
| 97 | + const result = applyLanguageConfigFallbacks(inputs, config); |
| 98 | + |
| 99 | + // Config fallbacks should be applied |
| 100 | + expect(result.buildMode).toBe('autobuild'); |
| 101 | + expect(result.buildCommand).toBe('gradle build'); |
| 102 | + expect(result.version).toBe('2.5.0'); |
| 103 | + expect(result.distribution).toBe('ubuntu-latest'); |
| 104 | + }); |
| 105 | + |
| 106 | + test('applies partial config fallbacks', () => { |
| 107 | + const inputs = { |
| 108 | + repo: 'owner/repo', |
| 109 | + language: 'swift', |
| 110 | + buildMode: 'manual', |
| 111 | + buildCommand: undefined, |
| 112 | + version: '2.0.0', |
| 113 | + distribution: undefined, |
| 114 | + }; |
| 115 | + const config = { |
| 116 | + languages_config: [ |
| 117 | + { |
| 118 | + language: 'swift', |
| 119 | + build_mode: 'autobuild', |
| 120 | + build_command: 'swift build', |
| 121 | + version: '1.0.0', |
| 122 | + distribution: 'macos-latest', |
| 123 | + }, |
| 124 | + ], |
| 125 | + }; |
| 126 | + |
| 127 | + const result = applyLanguageConfigFallbacks(inputs, config); |
| 128 | + |
| 129 | + // Input values preserved |
| 130 | + expect(result.buildMode).toBe('manual'); |
| 131 | + expect(result.version).toBe('2.0.0'); |
| 132 | + // Config fallbacks applied |
| 133 | + expect(result.buildCommand).toBe('swift build'); |
| 134 | + expect(result.distribution).toBe('macos-latest'); |
| 135 | + }); |
| 136 | + |
| 137 | + test('handles empty config.languages_config array', () => { |
| 138 | + const inputs = { |
| 139 | + repo: 'owner/repo', |
| 140 | + language: 'java-kotlin', |
| 141 | + buildMode: 'manual', |
| 142 | + buildCommand: 'mvn compile', |
| 143 | + version: '2.0.0', |
| 144 | + distribution: 'ubuntu-latest', |
| 145 | + }; |
| 146 | + const config = { |
| 147 | + languages_config: [], |
| 148 | + }; |
| 149 | + |
| 150 | + const result = applyLanguageConfigFallbacks(inputs, config); |
| 151 | + |
| 152 | + // Should return inputs unchanged |
| 153 | + expect(result).toEqual(inputs); |
| 154 | + }); |
| 155 | + |
| 156 | + test('handles missing config.languages_config property', () => { |
| 157 | + const inputs = { |
| 158 | + repo: 'owner/repo', |
| 159 | + language: 'python', |
| 160 | + buildMode: 'none', |
| 161 | + }; |
| 162 | + const config = {}; |
| 163 | + |
| 164 | + const result = applyLanguageConfigFallbacks(inputs, config); |
| 165 | + |
| 166 | + // Should return inputs unchanged |
| 167 | + expect(result).toEqual(inputs); |
| 168 | + }); |
| 169 | + |
| 170 | + test('treats empty string as a value (not falsy for fallback)', () => { |
| 171 | + const inputs = { |
| 172 | + repo: 'owner/repo', |
| 173 | + language: 'javascript', |
| 174 | + buildMode: '', |
| 175 | + buildCommand: '', |
| 176 | + version: '', |
| 177 | + distribution: '', |
| 178 | + }; |
| 179 | + const config = { |
| 180 | + languages_config: [ |
| 181 | + { |
| 182 | + language: 'javascript', |
| 183 | + build_mode: 'none', |
| 184 | + build_command: 'npm build', |
| 185 | + version: '1.0.0', |
| 186 | + distribution: 'ubuntu-latest', |
| 187 | + }, |
| 188 | + ], |
| 189 | + }; |
| 190 | + |
| 191 | + const result = applyLanguageConfigFallbacks(inputs, config); |
| 192 | + |
| 193 | + // Empty strings are falsy, so config fallbacks WILL be applied |
| 194 | + // This is current behavior - if we want to change it, we need to update the logic |
| 195 | + expect(result.buildMode).toBe('none'); |
| 196 | + expect(result.buildCommand).toBe('npm build'); |
| 197 | + expect(result.version).toBe('1.0.0'); |
| 198 | + expect(result.distribution).toBe('ubuntu-latest'); |
| 199 | + }); |
| 200 | + |
| 201 | + test('does not modify other input properties', () => { |
| 202 | + const inputs = { |
| 203 | + repo: 'owner/repo', |
| 204 | + language: 'go', |
| 205 | + buildMode: undefined, |
| 206 | + buildCommand: undefined, |
| 207 | + version: undefined, |
| 208 | + distribution: undefined, |
| 209 | + pathsIgnored: ['test', 'vendor'], |
| 210 | + rulesExcluded: ['go/rule1'], |
| 211 | + }; |
| 212 | + const config = { |
| 213 | + languages_config: [ |
| 214 | + { |
| 215 | + language: 'go', |
| 216 | + build_mode: 'autobuild', |
| 217 | + }, |
| 218 | + ], |
| 219 | + }; |
| 220 | + |
| 221 | + const result = applyLanguageConfigFallbacks(inputs, config); |
| 222 | + |
| 223 | + // Other properties should remain unchanged |
| 224 | + expect(result.repo).toBe('owner/repo'); |
| 225 | + expect(result.language).toBe('go'); |
| 226 | + expect(result.pathsIgnored).toEqual(['test', 'vendor']); |
| 227 | + expect(result.rulesExcluded).toEqual(['go/rule1']); |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
0 commit comments