flutter/packages/flutter_tools/test/commands.shard/permeable/widget_preview_test.dart
Andrew Kolos a9c50335c7
remove dependency on Usage from Pub class (#162279)
Towards https://github.com/flutter/flutter/issues/150575

Removes more usage of `Usage`. This PR is scoped to removing all
references to it in `pub`-related code.

<details>

<summary> Pre-launch checklist </summary> 


- [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.

</details>


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
2025-01-28 17:32:27 +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,
),
},
);
});
}