Files
plezy/test/test_helpers/prefs.dart
T
edde746 672047924c fix(test): stop the desktop prefs preflight deadlocking widget tests on Linux
PrefsRecovery.assertStoreReadable reads the real on-disk store through
path_provider, and on the hosts where it is active (Linux and Windows)
that genuine file IO can never complete inside testWidgets' fake async
zone. Since 7f0cad33 wired the preflight into shared-preference init,
every widget test that initialises settings on the Linux CI runner hung
for its full ten-minute timeout, turning the unit-test job into a
six-hour cancellation. The hermetic prefs fixture swaps the backends for
in-memory fakes anyway, so it now disables the preflight and restores it
on teardown; the repair-flow suite keeps opting in explicitly.

Verified in an ubuntu-24.04 container on Flutter 3.44.0: the full suite
now completes in nine minutes with zero failures.
2026-08-09 18:51:47 +02:00

39 lines
2.2 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:plezy/services/base_shared_preferences_service.dart';
import 'package:plezy/services/prefs_recovery.dart';
import 'package:plezy/services/settings_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
/// Reset shared-prefs platform mocks AND the cached singleton instances.
/// Call from `setUp` so each test starts with a clean slate.
void resetSharedPreferencesForTest({Map<String, Object> initialAsync = const {}}) {
final previousLegacyPlatform = SharedPreferencesStorePlatform.instance;
final previousAsyncPlatform = SharedPreferencesAsyncPlatform.instance;
addTearDown(() {
SharedPreferencesStorePlatform.instance = previousLegacyPlatform;
SharedPreferencesAsyncPlatform.instance = previousAsyncPlatform;
SharedPreferences.resetStatic();
SettingsService.resetForTesting();
BaseSharedPreferencesService.resetForTesting();
PrefsRecovery.debugSetSupportedPlatformOverride(null);
});
TestWidgetsFlutterBinding.ensureInitialized();
// The desktop store preflight (`PrefsRecovery.assertStoreReadable`) reads
// the real on-disk store through path_provider. These fixtures replace the
// preference backends with in-memory fakes, so there is no real store to
// validate — and on the hosts where the preflight is active (Linux and
// Windows) its genuine file IO can never complete inside `testWidgets`'
// fake async zone, deadlocking every widget test that initialises settings
// until the ten-minute timeout. Suites that exercise the preflight itself
// (`prefs_store_repair_flow_test.dart`) opt back in explicitly.
PrefsRecovery.debugSetSupportedPlatformOverride(false);
SharedPreferences.setMockInitialValues({});
SharedPreferencesAsyncPlatform.instance = initialAsync.isEmpty
? InMemorySharedPreferencesAsync.empty()
: InMemorySharedPreferencesAsync.withData(initialAsync);
BaseSharedPreferencesService.resetForTesting();
}