|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +jest.mock('@actions/core'); |
| 4 | +jest.mock('@actions/exec'); |
| 5 | + |
| 6 | +const core = require('@actions/core'); |
| 7 | +const { dumpsPretty, resolveJdkVersions, addBranch, addBranches } = require('../src/index'); |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + jest.clearAllMocks(); |
| 11 | + core.info.mockImplementation(() => {}); |
| 12 | + core.warning.mockImplementation(() => {}); |
| 13 | + core.error.mockImplementation(() => {}); |
| 14 | + core.setFailed.mockImplementation(() => {}); |
| 15 | + core.setSecret.mockImplementation(() => {}); |
| 16 | +}); |
| 17 | + |
| 18 | +function makeData() { |
| 19 | + return { |
| 20 | + defaults: { |
| 21 | + oss: { |
| 22 | + branches: { default: ['main'], scheduled: ['main'] }, |
| 23 | + jdkVersions: { main: ['17', '21', '25'], default: ['17', '21', '25'] }, |
| 24 | + }, |
| 25 | + commercial: { |
| 26 | + branches: { default: ['4.3.x'], scheduled: ['4.3.x'] }, |
| 27 | + jdkVersions: { '4.3.x': ['17', '21', '25'], default: ['17', '21'] }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + 'spring-cloud-config': { |
| 31 | + oss: { |
| 32 | + branches: { default: ['main'], scheduled: ['main'] }, |
| 33 | + jdkVersions: { main: ['17', '21', '25'] }, |
| 34 | + }, |
| 35 | + commercial: { |
| 36 | + branches: { default: ['3.3.x'], scheduled: ['3.3.x'] }, |
| 37 | + jdkVersions: { '3.3.x': ['17', '21'] }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +// ── dumpsPretty ───────────────────────────────────────────────────────────── |
| 44 | + |
| 45 | +describe('dumpsPretty', () => { |
| 46 | + test('keeps string arrays on a single line', () => { |
| 47 | + expect(dumpsPretty({ scheduled: ['5.0.x', 'main'] })).toContain('["5.0.x", "main"]'); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +// ── addBranch ─────────────────────────────────────────────────────────────── |
| 52 | + |
| 53 | +describe('addBranch', () => { |
| 54 | + test('adds the branch to oss scheduled and copies the source branch JDKs', () => { |
| 55 | + const data = makeData(); |
| 56 | + const changes = addBranch(data, 'spring-cloud/spring-cloud-config', '5.0.x', 'main'); |
| 57 | + |
| 58 | + expect(changes).toHaveLength(2); |
| 59 | + expect(data['spring-cloud-config'].oss.branches.scheduled).toEqual(['5.0.x', 'main']); |
| 60 | + expect(data['spring-cloud-config'].oss.jdkVersions['5.0.x']).toEqual(['17', '21', '25']); |
| 61 | + }); |
| 62 | + |
| 63 | + test('leaves branches.default alone — main stays the default line', () => { |
| 64 | + const data = makeData(); |
| 65 | + addBranch(data, 'spring-cloud/spring-cloud-config', '5.0.x', 'main'); |
| 66 | + expect(data['spring-cloud-config'].oss.branches.default).toEqual(['main']); |
| 67 | + }); |
| 68 | + |
| 69 | + test('copies the JDK list rather than aliasing the source branch entry', () => { |
| 70 | + const data = makeData(); |
| 71 | + addBranch(data, 'spring-cloud/spring-cloud-config', '5.0.x', 'main'); |
| 72 | + data['spring-cloud-config'].oss.jdkVersions['5.0.x'].push('26'); |
| 73 | + expect(data['spring-cloud-config'].oss.jdkVersions.main).toEqual(['17', '21', '25']); |
| 74 | + }); |
| 75 | + |
| 76 | + test('modifies the commercial section for a -commercial repo', () => { |
| 77 | + const data = makeData(); |
| 78 | + addBranch(data, 'spring-cloud/spring-cloud-config-commercial', '5.0.x', '3.3.x'); |
| 79 | + expect(data['spring-cloud-config'].commercial.branches.scheduled).toEqual(['5.0.x', '3.3.x']); |
| 80 | + expect(data['spring-cloud-config'].commercial.jdkVersions['5.0.x']).toEqual(['17', '21']); |
| 81 | + expect(data['spring-cloud-config'].oss.branches.scheduled).toEqual(['main']); |
| 82 | + }); |
| 83 | + |
| 84 | + test('is idempotent — a second call reports no changes', () => { |
| 85 | + const data = makeData(); |
| 86 | + addBranch(data, 'spring-cloud/spring-cloud-config', '5.0.x', 'main'); |
| 87 | + const changes = addBranch(data, 'spring-cloud/spring-cloud-config', '5.0.x', 'main'); |
| 88 | + expect(changes).toEqual([]); |
| 89 | + expect(data['spring-cloud-config'].oss.branches.scheduled).toEqual(['5.0.x', 'main']); |
| 90 | + }); |
| 91 | + |
| 92 | + test('seeds a missing project entry from defaults without mutating defaults', () => { |
| 93 | + const data = makeData(); |
| 94 | + const changes = addBranch(data, 'spring-cloud/spring-cloud-new-thing', '5.0.x', 'main'); |
| 95 | + |
| 96 | + expect(changes[0]).toContain('seeded from defaults.oss'); |
| 97 | + expect(data['spring-cloud-new-thing'].oss.branches.scheduled).toEqual(['5.0.x', 'main']); |
| 98 | + expect(data['spring-cloud-new-thing'].oss.jdkVersions['5.0.x']).toEqual(['17', '21', '25']); |
| 99 | + // The shared defaults entry must come back untouched. |
| 100 | + expect(data.defaults.oss.branches.scheduled).toEqual(['main']); |
| 101 | + expect(data.defaults.oss.jdkVersions['5.0.x']).toBeUndefined(); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +// ── resolveJdkVersions ────────────────────────────────────────────────────── |
| 106 | + |
| 107 | +describe('resolveJdkVersions', () => { |
| 108 | + test('falls back to the section default when the source branch has no entry', () => { |
| 109 | + const data = makeData(); |
| 110 | + const sec = { jdkVersions: { default: ['21'] } }; |
| 111 | + expect(resolveJdkVersions(data, 'oss', sec, 'main')).toEqual(['21']); |
| 112 | + }); |
| 113 | + |
| 114 | + test('falls back to the global defaults entry when the section has nothing', () => { |
| 115 | + const data = makeData(); |
| 116 | + expect(resolveJdkVersions(data, 'oss', { jdkVersions: {} }, 'main')).toEqual(['17', '21', '25']); |
| 117 | + }); |
| 118 | + |
| 119 | + test('falls back to 17/21 when projects.json has no defaults at all', () => { |
| 120 | + expect(resolveJdkVersions({}, 'oss', {}, 'main')).toEqual(['17', '21']); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +// ── addBranches ───────────────────────────────────────────────────────────── |
| 125 | + |
| 126 | +describe('addBranches', () => { |
| 127 | + test('applies every addition and returns one description per change', () => { |
| 128 | + const data = makeData(); |
| 129 | + const changes = addBranches(data, [ |
| 130 | + { repo: 'spring-cloud/spring-cloud-config', branch: '5.0.x', sourceBranch: 'main' }, |
| 131 | + { repo: 'spring-cloud/spring-cloud-commons', branch: '5.0.x' }, |
| 132 | + ]); |
| 133 | + |
| 134 | + expect(changes.filter((c) => c.startsWith('spring-cloud/spring-cloud-config:'))).toHaveLength(2); |
| 135 | + expect(data['spring-cloud-config'].oss.branches.scheduled).toContain('5.0.x'); |
| 136 | + expect(data['spring-cloud-commons'].oss.branches.scheduled).toContain('5.0.x'); |
| 137 | + }); |
| 138 | + |
| 139 | + test('defaults sourceBranch to main', () => { |
| 140 | + const data = makeData(); |
| 141 | + addBranches(data, [{ repo: 'spring-cloud/spring-cloud-config', branch: '5.0.x' }]); |
| 142 | + expect(data['spring-cloud-config'].oss.jdkVersions['5.0.x']).toEqual(['17', '21', '25']); |
| 143 | + }); |
| 144 | + |
| 145 | + test('throws when an addition is missing a branch', () => { |
| 146 | + expect(() => addBranches(makeData(), [{ repo: 'spring-cloud/spring-cloud-config' }])) |
| 147 | + .toThrow(/needs a 'repo' and a 'branch'/); |
| 148 | + }); |
| 149 | +}); |
0 commit comments