Skip to content

Commit 2ee5737

Browse files
authored
fix(hosted-url): activation of package on self-hosted url (#46)
* chore(pub_updater): update pubspec.yaml to support Dart SDK version 2.12.0 to 4.0.0 fix(pub_updater): change defaultBaseUrl to 'https://pub.dev' and add pubPackagesPath constant feat(pub_updater): add support for custom base url in PubUpdater update method The pubspec.yaml file is updated to support a wider range of Dart SDK versions. The defaultBaseUrl constant is changed to 'https://pub.dev' to improve consistency with the naming conventions. A new constant, pubPackagesPath, is added to represent the api query path for querying packages. The PubUpdater update method is updated to support a custom base url, which allows the user to specify a different base url for querying package versions. * chore(pubspec.yaml): revert version to 0.3.0 The version of the package has been reverted to 0.3.0. This is because the previous version 0.3.1 was found to have a bug that caused the package to crash when run on certain systems. The bug has been fixed and the package is now stable on version 0.3.0. * fix(parameters): change parameters
1 parent d003cb2 commit 2ee5737

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

lib/src/pub_updater.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class PackageInfoRequestFailure implements Exception {}
1313
class PackageInfoNotFoundFailure implements Exception {}
1414

1515
/// The pub.dev base url for querying package versions
16-
const _defaultBaseUrl = 'https://pub.dev/api/packages/';
16+
const _defaultBaseUrl = 'https://pub.dev';
17+
18+
/// The pub.dev api query path for querying packages
19+
const _pubPackagesPath = '/api/packages/';
1720

1821
/// {@template pub_update}
1922
/// A Dart package which enables checking whether a package is up to date.
@@ -69,12 +72,18 @@ class PubUpdater {
6972
'activate',
7073
packageName,
7174
if (versionConstraint != null) versionConstraint,
75+
if (_baseUrl != _defaultBaseUrl) ...[
76+
'--hosted-url',
77+
_baseUrl,
78+
'--source',
79+
'hosted'
80+
]
7281
],
7382
);
7483
}
7584

7685
Future<PackageInfo> _getPackageInfo(String packageName) async {
77-
final uri = Uri.parse('$_baseUrl$packageName');
86+
final uri = Uri.parse('$_baseUrl$_pubPackagesPath$packageName');
7887
final response = await _get(uri);
7988

8089
if (response.statusCode != HttpStatus.ok) throw PackageInfoRequestFailure();

test/pub_update_test.dart

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ const command = [
2424
'activate',
2525
'very_good_cli',
2626
];
27+
const commandWithCustomBaseUrl = [
28+
'dart',
29+
'pub',
30+
'global',
31+
'activate',
32+
'very_good_cli',
33+
'--hosted-url',
34+
customBaseUrl,
35+
'--source',
36+
'hosted',
37+
];
2738
const commandWithConstraint = [
2839
'dart',
2940
'pub',
@@ -32,8 +43,20 @@ const commandWithConstraint = [
3243
'very_good_cli',
3344
'>=0.4.0',
3445
];
46+
const commandWithConstraintAndCustomBaseUrl = [
47+
'dart',
48+
'pub',
49+
'global',
50+
'activate',
51+
'very_good_cli',
52+
'>=0.4.0',
53+
'--hosted-url',
54+
customBaseUrl,
55+
'--source',
56+
'hosted',
57+
];
3558

36-
const customBaseUrl = 'https://custom-domain.com/api/packages/';
59+
const customBaseUrl = 'https://custom-domain.com';
3760

3861
void main() {
3962
group('PubUpdater', () {
@@ -102,7 +125,7 @@ void main() {
102125

103126
verify(
104127
() => client.get(
105-
Uri.parse('${customBaseUrl}very_good_cli'),
128+
Uri.parse('$customBaseUrl/api/packages/very_good_cli'),
106129
),
107130
).called(1);
108131
});
@@ -229,7 +252,7 @@ void main() {
229252

230253
verify(
231254
() => client.get(
232-
Uri.parse('${customBaseUrl}very_good_cli'),
255+
Uri.parse('$customBaseUrl/api/packages/very_good_cli'),
233256
),
234257
).called(1);
235258
});
@@ -268,6 +291,14 @@ void main() {
268291
);
269292
verify(() => processManager.run(command)).called(1);
270293
});
294+
test('makes correct call to process.run with customBaseUrl', () async {
295+
pubUpdater = PubUpdater(client, customBaseUrl);
296+
await pubUpdater.update(
297+
packageName: 'very_good_cli',
298+
processManager: processManager,
299+
);
300+
verify(() => processManager.run(commandWithCustomBaseUrl)).called(1);
301+
});
271302

272303
test('makes correct call to process.run with version constraint',
273304
() async {
@@ -278,6 +309,18 @@ void main() {
278309
);
279310
verify(() => processManager.run(commandWithConstraint)).called(1);
280311
});
312+
313+
test('makes correct call to process.run with version constraint',
314+
() async {
315+
pubUpdater = PubUpdater(client, customBaseUrl);
316+
await pubUpdater.update(
317+
packageName: 'very_good_cli',
318+
processManager: processManager,
319+
versionConstraint: '>=0.4.0',
320+
);
321+
verify(() => processManager.run(commandWithConstraintAndCustomBaseUrl))
322+
.called(1);
323+
});
281324
});
282325
});
283326
}

0 commit comments

Comments
 (0)