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 3 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
23 changes: 19 additions & 4 deletions lib/src/pub_updater.dart
Expand Up @@ -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!',
felangel marked this conversation as resolved.
Show resolved Hide resolved
),
_client = client,
_baseUrl = customBaseUrl;

/// The base url used for querying package versions
final String _baseUrl;
final http.Client? _client;

Future<http.Response> _get(Uri uri) => _client?.get(uri) ?? http.get(uri);
Expand Down Expand Up @@ -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/');
}
}
61 changes: 59 additions & 2 deletions test/pub_update_test.dart
Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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(isA<AssertionError>()),
);
});

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 {
Expand All @@ -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(
Expand Down Expand Up @@ -115,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 {
Expand All @@ -132,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(
Expand Down