Skip to content
Snippets Groups Projects
Commit 7d06cb50 authored by Natalie Weizenbaum's avatar Natalie Weizenbaum
Browse files

Add validation for Flutter constraints.

Packages with Flutter constraints should only be published with SDK
constraints that restrict the SDK to versions that support Flutter
constraints.

Closes #1432

R=rnystrom@google.com

Review URL: https://codereview.chromium.org//2172883002 .
parent 9a178f3d
No related branches found
No related tags found
No related merge requests found
......@@ -4,9 +4,15 @@
import 'dart:async';
import 'package:pub_semver/pub_semver.dart';
import '../entrypoint.dart';
import '../validator.dart';
/// The range of all Dart SDK versions that don't support Flutter SDK
/// constraints.
final _preFlutterSupport = new VersionConstraint.parse("<1.19.0");
/// A validator that validates that a package's SDK constraint doesn't use the
/// "^" syntax.
class SdkConstraintValidator extends Validator {
......@@ -14,15 +20,33 @@ class SdkConstraintValidator extends Validator {
: super(entrypoint);
Future validate() async {
var constraint = entrypoint.root.pubspec.dartSdkConstraint;
if (!constraint.toString().startsWith("^")) return;
errors.add(
"^ version constraints aren't allowed for SDK constraints since "
"older versions of pub don't support them.\n"
"Expand it manually instead:\n"
"\n"
"environment:\n"
" sdk: \">=${constraint.min} <${constraint.max}\"");
var dartConstraint = entrypoint.root.pubspec.dartSdkConstraint;
if (dartConstraint.toString().startsWith("^")) {
errors.add(
"^ version constraints aren't allowed for SDK constraints since "
"older versions of pub don't support them.\n"
"Expand it manually instead:\n"
"\n"
"environment:\n"
" sdk: \">=${dartConstraint.min} <${dartConstraint.max}\"");
}
if (entrypoint.root.pubspec.flutterSdkConstraint != null &&
dartConstraint.allowsAny(_preFlutterSupport)) {
var newDartConstraint = dartConstraint.difference(_preFlutterSupport);
if (newDartConstraint.isEmpty ||
newDartConstraint ==
VersionConstraint.any.difference(_preFlutterSupport)) {
newDartConstraint = new VersionConstraint.parse("<2.0.0")
.difference(_preFlutterSupport);
}
errors.add(
"Older versions of pub don't support Flutter SDK constraints.\n"
"Make sure your SDK constraint excludes those old versions:\n"
"\n"
"environment:\n"
" sdk: \"$newDartConstraint\"");
}
}
}
......@@ -27,14 +27,68 @@ main() {
]).create();
expectNoValidationError(sdkConstraint);
});
integration('has a Flutter SDK constraint with an appropriate Dart SDK '
'constraint', () {
d.dir(appPath, [
d.pubspec({
"name": "test_pkg",
"version": "1.0.0",
"environment": {
"sdk": ">=1.19.0 <2.0.0",
"flutter": "^1.2.3"
}
})
]).create();
expectNoValidationError(sdkConstraint);
});
});
test("should consider a package invalid if it has an SDK constraint with "
"^", () {
d.dir(appPath, [
d.libPubspec("test_pkg", "1.0.0", sdk: "^1.8.0")
]).create();
expect(schedulePackageValidation(sdkConstraint),
completion(pairOf(anyElement(contains('">=1.8.0 <2.0.0"')), isEmpty)));
group("should consider a package invalid if it", () {
integration("has an SDK constraint with ^", () {
d.dir(appPath, [
d.libPubspec("test_pkg", "1.0.0", sdk: "^1.8.0")
]).create();
expect(
schedulePackageValidation(sdkConstraint),
completion(pairOf(
anyElement(contains('">=1.8.0 <2.0.0"')),
isEmpty)));
});
integration("has a Flutter SDK constraint with a too-broad SDK "
"constraint", () {
d.dir(appPath, [
d.pubspec({
"name": "test_pkg",
"version": "1.0.0",
"environment": {
"sdk": ">=1.18.0 <1.50.0",
"flutter": "^1.2.3"
}
})
]).create();
expect(
schedulePackageValidation(sdkConstraint),
completion(pairOf(
anyElement(contains('">=1.19.0 <1.50.0"')),
isEmpty)));
});
integration("has a Flutter SDK constraint with no SDK "
"constraint", () {
d.dir(appPath, [
d.pubspec({
"name": "test_pkg",
"version": "1.0.0",
"environment": {"flutter": "^1.2.3"}
})
]).create();
expect(
schedulePackageValidation(sdkConstraint),
completion(pairOf(
anyElement(contains('">=1.19.0 <2.0.0"')),
isEmpty)));
});
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment