Validate that an uploaded package has a LICENSE file.

BUG=7045

Review URL: https://codereview.chromium.org//11474047

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@15879 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
nweiz@google.com 2012-12-08 03:24:44 +00:00
parent 55ad513b3b
commit 3b8e03576e
5 changed files with 84 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import 'log.dart' as log;
import 'io.dart';
import 'system_cache.dart';
import 'utils.dart';
import 'validator/license.dart';
import 'validator/name.dart';
import 'validator/pubspec_field.dart';
@ -39,6 +40,7 @@ abstract class Validator {
static Future<Pair<List<String>, List<String>>> runAll(
Entrypoint entrypoint) {
var validators = [
new LicenseValidator(entrypoint),
new NameValidator(entrypoint),
new PubspecFieldValidator(entrypoint)
];

View file

@ -0,0 +1,28 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library pubspec_field_validator;
import '../entrypoint.dart';
import '../io.dart';
import '../system_cache.dart';
import '../validator.dart';
/// A validator that checks that a LICENSE-like file exists.
class LicenseValidator extends Validator {
LicenseValidator(Entrypoint entrypoint)
: super(entrypoint);
Future validate() {
return listDir(entrypoint.root.dir).transform((files) {
var licenseLike = new RegExp(
r"^([a-zA-Z0-9]+[-_])?(LICENSE|COPYING)(\..*)?$");
if (files.map(basename).some(licenseLike.hasMatch)) return;
errors.add("Your package must have a COPYING or LICENSE file containing "
"an open-source license. For more details, see "
"http://pub.dartlang.org/doc/pub-lish.html.");
});
}
}

View file

@ -15,7 +15,12 @@ import '../../pub/io.dart';
import '../../pub/utils.dart';
main() {
setUp(() => dir(appPath, [libPubspec("test_pkg", "1.0.0")]).scheduleCreate());
setUp(() {
dir(appPath, [
libPubspec("test_pkg", "1.0.0"),
file("LICENSE", "Eh, do what you want.")
]).scheduleCreate();
});
test('with no credentials.json, authenticates and saves credentials.json',
() {

View file

@ -47,7 +47,12 @@ void handleUpload(ScheduledServer server) {
}
main() {
setUp(() => dir(appPath, [libPubspec("test_pkg", "1.0.0")]).scheduleCreate());
setUp(() {
dir(appPath, [
libPubspec("test_pkg", "1.0.0"),
file("LICENSE", "Eh, do what you want.")
]).scheduleCreate();
});
test('archives and uploads a package', () {
var server = new ScheduledServer();

View file

@ -12,6 +12,7 @@ import '../../../pkg/unittest/lib/unittest.dart';
import '../../pub/entrypoint.dart';
import '../../pub/io.dart';
import '../../pub/validator.dart';
import '../../pub/validator/license.dart';
import '../../pub/validator/name.dart';
import '../../pub/validator/pubspec_field.dart';
@ -30,16 +31,49 @@ void expectValidationWarning(ValidatorCreator fn) {
Validator pubspecField(Entrypoint entrypoint) =>
new PubspecFieldValidator(entrypoint);
Validator license(Entrypoint entrypoint) => new LicenseValidator(entrypoint);
Validator name(Entrypoint entrypoint) => new NameValidator(entrypoint);
void scheduleNormalPackage() {
dir(appPath, [
libPubspec("test_pkg", "1.0.0"),
file("LICENSE", "Eh, do what you want.")
]).scheduleCreate();
}
main() {
group('should consider a package valid if it', () {
setUp(scheduleNormalPackage);
test('looks normal', () {
dir(appPath, [libPubspec("test_pkg", "1.0.0")]).scheduleCreate();
expectNoValidationError(license);
expectNoValidationError(pubspecField);
run();
});
test('has a COPYING file', () {
file(join(appPath, 'LICENSE'), '').scheduleDelete();
file(join(appPath, 'COPYING'), '').scheduleCreate();
expectNoValidationError(license);
run();
});
test('has a prefixed LICENSE file', () {
file(join(appPath, 'LICENSE'), '').scheduleDelete();
file(join(appPath, 'MIT_LICENSE'), '').scheduleCreate();
expectNoValidationError(license);
run();
});
test('has a suffixed LICENSE file', () {
file(join(appPath, 'LICENSE'), '').scheduleDelete();
file(join(appPath, 'LICENSE.md'), '').scheduleCreate();
expectNoValidationError(license);
run();
});
test('has "authors" instead of "author"', () {
var package = package("test_pkg", "1.0.0");
package["authors"] = [package.remove("author")];
@ -62,6 +96,8 @@ main() {
});
group('should consider a package invalid if it', () {
setUp(scheduleNormalPackage);
test('is missing the "homepage" field', () {
var package = package("test_pkg", "1.0.0");
package.remove("homepage");
@ -135,6 +171,12 @@ main() {
run();
});
test('has no LICENSE file', () {
file(join(appPath, 'LICENSE'), '').scheduleDelete();
expectValidationError(license);
run();
});
test('has an empty package name', () {
dir(appPath, [libPubspec("", "1.0.0")]).scheduleCreate();
expectValidationError(name);