Skip to content

Commit 342e82b

Browse files
authored
refactor: cleanup platform mocking (#2730)
1 parent 7f8043c commit 342e82b

File tree

64 files changed

+442
-528
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+442
-528
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
- Move replay and privacy from experimental to options ([#2755](https://github.com/getsentry/sentry-dart/pull/2755))
66
- Remove renderer from `flutter_context` ([#2751](https://github.com/getsentry/sentry-dart/pull/2751))
7+
- Cleanup platform mocking ([#2730](https://github.com/getsentry/sentry-dart/pull/2730))
8+
- The `PlatformChecker` was renamed to `RuntimeChecker`
9+
- Moved `PlatformChecker.platform` to `options.platform`
710

811
## 9.0.0-alpha.1
912

@@ -28,8 +31,7 @@
2831
- Responses are attached to the `Hint` object, which can be read in `beforeSend`/`beforeSendTransaction` callbacks via `hint.response`.
2932
- For now, only the `dio` integration is supported.
3033
- Enable privacy masking for screenshots by default ([#2728](https://github.com/getsentry/sentry-dart/pull/2728))
31-
32-
34+
3335
### Enhancements
3436

3537
- Replay: improve Android native interop performance by using JNI ([#2670](https://github.com/getsentry/sentry-dart/pull/2670))

dart/lib/sentry.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export 'src/integration.dart';
1919
export 'src/noop_isolate_error_integration.dart'
2020
if (dart.library.io) 'src/isolate_error_integration.dart';
2121
export 'src/performance_collector.dart';
22-
export 'src/platform_checker.dart';
22+
export 'src/runtime_checker.dart';
2323
export 'src/protocol.dart';
2424
export 'src/protocol/sentry_feedback.dart';
2525
export 'src/protocol/sentry_proxy.dart';

dart/lib/src/environment/environment_variables.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import '../platform_checker.dart';
1+
import '../runtime_checker.dart';
22
import '_io_environment_variables.dart'
33
if (dart.library.js_interop) '_web_environment_variables.dart' as env;
44

@@ -28,7 +28,7 @@ abstract class EnvironmentVariables {
2828

2929
/// Returns an environment based on the compilation mode of Dart or Flutter.
3030
/// This can be set as [SentryOptions.environment]
31-
String environmentForMode(PlatformChecker checker) {
31+
String environmentForMode(RuntimeChecker checker) {
3232
// We infer the environment based on the release/non-release and profile
3333
// constants.
3434

dart/lib/src/event_processor/enricher/io_enricher_event_processor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class IoEnricherEventProcessor implements EnricherEventProcessor {
8282
}
8383

8484
return <String, dynamic>{
85-
'compile_mode': _options.platformChecker.compileMode,
85+
'compile_mode': _options.runtimeChecker.compileMode,
8686
if (packageConfig != null) 'package_config': packageConfig,
8787
// The following information could potentially contain PII
8888
if (_options.sendDefaultPii) ...{

dart/lib/src/event_processor/enricher/io_platform_memory.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ class PlatformMemory {
1111
final SentryOptions options;
1212

1313
int? getTotalPhysicalMemory() {
14-
if (options.platformChecker.platform.isLinux) {
14+
if (options.platform.isLinux) {
1515
return _getLinuxMemInfoValue('MemTotal');
16-
} else if (options.platformChecker.platform.isWindows) {
16+
} else if (options.platform.isWindows) {
1717
return _getWindowsWmicValue('ComputerSystem', 'TotalPhysicalMemory');
1818
} else {
1919
return null;
2020
}
2121
}
2222

2323
int? getFreePhysicalMemory() {
24-
if (options.platformChecker.platform.isLinux) {
24+
if (options.platform.isLinux) {
2525
return _getLinuxMemInfoValue('MemFree');
26-
} else if (options.platformChecker.platform.isWindows) {
26+
} else if (options.platform.isWindows) {
2727
return _getWindowsWmicValue('OS', 'FreePhysicalMemory');
2828
} else {
2929
return null;

dart/lib/src/event_processor/enricher/web_enricher_event_processor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class WebEnricherEventProcessor implements EnricherEventProcessor {
8989

9090
Map<String, dynamic> _getDartContext() {
9191
return <String, dynamic>{
92-
'compile_mode': _options.platformChecker.compileMode,
92+
'compile_mode': _options.runtimeChecker.compileMode,
9393
};
9494
}
9595

dart/lib/src/load_dart_debug_images_integration.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import 'dart:typed_data';
2-
32
import 'package:meta/meta.dart';
43

5-
import '../sentry.dart';
4+
import 'event_processor.dart';
5+
import 'hint.dart';
6+
import 'hub.dart';
7+
import 'integration.dart';
8+
import 'protocol/debug_image.dart';
9+
import 'protocol/debug_meta.dart';
10+
import 'protocol/sentry_event.dart';
11+
import 'protocol/sentry_level.dart';
12+
import 'protocol/sentry_stack_trace.dart';
13+
import 'sentry_options.dart';
614

715
class LoadDartDebugImagesIntegration extends Integration<SentryOptions> {
816
@override
@@ -83,11 +91,11 @@ class LoadImageIntegrationEventProcessor implements EventProcessor {
8391
// It doesn't need to exist and is not used for symbolication.
8492
late final String codeFile;
8593

86-
final platform = _options.platformChecker.platform;
94+
final platform = _options.platform;
8795

8896
if (platform.isAndroid || platform.isWindows) {
8997
type = 'elf';
90-
debugId = _convertBuildIdToDebugId(stackTrace.buildId!, platform.endian);
98+
debugId = _convertBuildIdToDebugId(stackTrace.buildId!, Endian.host);
9199
if (platform.isAndroid) {
92100
codeFile = 'libapp.so';
93101
} else if (platform.isWindows) {
Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1-
import 'dart:io' as io show Platform;
1+
import 'dart:io' as io;
22

33
import 'platform.dart';
44

5-
const Platform instance = IOPlatform();
6-
75
/// [Platform] implementation that delegates directly to `dart:io`.
8-
class IOPlatform extends Platform {
9-
/// Creates a new [IOPlatform].
10-
const IOPlatform();
6+
class PlatformBase {
7+
const PlatformBase();
118

12-
@override
13-
String get operatingSystem => io.Platform.operatingSystem;
9+
OperatingSystem get operatingSystem {
10+
switch (io.Platform.operatingSystem) {
11+
case 'macos':
12+
return OperatingSystem.macos;
13+
case 'windows':
14+
return OperatingSystem.windows;
15+
case 'linux':
16+
return OperatingSystem.linux;
17+
case 'android':
18+
return OperatingSystem.android;
19+
case 'ios':
20+
return OperatingSystem.ios;
21+
case 'fuchsia':
22+
return OperatingSystem.fuchsia;
23+
default:
24+
return OperatingSystem.unknown;
25+
}
26+
}
1427

15-
@override
16-
String get operatingSystemVersion => io.Platform.operatingSystemVersion;
28+
String? get operatingSystemVersion => io.Platform.operatingSystemVersion;
1729

18-
@override
1930
String get localHostname => io.Platform.localHostname;
31+
32+
bool get isWeb => false;
2033
}

dart/lib/src/platform/_web_platform.dart

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,30 @@ import 'package:web/web.dart' as web;
22

33
import 'platform.dart';
44

5-
const Platform instance = WebPlatform();
6-
75
/// [Platform] implementation that delegates to `dart:web`.
8-
class WebPlatform extends Platform {
9-
/// Creates a new [Platform].
10-
const WebPlatform();
11-
12-
@override
13-
String get operatingSystem => _browserPlatform();
6+
class PlatformBase {
7+
const PlatformBase();
148

15-
@override
16-
String get operatingSystemVersion => 'unknown';
17-
18-
@override
19-
String get localHostname => web.window.location.hostname;
9+
bool get isWeb => true;
2010

21-
String _browserPlatform() {
11+
OperatingSystem get operatingSystem {
2212
final navigatorPlatform = web.window.navigator.platform.toLowerCase();
2313
if (navigatorPlatform.startsWith('mac')) {
24-
return 'macos';
14+
return OperatingSystem.macos;
2515
}
2616
if (navigatorPlatform.startsWith('win')) {
27-
return 'windows';
17+
return OperatingSystem.windows;
2818
}
2919
if (navigatorPlatform.contains('iphone') ||
3020
navigatorPlatform.contains('ipad') ||
3121
navigatorPlatform.contains('ipod')) {
32-
return 'ios';
22+
return OperatingSystem.ios;
3323
}
3424
if (navigatorPlatform.contains('android')) {
35-
return 'android';
25+
return OperatingSystem.android;
3626
}
3727
if (navigatorPlatform.contains('fuchsia')) {
38-
return 'fuchsia';
28+
return OperatingSystem.fuchsia;
3929
}
4030

4131
// Since some phones can report a window.navigator.platform as Linux, fall
@@ -44,8 +34,12 @@ class WebPlatform extends Platform {
4434
// pointing device, then we'll assume desktop linux, and otherwise we'll
4535
// assume Android.
4636
if (web.window.matchMedia('only screen and (pointer: fine)').matches) {
47-
return 'linux';
37+
return OperatingSystem.linux;
4838
}
49-
return 'android';
39+
return OperatingSystem.android;
5040
}
41+
42+
String? get operatingSystemVersion => null;
43+
44+
String get localHostname => web.window.location.hostname;
5145
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'platform.dart';
2+
3+
class MockPlatform extends Platform {
4+
@override
5+
late final bool isWeb;
6+
7+
@override
8+
late final OperatingSystem operatingSystem;
9+
10+
@override
11+
late final String? operatingSystemVersion;
12+
13+
@override
14+
late final bool supportsNativeIntegration;
15+
16+
MockPlatform(
17+
{OperatingSystem? operatingSystem,
18+
String? operatingSystemVersion,
19+
bool? isWeb,
20+
bool? supportsNativeIntegration}) {
21+
this.isWeb = isWeb ?? super.isWeb;
22+
this.operatingSystem = operatingSystem ?? super.operatingSystem;
23+
this.operatingSystemVersion =
24+
operatingSystemVersion ?? super.operatingSystemVersion;
25+
this.supportsNativeIntegration =
26+
supportsNativeIntegration ?? super.supportsNativeIntegration;
27+
}
28+
29+
factory MockPlatform.android({bool isWeb = false}) {
30+
return MockPlatform(operatingSystem: OperatingSystem.android, isWeb: isWeb);
31+
}
32+
33+
factory MockPlatform.iOS({bool isWeb = false}) {
34+
return MockPlatform(operatingSystem: OperatingSystem.ios, isWeb: isWeb);
35+
}
36+
37+
factory MockPlatform.macOS({bool isWeb = false}) {
38+
return MockPlatform(operatingSystem: OperatingSystem.macos, isWeb: isWeb);
39+
}
40+
41+
factory MockPlatform.linux({bool isWeb = false}) {
42+
return MockPlatform(operatingSystem: OperatingSystem.linux, isWeb: isWeb);
43+
}
44+
45+
factory MockPlatform.windows({bool isWeb = false}) {
46+
return MockPlatform(operatingSystem: OperatingSystem.windows, isWeb: isWeb);
47+
}
48+
49+
factory MockPlatform.fuchsia({bool isWeb = false}) {
50+
return MockPlatform(operatingSystem: OperatingSystem.fuchsia, isWeb: isWeb);
51+
}
52+
}

dart/lib/src/platform/platform.dart

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
1-
import 'dart:typed_data';
2-
31
import '_io_platform.dart' if (dart.library.js_interop) '_web_platform.dart'
4-
as platform;
5-
6-
const Platform instance = platform.instance;
2+
as impl;
73

8-
abstract class Platform {
4+
class Platform extends impl.PlatformBase {
95
const Platform();
106

11-
/// A string (`linux`, `macos`, `windows`, `android`, `ios`, or `fuchsia`)
12-
/// representing the operating system.
13-
String get operatingSystem;
14-
15-
/// A string representing the version of the operating system or platform.
16-
String get operatingSystemVersion;
7+
bool get isLinux => operatingSystem == OperatingSystem.linux;
178

18-
/// Get the local hostname for the system.
19-
String get localHostname;
9+
bool get isMacOS => operatingSystem == OperatingSystem.macos;
2010

21-
/// Endianness of this platform.
22-
Endian get endian => Endian.host;
11+
bool get isWindows => operatingSystem == OperatingSystem.windows;
2312

24-
/// True if the operating system is Linux.
25-
bool get isLinux => (operatingSystem == 'linux');
13+
bool get isAndroid => operatingSystem == OperatingSystem.android;
2614

27-
/// True if the operating system is OS X.
28-
bool get isMacOS => (operatingSystem == 'macos');
15+
bool get isIOS => operatingSystem == OperatingSystem.ios;
2916

30-
/// True if the operating system is Windows.
31-
bool get isWindows => (operatingSystem == 'windows');
17+
bool get isFuchsia => operatingSystem == OperatingSystem.fuchsia;
3218

33-
/// True if the operating system is Android.
34-
bool get isAndroid => (operatingSystem == 'android');
35-
36-
/// True if the operating system is iOS.
37-
bool get isIOS => (operatingSystem == 'ios');
19+
bool get supportsNativeIntegration => !isFuchsia;
20+
}
3821

39-
/// True if the operating system is Fuchsia
40-
bool get isFuchsia => (operatingSystem == 'fuchsia');
22+
enum OperatingSystem {
23+
android,
24+
fuchsia,
25+
ios,
26+
linux,
27+
macos,
28+
windows,
29+
unknown,
4130
}

dart/lib/src/protocol/sdk_version.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class SdkVersion {
7171
final json = AccessAwareMap(data);
7272
final packagesJson = json['packages'] as List<dynamic>?;
7373
final integrationsJson = json['integrations'] as List<dynamic>?;
74+
7475
return SdkVersion(
7576
name: json['name'],
7677
version: json['version'],

0 commit comments

Comments
 (0)