Use XML format in PlistParser (#94863)
* Switch to use xml in PlistParser * Review feedbacks: Change dynamic to Object, remove getValueFromFile
This commit is contained in:
@@ -11,10 +11,10 @@ import 'package:flutter_tools/src/convert.dart';
|
||||
import 'package:flutter_tools/src/doctor_validator.dart';
|
||||
import 'package:flutter_tools/src/intellij/intellij_validator.dart';
|
||||
import 'package:flutter_tools/src/ios/plist_parser.dart';
|
||||
import 'package:test/fake.dart';
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../../src/fake_process_manager.dart';
|
||||
import '../../src/fakes.dart';
|
||||
|
||||
final Platform macPlatform = FakePlatform(
|
||||
operatingSystem: 'macos',
|
||||
@@ -373,17 +373,6 @@ void main() {
|
||||
});
|
||||
}
|
||||
|
||||
class FakePlistParser extends Fake implements PlistParser {
|
||||
FakePlistParser(this.values);
|
||||
|
||||
final Map<String, String> values;
|
||||
|
||||
@override
|
||||
String? getValueFromFile(String plistFilePath, String key) {
|
||||
return values[key];
|
||||
}
|
||||
}
|
||||
|
||||
class IntelliJValidatorTestTarget extends IntelliJValidator {
|
||||
IntelliJValidatorTestTarget(String title, String installPath, FileSystem fileSystem)
|
||||
: super(title, installPath, fileSystem: fileSystem, userMessages: UserMessages());
|
||||
|
||||
@@ -33,6 +33,20 @@ const String base64PlistJson =
|
||||
'eyJDRkJ1bmRsZUV4ZWN1dGFibGUiOiJBcHAiLCJDRkJ1bmRsZUlkZW50aWZpZXIiOiJpby5mb'
|
||||
'HV0dGVyLmZsdXR0ZXIuYXBwIn0=';
|
||||
|
||||
const String base64PlistXmlWithComplexDatatypes =
|
||||
'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCFET0NUWVBFIHBsaXN0I'
|
||||
'FBVQkxJQyAiLS8vQXBwbGUvL0RURCBQTElTVCAxLjAvL0VOIiAiaHR0cDovL3d3dy5hcHBsZS'
|
||||
'5jb20vRFREcy9Qcm9wZXJ0eUxpc3QtMS4wLmR0ZCI+CjxwbGlzdCB2ZXJzaW9uPSIxLjAiPgo'
|
||||
'8ZGljdD4KICA8a2V5PkNGQnVuZGxlRXhlY3V0YWJsZTwva2V5PgogIDxzdHJpbmc+QXBwPC9z'
|
||||
'dHJpbmc+CiAgPGtleT5DRkJ1bmRsZUlkZW50aWZpZXI8L2tleT4KICA8c3RyaW5nPmlvLmZsd'
|
||||
'XR0ZXIuZmx1dHRlci5hcHA8L3N0cmluZz4KICA8a2V5PmludFZhbHVlPC9rZXk+CiAgPGludG'
|
||||
'VnZXI+MjwvaW50ZWdlcj4KICA8a2V5PmRvdWJsZVZhbHVlPC9rZXk+CiAgPHJlYWw+MS41PC9'
|
||||
'yZWFsPgogIDxrZXk+YmluYXJ5VmFsdWU8L2tleT4KICA8ZGF0YT5ZV0pqWkE9PTwvZGF0YT4K'
|
||||
'ICA8a2V5PmFycmF5VmFsdWU8L2tleT4KICA8YXJyYXk+CiAgICA8dHJ1ZSAvPgogICAgPGZhb'
|
||||
'HNlIC8+CiAgICA8aW50ZWdlcj4zPC9pbnRlZ2VyPgogIDwvYXJyYXk+CiAgPGtleT5kYXRlVm'
|
||||
'FsdWU8L2tleT4KICA8ZGF0ZT4yMDIxLTEyLTAxVDEyOjM0OjU2WjwvZGF0ZT4KPC9kaWN0Pgo'
|
||||
'8L3BsaXN0Pg==';
|
||||
|
||||
void main() {
|
||||
// The tests herein explicitly don't use `MemoryFileSystem` or a mocked
|
||||
// `ProcessManager` because doing so wouldn't actually test what we want to
|
||||
@@ -62,62 +76,79 @@ void main() {
|
||||
file.deleteSync();
|
||||
});
|
||||
|
||||
testWithoutContext('PlistParser.getValueFromFile works with xml file', () {
|
||||
testWithoutContext('PlistParser.getStringValueFromFile works with xml file', () {
|
||||
file.writeAsBytesSync(base64.decode(base64PlistXml));
|
||||
|
||||
expect(parser.getValueFromFile(file.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(parser.getValueFromFile(file.absolute.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(parser.getStringValueFromFile(file.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(parser.getStringValueFromFile(file.absolute.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(logger.statusText, isEmpty);
|
||||
expect(logger.errorText, isEmpty);
|
||||
}, skip: !platform.isMacOS); // [intended] requires macos tool chain.
|
||||
|
||||
testWithoutContext('PlistParser.getValueFromFile works with binary file', () {
|
||||
testWithoutContext('PlistParser.getStringValueFromFile works with binary file', () {
|
||||
file.writeAsBytesSync(base64.decode(base64PlistBinary));
|
||||
|
||||
expect(parser.getValueFromFile(file.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(parser.getValueFromFile(file.absolute.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(parser.getStringValueFromFile(file.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(parser.getStringValueFromFile(file.absolute.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(logger.statusText, isEmpty);
|
||||
expect(logger.errorText, isEmpty);
|
||||
}, skip: !platform.isMacOS); // [intended] requires macos tool chain.
|
||||
|
||||
testWithoutContext('PlistParser.getValueFromFile works with json file', () {
|
||||
testWithoutContext('PlistParser.getStringValueFromFile works with json file', () {
|
||||
file.writeAsBytesSync(base64.decode(base64PlistJson));
|
||||
|
||||
expect(parser.getValueFromFile(file.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(parser.getValueFromFile(file.absolute.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(parser.getStringValueFromFile(file.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(parser.getStringValueFromFile(file.absolute.path, 'CFBundleIdentifier'), 'io.flutter.flutter.app');
|
||||
expect(logger.statusText, isEmpty);
|
||||
expect(logger.errorText, isEmpty);
|
||||
}, skip: !platform.isMacOS); // [intended] requires macos tool chain.
|
||||
|
||||
testWithoutContext('PlistParser.getValueFromFile returns null for non-existent plist file', () {
|
||||
expect(parser.getValueFromFile('missing.plist', 'CFBundleIdentifier'), null);
|
||||
testWithoutContext('PlistParser.getStringValueFromFile returns null for non-existent plist file', () {
|
||||
expect(parser.getStringValueFromFile('missing.plist', 'CFBundleIdentifier'), null);
|
||||
expect(logger.statusText, isEmpty);
|
||||
expect(logger.errorText, isEmpty);
|
||||
}, skip: !platform.isMacOS); // [intended] requires macos tool chain.
|
||||
|
||||
testWithoutContext('PlistParser.getValueFromFile returns null for non-existent key within plist', () {
|
||||
testWithoutContext('PlistParser.getStringValueFromFile returns null for non-existent key within plist', () {
|
||||
file.writeAsBytesSync(base64.decode(base64PlistXml));
|
||||
|
||||
expect(parser.getValueFromFile(file.path, 'BadKey'), null);
|
||||
expect(parser.getValueFromFile(file.absolute.path, 'BadKey'), null);
|
||||
expect(parser.getStringValueFromFile(file.path, 'BadKey'), null);
|
||||
expect(parser.getStringValueFromFile(file.absolute.path, 'BadKey'), null);
|
||||
expect(logger.statusText, isEmpty);
|
||||
expect(logger.errorText, isEmpty);
|
||||
}, skip: !platform.isMacOS); // [intended] requires macos tool chain.
|
||||
|
||||
testWithoutContext('PlistParser.getValueFromFile returns null for malformed plist file', () {
|
||||
testWithoutContext('PlistParser.getStringValueFromFile returns null for malformed plist file', () {
|
||||
file.writeAsBytesSync(const <int>[1, 2, 3, 4, 5, 6]);
|
||||
|
||||
expect(parser.getValueFromFile(file.path, 'CFBundleIdentifier'), null);
|
||||
expect(parser.getStringValueFromFile(file.path, 'CFBundleIdentifier'), null);
|
||||
expect(logger.statusText, isNotEmpty);
|
||||
expect(logger.errorText, isEmpty);
|
||||
}, skip: !platform.isMacOS); // [intended] requires macos tool chain.
|
||||
|
||||
testWithoutContext('PlistParser.getValueFromFile throws when /usr/bin/plutil is not found', () async {
|
||||
testWithoutContext('PlistParser.getStringValueFromFile throws when /usr/bin/plutil is not found', () async {
|
||||
file.writeAsBytesSync(base64.decode(base64PlistXml));
|
||||
|
||||
expect(
|
||||
() => parser.getValueFromFile('irrelevant.plist', 'ununsed'),
|
||||
() => parser.getStringValueFromFile(file.path, 'unused'),
|
||||
throwsA(isA<FileNotFoundException>()),
|
||||
);
|
||||
expect(logger.statusText, isEmpty);
|
||||
expect(logger.errorText, isEmpty);
|
||||
}, skip: platform.isMacOS); // [intended] requires macos tool chain.
|
||||
|
||||
testWithoutContext('PlistParser.parseFile can handle different datatypes', () async {
|
||||
file.writeAsBytesSync(base64.decode(base64PlistXmlWithComplexDatatypes));
|
||||
final Map<String, Object> values = parser.parseFile(file.path);
|
||||
|
||||
expect(values['CFBundleIdentifier'], 'io.flutter.flutter.app');
|
||||
expect(values['CFBundleIdentifier'], 'io.flutter.flutter.app');
|
||||
expect(values['intValue'], 2);
|
||||
expect(values['doubleValue'], 1.5);
|
||||
expect(values['binaryValue'], base64.decode('YWJjZA=='));
|
||||
expect(values['arrayValue'], <dynamic>[true, false, 3]);
|
||||
expect(values['dateValue'], DateTime.utc(2021, 12, 1, 12, 34, 56));
|
||||
expect(logger.statusText, isEmpty);
|
||||
expect(logger.errorText, isEmpty);
|
||||
}, skip: !platform.isMacOS); // [intended] requires macos tool chain.
|
||||
}
|
||||
|
||||
@@ -289,20 +289,26 @@ class FakeStdio extends Stdio {
|
||||
}
|
||||
|
||||
class FakePlistParser implements PlistParser {
|
||||
final Map<String, dynamic> _underlyingValues = <String, String>{};
|
||||
FakePlistParser([Map<String, Object>? underlyingValues]):
|
||||
_underlyingValues = underlyingValues ?? <String, Object>{};
|
||||
|
||||
void setProperty(String key, dynamic value) {
|
||||
final Map<String, Object> _underlyingValues;
|
||||
|
||||
void setProperty(String key, Object value) {
|
||||
_underlyingValues[key] = value;
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, dynamic> parseFile(String plistFilePath) {
|
||||
String? plistXmlContent(String plistFilePath) => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
Map<String, Object> parseFile(String plistFilePath) {
|
||||
return _underlyingValues;
|
||||
}
|
||||
|
||||
@override
|
||||
String getValueFromFile(String plistFilePath, String key) {
|
||||
return _underlyingValues[key] as String;
|
||||
String? getStringValueFromFile(String plistFilePath, String key) {
|
||||
return _underlyingValues[key] as String?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user