TF-1822: Write test for all func of FileUtils class

(cherry picked from commit 562f151134416c674467e5a59919ea37adbb7e5f)
This commit is contained in:
HuyNguyen
2023-05-08 23:28:40 +07:00
committed by Dat Vu
parent be681cb7a7
commit f3429619aa
4 changed files with 67 additions and 20 deletions
+16 -10
View File
@@ -6,7 +6,7 @@ import 'package:path_provider/path_provider.dart';
class FileUtils {
static Future<String> _getInternalStorageDirPath(
Future<String> _getInternalStorageDirPath(
String nameFile,
{
String? folderPath,
@@ -14,25 +14,32 @@ class FileUtils {
}
) async {
if (!BuildUtils.isWeb) {
String fileDirectory = (await getApplicationDocumentsDirectory()).absolute.path;
if (folderPath != null) {
fileDirectory = '$fileDirectory.$folderPath';
fileDirectory = '$fileDirectory/$folderPath';
}
fileDirectory = '$fileDirectory.$nameFile';
Directory directory = Directory(fileDirectory);
if (!await directory.exists()) {
await directory.create(recursive: true);
}
fileDirectory = '$fileDirectory/$nameFile';
if (extensionFile != null) {
fileDirectory = '$fileDirectory.$extensionFile';
}
return fileDirectory;
} else {
throw DeviceNotSupportedException();
}
}
static Future<File> saveToFile(
Future<File> saveToFile(
String nameFile,
String content,
{
@@ -46,20 +53,19 @@ class FileUtils {
extensionFile: extensionFile
);
final file = File('$internalStorageDirPath.txt');
final file = File(internalStorageDirPath);
log("FileUtils()::saveToFile: $file");
return await file.writeAsString(content, mode: FileMode.append);
}
static Future<String> getFromFile(
Future<String> getFromFile(
String nameFile,
{
String? folderPath,
String? extensionFile
}
) async {
String emailContent = '';
final internalStorageDirPath = await _getInternalStorageDirPath(
nameFile,
@@ -67,8 +73,8 @@ class FileUtils {
extensionFile: extensionFile
);
final file = File('$internalStorageDirPath.txt');
emailContent = await file.readAsString();
final file = File(internalStorageDirPath);
final emailContent = await file.readAsString();
log("FileUtils()::getFromFile: $emailContent");
-8
View File
@@ -121,14 +121,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.5"
external_path:
dependency: "direct main"
description:
name: external_path
sha256: "2095c626fbbefe70d5a4afc9b1137172a68ee2c276e51c3c1283394485bea8f4"
url: "https://pub.dev"
source: hosted
version: "1.0.3"
fake_async:
dependency: transitive
description:
-2
View File
@@ -65,8 +65,6 @@ dependencies:
path_provider: 2.0.13
external_path: 1.0.3
path_provider_platform_interface: 2.0.1
# flutter_test from sdk depends on collection 1.17.0
+51
View File
@@ -0,0 +1,51 @@
import 'package:core/utils/file_utils.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
const String kApplicationDocumentsPath = 'applicationDocumentsPath';
void main() {
const fileName = 'test_file';
const fileContent = 'Hello, World!';
group('fileUtils test', () {
setUp(() async {
PathProviderPlatform.instance = FakePathProviderPlatform();
});
test('Store private HTLM String to File', () async {
final file = await FileUtils().saveToFile(fileName, fileContent);
expect(await file.exists(), equals(true));
expect(await file.readAsString(), equals(fileContent));
file.delete();
});
test('Get HTML String from File Private', () async {
/// Create a temporary file that will be deleted after `getFromFile` is done
final file = await FileUtils().saveToFile(fileName, fileContent);
final htmlString = await FileUtils().getFromFile(fileName);
expect(htmlString.isNotEmpty, equals(true));
expect(htmlString, equals(fileContent));
file.delete();
});
});
}
class FakePathProviderPlatform extends Fake
with MockPlatformInterfaceMixin
implements PathProviderPlatform {
@override
Future<String?> getApplicationDocumentsPath() async {
return kApplicationDocumentsPath;
}
}