Files
packages/script/tool/test/update_min_sdk_command_test.dart
stuartmorgan 8c287e99d6 [tool] Move changed file detection to base command class (#8730)
Consolidates the code to find all changed file paths into the `PackageLoopingCommand` class that is the base of almost all of the repo tooling commands. This in a preparatory PR for a future change to allow each command to define a list of files or file patterns that definitively *don't* affect that test, so that CI can be smarter about what tests to run (e.g., not running expensive integration tests for README changes).

A side effect of this change is that tests of almost all commands now need a mock `GitDir` instance. This would add a lot of copy/pasted boilerplate to the test setup, and there is already too much of that, so instead this refactors common test setup:
- Creating a memory file system
- Populating it with a packages directory
- Creating a RecordingProcessRunner to mock out process calls
- Creating a mock GitDir that forwards to a RecordingProcessRunner

into a helper method (using records and destructuring to easily return multiple values). While some tests don't need all of these steps, those that don't can easily ignore parts of it, and it will make it much easier to update tests in the future if they need them, and it makes the setup much more consistent which makes it easier to reason about test setup in general.

Prep for https://github.com/flutter/flutter/issues/136394
2025-03-25 21:29:17 +00:00

130 lines
3.9 KiB
Dart

// Copyright 2013 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:file/file.dart';
import 'package:flutter_plugin_tools/src/update_min_sdk_command.dart';
import 'package:git/git.dart';
import 'package:test/test.dart';
import 'util.dart';
void main() {
late Directory packagesDir;
late CommandRunner<void> runner;
setUp(() {
final GitDir gitDir;
(:packagesDir, processRunner: _, gitProcessRunner: _, :gitDir) =
configureBaseCommandMocks();
final UpdateMinSdkCommand command = UpdateMinSdkCommand(
packagesDir,
gitDir: gitDir,
);
runner = CommandRunner<void>(
'update_min_sdk_command', 'Test for update_min_sdk_command');
runner.addCommand(command);
});
test('fails if --flutter-min is missing', () async {
Error? commandError;
await runCapturingPrint(runner, <String>[
'update-min-sdk',
], errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ArgumentError>());
});
test('updates Dart when only Dart is present, with manual range', () async {
final RepositoryPackage package = createFakePackage(
'a_package', packagesDir,
dartConstraint: '>=3.0.0 <4.0.0');
await runCapturingPrint(runner, <String>[
'update-min-sdk',
'--flutter-min',
'3.13.0', // Corresponds to Dart 3.1.0
]);
final String dartVersion =
package.parsePubspec().environment['sdk'].toString();
expect(dartVersion, '^3.1.0');
});
test('updates Dart when only Dart is present, with carrot', () async {
final RepositoryPackage package =
createFakePackage('a_package', packagesDir, dartConstraint: '^3.0.0');
await runCapturingPrint(runner, <String>[
'update-min-sdk',
'--flutter-min',
'3.13.0', // Corresponds to Dart 3.1.0
]);
final String dartVersion =
package.parsePubspec().environment['sdk'].toString();
expect(dartVersion, '^3.1.0');
});
test('does not update Dart if it is already higher', () async {
final RepositoryPackage package =
createFakePackage('a_package', packagesDir, dartConstraint: '^3.2.0');
await runCapturingPrint(runner, <String>[
'update-min-sdk',
'--flutter-min',
'3.13.0', // Corresponds to Dart 3.1.0
]);
final String dartVersion =
package.parsePubspec().environment['sdk'].toString();
expect(dartVersion, '^3.2.0');
});
test('updates both Dart and Flutter when both are present', () async {
final RepositoryPackage package = createFakePackage(
'a_package', packagesDir,
isFlutter: true,
dartConstraint: '>=3.0.0 <4.0.0',
flutterConstraint: '>=3.10.0');
await runCapturingPrint(runner, <String>[
'update-min-sdk',
'--flutter-min',
'3.13.0', // Corresponds to Dart 3.1.0
]);
final String dartVersion =
package.parsePubspec().environment['sdk'].toString();
final String flutterVersion =
package.parsePubspec().environment['flutter'].toString();
expect(dartVersion, '^3.1.0');
expect(flutterVersion, '>=3.13.0');
});
test('does not update Flutter if it is already higher', () async {
final RepositoryPackage package = createFakePackage(
'a_package', packagesDir,
isFlutter: true,
dartConstraint: '^3.2.0',
flutterConstraint: '>=3.16.0');
await runCapturingPrint(runner, <String>[
'update-min-sdk',
'--flutter-min',
'3.13.0', // Corresponds to Dart 3.1.0
]);
final String dartVersion =
package.parsePubspec().environment['sdk'].toString();
final String flutterVersion =
package.parsePubspec().environment['flutter'].toString();
expect(dartVersion, '^3.2.0');
expect(flutterVersion, '>=3.16.0');
});
}