Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Check the description length in pubspec-check
  • Loading branch information
stuartmorgan-g committed Sep 29, 2021
commit c4d398375e6bcce31849b0807a0ec324c6d7fb5b
2 changes: 2 additions & 0 deletions script/tool/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
major version change restriction.
- Improved error handling and error messages in CHANGELOG version checks.
- `license-check` now validates Kotlin files.
- `pubspec-check` now checks that the description is of the pub-recommended
length.

## 0.7.1

Expand Down
28 changes: 28 additions & 0 deletions script/tool/lib/src/pubspec_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ class PubspecCheckCommand extends PackageLoopingCommand {
'${indentation * 2}$_expectedIssueLinkFormat<package label>');
passing = false;
}

final String? descriptionError =
_checkDescription(pubspec, package: package);
if (descriptionError != null) {
printError('$indentation$descriptionError');
passing = false;
}
}

return passing;
Expand Down Expand Up @@ -180,6 +187,27 @@ class PubspecCheckCommand extends PackageLoopingCommand {
return errorMessages;
}

// Validates the "description" field for a package, returning an error
// string if there are any issues.
String? _checkDescription(
Pubspec pubspec, {
required RepositoryPackage package,
}) {
final String? description = pubspec.description;
if (description == null) {
return 'Missing "description"';
}

if (description.length < 60) {
return '"description" is too short. pub.dev recommends package '
'descriptions of 60-180 characters.';
}
if (description.length > 180) {
return '"description" is too long. pub.dev recommends package '
'descriptions of 60-180 characters.';
}
}

bool _checkIssueLink(Pubspec pubspec) {
return pubspec.issueTracker
?.toString()
Expand Down
63 changes: 63 additions & 0 deletions script/tool/test/pubspec_check_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void main() {
bool includeHomepage = false,
bool includeIssueTracker = true,
bool publishable = true,
String? description,
}) {
final String repositoryPath = repositoryPackagesDirRelativePath ?? name;
final String repoLink = 'https://github.com/flutter/'
Expand All @@ -64,8 +65,11 @@ void main() {
final String issueTrackerLink =
'https://github.com/flutter/flutter/issues?'
'q=is%3Aissue+is%3Aopen+label%3A%22p%3A+$name%22';
description ??= 'A test package for validating that the pubspec.yaml '
'follows repo best practices.';
return '''
name: $name
description: $description
${includeRepository ? 'repository: $repoLink' : ''}
${includeHomepage ? 'homepage: $repoLink' : ''}
${includeIssueTracker ? 'issue_tracker: $issueTrackerLink' : ''}
Expand Down Expand Up @@ -327,6 +331,65 @@ ${devDependenciesSection()}
);
});

test('fails when description is too short', () async {
final Directory pluginDirectory = createFakePlugin('plugin', packagesDir);

pluginDirectory.childFile('pubspec.yaml').writeAsStringSync('''
${headerSection('plugin', isPlugin: true, description: 'Too short')}
${environmentSection()}
${flutterSection(isPlugin: true)}
${dependenciesSection()}
${devDependenciesSection()}
''');

Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['pubspec-check'], errorHandler: (Error e) {
commandError = e;
});

expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('"description" is too short. pub.dev recommends package '
'descriptions of 60-180 characters.'),
]),
);
});

test('fails when description is too long', () async {
final Directory pluginDirectory = createFakePlugin('plugin', packagesDir);

const String description = 'This description is too long. It just goes '
'on and on and on and on and on. pub.dev will down-score it because '
'there is just too much here. Someone shoul really cut this down to just '
'the core description so that search results are more useful and the '
'package does not lose pub points.';
pluginDirectory.childFile('pubspec.yaml').writeAsStringSync('''
${headerSection('plugin', isPlugin: true, description: description)}
${environmentSection()}
${flutterSection(isPlugin: true)}
${dependenciesSection()}
${devDependenciesSection()}
''');

Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['pubspec-check'], errorHandler: (Error e) {
commandError = e;
});

expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('"description" is too long. pub.dev recommends package '
'descriptions of 60-180 characters.'),
]),
);
});

test('fails when environment section is out of order', () async {
final Directory pluginDirectory = createFakePlugin('plugin', packagesDir);

Expand Down