flutter/packages/flutter_tools/test/commands.shard/permeable/widget_preview_test.dart
auto-submit[bot] 1807704009
Reverts "[ Widget Previews ] Add support for detecting previews and generating code (#161911)" (#162327)
<!-- start_original_pr_link -->
Reverts: flutter/flutter#161911
<!-- end_original_pr_link -->
<!-- start_initiating_author -->
Initiated by: matanlurey
<!-- end_initiating_author -->
<!-- start_revert_reason -->
Reason for reverting: Mid-air collision with another Flutter tool
update.
<!-- end_revert_reason -->
<!-- start_original_pr_author -->
Original PR Author: bkonyi
<!-- end_original_pr_author -->

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

<!-- start_revert_body -->
This change reverts the following previous change:
`flutter widget-preview start` will now look for functions annotated
with `@Preview()` within the developer's project. These functions will
be used to generate
`.dart_tool/widget_preview_scaffold/lib/generated_preview.dart`, which
inserts the returned value from each preview function into a
`List<WidgetPreview>` returned from a `previews()` method that is
invoked by the widget preview scaffold root.

**Example generated_preview.dart:**

```dart
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:foo/foo.dart' as _i1;
import 'package:foo/src/bar.dart' as _i2;
import 'package:widget_preview/widget_preview.dart';

List<WidgetPreview> previews() => [_i1.preview(), _i2.barPreview1(), _i2.barPreview2()];
```
<!-- end_revert_body -->

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

219 lines
7.0 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 'dart:io' as io show IOOverrides;
import 'package:args/command_runner.dart';
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/commands/widget_preview.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/fakes.dart';
import '../../src/test_flutter_command_runner.dart';
import 'utils/project_testing_utils.dart';
void main() {
late Directory tempDir;
late LoggingProcessManager loggingProcessManager;
late FakeStdio mockStdio;
setUp(() {
loggingProcessManager = LoggingProcessManager();
tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_create_test.');
mockStdio = FakeStdio();
});
tearDown(() {
tryToDelete(tempDir);
});
Future<String> createRootProject() async {
return createProject(tempDir, arguments: <String>['--pub']);
}
Future<void> runWidgetPreviewCommand(List<String> arguments) async {
final CommandRunner<void> runner = createTestCommandRunner(WidgetPreviewCommand());
await runner.run(<String>['widget-preview', ...arguments]);
}
Future<void> startWidgetPreview({
required String? rootProjectPath,
List<String>? arguments,
}) async {
await runWidgetPreviewCommand(<String>[
'start',
...?arguments,
if (rootProjectPath != null) rootProjectPath,
]);
expect(
globals.fs
.directory(rootProjectPath ?? globals.fs.currentDirectory.path)
.childDirectory('.dart_tool')
.childDirectory('widget_preview_scaffold'),
exists,
);
}
Future<void> cleanWidgetPreview({required String rootProjectPath}) async {
await runWidgetPreviewCommand(<String>['clean', rootProjectPath]);
expect(
globals.fs
.directory(rootProjectPath)
.childDirectory('.dart_tool')
.childDirectory('widget_preview_scaffold'),
isNot(exists),
);
}
group('flutter widget-preview', () {
group('start exits if', () {
testUsingContext('given an invalid directory', () async {
try {
await runWidgetPreviewCommand(<String>['start', 'foo']);
fail('Successfully executed with multiple project paths');
} on ToolExit catch (e) {
expect(e.message, contains('Could not find foo'));
}
});
testUsingContext('more than one project directory is provided', () async {
try {
await runWidgetPreviewCommand(<String>['start', tempDir.path, tempDir.path]);
fail('Successfully executed with multiple project paths');
} on ToolExit catch (e) {
expect(e.message, contains('Only one directory should be provided.'));
}
});
testUsingContext('run outside of a Flutter project directory', () async {
try {
await startWidgetPreview(rootProjectPath: tempDir.path);
fail('Successfully executed outside of a Flutter project directory');
} on ToolExit catch (e) {
expect(e.message, contains('${tempDir.path} is not a valid Flutter project.'));
}
});
});
testUsingContext(
'start creates .dart_tool/widget_preview_scaffold',
() async {
final String rootProjectPath = await createRootProject();
await startWidgetPreview(rootProjectPath: rootProjectPath);
},
overrides: <Type, Generator>{
Pub:
() => Pub.test(
fileSystem: globals.fs,
logger: globals.logger,
processManager: globals.processManager,
botDetector: globals.botDetector,
platform: globals.platform,
stdio: mockStdio,
),
},
);
testUsingContext(
'start creates .dart_tool/widget_preview_scaffold in the CWD',
() async {
final String rootProjectPath = await createRootProject();
await io.IOOverrides.runZoned<Future<void>>(() async {
// Try to execute using the CWD.
await startWidgetPreview(rootProjectPath: null);
}, getCurrentDirectory: () => globals.fs.directory(rootProjectPath));
},
overrides: <Type, Generator>{
Pub:
() => Pub.test(
fileSystem: globals.fs,
logger: globals.logger,
processManager: globals.processManager,
botDetector: globals.botDetector,
platform: globals.platform,
stdio: mockStdio,
),
},
);
testUsingContext(
'clean deletes .dart_tool/widget_preview_scaffold',
() async {
final String rootProjectPath = await createRootProject();
await startWidgetPreview(rootProjectPath: rootProjectPath);
await cleanWidgetPreview(rootProjectPath: rootProjectPath);
},
overrides: <Type, Generator>{
Pub:
() => Pub.test(
fileSystem: globals.fs,
logger: globals.logger,
processManager: globals.processManager,
botDetector: globals.botDetector,
platform: globals.platform,
stdio: mockStdio,
),
},
);
testUsingContext(
'invokes pub in online and offline modes',
() async {
// Run pub online first in order to populate the pub cache.
final String rootProjectPath = await createRootProject();
loggingProcessManager.clear();
final RegExp dartCommand = RegExp(r'dart-sdk[\\/]bin[\\/]dart');
await startWidgetPreview(rootProjectPath: rootProjectPath);
expect(
loggingProcessManager.commands,
contains(
predicate(
(List<String> c) =>
dartCommand.hasMatch(c[0]) && c[1].contains('pub') && !c.contains('--offline'),
),
),
);
await cleanWidgetPreview(rootProjectPath: rootProjectPath);
// Run pub offline.
loggingProcessManager.clear();
await startWidgetPreview(
rootProjectPath: rootProjectPath,
arguments: <String>['--pub', '--offline'],
);
expect(
loggingProcessManager.commands,
contains(
predicate(
(List<String> c) =>
dartCommand.hasMatch(c[0]) && c[1].contains('pub') && c.contains('--offline'),
),
),
);
},
overrides: <Type, Generator>{
ProcessManager: () => loggingProcessManager,
Pub:
() => Pub.test(
fileSystem: globals.fs,
logger: globals.logger,
processManager: globals.processManager,
botDetector: globals.botDetector,
platform: globals.platform,
stdio: mockStdio,
),
},
);
});
}