Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow PubUpdater to be instantiated with custom dart api base url #33

Merged
merged 6 commits into from Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/src/pub_updater.dart
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
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