Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Remove leading dot and warn users about it
Browse files Browse the repository at this point in the history
  • Loading branch information
tugorez committed Feb 20, 2021
1 parent 0f58fa2 commit 2e9a316
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
37 changes: 20 additions & 17 deletions packages/file_selector/file_selector/lib/file_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Future<XFile> openFile({
String initialDirectory,
String confirmButtonText,
}) {
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
_verifyTypeGroups(acceptedTypeGroups);
return FileSelectorPlatform.instance.openFile(
acceptedTypeGroups: acceptedTypeGroups,
initialDirectory: initialDirectory,
Expand All @@ -27,7 +27,7 @@ Future<List<XFile>> openFiles({
String initialDirectory,
String confirmButtonText,
}) {
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
_verifyTypeGroups(acceptedTypeGroups);
return FileSelectorPlatform.instance.openFiles(
acceptedTypeGroups: acceptedTypeGroups,
initialDirectory: initialDirectory,
Expand All @@ -41,7 +41,7 @@ Future<String> getSavePath({
String suggestedName,
String confirmButtonText,
}) async {
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
_verifyTypeGroups(acceptedTypeGroups);
return FileSelectorPlatform.instance.getSavePath(
acceptedTypeGroups: acceptedTypeGroups,
initialDirectory: initialDirectory,
Expand All @@ -58,21 +58,24 @@ Future<String> getDirectoryPath({
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
}

List<XTypeGroup> _verifyTypeGroups(List<XTypeGroup> groups) {
if (groups == null) return groups;
void _verifyTypeGroups(List<XTypeGroup> groups) {
if (groups == null) return;
for (var i = 0; i < groups.length; i++) {
if (groups[i] == null || groups[i].extensions == null) continue;
for (var j = 0; j < groups[i].extensions.length; j++) {
if (groups[i].extensions[j] == null) continue;
if (groups[i].extensions[j].startsWith('.')) {
if (kDebugMode) {
print('acceptedTypeGroups[${i}].extensions[${j}]'
' with value "${groups[i].extensions[j]} is invalid.'
' Please remove the leading dot.');
}
groups[i].extensions[j] = groups[i].extensions[j].substring(1);
}
_verifyExtensions(groups[i], i);
}
}

void _verifyExtensions(XTypeGroup group, int groupIndex) {
if (group == null || group.extensions == null) return;
for (var extIndex = 0; extIndex < group.extensions.length; extIndex++) {
if (group.extensions[extIndex] == null) continue;
if (!group.extensions[extIndex].startsWith('.')) continue;
if (kDebugMode) {
print('acceptedTypeGroups[${groupIndex}].extensions[${extIndex}]'
' with value "${group.extensions[extIndex]} is invalid.'
' We are removing the leading dot and therefore mutating acceptedTypeGroups.'
' Please fix it.');
}
group.extensions[extIndex] = group.extensions[extIndex].substring(1);
}
return groups;
}
39 changes: 33 additions & 6 deletions packages/file_selector/file_selector/test/file_selector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,18 @@ void main() {
});

test('works with an extension with leading dot', () async {
final correctTypeGroups = [
XTypeGroup(label: 'images', extensions: [
'jpg',
'png',
]),
];
// We consider this correct because at least one extension has a leading dot.
final incorrectTypeGroups = [
XTypeGroup(label: 'images', extensions: [
'.jpg',
'.png',
]),
];

//ignore: unawaited_futures
openFile(acceptedTypeGroups: incorrectTypeGroups);
verify(mock.openFile(acceptedTypeGroups: incorrectTypeGroups));
expect(incorrectTypeGroups[0].extensions, ['jpg', 'png']);
});
});

Expand Down Expand Up @@ -149,6 +146,21 @@ void main() {
final files = await openFiles(acceptedTypeGroups: acceptedTypeGroups);
expect(files, expectedFiles);
});

test('works with an extension with leading dot', () async {
// We consider this correct because at least one extension has a leading dot.
final incorrectTypeGroups = [
XTypeGroup(label: 'images', extensions: [
'.jpg',
'.png',
]),
];

// ignore: unawaited_futures
openFiles(acceptedTypeGroups: incorrectTypeGroups);
verify(mock.openFiles(acceptedTypeGroups: incorrectTypeGroups));
expect(incorrectTypeGroups[0].extensions, ['jpg', 'png']);
});
});

group('getSavePath', () {
Expand Down Expand Up @@ -212,6 +224,21 @@ void main() {
final savePath = await getSavePath(suggestedName: suggestedName);
expect(savePath, expectedSavePath);
});

test('works with an extension with leading dot', () async {
// We consider this correct because at least one extension has a leading dot.
final incorrectTypeGroups = [
XTypeGroup(label: 'images', extensions: [
'.jpg',
'.png',
]),
];

// ignore: unawaited_futures
getSavePath(acceptedTypeGroups: incorrectTypeGroups);
verify(mock.getSavePath(acceptedTypeGroups: incorrectTypeGroups));
expect(incorrectTypeGroups[0].extensions, ['jpg', 'png']);
});
});

group('getDirectoryPath', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,4 @@ class XTypeGroup {
'webWildCards': webWildCards,
};
}

bool operator ==(o) =>
label == o.label &&
listEquals(extensions, o.extensions) &&
listEquals(mimeTypes, o.mimeTypes) &&
listEquals(macUTIs, o.macUTIs) &&
listEquals(webWildCards, o.webWildCards);
}

0 comments on commit 2e9a316

Please sign in to comment.