From b5bcaec3ee3027eecba6f996ca4ef3c9d8d4ad13 Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 24 Oct 2022 15:40:36 +0200 Subject: [PATCH 1/6] pub updater can be instantiated with custom dart api base url --- lib/src/pub_updater.dart | 23 +++++++++++++++++---- test/pub_update_test.dart | 42 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/lib/src/pub_updater.dart b/lib/src/pub_updater.dart index a30a166..a62d5c9 100644 --- a/lib/src/pub_updater.dart +++ b/lib/src/pub_updater.dart @@ -11,15 +11,24 @@ class PackageInfoRequestFailure implements Exception {} /// Exception thrown when the provided package information is not found. class PackageInfoNotFoundFailure implements Exception {} +/// The pub.dev base url for querying package versions +const _defaultBaseUrl = 'https://pub.dev/api/packages/'; + /// {@template pub_update} /// A Dart package which enables checking whether a package is up to date. /// {@endtemplate} class PubUpdater { /// {@macro pub_update} - PubUpdater([http.Client? client]) : _client = client; - - /// The pub.dev base url for querying package versions - static const _baseUrl = 'https://pub.dev/api/packages/'; + PubUpdater([http.Client? client, String customBaseUrl = _defaultBaseUrl]) + : assert( + _isValidBaseURL(customBaseUrl), + '$customBaseUrl is not a valid pub api URL!', + ), + _client = client, + _baseUrl = customBaseUrl; + + /// The base url used for querying package versions + final String _baseUrl; final http.Client? _client; Future _get(Uri uri) => _client?.get(uri) ?? http.get(uri); @@ -67,4 +76,10 @@ class PubUpdater { return PackageInfo.fromJson(packageJson); } + + /// Checks whether the passed [baseUrl] is a valid pub api url + static bool _isValidBaseURL(String baseUrl) { + return (Uri.tryParse(baseUrl)?.isAbsolute ?? false) && + baseUrl.endsWith('/api/packages/'); + } } diff --git a/test/pub_update_test.dart b/test/pub_update_test.dart index 82e746e..a220154 100644 --- a/test/pub_update_test.dart +++ b/test/pub_update_test.dart @@ -21,11 +21,16 @@ const emptyResponseBody = '{}'; const command = ['dart', 'pub', 'global', 'activate', 'very_good_cli']; +const customDomain = 'custom-domain.com'; + +const customBaseUrl = 'https://$customDomain/api/packages/'; + void main() { group('PubUpdater', () { late Client client; late Response response; late PubUpdater pubUpdater; + late PubUpdater pubUpdaterWithCustomBaseURL; late ProcessManager processManager; setUpAll(() { registerFallbackValue(Uri()); @@ -35,6 +40,10 @@ void main() { client = MockClient(); response = MockResponse(); pubUpdater = PubUpdater(client); + pubUpdaterWithCustomBaseURL = PubUpdater( + client, + customBaseUrl, + ); processManager = MockProcessManager(); when(() => client.get(any())).thenAnswer((_) async => response); @@ -49,8 +58,19 @@ void main() { expect(PubUpdater(), isNotNull); }); + test('cannot be instantiated with incorrect custom base URL', () { + expect( + () => PubUpdater(null, 'this-is-wrong.com'), + throwsA(TypeMatcher()), + ); + }); + + test('can be instantiated with correct custom base URL', () { + expect(PubUpdater(null, customBaseUrl), isNotNull); + }); + group('isUpToDate', () { - test('makes correct http request', () async { + test('makes correct http request (default)', () async { when(() => response.body).thenReturn(emptyResponseBody); try { @@ -70,6 +90,26 @@ void main() { ).called(1); }); + test('makes correct http request (custom domain)', () async { + when(() => response.body).thenReturn(emptyResponseBody); + + try { + await pubUpdaterWithCustomBaseURL.isUpToDate( + packageName: 'very_good_cli', + currentVersion: '0.3.3', + ); + } catch (_) {} + + verify( + () => client.get( + Uri.https( + customDomain, + '/api/packages/very_good_cli', + ), + ), + ).called(1); + }); + test('returns false when currentVersion < latestVersion', () async { expect( await pubUpdater.isUpToDate( From ce62fe54a25994bf40c05dc15542efaaaf9b2ba8 Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 24 Oct 2022 15:53:00 +0200 Subject: [PATCH 2/6] replaced type matcher with isA --- test/pub_update_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pub_update_test.dart b/test/pub_update_test.dart index a220154..d51ffe6 100644 --- a/test/pub_update_test.dart +++ b/test/pub_update_test.dart @@ -61,7 +61,7 @@ void main() { test('cannot be instantiated with incorrect custom base URL', () { expect( () => PubUpdater(null, 'this-is-wrong.com'), - throwsA(TypeMatcher()), + throwsA(isA()), ); }); From 499808491cb298b3b0391c709542305a54e57881 Mon Sep 17 00:00:00 2001 From: Julien Date: Mon, 24 Oct 2022 15:55:52 +0200 Subject: [PATCH 3/6] added test --- test/pub_update_test.dart | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/pub_update_test.dart b/test/pub_update_test.dart index d51ffe6..02c9c45 100644 --- a/test/pub_update_test.dart +++ b/test/pub_update_test.dart @@ -155,7 +155,7 @@ void main() { }); group('getLatestVersion', () { - test('makes correct http request', () async { + test('makes correct http request (default)', () async { when(() => response.body).thenReturn(emptyResponseBody); try { @@ -172,6 +172,23 @@ void main() { ).called(1); }); + test('makes correct http request (custom domain)', () async { + when(() => response.body).thenReturn(emptyResponseBody); + + try { + await pubUpdaterWithCustomBaseURL.getLatestVersion('very_good_cli'); + } catch (_) {} + + verify( + () => client.get( + Uri.https( + customDomain, + '/api/packages/very_good_cli', + ), + ), + ).called(1); + }); + test('returns correct version', () async { when(() => response.body).thenReturn(validPackageInfoResponseBody); expect( From 9ff052dea380215325b02d37a900f1f6e95e122a Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Thu, 17 Nov 2022 15:12:53 -0600 Subject: [PATCH 4/6] apply feedback --- lib/src/pub_updater.dart | 21 +++++-------------- test/pub_update_test.dart | 43 +++++++++++---------------------------- 2 files changed, 17 insertions(+), 47 deletions(-) diff --git a/lib/src/pub_updater.dart b/lib/src/pub_updater.dart index a62d5c9..d810e5c 100644 --- a/lib/src/pub_updater.dart +++ b/lib/src/pub_updater.dart @@ -19,17 +19,12 @@ const _defaultBaseUrl = 'https://pub.dev/api/packages/'; /// {@endtemplate} class PubUpdater { /// {@macro pub_update} - PubUpdater([http.Client? client, String customBaseUrl = _defaultBaseUrl]) - : assert( - _isValidBaseURL(customBaseUrl), - '$customBaseUrl is not a valid pub api URL!', - ), - _client = client, - _baseUrl = customBaseUrl; - - /// The base url used for querying package versions - final String _baseUrl; + PubUpdater([http.Client? client, String baseUrl = _defaultBaseUrl]) + : _client = client, + _baseUrl = baseUrl; + final http.Client? _client; + final String _baseUrl; Future _get(Uri uri) => _client?.get(uri) ?? http.get(uri); @@ -76,10 +71,4 @@ class PubUpdater { return PackageInfo.fromJson(packageJson); } - - /// Checks whether the passed [baseUrl] is a valid pub api url - static bool _isValidBaseURL(String baseUrl) { - return (Uri.tryParse(baseUrl)?.isAbsolute ?? false) && - baseUrl.endsWith('/api/packages/'); - } } diff --git a/test/pub_update_test.dart b/test/pub_update_test.dart index 02c9c45..f864c53 100644 --- a/test/pub_update_test.dart +++ b/test/pub_update_test.dart @@ -21,17 +21,15 @@ const emptyResponseBody = '{}'; const command = ['dart', 'pub', 'global', 'activate', 'very_good_cli']; -const customDomain = 'custom-domain.com'; - -const customBaseUrl = 'https://$customDomain/api/packages/'; +const customBaseUrl = 'https://custom-domain.com/api/packages/'; void main() { group('PubUpdater', () { late Client client; late Response response; late PubUpdater pubUpdater; - late PubUpdater pubUpdaterWithCustomBaseURL; late ProcessManager processManager; + setUpAll(() { registerFallbackValue(Uri()); }); @@ -40,10 +38,6 @@ void main() { client = MockClient(); response = MockResponse(); pubUpdater = PubUpdater(client); - pubUpdaterWithCustomBaseURL = PubUpdater( - client, - customBaseUrl, - ); processManager = MockProcessManager(); when(() => client.get(any())).thenAnswer((_) async => response); @@ -58,19 +52,12 @@ void main() { expect(PubUpdater(), isNotNull); }); - test('cannot be instantiated with incorrect custom base URL', () { - expect( - () => PubUpdater(null, 'this-is-wrong.com'), - throwsA(isA()), - ); - }); - test('can be instantiated with correct custom base URL', () { expect(PubUpdater(null, customBaseUrl), isNotNull); }); group('isUpToDate', () { - test('makes correct http request (default)', () async { + test('makes correct http request', () async { when(() => response.body).thenReturn(emptyResponseBody); try { @@ -90,11 +77,11 @@ void main() { ).called(1); }); - test('makes correct http request (custom domain)', () async { + test('makes correct http request with custom base url', () async { when(() => response.body).thenReturn(emptyResponseBody); - + pubUpdater = PubUpdater(client, customBaseUrl); try { - await pubUpdaterWithCustomBaseURL.isUpToDate( + await pubUpdater.isUpToDate( packageName: 'very_good_cli', currentVersion: '0.3.3', ); @@ -102,10 +89,7 @@ void main() { verify( () => client.get( - Uri.https( - customDomain, - '/api/packages/very_good_cli', - ), + Uri.parse('${customBaseUrl}very_good_cli'), ), ).called(1); }); @@ -155,7 +139,7 @@ void main() { }); group('getLatestVersion', () { - test('makes correct http request (default)', () async { + test('makes correct http request', () async { when(() => response.body).thenReturn(emptyResponseBody); try { @@ -172,19 +156,16 @@ void main() { ).called(1); }); - test('makes correct http request (custom domain)', () async { + test('makes correct http request with custom base url', () async { when(() => response.body).thenReturn(emptyResponseBody); - + pubUpdater = PubUpdater(client, customBaseUrl); try { - await pubUpdaterWithCustomBaseURL.getLatestVersion('very_good_cli'); + await pubUpdater.getLatestVersion('very_good_cli'); } catch (_) {} verify( () => client.get( - Uri.https( - customDomain, - '/api/packages/very_good_cli', - ), + Uri.parse('${customBaseUrl}very_good_cli'), ), ).called(1); }); From fde2e6d9a282f7f1d71631beb6c9422fc4161723 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Thu, 17 Nov 2022 15:13:39 -0600 Subject: [PATCH 5/6] rename --- test/pub_update_test.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/pub_update_test.dart b/test/pub_update_test.dart index f864c53..3757cd2 100644 --- a/test/pub_update_test.dart +++ b/test/pub_update_test.dart @@ -52,7 +52,7 @@ void main() { expect(PubUpdater(), isNotNull); }); - test('can be instantiated with correct custom base URL', () { + test('can be instantiated with a custom base url', () { expect(PubUpdater(null, customBaseUrl), isNotNull); }); @@ -77,7 +77,7 @@ void main() { ).called(1); }); - test('makes correct http request with custom base url', () async { + test('makes correct http request with a custom base url', () async { when(() => response.body).thenReturn(emptyResponseBody); pubUpdater = PubUpdater(client, customBaseUrl); try { @@ -156,7 +156,7 @@ void main() { ).called(1); }); - test('makes correct http request with custom base url', () async { + test('makes correct http request with a custom base url', () async { when(() => response.body).thenReturn(emptyResponseBody); pubUpdater = PubUpdater(client, customBaseUrl); try { From 9506fa8cb1289d0ec5a3db668ed0d7d362b94ff5 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Thu, 17 Nov 2022 15:15:34 -0600 Subject: [PATCH 6/6] minor formatting --- test/pub_update_test.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/pub_update_test.dart b/test/pub_update_test.dart index 3757cd2..81e8500 100644 --- a/test/pub_update_test.dart +++ b/test/pub_update_test.dart @@ -80,6 +80,7 @@ void main() { test('makes correct http request with a custom base url', () async { when(() => response.body).thenReturn(emptyResponseBody); pubUpdater = PubUpdater(client, customBaseUrl); + try { await pubUpdater.isUpToDate( packageName: 'very_good_cli', @@ -159,6 +160,7 @@ void main() { test('makes correct http request with a custom base url', () async { when(() => response.body).thenReturn(emptyResponseBody); pubUpdater = PubUpdater(client, customBaseUrl); + try { await pubUpdater.getLatestVersion('very_good_cli'); } catch (_) {}