[flutter_tools] allow applications to specify additional license files to be bundled into the application NOTICES automatically (#73430)

This commit is contained in:
Jonah Williams
2021-01-14 16:59:04 -08:00
committed by GitHub
parent 2a64fdbc43
commit 8d72307c47
5 changed files with 393 additions and 106 deletions

View File

@@ -30,6 +30,7 @@ void main() {
expect(flutterManifest.fontsDescriptor, isEmpty);
expect(flutterManifest.fonts, isEmpty);
expect(flutterManifest.assets, isEmpty);
expect(flutterManifest.additionalLicenses, isEmpty);
});
testWithoutContext('FlutterManifest is null when the pubspec.yaml file is not a map', () async {
@@ -1082,6 +1083,65 @@ flutter:
expect(logger.errorText,
contains('Cannot find the `flutter.plugin.platforms` key in the `pubspec.yaml` file. '));
});
testWithoutContext('FlutterManifest can specify additional LICENSE files', () async {
const String manifest = '''
name: test
dependencies:
flutter:
sdk: flutter
flutter:
licenses:
- foo.txt
''';
final BufferLogger logger = BufferLogger.test();
final FlutterManifest flutterManifest = FlutterManifest.createFromString(
manifest,
logger: logger,
);
expect(flutterManifest.additionalLicenses, <String>['foo.txt']);
});
testWithoutContext('FlutterManifest can validate incorrect licenses key', () async {
const String manifest = '''
name: test
dependencies:
flutter:
sdk: flutter
flutter:
licenses: foo.txt
''';
final BufferLogger logger = BufferLogger.test();
final FlutterManifest flutterManifest = FlutterManifest.createFromString(
manifest,
logger: logger,
);
expect(flutterManifest, null);
expect(logger.errorText, 'Expected "licenses" to be a list of files, but got foo.txt (String)\n');
});
testWithoutContext('FlutterManifest validates individual list items', () async {
const String manifest = '''
name: test
dependencies:
flutter:
sdk: flutter
flutter:
licenses:
- foo.txt
- bar: fizz
''';
final BufferLogger logger = BufferLogger.test();
final FlutterManifest flutterManifest = FlutterManifest.createFromString(
manifest,
logger: logger,
);
expect(flutterManifest, null);
expect(logger.errorText, 'Expected "licenses" to be a list of files, but element 1 was a YamlMap\n');
});
}
Matcher matchesManifest({

View File

@@ -3,9 +3,9 @@
// found in the LICENSE file.
import 'package:file/memory.dart';
import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/license_collector.dart';
import 'package:package_config/package_config.dart';
import 'package:package_config/package_config_types.dart';
@@ -290,7 +290,7 @@ void main() {
}
));
final PackageConfig packageConfig = await loadPackageConfig(packageConfigFile.absolute);
final LicenseResult result = licenseCollector.obtainLicenses(packageConfig);
final LicenseResult result = licenseCollector.obtainLicenses(packageConfig, <String, List<File>>{});
// All included licenses are combined in the result.
expect(result.combinedLicenses, contains(_kApacheLicense));
@@ -310,4 +310,125 @@ void main() {
'/fizz/LICENSE'
]));
});
testWithoutContext('includes additional LICENSE files as specified by pubspec.yaml', () async {
fileSystem.file('foo/NOTICES')
..createSync(recursive: true)
..writeAsStringSync(_kMitLicense);
fileSystem.file('bar/NOTICES')
..createSync(recursive: true)
..writeAsStringSync(_kApacheLicense);
fileSystem.file('foo.txt').writeAsStringSync('foo.txt');
fileSystem.file('bar.txt').writeAsStringSync('bar.txt');
final File packageConfigFile = fileSystem.file('package_config.json')
..writeAsStringSync(json.encode(
<String, Object>{
'configVersion': 2,
'packages': <Object>[
<String, Object>{
'name': 'foo',
'rootUri': 'file:///foo/',
'packageUri': 'lib/',
'languageVersion': '2.2'
},
<String, Object>{
'name': 'bar',
'rootUri': 'file:///bar/',
'packageUri': 'lib/',
'languageVersion': '2.2'
},
],
}
));
final PackageConfig packageConfig = await loadPackageConfig(packageConfigFile.absolute);
final LicenseResult result = licenseCollector.obtainLicenses(packageConfig, <String, List<File>>{
'foo': <File>[fileSystem.file('foo.txt').absolute],
'bar': <File>[fileSystem.file('bar.txt').absolute],
});
// Additional license files are included in the result.
expect(result.combinedLicenses, contains(_kApacheLicense));
expect(result.combinedLicenses, contains(_kMitLicense));
expect(result.combinedLicenses, contains('foo.txt'));
expect(result.combinedLicenses, contains('bar.txt'));
// All input licenses included in result.
final Iterable<String> filePaths = result.dependencies.map((File file) => file.path);
expect(filePaths, unorderedEquals(<String>[
'/foo/NOTICES',
'/bar/NOTICES',
'/foo.txt',
'/bar.txt',
]));
});
testWithoutContext('Returns a LicenseResult with an error message if an additional LICENSE file does not exist', () async {
fileSystem.file('foo/NOTICES')
..createSync(recursive: true)
..writeAsStringSync(_kMitLicense);
final File packageConfigFile = fileSystem.file('package_config.json')
..writeAsStringSync(json.encode(
<String, Object>{
'configVersion': 2,
'packages': <Object>[
<String, Object>{
'name': 'foo',
'rootUri': 'file:///foo/',
'packageUri': 'lib/',
'languageVersion': '2.2'
},
],
}
));
final PackageConfig packageConfig = await loadPackageConfig(packageConfigFile.absolute);
final LicenseResult licenseResult = licenseCollector.obtainLicenses(packageConfig, <String, List<File>>{
'foo': <File>[fileSystem.file('foo.txt').absolute, fileSystem.file('foo_2.txt').absolute], // Files do not exist.
});
expect(licenseResult.combinedLicenses, '');
expect(licenseResult.dependencies, isEmpty);
expect(licenseResult.errorMessages, <String>[
'package foo specified an additional license at /foo.txt, but this file does not exist.',
'package foo specified an additional license at /foo_2.txt, but this file does not exist.'
]);
});
testWithoutContext('Returns a LicenseResult with an error message if an additional license file is not valid utf8', () async {
fileSystem.file('foo/NOTICES')
..createSync(recursive: true)
..writeAsStringSync(_kMitLicense);
fileSystem.file('foo.txt')
..createSync(recursive: true)
..writeAsBytesSync(<int>[0xFFFE]);
final File packageConfigFile = fileSystem.file('package_config.json')
..writeAsStringSync(json.encode(
<String, Object>{
'configVersion': 2,
'packages': <Object>[
<String, Object>{
'name': 'foo',
'rootUri': 'file:///foo/',
'packageUri': 'lib/',
'languageVersion': '2.2'
},
],
}
));
final PackageConfig packageConfig = await loadPackageConfig(packageConfigFile.absolute);
final LicenseResult licenseResult = licenseCollector.obtainLicenses(packageConfig, <String, List<File>>{
'foo': <File>[fileSystem.file('foo.txt').absolute],
});
expect(licenseResult.combinedLicenses, '');
expect(licenseResult.dependencies, isEmpty);
expect(licenseResult.errorMessages.single,
'package foo specified an additional license at /foo.txt, but this file could not be read:'
'\nFormatException: Invalid UTF-8 byte (at offset 0)',
);
});
}