|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { getBomUrl, fetchBomXml, extractVersions, nameToEnvVar } = require('../src/index'); |
| 4 | + |
| 5 | +const OSS_REPO = 'spring-cloud/spring-cloud-release'; |
| 6 | +const COMMERCIAL_REPO = 'spring-cloud/spring-cloud-release-commercial'; |
| 7 | +const bomUrl = (repo, ref) => |
| 8 | + `https://raw.githubusercontent.com/${repo}/${ref}/pom.xml`; |
| 9 | + |
| 10 | +const fixturePath = (name) => path.join(__dirname, 'fixtures', name); |
| 11 | +const loadFixture = (name) => fs.readFileSync(fixturePath(name), 'utf-8'); |
| 12 | + |
| 13 | +// ─── getBomUrl ─────────────────────────────────────────────────────────────── |
| 14 | + |
| 15 | +describe('getBomUrl', () => { |
| 16 | + it('returns the OSS BOM URL for the given ref', () => { |
| 17 | + expect(getBomUrl(false, '2023.0.x')).toBe(bomUrl(OSS_REPO, '2023.0.x')); |
| 18 | + }); |
| 19 | + |
| 20 | + it('returns the commercial BOM URL for the given ref', () => { |
| 21 | + expect(getBomUrl(true, '2023.0.x')).toBe(bomUrl(COMMERCIAL_REPO, '2023.0.x')); |
| 22 | + }); |
| 23 | + |
| 24 | + it('defaults to main when ref is main', () => { |
| 25 | + expect(getBomUrl(false, 'main')).toBe(bomUrl(OSS_REPO, 'main')); |
| 26 | + }); |
| 27 | + |
| 28 | + it('accepts a tag as the ref', () => { |
| 29 | + expect(getBomUrl(false, 'v2023.0.1')).toBe(bomUrl(OSS_REPO, 'v2023.0.1')); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +// ─── fetchBomXml ───────────────────────────────────────────────────────────── |
| 34 | + |
| 35 | +describe('fetchBomXml', () => { |
| 36 | + afterEach(() => jest.restoreAllMocks()); |
| 37 | + |
| 38 | + it('fetches XML without an Authorization header for public repos', async () => { |
| 39 | + const url = bomUrl(OSS_REPO, '2023.0.x'); |
| 40 | + const mockXml = loadFixture('minimal-bom.xml'); |
| 41 | + jest.spyOn(global, 'fetch').mockResolvedValue({ |
| 42 | + ok: true, |
| 43 | + text: async () => mockXml, |
| 44 | + }); |
| 45 | + |
| 46 | + const xml = await fetchBomXml(url, ''); |
| 47 | + |
| 48 | + expect(global.fetch).toHaveBeenCalledWith(url, { |
| 49 | + headers: { Accept: 'text/plain' }, |
| 50 | + }); |
| 51 | + expect(xml).toBe(mockXml); |
| 52 | + }); |
| 53 | + |
| 54 | + it('includes an Authorization header when a token is provided', async () => { |
| 55 | + const url = bomUrl(COMMERCIAL_REPO, '2023.0.x'); |
| 56 | + const mockXml = loadFixture('minimal-bom.xml'); |
| 57 | + jest.spyOn(global, 'fetch').mockResolvedValue({ |
| 58 | + ok: true, |
| 59 | + text: async () => mockXml, |
| 60 | + }); |
| 61 | + |
| 62 | + await fetchBomXml(url, 'my-secret-token'); |
| 63 | + |
| 64 | + expect(global.fetch).toHaveBeenCalledWith(url, { |
| 65 | + headers: { |
| 66 | + Accept: 'text/plain', |
| 67 | + Authorization: 'Bearer my-secret-token', |
| 68 | + }, |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('throws a descriptive error on a non-OK response', async () => { |
| 73 | + jest.spyOn(global, 'fetch').mockResolvedValue({ |
| 74 | + ok: false, |
| 75 | + status: 404, |
| 76 | + statusText: 'Not Found', |
| 77 | + }); |
| 78 | + |
| 79 | + await expect(fetchBomXml(bomUrl(OSS_REPO, 'main'), '')).rejects.toThrow( |
| 80 | + 'Failed to fetch BOM (HTTP 404: Not Found)' |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('appends an access hint on 401 Unauthorized', async () => { |
| 85 | + jest.spyOn(global, 'fetch').mockResolvedValue({ |
| 86 | + ok: false, |
| 87 | + status: 401, |
| 88 | + statusText: 'Unauthorized', |
| 89 | + }); |
| 90 | + |
| 91 | + await expect(fetchBomXml(bomUrl(COMMERCIAL_REPO, '2023.0.x'), 'bad-token')).rejects.toThrow( |
| 92 | + 'ensure the token has read access to the repository' |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('appends an access hint on 403 Forbidden', async () => { |
| 97 | + jest.spyOn(global, 'fetch').mockResolvedValue({ |
| 98 | + ok: false, |
| 99 | + status: 403, |
| 100 | + statusText: 'Forbidden', |
| 101 | + }); |
| 102 | + |
| 103 | + await expect(fetchBomXml(bomUrl(COMMERCIAL_REPO, '2023.0.x'), 'bad-token')).rejects.toThrow( |
| 104 | + 'ensure the token has read access to the repository' |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +// ─── extractVersions ──────────────────────────────────────────────────────── |
| 110 | + |
| 111 | +describe('extractVersions', () => { |
| 112 | + describe('with a full Spring Cloud BOM', () => { |
| 113 | + let versions; |
| 114 | + |
| 115 | + beforeAll(() => { |
| 116 | + versions = extractVersions(loadFixture('sample-bom.xml')); |
| 117 | + }); |
| 118 | + |
| 119 | + it('extracts the release train version', () => { |
| 120 | + expect(versions['release-train']).toBe('2023.0.1'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('extracts spring-boot.version', () => { |
| 124 | + expect(versions['spring-boot']).toBe('3.2.3'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('extracts spring-cloud-config.version', () => { |
| 128 | + expect(versions['spring-cloud-config']).toBe('4.1.1'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('extracts spring-cloud-gateway.version', () => { |
| 132 | + expect(versions['spring-cloud-gateway']).toBe('4.1.2'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('extracts spring-cloud-kubernetes.version', () => { |
| 136 | + expect(versions['spring-cloud-kubernetes']).toBe('3.1.1'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('does NOT include properties that do not end in .version', () => { |
| 140 | + // java.compiler.release and project.build.sourceEncoding should be ignored |
| 141 | + expect(versions['java.compiler.release']).toBeUndefined(); |
| 142 | + expect(versions['project.build.sourceEncoding']).toBeUndefined(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('includes all expected version entries', () => { |
| 146 | + const expectedKeys = [ |
| 147 | + 'release-train', |
| 148 | + 'spring-boot', |
| 149 | + 'spring-cloud-build', |
| 150 | + 'spring-cloud-config', |
| 151 | + 'spring-cloud-bus', |
| 152 | + 'spring-cloud-commons', |
| 153 | + 'spring-cloud-circuitbreaker', |
| 154 | + 'spring-cloud-gateway', |
| 155 | + 'spring-cloud-kubernetes', |
| 156 | + 'spring-cloud-openfeign', |
| 157 | + 'spring-cloud-sleuth', |
| 158 | + 'spring-cloud-stream', |
| 159 | + 'spring-cloud-vault', |
| 160 | + 'spring-cloud-zookeeper', |
| 161 | + ]; |
| 162 | + expect(Object.keys(versions).sort()).toEqual(expectedKeys.sort()); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('with a minimal BOM (single property)', () => { |
| 167 | + it('extracts the only version property', () => { |
| 168 | + const versions = extractVersions(loadFixture('minimal-bom.xml')); |
| 169 | + expect(versions['spring-boot']).toBe('3.2.0'); |
| 170 | + expect(versions['release-train']).toBe('2023.0.0'); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('with a BOM that has no <properties>', () => { |
| 175 | + it('returns only the release-train version', () => { |
| 176 | + const versions = extractVersions(loadFixture('no-properties-bom.xml')); |
| 177 | + expect(Object.keys(versions)).toEqual(['release-train']); |
| 178 | + expect(versions['release-train']).toBe('2023.0.0'); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe('with invalid XML', () => { |
| 183 | + it('throws an error when there is no <project> root', () => { |
| 184 | + expect(() => extractVersions('<notaproject/>')).toThrow( |
| 185 | + 'Invalid POM file: no <project> root element found' |
| 186 | + ); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
| 190 | + |
| 191 | +// ─── nameToEnvVar ──────────────────────────────────────────────────────────── |
| 192 | + |
| 193 | +describe('nameToEnvVar', () => { |
| 194 | + it('converts release-train to RELEASE_TRAIN_VERSION', () => { |
| 195 | + expect(nameToEnvVar('release-train')).toBe('RELEASE_TRAIN_VERSION'); |
| 196 | + }); |
| 197 | + |
| 198 | + it('converts spring-boot to SPRING_BOOT_VERSION', () => { |
| 199 | + expect(nameToEnvVar('spring-boot')).toBe('SPRING_BOOT_VERSION'); |
| 200 | + }); |
| 201 | + |
| 202 | + it('converts spring-cloud-config to SPRING_CLOUD_CONFIG_VERSION', () => { |
| 203 | + expect(nameToEnvVar('spring-cloud-config')).toBe('SPRING_CLOUD_CONFIG_VERSION'); |
| 204 | + }); |
| 205 | + |
| 206 | + it('converts spring-cloud-kubernetes to SPRING_CLOUD_KUBERNETES_VERSION', () => { |
| 207 | + expect(nameToEnvVar('spring-cloud-kubernetes')).toBe('SPRING_CLOUD_KUBERNETES_VERSION'); |
| 208 | + }); |
| 209 | +}); |
0 commit comments