flutter/packages/flutter_tools/test/commands.shard/hermetic/create_usage_test.dart
auto-submit[bot] 4372bfbc6c
Reverts "Add workspace (#169451)" (#169468)
<!-- start_original_pr_link -->
Reverts: flutter/flutter#169451
<!-- end_original_pr_link -->
<!-- start_initiating_author -->
Initiated by: matanlurey
<!-- end_initiating_author -->
<!-- start_revert_reason -->
Reason for reverting: Broke a number of post-submit tests
(ios_app_extension, packages_autoroller).
<!-- end_revert_reason -->
<!-- start_original_pr_author -->
Original PR Author: mosuem
<!-- end_original_pr_author -->

<!-- start_reviewers -->
Reviewed By: {matanlurey}
<!-- end_reviewers -->

<!-- start_revert_body -->
This change reverts the following previous change:
Reland after #169357.

Switch Flutter to use pub workspaces as a preparation to unpin selected
packages.

Assumptions:

1. No packages in this repository are published to pub.dev --> We can
use `any` dependencies in most local pubspecs, as the global constraint
defines the version. An exception are the packages used outside of this
repo with an `sdk` dependency, namely `flutter_localizations`,
`flutter_test`, and `flutter`.
2. The "universes" `{flutter_tools}` and `{flutter,
flutter_localizations, flutter_goldens}` can use different packages
versions, as they are not resolved together. --> We do not need to
upgrade them in sync, we can first do one "universe", then the other.

Based on these assumptions, we use
https://github.com/mosuem/pubspec_merger.dart to merge all packages in
the `flutter` universe into a top-level pub workspace.

The `flutter` and `flutter_tools` workspaces being separate also ensures
that changes to `flutter` will not inadvertently break `flutter_tools`,
with not-so-nice consequences for our users which would be unable to run
`flutter upgrade`.

There is a third "top-level" pubspec besides `./pubspec.yaml` and
`packages/flutter_tools/pubspec.yaml`, namely
`packages/flutter_tools/.../widget_preview_scaffold/pubspec.yaml`. This
is an artifact due to it living under `flutter_tools`, so it can't be
part of the `./pubspec.yaml` workspace. Moving it would be a larger
change, and out of the scope of this PR.

This required a rewrite of the update-packages tool, but the main
functionality stays the same, as well as the argument names, to ensure a
seamless transition.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

<!-- end_revert_body -->

Co-authored-by: auto-submit[bot] <flutter-engprod-team@google.com>
2025-05-26 14:07:27 +00:00

323 lines
11 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/android/java.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/create.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/doctor.dart';
import 'package:flutter_tools/src/doctor_validator.dart';
import 'package:flutter_tools/src/features.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/project.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/fakes.dart';
import '../../src/package_config.dart';
import '../../src/test_flutter_command_runner.dart';
import '../../src/testbed.dart';
class FakePub extends Fake implements Pub {
int calledGetOffline = 0;
int calledOnline = 0;
@override
Future<void> get({
PubContext? context,
required FlutterProject project,
bool upgrade = false,
bool offline = false,
bool generateSyntheticPackage = false,
bool generateSyntheticPackageForExample = false,
String? flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
PubOutputMode outputMode = PubOutputMode.all,
}) async {
writePackageConfigFiles(directory: project.directory, mainLibName: 'my_app');
if (offline) {
calledGetOffline += 1;
} else {
calledOnline += 1;
}
}
}
void main() {
group('usageValues', () {
late TestBed testbed;
late FakePub fakePub;
setUpAll(() {
Cache.disableLocking();
Cache.flutterRoot = 'flutter';
});
setUp(() {
testbed = TestBed(
setup: () {
fakePub = FakePub();
Cache.flutterRoot = 'flutter';
final List<String> filePaths = <String>[
globals.fs.path.join('flutter', 'packages', 'flutter', 'pubspec.yaml'),
globals.fs.path.join('flutter', 'packages', 'flutter_driver', 'pubspec.yaml'),
globals.fs.path.join('flutter', 'packages', 'flutter_test', 'pubspec.yaml'),
globals.fs.path.join(
'flutter',
'bin',
'cache',
'artifacts',
'gradle_wrapper',
'wrapper',
),
globals.fs.path.join('usr', 'local', 'bin', 'adb'),
globals.fs.path.join('Android', 'platform-tools', 'adb.exe'),
];
for (final String filePath in filePaths) {
globals.fs.file(filePath).createSync(recursive: true);
}
final List<String> templatePaths = <String>[
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'app'),
globals.fs.path.join(
'flutter',
'packages',
'flutter_tools',
'templates',
'app_integration_test',
),
globals.fs.path.join(
'flutter',
'packages',
'flutter_tools',
'templates',
'app_test_widget',
),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'cocoapods'),
globals.fs.path.join(
'flutter',
'packages',
'flutter_tools',
'templates',
'module',
'common',
),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'package'),
globals.fs.path.join(
'flutter',
'packages',
'flutter_tools',
'templates',
'package_ffi',
),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'plugin'),
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'plugin_ffi'),
globals.fs.path.join(
'flutter',
'packages',
'flutter_tools',
'templates',
'plugin_shared',
),
globals.fs.path.join(
'flutter',
'packages',
'flutter_tools',
'templates',
'plugin_cocoapods',
),
];
for (final String templatePath in templatePaths) {
globals.fs.directory(templatePath).createSync(recursive: true);
globals.fs
.directory(templatePath)
.childFile('pubspec.yaml.tmpl')
.writeAsStringSync('name: my_app');
}
// Set up enough of the packages to satisfy the templating code.
final File packagesFile = globals.fs.file(
globals.fs.path.join(
'flutter',
'packages',
'flutter_tools',
'.dart_tool',
'package_config.json',
),
);
final File flutterManifest = globals.fs.file(
globals.fs.path.join(
'flutter',
'packages',
'flutter_tools',
'templates',
'template_manifest.json',
),
)..createSync(recursive: true);
final Directory templateImagesDirectory = globals.fs.directory('flutter_template_images');
templateImagesDirectory.createSync(recursive: true);
packagesFile.createSync(recursive: true);
packagesFile.writeAsStringSync(
json.encode(<String, Object>{
'configVersion': 2,
'packages': <Object>[
<String, Object>{
'name': 'flutter_template_images',
'languageVersion': '2.8',
'rootUri': templateImagesDirectory.uri.toString(),
'packageUri': 'lib/',
},
],
}),
);
flutterManifest.writeAsStringSync('{"files":[]}');
},
overrides: <Type, Generator>{
DoctorValidatorsProvider: () => FakeDoctorValidatorsProvider(),
},
);
});
testUsingContext(
'set template type as usage value',
() => testbed.run(() async {
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=module', 'testy']);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createProjectType'],
'module',
);
await runner.run(<String>['create', '--no-pub', '--template=app', 'testy1']);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createProjectType'],
'app',
);
await runner.run(<String>['create', '--no-pub', '--template=package', 'testy3']);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createProjectType'],
'package',
);
await runner.run(<String>['create', '--no-pub', '--template=plugin', 'testy4']);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createProjectType'],
'plugin',
);
await runner.run(<String>['create', '--no-pub', '--template=plugin_ffi', 'testy5']);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createProjectType'],
'plugin_ffi',
);
await runner.run(<String>['create', '--no-pub', '--template=package_ffi', 'testy6']);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createProjectType'],
'package_ffi',
);
}),
overrides: <Type, Generator>{Java: () => FakeJava()},
);
testUsingContext(
'set iOS host language type as usage value',
() => testbed.run(() async {
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=app', 'testy']);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createIosLanguage'],
'swift',
);
await runner.run(<String>[
'create',
'--no-pub',
'--template=app',
'--ios-language=objc',
'testy',
]);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createIosLanguage'],
'objc',
);
}),
overrides: <Type, Generator>{Java: () => FakeJava()},
);
testUsingContext(
'set Android host language type as usage value',
() => testbed.run(() async {
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', '--no-pub', '--template=app', 'testy']);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createAndroidLanguage'],
'kotlin',
);
await runner.run(<String>[
'create',
'--no-pub',
'--template=app',
'--android-language=java',
'testy',
]);
expect(
(await command.unifiedAnalyticsUsageValues('create')).eventData['createAndroidLanguage'],
'java',
);
}),
overrides: <Type, Generator>{Java: () => FakeJava()},
);
testUsingContext(
'create --offline',
() => testbed.run(() async {
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', 'testy', '--offline']);
expect(fakePub.calledOnline, 0);
expect(fakePub.calledGetOffline, 1);
expect(command.argParser.options.containsKey('offline'), true);
expect(command.shouldUpdateCache, true);
}, overrides: <Type, Generator>{Java: () => null, Pub: () => fakePub}),
);
testUsingContext(
'package_ffi template not enabled',
() async {
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
expect(
runner.run(<String>['create', '--no-pub', '--template=package_ffi', 'my_ffi_package']),
throwsUsageException(
message: '"package_ffi" is not an allowed value for option "--template"',
),
);
},
overrides: <Type, Generator>{
FeatureFlags:
() => TestFeatureFlags(
isNativeAssetsEnabled:
false, // ignore: avoid_redundant_argument_values, If we graduate the feature to true by default, don't break this test.
),
},
);
});
}
class FakeDoctorValidatorsProvider implements DoctorValidatorsProvider {
@override
List<DoctorValidator> get validators => <DoctorValidator>[];
@override
List<Workflow> get workflows => <Workflow>[];
}