diff --git a/lib/src/platform_file.dart b/lib/src/platform_file.dart index 2d2882a8..6a4460d0 100644 --- a/lib/src/platform_file.dart +++ b/lib/src/platform_file.dart @@ -54,7 +54,8 @@ class PlatformFile { /// File content as stream final Stream>? readStream; - /// The file size in bytes. + /// The file size in bytes. Defaults to `0` if the file size could not be + /// determined. final int size; /// File extension for this file. diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 7f7687d6..be1c83eb 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -39,7 +39,7 @@ Future createPlatformFile( name: basename(file.path), path: file.path, readStream: readStream, - size: await file.length(), + size: file.existsSync() ? file.lengthSync() : 0, ); Future runExecutableWithArguments( diff --git a/test/common.dart b/test/common.dart index 8183e197..65bab2f1 100644 --- a/test/common.dart +++ b/test/common.dart @@ -1,27 +1,33 @@ import 'dart:io'; setUpTestFiles( - String imageTestFile, - String pdfTestFile, - String yamlTestFile, + String appTestFilePath, + String imageTestFilePath, + String pdfTestFilePath, + String yamlTestFilePath, ) { + // .app files on macOS are actually directories but they are treated as files + Directory(appTestFilePath).createSync(); + File( './test/test_files/franz-michael-schneeberger-unsplash.jpg', - ).copySync(imageTestFile); + ).copySync(imageTestFilePath); File( './test/test_files/test.pdf', - ).copySync(pdfTestFile); + ).copySync(pdfTestFilePath); File( './test/test_files/test.yml', - ).copySync(yamlTestFile); + ).copySync(yamlTestFilePath); } tearDownTestFiles( - String imageTestFile, - String pdfTestFile, - String yamlTestFile, + String appTestFilePath, + String imageTestFilePath, + String pdfTestFilePath, + String yamlTestFilePath, ) { - File(imageTestFile).deleteSync(); - File(pdfTestFile).deleteSync(); - File(yamlTestFile).deleteSync(); + Directory(appTestFilePath).deleteSync(); + File(imageTestFilePath).deleteSync(); + File(pdfTestFilePath).deleteSync(); + File(yamlTestFilePath).deleteSync(); } diff --git a/test/file_picker_utils_test.dart b/test/file_picker_utils_test.dart index bb3b165e..adfb8bb2 100644 --- a/test/file_picker_utils_test.dart +++ b/test/file_picker_utils_test.dart @@ -1,23 +1,60 @@ @TestOn('linux || mac-os') +import 'dart:io'; import 'package:file_picker/src/utils.dart'; import 'package:flutter_test/flutter_test.dart'; import 'common.dart'; void main() { + final appTestFilePath = '/tmp/test_utils.app'; final imageTestFile = '/tmp/test_utils.jpg'; final pdfTestFile = '/tmp/test_utils.pdf'; final yamlTestFile = '/tmp/test_utils.yml'; setUpAll( - () => setUpTestFiles(imageTestFile, pdfTestFile, yamlTestFile), + () => setUpTestFiles( + appTestFilePath, imageTestFile, pdfTestFile, yamlTestFile), ); tearDownAll( - () => tearDownTestFiles(imageTestFile, pdfTestFile, yamlTestFile), + () => tearDownTestFiles( + appTestFilePath, imageTestFile, pdfTestFile, yamlTestFile), ); + group('createPlatformFile()', () { + test('should return an instance of PlatformFile', () async { + final imageFile = File(imageTestFile); + final bytes = imageFile.readAsBytesSync(); + final readStream = imageFile.openRead(); + + final platformFile = + await createPlatformFile(imageFile, bytes, readStream); + + expect(platformFile.bytes, equals(bytes)); + expect(platformFile.name, equals('test_utils.jpg')); + expect(platformFile.readStream, equals(readStream)); + expect(platformFile.size, equals(bytes.length)); + }); + + test( + 'should not throw an exception when picking .app files on macOS (.app files on macOS are actually directories but they are treated as files, similar to .exe files on Windows)', + () async { + final appFile = File(appTestFilePath); + + final platformFile = await createPlatformFile(appFile, null, null); + + expect(platformFile.bytes, equals(null)); + expect(platformFile.name, equals('test_utils.app')); + expect(platformFile.readStream, equals(null)); + expect( + platformFile.size, + equals(0), + reason: 'Expect size to be 0 because .app files are directories.', + ); + }); + }); + group('filePathsToPlatformFiles()', () { test('should transform a list of file paths into a list of PlatformFiles', () async {