From ffd1783656324460af608e8ce146f3a1ee21f47e Mon Sep 17 00:00:00 2001 From: evanweible-wf Date: Wed, 14 Oct 2015 15:35:33 -0500 Subject: [PATCH] Copy License: trim leading/trailing empty lines --- lib/src/tasks/copy_license/api.dart | 13 +++++++++++++ .../copy_license/license_with_empty_lines/LICENSE | 6 ++++++ .../license_with_empty_lines/lib/main.dart | 5 +++++ .../license_with_empty_lines/pubspec.yaml | 5 +++++ test/integration/copy_license_test.dart | 14 ++++++++++++++ 5 files changed, 43 insertions(+) create mode 100644 test/fixtures/copy_license/license_with_empty_lines/LICENSE create mode 100644 test/fixtures/copy_license/license_with_empty_lines/lib/main.dart create mode 100644 test/fixtures/copy_license/license_with_empty_lines/pubspec.yaml diff --git a/lib/src/tasks/copy_license/api.dart b/lib/src/tasks/copy_license/api.dart index cb48d840..7d88a306 100644 --- a/lib/src/tasks/copy_license/api.dart +++ b/lib/src/tasks/copy_license/api.dart @@ -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; @@ -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 affectedFiles = []; final Future done = new Future.value(); diff --git a/test/fixtures/copy_license/license_with_empty_lines/LICENSE b/test/fixtures/copy_license/license_with_empty_lines/LICENSE new file mode 100644 index 00000000..bc4b05d9 --- /dev/null +++ b/test/fixtures/copy_license/license_with_empty_lines/LICENSE @@ -0,0 +1,6 @@ + + +This license has empty leading and trailing lines. + +They should be trimmed to avoid empty leading and trailing comment lines. + diff --git a/test/fixtures/copy_license/license_with_empty_lines/lib/main.dart b/test/fixtures/copy_license/license_with_empty_lines/lib/main.dart new file mode 100644 index 00000000..48690063 --- /dev/null +++ b/test/fixtures/copy_license/license_with_empty_lines/lib/main.dart @@ -0,0 +1,5 @@ +library copy_license_no_licenses; + +class Obj extends Object { + // Useful. +} \ No newline at end of file diff --git a/test/fixtures/copy_license/license_with_empty_lines/pubspec.yaml b/test/fixtures/copy_license/license_with_empty_lines/pubspec.yaml new file mode 100644 index 00000000..d801bd9e --- /dev/null +++ b/test/fixtures/copy_license/license_with_empty_lines/pubspec.yaml @@ -0,0 +1,5 @@ +name: copy_license_has_licenses +version: 0.0.0 +dev_dependencies: + dart_dev: + path: ../../../.. \ No newline at end of file diff --git a/test/integration/copy_license_test.dart b/test/integration/copy_license_test.dart index 20a97c99..bda0b2e0 100644 --- a/test/integration/copy_license_test.dart +++ b/test/integration/copy_license_test.dart @@ -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 createTemporaryProject(String source) async { String tempProject = '${source}_temp'; @@ -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); + }); }); }