Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions app/lib/fake/server/fake_server_entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import 'package:pub_dev/shared/configuration.dart';
import 'package:pub_dev/shared/handlers.dart';
import 'package:pub_dev/shared/logging.dart';
import 'package:pub_dev/task/cloudcompute/fakecloudcompute.dart';
import 'package:pub_dev/tool/test_profile/import_source.dart';
import 'package:pub_dev/tool/test_profile/importer.dart';
import 'package:pub_dev/tool/test_profile/models.dart';
import 'package:shelf/shelf.dart' as shelf;
Expand Down Expand Up @@ -173,10 +172,7 @@ Future<shelf.Response> _testProfile(shelf.Request rq) async {
final profile =
TestProfile.fromJson(map['testProfile'] as Map<String, dynamic>);
// ignore: invalid_use_of_visible_for_testing_member
await importProfile(
profile: profile,
source: ImportSource.autoGenerated(),
);
await importProfile(profile: profile);
final analysis = (map['analysis'] as String?) ?? 'fake';
await apiExporter!.synchronizeExportedApi();
await processTaskFakeLocalOrWorker(analysis);
Expand Down
14 changes: 2 additions & 12 deletions app/lib/fake/tool/init_data_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ class FakeInitDataFileCommand extends Command {
argParser
..addOption('test-profile',
help: 'The file to read the test profile from.')
..addOption(
'source',
allowed: ['pub.dev', 'fake'],
help: 'Download package content from pub.dev or create fake sources.',
defaultsTo: 'pub.dev',
)
..addOption(
'analysis',
allowed: ['none', 'fake', 'local', 'worker'],
Expand All @@ -57,7 +51,6 @@ class FakeInitDataFileCommand extends Command {
});

final analysis = argResults!['analysis'] as String;
final source = argResults!['source'] as String;
final dataFile = argResults!['data-file'] as String;
final profile = TestProfile.fromYaml(
await File(argResults!['test-profile'] as String).readAsString(),
Expand All @@ -79,11 +72,8 @@ class FakeInitDataFileCommand extends Command {
fn: () async {
// ignore: invalid_use_of_visible_for_testing_member
await importProfile(
profile: profile,
source: source == 'pub.dev'
? ImportSource.fromPubDev(archiveCachePath: archiveCachePath)
: ImportSource.autoGenerated(),
);
profile: profile,
source: ImportSource(pubDevArchiveCachePath: archiveCachePath));

await processTaskFakeLocalOrWorker(analysis);
});
Expand Down
67 changes: 20 additions & 47 deletions app/lib/tool/test_profile/import_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,28 @@ import '../../shared/urls.dart' as urls;
import 'models.dart';
import 'resolver.dart' as resolver;

final _autoGeneratedImportSource = _AutoGeneratedImportSource();
/// Utility class for resolving and getting data for profiles.
class ImportSource {
final _client = Client();

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

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

/// Close resources that were opened during the sourcing of data.
Future<void> close();

/// Creates a source that resolves and downloads data from pub.dev.
static ImportSource fromPubDev({
String? archiveCachePath,
ImportSource({
String? pubDevArchiveCachePath,
}) {
archiveCachePath ??= p.join('.dart_tool', 'pub-test-profile', 'archives');
return _PubDevImportSource(archiveCachePath: archiveCachePath);
this.pubDevArchiveCachePath = pubDevArchiveCachePath ??
p.join('.dart_tool', 'pub-test-profile', 'archives');
}

/// Creates a source that generates data based on random seed, without any
/// network (or file) access.
static ImportSource autoGenerated() => _autoGeneratedImportSource;
}

abstract class _ImportSource implements ImportSource {
final _client = Client();
/// Close resources that were opened during the sourcing of data.
Future<void> close() async {
_client.close();
}

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

@override
Future<void> close() async {
_client.close();
}
}

/// Resolves and downloads data from pub.dev.
class _PubDevImportSource extends _ImportSource {
final String archiveCachePath;

_PubDevImportSource({
required this.archiveCachePath,
});

@override
Future<List<int>> getArchiveBytes(String package, String version) async {
Future<List<int>> getPubDevArchiveBytes(
String package, String version) async {
final archiveName = '$package-$version.tar.gz';
final file = File(p.join(archiveCachePath, archiveName));
final file = File(p.join(pubDevArchiveCachePath, archiveName));
// download package archive if not already in the cache
if (!await file.exists()) {
// TODO: Use archive_url from version-listing, that is stable!
Expand All @@ -120,14 +98,9 @@ class _PubDevImportSource extends _ImportSource {
}
return await file.readAsBytes();
}
}

/// Generates data based on random seed, without any network (or file) access.
class _AutoGeneratedImportSource extends _ImportSource {
final _archives = <String, List<int>>{};

@override
Future<List<int>> getArchiveBytes(String package, String version) async {
Future<List<int>> getGeneratedArchiveBytes(
String package, String version) async {
final key = '$package/$version';
final hasher = createHasher(key);
if (_archives.containsKey(key)) {
Expand Down
7 changes: 5 additions & 2 deletions app/lib/tool/test_profile/importer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import 'normalizer.dart';
@visibleForTesting
Future<void> importProfile({
required TestProfile profile,
required ImportSource source,
ImportSource? source,
String? pubHostedUrl,
String? adminUserEmail,
}) async {
source ??= ImportSource();
final resolvedVersions = await source.resolveVersions(profile);
resolvedVersions.sort();

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

var bytes = pendingBytes['${rv.package}/${rv.version}'] ??
await source.getArchiveBytes(rv.package, rv.version);
(profile.isGenerated(rv.package, rv.version)
? await source.getGeneratedArchiveBytes(rv.package, rv.version)
: await source.getPubDevArchiveBytes(rv.package, rv.version));
bytes = await _mayCleanupTarModeBits(bytes);
try {
await withRetryPubApiClient(
Expand Down
8 changes: 8 additions & 0 deletions app/lib/tool/test_profile/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:yaml/yaml.dart' as yaml;

Expand Down Expand Up @@ -58,6 +59,13 @@ class TestProfile {
defaultUser: email,
);
}

/// Returns whether the [package]/[version] is part of the generated packages list.
bool isGenerated(String package, String version) {
final p = generatedPackages.firstWhereOrNull((p) => p.name == package);
final v = p?.versions?.firstWhereOrNull((v) => v.version == version);
return v != null;
}
}

@JsonSerializable(explicitToJson: true, includeIfNull: false)
Expand Down
3 changes: 0 additions & 3 deletions app/test/package/api_export/api_exporter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import 'package:pub_dev/shared/datastore.dart';
import 'package:pub_dev/shared/storage.dart';
import 'package:pub_dev/shared/utils.dart';
import 'package:pub_dev/shared/versions.dart';
import 'package:pub_dev/tool/test_profile/import_source.dart';
import 'package:pub_dev/tool/test_profile/importer.dart';
import 'package:pub_dev/tool/test_profile/models.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -137,7 +136,6 @@ Future<void> _testExportedApiSynchronization(
_log.info('## New package');
{
await importProfile(
source: ImportSource.autoGenerated(),
profile: TestProfile(
defaultUser: userAtPubDevEmail,
generatedPackages: [
Expand Down Expand Up @@ -183,7 +181,6 @@ Future<void> _testExportedApiSynchronization(
_log.info('## New package version');
{
await importProfile(
source: ImportSource.autoGenerated(),
profile: TestProfile(
defaultUser: userAtPubDevEmail,
generatedPackages: [
Expand Down
16 changes: 3 additions & 13 deletions app/test/package/preview_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,15 @@ void main() {
});
}

class _ImportSource implements ImportSource {
class _ImportSource extends ImportSource {
final Version? Function() _currentSdkVersionFn;
final Version? Function() _futureSdkVersionFn;
final _defaultSource = ImportSource.autoGenerated();

_ImportSource(this._currentSdkVersionFn, this._futureSdkVersionFn);

@override
Future<List<ResolvedVersion>> resolveVersions(TestProfile profile) async {
return await _defaultSource.resolveVersions(profile);
}

@override
Future<List<int>> getArchiveBytes(String package, String version) async {
Future<List<int>> getGeneratedArchiveBytes(
String package, String version) async {
final archive = ArchiveBuilder();

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

return archive.toTarGzBytes();
}

@override
Future<void> close() async {
await _defaultSource.close();
}
}
4 changes: 2 additions & 2 deletions app/test/shared/test_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class FakeAppengineEnv {
if (testProfile != null) {
await importProfile(
profile: testProfile,
source: importSource ?? ImportSource.autoGenerated(),
source: importSource,
);
}
if (processJobsWithFakeRunners) {
Expand Down Expand Up @@ -208,7 +208,7 @@ void testWithFakeTime(

await importProfile(
profile: testProfile ?? defaultTestProfile,
source: importSource ?? ImportSource.autoGenerated(),
source: importSource,
);
await nameTracker.reloadFromDatastore();
await generateFakeDownloadCountsInDatastore();
Expand Down
2 changes: 0 additions & 2 deletions app/test/task/fallback_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:pub_dev/fake/backend/fake_pub_worker.dart';
import 'package:pub_dev/scorecard/backend.dart';
import 'package:pub_dev/shared/versions.dart';
import 'package:pub_dev/task/backend.dart';
import 'package:pub_dev/tool/test_profile/import_source.dart';
import 'package:pub_dev/tool/test_profile/importer.dart';
import 'package:pub_dev/tool/test_profile/models.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -104,7 +103,6 @@ void main() {
],
defaultUser: adminAtPubDevEmail,
),
source: ImportSource.autoGenerated(),
);
expect(await taskBackend.latestFinishedVersion('oxygen'), '1.2.0');
await processTasksWithFakePanaAndDartdoc();
Expand Down
5 changes: 0 additions & 5 deletions app/test/task/task_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import 'package:pana/pana.dart';
import 'package:pub_dev/task/backend.dart';
import 'package:pub_dev/task/cloudcompute/fakecloudcompute.dart';
import 'package:pub_dev/task/models.dart';
import 'package:pub_dev/tool/test_profile/import_source.dart';
import 'package:pub_dev/tool/test_profile/importer.dart';
import 'package:pub_dev/tool/test_profile/models.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -314,7 +313,6 @@ void main() {

// Create a package
await importProfile(
source: ImportSource.autoGenerated(),
profile: TestProfile(
defaultUser: '[email protected]',
generatedPackages: [
Expand Down Expand Up @@ -501,7 +499,6 @@ void main() {

// Create a new version of existing package, this should trigger analysis
await importProfile(
source: ImportSource.autoGenerated(),
profile: TestProfile(
defaultUser: '[email protected]',
generatedPackages: [
Expand Down Expand Up @@ -638,7 +635,6 @@ void main() {
// Create a new version of neon package, this should trigger analysis
// of neon, but also of oxygen
await importProfile(
source: ImportSource.autoGenerated(),
profile: TestProfile(
defaultUser: '[email protected]',
generatedPackages: [
Expand Down Expand Up @@ -729,7 +725,6 @@ void main() {

// Create new versions, removing the token from the first version
await importProfile(
source: ImportSource.autoGenerated(),
profile: TestProfile(
defaultUser: '[email protected]',
generatedPackages: [
Expand Down
3 changes: 0 additions & 3 deletions app/test/tool/test_profile/importer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:pub_dev/account/models.dart';
import 'package:pub_dev/package/backend.dart';
import 'package:pub_dev/package/models.dart';
import 'package:pub_dev/publisher/models.dart';
import 'package:pub_dev/tool/test_profile/import_source.dart';
import 'package:pub_dev/tool/test_profile/models.dart';
import 'package:test/test.dart';

Expand All @@ -28,7 +27,6 @@ void main() {
)
],
),
importSource: ImportSource.fromPubDev(),
fn: () async {
final users = await dbService.query<User>().run().toList();
expect(users.single.userId, hasLength(36));
Expand Down Expand Up @@ -59,7 +57,6 @@ void main() {
TestPackage(name: 'http', uploaders: ['[email protected]']),
],
),
importSource: ImportSource.fromPubDev(),
fn: () async {
final users = await dbService.query<User>().run().toList();
expect(users.single.userId, hasLength(36));
Expand Down