Skip to content

Commit

Permalink
feat: support custom base urls (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienandco authored Nov 17, 2022
1 parent 29af6ec commit 682c6dd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/src/pub_updater.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ 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;
PubUpdater([http.Client? client, String baseUrl = _defaultBaseUrl])
: _client = client,
_baseUrl = baseUrl;

/// The pub.dev base url for querying package versions
static const _baseUrl = 'https://pub.dev/api/packages/';
final http.Client? _client;
final String _baseUrl;

Future<http.Response> _get(Uri uri) => _client?.get(uri) ?? http.get(uri);

Expand Down
40 changes: 40 additions & 0 deletions test/pub_update_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ const emptyResponseBody = '{}';

const command = ['dart', 'pub', 'global', 'activate', 'very_good_cli'];

const customBaseUrl = 'https://custom-domain.com/api/packages/';

void main() {
group('PubUpdater', () {
late Client client;
late Response response;
late PubUpdater pubUpdater;
late ProcessManager processManager;

setUpAll(() {
registerFallbackValue(Uri());
});
Expand All @@ -49,6 +52,10 @@ void main() {
expect(PubUpdater(), isNotNull);
});

test('can be instantiated with a custom base url', () {
expect(PubUpdater(null, customBaseUrl), isNotNull);
});

group('isUpToDate', () {
test('makes correct http request', () async {
when(() => response.body).thenReturn(emptyResponseBody);
Expand All @@ -70,6 +77,24 @@ void main() {
).called(1);
});

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',
currentVersion: '0.3.3',
);
} catch (_) {}

verify(
() => client.get(
Uri.parse('${customBaseUrl}very_good_cli'),
),
).called(1);
});

test('returns false when currentVersion < latestVersion', () async {
expect(
await pubUpdater.isUpToDate(
Expand Down Expand Up @@ -132,6 +157,21 @@ void main() {
).called(1);
});

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 (_) {}

verify(
() => client.get(
Uri.parse('${customBaseUrl}very_good_cli'),
),
).called(1);
});

test('returns correct version', () async {
when(() => response.body).thenReturn(validPackageInfoResponseBody);
expect(
Expand Down

0 comments on commit 682c6dd

Please sign in to comment.