From c559c5cc6b4744d242730b7fe4b7635f5a35e94f Mon Sep 17 00:00:00 2001 From: appflowy Date: Sat, 19 Feb 2022 16:48:57 +0800 Subject: [PATCH] feat: config bloc test --- frontend/Makefile.toml | 5 +- .../app_flowy/packages/flowy_sdk/lib/ffi.dart | 22 +++-- frontend/app_flowy/pubspec.lock | 84 +++++++++++++++++++ frontend/app_flowy/pubspec.yaml | 1 + frontend/app_flowy/test/util/test_env.dart | 42 ++++++++++ frontend/app_flowy/test/widget_test.dart | 1 - .../app_flowy/test/workspace_bloc_test.dart | 30 +++++++ frontend/scripts/makefile/desktop.toml | 52 ++++++------ frontend/scripts/makefile/protobuf.toml | 13 --- 9 files changed, 204 insertions(+), 46 deletions(-) create mode 100644 frontend/app_flowy/test/util/test_env.dart delete mode 100644 frontend/app_flowy/test/widget_test.dart create mode 100644 frontend/app_flowy/test/workspace_bloc_test.dart diff --git a/frontend/Makefile.toml b/frontend/Makefile.toml index f7c47820cd..eacdf7d00f 100644 --- a/frontend/Makefile.toml +++ b/frontend/Makefile.toml @@ -14,6 +14,7 @@ RUST_LOG = "info" CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = true CARGO_MAKE_CRATE_FS_NAME = "dart_ffi" CARGO_MAKE_CRATE_NAME = "dart-ffi" +LIB_NAME = "dart_ffi" VERSION = "0.0.3" FEATURES = "flutter" PRODUCT_NAME = "AppFlowy" @@ -31,6 +32,8 @@ RUST_COMPILE_TARGET = "x86_64-apple-darwin" BUILD_FLAG = "debug" FLUTTER_OUTPUT_DIR = "Debug" PRODUCT_EXT = "app" +CRATE_TYPE = "cdylib" +SDK_EXT = "dylib" [env.production-mac-aarch64] BUILD_FLAG = "release" @@ -39,6 +42,7 @@ RUST_COMPILE_TARGET = "aarch64-apple-darwin" FLUTTER_OUTPUT_DIR = "Release" PRODUCT_EXT = "app" APP_ENVIRONMENT = "production" +CRATE_TYPE = "staticlib" [env.production-mac-x86] BUILD_FLAG = "release" @@ -153,7 +157,6 @@ script = [ ] script_runner = "@duckscript" - [tasks.test-build] condition = { env_set = [ "FLUTTER_FLOWY_SDK_PATH"] } script = [ diff --git a/frontend/app_flowy/packages/flowy_sdk/lib/ffi.dart b/frontend/app_flowy/packages/flowy_sdk/lib/ffi.dart index 71ea1b7286..f7d4b5b2a4 100644 --- a/frontend/app_flowy/packages/flowy_sdk/lib/ffi.dart +++ b/frontend/app_flowy/packages/flowy_sdk/lib/ffi.dart @@ -12,12 +12,22 @@ final DynamicLibrary _dl = _open(); /// Reference to the Dynamic Library, it should be only used for low-level access final DynamicLibrary dl = _dl; DynamicLibrary _open() { - if (Platform.isAndroid) return DynamicLibrary.open('libdart_ffi.so'); - if (Platform.isMacOS) return DynamicLibrary.executable(); - if (Platform.isIOS) return DynamicLibrary.executable(); - if (Platform.isWindows) return DynamicLibrary.open('dart_ffi.dll'); - if (Platform.isLinux) return DynamicLibrary.open('libdart_ffi.so'); - throw UnsupportedError('This platform is not supported.'); + if (Platform.environment.containsKey('FLUTTER_TEST')) { + final prefix = "${Directory.systemTemp.path}/appflowy"; + if (Platform.isLinux) return DynamicLibrary.open('${prefix}/libdart_ffi.so'); + if (Platform.isAndroid) return DynamicLibrary.open('${prefix}/libdart_ffi.so'); + if (Platform.isMacOS) return DynamicLibrary.open('${prefix}/libdart_ffi.dylib'); + if (Platform.isIOS) return DynamicLibrary.open('${prefix}/libdart_ffi.dylib'); + if (Platform.isWindows) return DynamicLibrary.open('${prefix}/dart_ffi.dll'); + throw UnsupportedError('This platform is not supported.'); + } else { + if (Platform.isLinux) return DynamicLibrary.open('libdart_ffi.so'); + if (Platform.isAndroid) return DynamicLibrary.open('libdart_ffi.so'); + if (Platform.isMacOS) return DynamicLibrary.executable(); + if (Platform.isIOS) return DynamicLibrary.executable(); + if (Platform.isWindows) return DynamicLibrary.open('dart_ffi.dll'); + throw UnsupportedError('This platform is not supported.'); + } } /// C function `async_event`. diff --git a/frontend/app_flowy/pubspec.lock b/frontend/app_flowy/pubspec.lock index 0a8aaf79d7..f8d24d0552 100644 --- a/frontend/app_flowy/pubspec.lock +++ b/frontend/app_flowy/pubspec.lock @@ -43,6 +43,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "8.0.2" + bloc_test: + dependency: "direct dev" + description: + name: bloc_test + url: "https://pub.dartlang.org" + source: hosted + version: "9.0.2" boolean_selector: dependency: transitive description: @@ -211,6 +218,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.1" + coverage: + dependency: transitive + description: + name: coverage + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" cross_file: dependency: transitive description: @@ -702,6 +716,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.1" + mocktail: + dependency: transitive + description: + name: mocktail + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0" nested: dependency: transitive description: @@ -716,6 +737,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.4.2" + node_preamble: + dependency: transitive + description: + name: node_preamble + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" package_config: dependency: transitive description: @@ -982,6 +1010,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.0" + shelf_packages_handler: + dependency: transitive + description: + name: shelf_packages_handler + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + shelf_static: + dependency: transitive + description: + name: shelf_static + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" shelf_web_socket: dependency: transitive description: @@ -1008,6 +1050,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.1" + source_map_stack_trace: + dependency: transitive + description: + name: source_map_stack_trace + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + source_maps: + dependency: transitive + description: + name: source_maps + url: "https://pub.dartlang.org" + source: hosted + version: "0.10.10" source_span: dependency: transitive description: @@ -1071,6 +1127,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.0" + test: + dependency: transitive + description: + name: test + url: "https://pub.dartlang.org" + source: hosted + version: "1.19.5" test_api: dependency: transitive description: @@ -1078,6 +1141,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.4.8" + test_core: + dependency: transitive + description: + name: test_core + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.9" textstyle_extensions: dependency: transitive description: @@ -1211,6 +1281,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.6" + vm_service: + dependency: transitive + description: + name: vm_service + url: "https://pub.dartlang.org" + source: hosted + version: "7.5.0" watcher: dependency: transitive description: @@ -1225,6 +1302,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.0" + webkit_inspection_protocol: + dependency: transitive + description: + name: webkit_inspection_protocol + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" win32: dependency: transitive description: diff --git a/frontend/app_flowy/pubspec.yaml b/frontend/app_flowy/pubspec.yaml index 54155390a5..31721eb5d1 100644 --- a/frontend/app_flowy/pubspec.yaml +++ b/frontend/app_flowy/pubspec.yaml @@ -80,6 +80,7 @@ dev_dependencies: sdk: flutter build_runner: freezed: "^0.14.2" + bloc_test: ^9.0.2 # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is diff --git a/frontend/app_flowy/test/util/test_env.dart b/frontend/app_flowy/test/util/test_env.dart new file mode 100644 index 0000000000..34070894c8 --- /dev/null +++ b/frontend/app_flowy/test/util/test_env.dart @@ -0,0 +1,42 @@ +import 'package:app_flowy/startup/startup.dart'; +import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart'; +import 'package:easy_localization/easy_localization.dart'; +import 'package:flowy_infra/uuid.dart'; +import 'package:flowy_sdk/log.dart'; +import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart'; +import 'package:flutter/material.dart'; + +class FlowyTest { + static Future setup() async { + WidgetsFlutterBinding.ensureInitialized(); + // await EasyLocalization.ensureInitialized(); + + System.run(FlowyTestApp()); + return FlowyTest(); + } + + Future signIn() async { + final authRepo = getIt(); + const password = "AppFlowy123@"; + final uid = uuid(); + final userEmail = "$uid@appflowy.io"; + final result = await authRepo.signUp( + name: "FlowyTestUser", + password: password, + email: userEmail, + ); + return result.fold( + (user) => user, + (error) { + throw StateError("$error"); + }, + ); + } +} + +class FlowyTestApp implements EntryPoint { + @override + Widget create() { + return Container(); + } +} diff --git a/frontend/app_flowy/test/widget_test.dart b/frontend/app_flowy/test/widget_test.dart deleted file mode 100644 index 8b13789179..0000000000 --- a/frontend/app_flowy/test/widget_test.dart +++ /dev/null @@ -1 +0,0 @@ - diff --git a/frontend/app_flowy/test/workspace_bloc_test.dart b/frontend/app_flowy/test/workspace_bloc_test.dart new file mode 100644 index 0000000000..ef19874593 --- /dev/null +++ b/frontend/app_flowy/test/workspace_bloc_test.dart @@ -0,0 +1,30 @@ +import 'package:app_flowy/startup/startup.dart'; +import 'package:app_flowy/workspace/application/workspace/welcome_bloc.dart'; +import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:bloc_test/bloc_test.dart'; + +import 'util/test_env.dart'; + +void main() { + UserProfile? userInfo; + setUpAll(() async { + final flowyTest = await FlowyTest.setup(); + userInfo = await flowyTest.signIn(); + }); + + group('WelcomeBloc', () { + blocTest( + "create workspace", + build: () => getIt(param1: userInfo), + act: (bloc) { + bloc.add(const WelcomeEvent.initial()); + }, + wait: const Duration(seconds: 2), + verify: (bloc) { + assert(bloc.state.isLoading == false); + assert((bloc.state.workspaces.length) == 1); + }, + ); + }); +} diff --git a/frontend/scripts/makefile/desktop.toml b/frontend/scripts/makefile/desktop.toml index 3b4f8aed4e..06b41fe68b 100644 --- a/frontend/scripts/makefile/desktop.toml +++ b/frontend/scripts/makefile/desktop.toml @@ -16,17 +16,17 @@ linux_alias = "flowy-sdk-dev-linux" [tasks.flowy-sdk-dev-macos] category = "Build" dependencies = ["env_check"] -run_task = { name = ["setup-crate-type","sdk-build", "post-desktop", "restore-crate-type"] } +run_task = { name = ["setup-crate-type","sdk-build", "post-desktop", "restore-crate-type", "copy-to-sys-tmpdir"] } [tasks.flowy-sdk-dev-windows] category = "Build" dependencies = ["env_check"] -run_task = { name = ["setup-crate-type","sdk-build", "post-desktop", "restore-crate-type"] } +run_task = { name = ["setup-crate-type","sdk-build", "post-desktop", "restore-crate-type", "copy-to-sys-tmpdir"] } [tasks.flowy-sdk-dev-linux] category = "Build" dependencies = ["env_check"] -run_task = { name = ["setup-crate-type","sdk-build", "post-desktop", "restore-crate-type"] } +run_task = { name = ["setup-crate-type","sdk-build", "post-desktop", "restore-crate-type", "copy-to-sys-tmpdir"] } # [tasks.sdk-build] @@ -82,27 +82,29 @@ private = true script = [ """ echo "🚀 🚀 🚀 Flowy-SDK(macOS) build success" + dart_ffi_dir= set ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages/flowy_sdk/${TARGET_OS} + lib = set lib${LIB_NAME}.${SDK_EXT} - cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/target/${RUST_COMPILE_TARGET}/${BUILD_FLAG}/lib${CARGO_MAKE_CRATE_FS_NAME}.${SDK_EXT} \ - ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages/flowy_sdk/${TARGET_OS}/lib${CARGO_MAKE_CRATE_FS_NAME}.${SDK_EXT} + cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/target/${RUST_COMPILE_TARGET}/${BUILD_FLAG}/${lib} \ + ${dart_ffi_dir}/${lib} cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/${CARGO_MAKE_CRATE_NAME}/binding.h \ - ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/packages/flowy_sdk/${TARGET_OS}/Classes/binding.h + ${dart_ffi_dir}/Classes/binding.h """, ] script_runner = "@duckscript" - [tasks.post-desktop-windows] private = true script = [ """ echo "🚀 🚀 🚀 Flowy-SDK(windows) build success" dart_ffi_dir= set ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/windows/flutter/dart_ffi + lib = set ${LIB_NAME}.${SDK_EXT} # copy dll - cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/target/${RUST_COMPILE_TARGET}/${BUILD_FLAG}/${CARGO_MAKE_CRATE_FS_NAME}.${SDK_EXT} \ - ${dart_ffi_dir}/${CARGO_MAKE_CRATE_FS_NAME}.${SDK_EXT} + cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/target/${RUST_COMPILE_TARGET}/${BUILD_FLAG}/${lib} \ + ${dart_ffi_dir}/${lib} # copy binding.h cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/${CARGO_MAKE_CRATE_NAME}/binding.h \ @@ -117,10 +119,11 @@ script = [ """ echo "🚀 🚀 🚀 Flowy-SDK(linux) build success" dart_ffi_dir= set ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/app_flowy/linux/flutter/dart_ffi + lib = set lib${LIB_NAME}.${SDK_EXT} # copy dll - cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/target/${RUST_COMPILE_TARGET}/${BUILD_FLAG}/lib${CARGO_MAKE_CRATE_FS_NAME}.${SDK_EXT} \ - ${dart_ffi_dir}/lib${CARGO_MAKE_CRATE_FS_NAME}.${SDK_EXT} + cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/target/${RUST_COMPILE_TARGET}/${BUILD_FLAG}/${lib} \ + ${dart_ffi_dir}/${lib} # copy binding.h cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/${CARGO_MAKE_CRATE_NAME}/binding.h \ @@ -129,29 +132,28 @@ script = [ ] script_runner = "@duckscript" -# [tasks.copy-to-sys-tmpdir] private = true -condition = { profiles = [ "development-mac" ], platforms = ["mac"] } script = [ """ - # post the dylib target_path that use for flutter unit test - target_path = set ${TMPDIR}/appflowy/lib${CARGO_MAKE_CRATE_FS_NAME}.${SDK_EXT} - rm ${target_path} - cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/target/${RUST_COMPILE_TARGET}/${BUILD_FLAG}/lib${CARGO_MAKE_CRATE_FS_NAME}.${SDK_EXT} ${target_path} + # Copy the flowy_sdk lib to system temp directory for flutter unit test. + lib = set lib${LIB_NAME}.${SDK_EXT} + dest = set ${TMPDIR}/appflowy/${lib} + echo ${dest} + rm ${dest} + + + cp ${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/rust-lib/target/${RUST_COMPILE_TARGET}/${BUILD_FLAG}/${lib} \ + ${dest} """, ] script_runner = "@duckscript" -[tasks.check_protoc_cmd] +[tasks.copy-to-sys-tmpdir.windows] +private = true script = [ """ - ret = which protoc - if is_empty ${ret} - echo Please make sure /bin/ is in PATH env var - echo See BUILD_ON_{LINUX|WINDOWS}.md for how to get protoc - exit -1 - end + # Doesn't work on windows """, ] -script_runner = "@duckscript" +script_runner = "@duckscript" \ No newline at end of file diff --git a/frontend/scripts/makefile/protobuf.toml b/frontend/scripts/makefile/protobuf.toml index 82084bd3e9..f507b7aba6 100644 --- a/frontend/scripts/makefile/protobuf.toml +++ b/frontend/scripts/makefile/protobuf.toml @@ -52,19 +52,6 @@ dart pub global activate protoc_plugin script_runner = "@shell" -[tasks.check_protoc_cmd] -script = [ - """ - ret = which protoc - if is_empty ${ret} - echo Please make sure /bin/ is in PATH env var - echo See BUILD_ON_{LINUX|WINDOWS}.md for how to get protoc - exit -1 - end - """, -] -script_runner = "@duckscript" - [tasks.gen_pb_file] script = [ """