Skip to content

Commit f6b7ce2

Browse files
authored
Merge the ImportSource implementations. (#8637)
1 parent ce31392 commit f6b7ce2

File tree

11 files changed

+41
-94
lines changed

11 files changed

+41
-94
lines changed

app/lib/fake/server/fake_server_entrypoint.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import 'package:pub_dev/shared/configuration.dart';
1919
import 'package:pub_dev/shared/handlers.dart';
2020
import 'package:pub_dev/shared/logging.dart';
2121
import 'package:pub_dev/task/cloudcompute/fakecloudcompute.dart';
22-
import 'package:pub_dev/tool/test_profile/import_source.dart';
2322
import 'package:pub_dev/tool/test_profile/importer.dart';
2423
import 'package:pub_dev/tool/test_profile/models.dart';
2524
import 'package:shelf/shelf.dart' as shelf;
@@ -173,10 +172,7 @@ Future<shelf.Response> _testProfile(shelf.Request rq) async {
173172
final profile =
174173
TestProfile.fromJson(map['testProfile'] as Map<String, dynamic>);
175174
// ignore: invalid_use_of_visible_for_testing_member
176-
await importProfile(
177-
profile: profile,
178-
source: ImportSource.autoGenerated(),
179-
);
175+
await importProfile(profile: profile);
180176
final analysis = (map['analysis'] as String?) ?? 'fake';
181177
await apiExporter!.synchronizeExportedApi();
182178
await processTaskFakeLocalOrWorker(analysis);

app/lib/fake/tool/init_data_file.dart

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ class FakeInitDataFileCommand extends Command {
2929
argParser
3030
..addOption('test-profile',
3131
help: 'The file to read the test profile from.')
32-
..addOption(
33-
'source',
34-
allowed: ['pub.dev', 'fake'],
35-
help: 'Download package content from pub.dev or create fake sources.',
36-
defaultsTo: 'pub.dev',
37-
)
3832
..addOption(
3933
'analysis',
4034
allowed: ['none', 'fake', 'local', 'worker'],
@@ -57,7 +51,6 @@ class FakeInitDataFileCommand extends Command {
5751
});
5852

5953
final analysis = argResults!['analysis'] as String;
60-
final source = argResults!['source'] as String;
6154
final dataFile = argResults!['data-file'] as String;
6255
final profile = TestProfile.fromYaml(
6356
await File(argResults!['test-profile'] as String).readAsString(),
@@ -79,11 +72,8 @@ class FakeInitDataFileCommand extends Command {
7972
fn: () async {
8073
// ignore: invalid_use_of_visible_for_testing_member
8174
await importProfile(
82-
profile: profile,
83-
source: source == 'pub.dev'
84-
? ImportSource.fromPubDev(archiveCachePath: archiveCachePath)
85-
: ImportSource.autoGenerated(),
86-
);
75+
profile: profile,
76+
source: ImportSource(pubDevArchiveCachePath: archiveCachePath));
8777

8878
await processTaskFakeLocalOrWorker(analysis);
8979
});

app/lib/tool/test_profile/import_source.dart

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,28 @@ import '../../shared/urls.dart' as urls;
2020
import 'models.dart';
2121
import 'resolver.dart' as resolver;
2222

23-
final _autoGeneratedImportSource = _AutoGeneratedImportSource();
23+
/// Utility class for resolving and getting data for profiles.
24+
class ImportSource {
25+
final _client = Client();
2426

25-
/// Interface for resolving and getting data for profiles.
26-
abstract class ImportSource {
27-
/// Resolve all the package-version required for the [profile].
28-
Future<List<ResolvedVersion>> resolveVersions(TestProfile profile);
27+
/// Cached archive bytes for generated packages (keys in `<package>/<version>` format).
28+
final _archives = <String, List<int>>{};
2929

30-
/// Gets the archive bytes for [package]-[version].
31-
Future<List<int>> getArchiveBytes(String package, String version);
30+
late final String pubDevArchiveCachePath;
3231

33-
/// Close resources that were opened during the sourcing of data.
34-
Future<void> close();
35-
36-
/// Creates a source that resolves and downloads data from pub.dev.
37-
static ImportSource fromPubDev({
38-
String? archiveCachePath,
32+
ImportSource({
33+
String? pubDevArchiveCachePath,
3934
}) {
40-
archiveCachePath ??= p.join('.dart_tool', 'pub-test-profile', 'archives');
41-
return _PubDevImportSource(archiveCachePath: archiveCachePath);
35+
this.pubDevArchiveCachePath = pubDevArchiveCachePath ??
36+
p.join('.dart_tool', 'pub-test-profile', 'archives');
4237
}
4338

44-
/// Creates a source that generates data based on random seed, without any
45-
/// network (or file) access.
46-
static ImportSource autoGenerated() => _autoGeneratedImportSource;
47-
}
48-
49-
abstract class _ImportSource implements ImportSource {
50-
final _client = Client();
39+
/// Close resources that were opened during the sourcing of data.
40+
Future<void> close() async {
41+
_client.close();
42+
}
5143

52-
@override
44+
/// Resolve all the package-version required for the [profile].
5345
Future<List<ResolvedVersion>> resolveVersions(TestProfile profile) async {
5446
return <ResolvedVersion>[
5547
...await resolver.resolveVersions(_client, profile),
@@ -91,24 +83,10 @@ abstract class _ImportSource implements ImportSource {
9183
return versions;
9284
}
9385

94-
@override
95-
Future<void> close() async {
96-
_client.close();
97-
}
98-
}
99-
100-
/// Resolves and downloads data from pub.dev.
101-
class _PubDevImportSource extends _ImportSource {
102-
final String archiveCachePath;
103-
104-
_PubDevImportSource({
105-
required this.archiveCachePath,
106-
});
107-
108-
@override
109-
Future<List<int>> getArchiveBytes(String package, String version) async {
86+
Future<List<int>> getPubDevArchiveBytes(
87+
String package, String version) async {
11088
final archiveName = '$package-$version.tar.gz';
111-
final file = File(p.join(archiveCachePath, archiveName));
89+
final file = File(p.join(pubDevArchiveCachePath, archiveName));
11290
// download package archive if not already in the cache
11391
if (!await file.exists()) {
11492
// TODO: Use archive_url from version-listing, that is stable!
@@ -120,14 +98,9 @@ class _PubDevImportSource extends _ImportSource {
12098
}
12199
return await file.readAsBytes();
122100
}
123-
}
124101

125-
/// Generates data based on random seed, without any network (or file) access.
126-
class _AutoGeneratedImportSource extends _ImportSource {
127-
final _archives = <String, List<int>>{};
128-
129-
@override
130-
Future<List<int>> getArchiveBytes(String package, String version) async {
102+
Future<List<int>> getGeneratedArchiveBytes(
103+
String package, String version) async {
131104
final key = '$package/$version';
132105
final hasher = createHasher(key);
133106
if (_archives.containsKey(key)) {

app/lib/tool/test_profile/importer.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ import 'normalizer.dart';
2828
@visibleForTesting
2929
Future<void> importProfile({
3030
required TestProfile profile,
31-
required ImportSource source,
31+
ImportSource? source,
3232
String? pubHostedUrl,
3333
String? adminUserEmail,
3434
}) async {
35+
source ??= ImportSource();
3536
final resolvedVersions = await source.resolveVersions(profile);
3637
resolvedVersions.sort();
3738

@@ -96,7 +97,9 @@ Future<void> importProfile({
9697
lastActiveUploaderEmails[rv.package] = uploaderEmail;
9798

9899
var bytes = pendingBytes['${rv.package}/${rv.version}'] ??
99-
await source.getArchiveBytes(rv.package, rv.version);
100+
(profile.isGenerated(rv.package, rv.version)
101+
? await source.getGeneratedArchiveBytes(rv.package, rv.version)
102+
: await source.getPubDevArchiveBytes(rv.package, rv.version));
100103
bytes = await _mayCleanupTarModeBits(bytes);
101104
try {
102105
await withRetryPubApiClient(

app/lib/tool/test_profile/models.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:convert';
66

7+
import 'package:collection/collection.dart';
78
import 'package:json_annotation/json_annotation.dart';
89
import 'package:yaml/yaml.dart' as yaml;
910

@@ -58,6 +59,13 @@ class TestProfile {
5859
defaultUser: email,
5960
);
6061
}
62+
63+
/// Returns whether the [package]/[version] is part of the generated packages list.
64+
bool isGenerated(String package, String version) {
65+
final p = generatedPackages.firstWhereOrNull((p) => p.name == package);
66+
final v = p?.versions?.firstWhereOrNull((v) => v.version == version);
67+
return v != null;
68+
}
6169
}
6270

6371
@JsonSerializable(explicitToJson: true, includeIfNull: false)

app/test/package/api_export/api_exporter_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import 'package:pub_dev/shared/datastore.dart';
1616
import 'package:pub_dev/shared/storage.dart';
1717
import 'package:pub_dev/shared/utils.dart';
1818
import 'package:pub_dev/shared/versions.dart';
19-
import 'package:pub_dev/tool/test_profile/import_source.dart';
2019
import 'package:pub_dev/tool/test_profile/importer.dart';
2120
import 'package:pub_dev/tool/test_profile/models.dart';
2221
import 'package:test/test.dart';
@@ -137,7 +136,6 @@ Future<void> _testExportedApiSynchronization(
137136
_log.info('## New package');
138137
{
139138
await importProfile(
140-
source: ImportSource.autoGenerated(),
141139
profile: TestProfile(
142140
defaultUser: userAtPubDevEmail,
143141
generatedPackages: [
@@ -183,7 +181,6 @@ Future<void> _testExportedApiSynchronization(
183181
_log.info('## New package version');
184182
{
185183
await importProfile(
186-
source: ImportSource.autoGenerated(),
187184
profile: TestProfile(
188185
defaultUser: userAtPubDevEmail,
189186
generatedPackages: [

app/test/package/preview_test.dart

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,15 @@ void main() {
226226
});
227227
}
228228

229-
class _ImportSource implements ImportSource {
229+
class _ImportSource extends ImportSource {
230230
final Version? Function() _currentSdkVersionFn;
231231
final Version? Function() _futureSdkVersionFn;
232-
final _defaultSource = ImportSource.autoGenerated();
233232

234233
_ImportSource(this._currentSdkVersionFn, this._futureSdkVersionFn);
235234

236235
@override
237-
Future<List<ResolvedVersion>> resolveVersions(TestProfile profile) async {
238-
return await _defaultSource.resolveVersions(profile);
239-
}
240-
241-
@override
242-
Future<List<int>> getArchiveBytes(String package, String version) async {
236+
Future<List<int>> getGeneratedArchiveBytes(
237+
String package, String version) async {
243238
final archive = ArchiveBuilder();
244239

245240
final minSdk =
@@ -263,9 +258,4 @@ class _ImportSource implements ImportSource {
263258

264259
return archive.toTarGzBytes();
265260
}
266-
267-
@override
268-
Future<void> close() async {
269-
await _defaultSource.close();
270-
}
271261
}

app/test/shared/test_services.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class FakeAppengineEnv {
103103
if (testProfile != null) {
104104
await importProfile(
105105
profile: testProfile,
106-
source: importSource ?? ImportSource.autoGenerated(),
106+
source: importSource,
107107
);
108108
}
109109
if (processJobsWithFakeRunners) {
@@ -208,7 +208,7 @@ void testWithFakeTime(
208208

209209
await importProfile(
210210
profile: testProfile ?? defaultTestProfile,
211-
source: importSource ?? ImportSource.autoGenerated(),
211+
source: importSource,
212212
);
213213
await nameTracker.reloadFromDatastore();
214214
await generateFakeDownloadCountsInDatastore();

app/test/task/fallback_test.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:pub_dev/fake/backend/fake_pub_worker.dart';
66
import 'package:pub_dev/scorecard/backend.dart';
77
import 'package:pub_dev/shared/versions.dart';
88
import 'package:pub_dev/task/backend.dart';
9-
import 'package:pub_dev/tool/test_profile/import_source.dart';
109
import 'package:pub_dev/tool/test_profile/importer.dart';
1110
import 'package:pub_dev/tool/test_profile/models.dart';
1211
import 'package:test/test.dart';
@@ -104,7 +103,6 @@ void main() {
104103
],
105104
defaultUser: adminAtPubDevEmail,
106105
),
107-
source: ImportSource.autoGenerated(),
108106
);
109107
expect(await taskBackend.latestFinishedVersion('oxygen'), '1.2.0');
110108
await processTasksWithFakePanaAndDartdoc();

app/test/task/task_test.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import 'package:pana/pana.dart';
1616
import 'package:pub_dev/task/backend.dart';
1717
import 'package:pub_dev/task/cloudcompute/fakecloudcompute.dart';
1818
import 'package:pub_dev/task/models.dart';
19-
import 'package:pub_dev/tool/test_profile/import_source.dart';
2019
import 'package:pub_dev/tool/test_profile/importer.dart';
2120
import 'package:pub_dev/tool/test_profile/models.dart';
2221
import 'package:test/test.dart';
@@ -314,7 +313,6 @@ void main() {
314313

315314
// Create a package
316315
await importProfile(
317-
source: ImportSource.autoGenerated(),
318316
profile: TestProfile(
319317
defaultUser: '[email protected]',
320318
generatedPackages: [
@@ -501,7 +499,6 @@ void main() {
501499

502500
// Create a new version of existing package, this should trigger analysis
503501
await importProfile(
504-
source: ImportSource.autoGenerated(),
505502
profile: TestProfile(
506503
defaultUser: '[email protected]',
507504
generatedPackages: [
@@ -638,7 +635,6 @@ void main() {
638635
// Create a new version of neon package, this should trigger analysis
639636
// of neon, but also of oxygen
640637
await importProfile(
641-
source: ImportSource.autoGenerated(),
642638
profile: TestProfile(
643639
defaultUser: '[email protected]',
644640
generatedPackages: [
@@ -729,7 +725,6 @@ void main() {
729725

730726
// Create new versions, removing the token from the first version
731727
await importProfile(
732-
source: ImportSource.autoGenerated(),
733728
profile: TestProfile(
734729
defaultUser: '[email protected]',
735730
generatedPackages: [

0 commit comments

Comments
 (0)