From 08171884cc8f7c03f97db15c015444ebcc2f4222 Mon Sep 17 00:00:00 2001 From: Qing Tomlinson Date: Tue, 23 Jul 2024 16:34:03 -0700 Subject: [PATCH] Fix fetching latest version for some pod components Versions retrieved from the pod registry are sorted lexically. An example can be found at https://trunk.cocoapods.org/api/v1/pods/xcbeautify. Lexically, "0.17.0" < "0.9.1", so the latest version is not always the last item in the versions list. Change to sort by timestamp to retrieve the latest version. --- providers/fetch/podFetch.js | 6 +- test/fixtures/pod/versions2.json | 124 +++++++++++++++++++++ test/unit/providers/fetch/podFetchTests.js | 11 +- 3 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/pod/versions2.json diff --git a/providers/fetch/podFetch.js b/providers/fetch/podFetch.js index dd6dcc91..4dcac567 100644 --- a/providers/fetch/podFetch.js +++ b/providers/fetch/podFetch.js @@ -140,7 +140,11 @@ class PodFetch extends AbstractFetch { if (spec.revision) { return versions.find(version => version.name === spec.revision) } else { - return versions[versions.length - 1] // the versions are already sorted + return versions.sort((a, b) => { + const aDate = typeof a.created_at === 'string' ? a.created_at : '' + const bDate = typeof b.created_at === 'string' ? b.created_at : '' + return bDate.localeCompare(aDate) + })[0] } } else { return null diff --git a/test/fixtures/pod/versions2.json b/test/fixtures/pod/versions2.json new file mode 100644 index 00000000..5cdb4378 --- /dev/null +++ b/test/fixtures/pod/versions2.json @@ -0,0 +1,124 @@ +{ + "versions": [ + { + "name": "0.10.2", + "created_at": "2021-12-03 22:39:52 UTC" + }, + { + "name": "0.11.0", + "created_at": "2021-12-22 06:52:38 UTC" + }, + { + "name": "0.12.0", + "created_at": "2022-02-28 09:47:12 UTC" + }, + { + "name": "0.13.0", + "created_at": "2022-05-27 10:49:32 UTC" + }, + { + "name": "0.14.0", + "created_at": "2022-09-18 12:25:31 UTC" + }, + { + "name": "0.15.0", + "created_at": "2022-10-08 12:30:37 UTC" + }, + { + "name": "0.16.0", + "created_at": "2023-02-19 01:26:47 UTC" + }, + { + "name": "0.17.0", + "created_at": "2023-02-19 01:45:57 UTC" + }, + { + "name": "0.3.5", + "created_at": "2018-12-09 23:53:19 UTC" + }, + { + "name": "0.3.6", + "created_at": "2018-12-15 05:56:51 UTC" + }, + { + "name": "0.3.7", + "created_at": "2019-02-04 14:17:14 UTC" + }, + { + "name": "0.3.8", + "created_at": "2019-04-07 00:47:43 UTC" + }, + { + "name": "0.4.0", + "created_at": "2019-04-17 04:12:45 UTC" + }, + { + "name": "0.4.1", + "created_at": "2019-04-17 08:25:41 UTC" + }, + { + "name": "0.5.0", + "created_at": "2019-07-27 05:09:12 UTC" + }, + { + "name": "0.6.0", + "created_at": "2019-09-16 22:38:50 UTC" + }, + { + "name": "0.7.0", + "created_at": "2019-12-09 00:04:16 UTC" + }, + { + "name": "0.7.1", + "created_at": "2019-12-21 05:26:01 UTC" + }, + { + "name": "0.7.2", + "created_at": "2019-12-23 14:09:48 UTC" + }, + { + "name": "0.7.3", + "created_at": "2020-01-19 09:15:23 UTC" + }, + { + "name": "0.7.4", + "created_at": "2020-02-21 11:47:13 UTC" + }, + { + "name": "0.7.5", + "created_at": "2020-03-09 01:16:13 UTC" + }, + { + "name": "0.7.6", + "created_at": "2020-05-14 09:18:29 UTC" + }, + { + "name": "0.8.0", + "created_at": "2020-05-26 02:32:55 UTC" + }, + { + "name": "0.8.1", + "created_at": "2020-08-23 23:35:54 UTC" + }, + { + "name": "0.9.0", + "created_at": "2020-12-19 10:14:22 UTC" + }, + { + "name": "0.9.1", + "created_at": "2021-01-01 05:52:47 UTC" + } + ], + "owners": [ + { + "created_at": "2014-11-08 08:56:14 UTC", + "email": "t@thi.im", + "name": "Thi Doan" + }, + { + "created_at": "2022-01-08 12:04:30 UTC", + "email": "pedro@craftweg.com", + "name": "Pedro Piñera¨" + } + ] +} \ No newline at end of file diff --git a/test/unit/providers/fetch/podFetchTests.js b/test/unit/providers/fetch/podFetchTests.js index 5a8a9a2a..74e0fd16 100644 --- a/test/unit/providers/fetch/podFetchTests.js +++ b/test/unit/providers/fetch/podFetchTests.js @@ -10,11 +10,12 @@ describe('podFetch', () => { return JSON.parse(fs.readFileSync(`test/fixtures/pod/${fileName}`)) } + const getVersionsStub = sinon.stub() const PodFetch = proxyquire('../../../../providers/fetch/podFetch', { requestretry: { defaults: () => { return { - get: sinon.stub().resolves({ body: loadJson('versions.json'), statusCode: 200 }) + get: getVersionsStub } } }, @@ -29,6 +30,7 @@ describe('podFetch', () => { }) it('spec with version', async () => { + getVersionsStub.resolves({ body: loadJson('versions.json'), statusCode: 200 }) const result = await fetch.handle(new Request('test', 'cd:/pod/cocoapods/-/SwiftLCS/1.0')) result.fetchResult.copyTo(result) expect(result.url).to.be.equal('cd:/pod/cocoapods/-/SwiftLCS/1.0') @@ -39,10 +41,17 @@ describe('podFetch', () => { }) it('spec without version', async () => { + getVersionsStub.resolves({ body: loadJson('versions.json'), statusCode: 200 }) const result = await fetch.handle(new Request('test', 'cd:/pod/cocoapods/-/SwiftLCS')) result.fetchResult.copyTo(result) expect(result.url).to.be.equal('cd:/pod/cocoapods/-/SwiftLCS/1.3.4') expect(result.document.releaseDate).to.be.equal('2019-04-10 00:22:10 UTC') expect(result.casedSpec.toUrl()).to.be.equal('cd:/pod/cocoapods/-/SwiftLCS/1.3.4') }) + + it('spec without version when version is lexically sorted', async () => { + getVersionsStub.resolves({ body: loadJson('versions2.json'), statusCode: 200 }) + const version = await fetch._getVersion(new Request('test', 'cd:/pod/cocoapods/-/xcbeautify')) + expect(version.name).to.be.equal('0.17.0') + }) })