Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions lib/src/tasks/copy_license/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ CopyLicenseTask copyLicense(
'License file "$licensePath" does not exist.');

String licenseContents = license.readAsStringSync();
licenseContents = trimLeadingAndTrailingEmptyLines(licenseContents);

directories.forEach((path) {
Directory dir = new Directory(path);
if (!dir.existsSync()) return;
Expand Down Expand Up @@ -100,6 +102,17 @@ String licenseForFileType(File file, String license) {
return '$opening$l$closing\n';
}

String trimLeadingAndTrailingEmptyLines(String input) {
var lines = input.split('\n');
while (lines.first.trim().isEmpty) {
lines.removeAt(0);
}
while (lines.last.trim().isEmpty) {
lines.removeLast();
}
return lines.join('\n');
}

class CopyLicenseTask extends Task {
List<String> affectedFiles = [];
final Future done = new Future.value();
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/copy_license/license_with_empty_lines/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


This license has empty leading and trailing lines.

They should be trimmed to avoid empty leading and trailing comment lines.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library copy_license_no_licenses;

class Obj extends Object {
// Useful.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: copy_license_has_licenses
version: 0.0.0
dev_dependencies:
dart_dev:
path: ../../../..
14 changes: 14 additions & 0 deletions test/integration/copy_license_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const String projectWithLicenses = 'test/fixtures/copy_license/has_licenses';
const String projectWithoutLicenseFile =
'test/fixtures/copy_license/no_license_file';
const String projectWithoutLicenses = 'test/fixtures/copy_license/no_licenses';
const String projectWithUntrimmedLicense =
'test/fixtures/copy_license/license_with_empty_lines';

Future<String> createTemporaryProject(String source) async {
String tempProject = '${source}_temp';
Expand Down Expand Up @@ -95,5 +97,17 @@ void main() {
expect(await copyLicense(projectPath), unorderedEquals(expectedFiles));
deleteTemporaryProject(projectPath);
});

test('should trim leading and trailing empty lines from license', () async {
String projectPath =
await createTemporaryProject(projectWithUntrimmedLicense);
expect(await copyLicense(projectPath), isNotEmpty);
String contents =
new File('$projectPath/lib/main.dart').readAsStringSync();
var lines = contents.split('\n');
var licenseLines = lines.where((line) => line.startsWith('//'));
expect(licenseLines.length, equals(3));
deleteTemporaryProject(projectPath);
});
});
}