Skip to content

Commit

Permalink
Merge pull request #1797 from gagoman/feature/1791
Browse files Browse the repository at this point in the history
Added more verbose error description for upload size validator
  • Loading branch information
nex3 authored Feb 6, 2018
2 parents df4f2e6 + dd53e6e commit 5314f23
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Package {
}

/// Returns whether or not this package is in a Git repo.
bool get _inGitRepo {
bool get inGitRepo {
if (_inGitRepoCache != null) return _inGitRepoCache;

if (dir == null || !git.isInstalled) {
Expand Down Expand Up @@ -242,7 +242,7 @@ class Package {
// path package, since re-parsing a path is very expensive relative to
// string operations.
Iterable<String> files;
if (useGitIgnore && _inGitRepo) {
if (useGitIgnore && inGitRepo) {
// List all files that aren't gitignored, including those not checked in
// to Git. Use [beneath] as the working dir rather than passing it as a
// parameter so that we list a submodule using its own git logic.
Expand Down
18 changes: 16 additions & 2 deletions lib/src/validator/size.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:math' as math;

import '../entrypoint.dart';
import '../io.dart';
import '../validator.dart';

/// The maximum size of the package to upload (100 MB).
Expand All @@ -21,8 +22,21 @@ class SizeValidator extends Validator {
return packageSize.then((size) {
if (size <= _MAX_SIZE) return;
var sizeInMb = (size / math.pow(2, 20)).toStringAsPrecision(4);
errors.add("Your package is $sizeInMb MB. Hosted packages must be "
"smaller than 100 MB.");
// Current implementation of Package.listFiles skips hidden files
var ignoreExists = fileExists(entrypoint.root.path('.gitignore'));

var error = new StringBuffer("Your package is $sizeInMb MB. Hosted "
"packages must be smaller than 100 MB.");

if (ignoreExists && !entrypoint.root.inGitRepo) {
error.write(" Your .gitignore has no effect since your project "
"does not appear to be in version control.");
} else if (!ignoreExists && entrypoint.root.inGitRepo) {
error.write(" Consider adding a .gitignore to avoid including "
"temporary files.");
}

errors.add(error.toString());
});
}
}
49 changes: 45 additions & 4 deletions test/validator/size_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,56 @@ ValidatorCreator size(int size) {
return (entrypoint) => new SizeValidator(entrypoint, new Future.value(size));
}

expectSizeValidationError(Matcher matcher) {
expect(validatePackage(size(100 * math.pow(2, 20) + 1)),
completion(pairOf(contains(matcher), anything)));
}

main() {
setUp(d.validPackage.create);
test('considers a package valid if it is <= 100 MB', () async {
await d.validPackage.create();

test('considers a package valid if it is <= 100 MB', () {
expectNoValidationError(size(100));
expectNoValidationError(size(100 * math.pow(2, 20)));
});

test('considers a package invalid if it is more than 100 MB', () {
expectValidationError(size(100 * math.pow(2, 20) + 1));
group('considers a package invalid if it is more than 100 MB', () {
test('package is not under source control and no .gitignore exists',
() async {
await d.validPackage.create();

expectSizeValidationError(
equals("Your package is 100.0 MB. Hosted packages must "
"be smaller than 100 MB."));
});

test('package is not under source control and .gitignore exists', () async {
await d.validPackage.create();
await d.dir(appPath, [d.file('.gitignore', 'ignored')]).create();

expectSizeValidationError(allOf(
contains("Hosted packages must be smaller than 100 MB."),
contains("Your .gitignore has no effect since your project "
"does not appear to be in version control.")));
});

test('package is under source control and no .gitignore exists', () async {
await d.validPackage.create();
await d.git(appPath).create();

expectSizeValidationError(allOf(
contains("Hosted packages must be smaller than 100 MB."),
contains("Consider adding a .gitignore to avoid including "
"temporary files.")));
});

test('package is under source control and .gitignore exists', () async {
await d.validPackage.create();
await d.git(appPath, [d.file('.gitignore', 'ignored')]).create();

expectSizeValidationError(
equals("Your package is 100.0 MB. Hosted packages must "
"be smaller than 100 MB."));
});
});
}

0 comments on commit 5314f23

Please sign in to comment.