dart migrate: handle an SDK constraint with no min

Fixed https://github.com/dart-lang/sdk/issues/43989

Change-Id: Iddc5e5f0b53a5ec0540b002654bc46b40dc8a46a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179421
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Sam Rawlins 2021-01-15 18:03:07 +00:00 committed by commit-bot@chromium.org
parent f452e763e0
commit bdb60329e9
2 changed files with 89 additions and 8 deletions

View file

@ -396,16 +396,34 @@ ${depLines.join('\n')}
VersionConstraint currentConstraint;
if (node.value is String) {
currentConstraint = VersionConstraint.parse(node.value as String);
if (currentConstraint is VersionRange &&
currentConstraint.min >= minimumVersion) {
// The current version constraint is already up to date. Do not edit.
var invalidVersionMessage =
'The current SDK constraint in pubspec.yaml is invalid. A '
'minimum version, such as ">=2.7.0", is required when launching '
"'dart migrate'.";
if (currentConstraint is Version) {
// In this case, the constraint is an exact version, like 2.0.0.
_logger.stderr(invalidVersionMessage);
return false;
} else if (currentConstraint is VersionRange) {
if (currentConstraint.min == null) {
_logger.stderr(invalidVersionMessage);
return false;
} else if (currentConstraint.min >= minimumVersion) {
// The current version constraint is already up to date. Do not
// edit.
return false;
} else {
// TODO(srawlins): This overwrites the current maximum version. In
// the uncommon situation that there is a special maximum, it should
// not.
pubspec._replaceSpan(node.span, fullVersionConstraint, listener);
return true;
}
} else {
// TODO(srawlins): This overwrites the current maximum version. In
// the uncommon situation that there is a special maximum, it should
// not.
pubspec._replaceSpan(node.span, fullVersionConstraint, listener);
return true;
// The constraint is something different, like a union, like
// '>=1.0.0 <2.0.0 >=3.0.0 <4.0.0', which is not valid.
_logger.stderr(invalidVersionMessage);
return false;
}
} else {
// Something is odd with the constraint we've found in pubspec.yaml;

View file

@ -1863,6 +1863,48 @@ environment: 1
projectDir, simpleProject(migrated: true, pubspecText: pubspecText));
}
test_pubspec_environment_sdk_is_exact_version() async {
var pubspecText = '''
name: test
environment:
sdk: '2.0.0'
''';
var projectContents = simpleProject(pubspecText: pubspecText);
var projectDir = createProjectDir(projectContents);
var cliRunner = _createCli()
.decodeCommandLineArgs(_parseArgs(['--apply-changes', projectDir]));
await cliRunner.run();
// The Dart source code should still be migrated.
assertProjectContents(
projectDir,
simpleProject(
migrated: true,
pubspecText: pubspecText,
// The package config file should not have been touched.
packageConfigText: _getPackageConfigText(migrated: false)));
}
test_pubspec_environment_sdk_is_missing_min() async {
var pubspecText = '''
name: test
environment:
sdk: '<3.0.0'
''';
var projectContents = simpleProject(pubspecText: pubspecText);
var projectDir = createProjectDir(projectContents);
var cliRunner = _createCli()
.decodeCommandLineArgs(_parseArgs(['--apply-changes', projectDir]));
await cliRunner.run();
// The Dart source code should still be migrated.
assertProjectContents(
projectDir,
simpleProject(
migrated: true,
pubspecText: pubspecText,
// The package config file should not have been touched.
packageConfigText: _getPackageConfigText(migrated: false)));
}
test_pubspec_environment_sdk_is_not_string() async {
var pubspecText = '''
name: test
@ -1884,6 +1926,27 @@ environment:
packageConfigText: _getPackageConfigText(migrated: false)));
}
test_pubspec_environment_sdk_is_union() async {
var pubspecText = '''
name: test
environment:
sdk: '>=2.0.0 <2.1.0 >=2.2.0 <3.0.0'
''';
var projectContents = simpleProject(pubspecText: pubspecText);
var projectDir = createProjectDir(projectContents);
var cliRunner = _createCli()
.decodeCommandLineArgs(_parseArgs(['--apply-changes', projectDir]));
await cliRunner.run();
// The Dart source code should still be migrated.
assertProjectContents(
projectDir,
simpleProject(
migrated: true,
pubspecText: pubspecText,
// The package config file should not have been touched.
packageConfigText: _getPackageConfigText(migrated: false)));
}
test_pubspec_has_unusual_max_sdk_constraint() async {
// No one should be using a weird max SDK constraint like this. If they are
// doing so, we'll fix it to 3.0.0.