Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Sep 14, 2024
1 parent a842788 commit 3263ffd
Show file tree
Hide file tree
Showing 19 changed files with 328 additions and 147 deletions.
3 changes: 1 addition & 2 deletions dart/test/debug_image_extractor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ isolate_dso_base: 10000000

class Fixture {
DebugImageExtractor getSut({required MockPlatform platform}) {
final options = defaultTestOptions()
..platformChecker = MockPlatformChecker(platform: platform);
final options = defaultTestOptions(MockPlatformChecker(platform: platform));
return DebugImageExtractor(options);
}
}
5 changes: 2 additions & 3 deletions dart/test/event_processor/enricher/io_enricher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,8 @@ class Fixture {
bool hasNativeIntegration = false,
bool includePii = false,
}) {
final options = defaultTestOptions()
..platformChecker =
MockPlatformChecker(hasNativeIntegration: hasNativeIntegration)
final options = defaultTestOptions(
MockPlatformChecker(hasNativeIntegration: hasNativeIntegration))
..sendDefaultPii = includePii;

return IoEnricherEventProcessor(options);
Expand Down
4 changes: 2 additions & 2 deletions dart/test/event_processor/enricher/web_enricher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ void main() {

class Fixture {
WebEnricherEventProcessor getSut() {
final options = defaultTestOptions()
..platformChecker = MockPlatformChecker(hasNativeIntegration: false);
final options =
defaultTestOptions(MockPlatformChecker(hasNativeIntegration: false));
return enricherEventProcessor(options) as WebEnricherEventProcessor;
}
}
4 changes: 2 additions & 2 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1896,8 +1896,8 @@ class Fixture {
final recorder = MockClientReportRecorder();
final transport = MockTransport();

final options = defaultTestOptions()
..platformChecker = MockPlatformChecker(platform: MockPlatform.iOS());
final options =
defaultTestOptions(MockPlatformChecker(platform: MockPlatform.iOS()));

late SentryTransactionContext _context;
late SentryTracer tracer;
Expand Down
12 changes: 4 additions & 8 deletions dart/test/sentry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ void main() {
});

test('options.environment debug', () async {
final sentryOptions = defaultTestOptions()
..platformChecker = FakePlatformChecker.debugMode();
final sentryOptions = defaultTestOptions(FakePlatformChecker.debugMode());
await Sentry.init(
(options) {
options.dsn = fakeDsn;
Expand All @@ -385,8 +384,7 @@ void main() {
});

test('options.environment profile', () async {
final sentryOptions = defaultTestOptions()
..platformChecker = FakePlatformChecker.profileMode();
final sentryOptions = defaultTestOptions(FakePlatformChecker.profileMode());

await Sentry.init(
(options) {
Expand All @@ -399,8 +397,7 @@ void main() {
});

test('options.environment production (defaultEnvironment)', () async {
final sentryOptions = defaultTestOptions()
..platformChecker = FakePlatformChecker.releaseMode();
final sentryOptions = defaultTestOptions(FakePlatformChecker.releaseMode());
await Sentry.init(
(options) {
options.dsn = fakeDsn;
Expand All @@ -412,8 +409,7 @@ void main() {
});

test('options.logger is set by setting the debug flag', () async {
final sentryOptions = defaultTestOptions()
..platformChecker = FakePlatformChecker.debugMode();
final sentryOptions = defaultTestOptions(FakePlatformChecker.debugMode());

await Sentry.init(
(options) {
Expand Down
3 changes: 1 addition & 2 deletions dart/test/stack_trace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ class Fixture {
bool considerInAppFramesByDefault = true,
bool isWeb = false,
}) {
final options = defaultTestOptions()
..platformChecker = MockPlatformChecker(isWebValue: isWeb);
final options = defaultTestOptions(MockPlatformChecker(isWebValue: isWeb));
inAppIncludes.forEach(options.addInAppInclude);
inAppExcludes.forEach(options.addInAppExclude);
options.considerInAppFramesByDefault = considerInAppFramesByDefault;
Expand Down
5 changes: 3 additions & 2 deletions dart/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ const String _testDsnWithPath =
const String _testDsnWithPort =
'https://public:[email protected]:8888/1';

SentryOptions defaultTestOptions() {
return SentryOptions(dsn: testDsn)..automatedTestMode = true;
SentryOptions defaultTestOptions([PlatformChecker? checker]) {
return SentryOptions(dsn: testDsn, checker: checker)
..automatedTestMode = true;
}

void testHeaders(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,9 @@ void main() {

testWidgets('$FlutterEnricherEventProcessor gets added on init',
(tester) async {
final sentryOptions = defaultTestOptions()
// use a mockplatform checker so that we don't need to mock platform channels
..platformChecker = MockPlatformChecker(hasNativeIntegration: false);
// use a mockplatform checker so that we don't need to mock platform channels
final sentryOptions =
defaultTestOptions(MockPlatformChecker(hasNativeIntegration: false));

loadTestPackage();
await SentryFlutter.init((options) {
Expand Down Expand Up @@ -417,8 +417,7 @@ class Fixture {
hasNativeIntegration: hasNativeIntegration,
);

final options = defaultTestOptions()
..platformChecker = platformChecker
final options = defaultTestOptions(platformChecker)
..reportPackages = reportPackages;
final customizedOptions = optionsBuilder?.call(options) ?? options;
return FlutterEnricherEventProcessor(customizedOptions);
Expand Down
13 changes: 5 additions & 8 deletions flutter/test/initialization_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ import 'mocks.dart';
// https://github.com/getsentry/sentry-dart/issues/508
// There are no asserts, test are succesfull if no exceptions are thrown.
void main() {
setUp(() async {
await Sentry.close();
});
final native = NativeChannelFixture();

void optionsInitializer(SentryFlutterOptions options) {
options.dsn = fakeDsn;
options.automatedTestMode = true;

// LoadReleaseIntegration throws because package_info channel is not available
options.removeIntegration(
options.integrations.firstWhere((i) => i is LoadReleaseIntegration));
}

test('async re-initilization', () async {
await SentryFlutter.init(optionsInitializer);
await SentryFlutter.init(optionsInitializer,
options: defaultTestOptions()..methodChannel = native.channel);

await Sentry.close();

await SentryFlutter.init(optionsInitializer);
await SentryFlutter.init(optionsInitializer,
options: defaultTestOptions()..methodChannel = native.channel);

await Sentry.close();
});
Expand Down
3 changes: 1 addition & 2 deletions flutter/test/integrations/debug_print_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ class Fixture {
bool debug = false,
bool enablePrintBreadcrumbs = true,
}) {
return defaultTestOptions()
..platformChecker = MockPlatformChecker(isDebug: debug)
return defaultTestOptions(MockPlatformChecker(isDebug: debug))
..enablePrintBreadcrumbs = enablePrintBreadcrumbs;
}

Expand Down
8 changes: 8 additions & 0 deletions flutter/test/integrations/flutter_error_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ void main() {

final hub = Hub(fixture.options);
final client = MockSentryClient();
when(client.captureEvent(any,
scope: anyNamed('scope'),
stackTrace: anyNamed('stackTrace'),
hint: anyNamed('hint')))
.thenAnswer((_) => Future.value(SentryId.newId()));
when(client.captureTransaction(any,
scope: anyNamed('scope'), traceContext: anyNamed('traceContext')))
.thenAnswer((_) => Future.value(SentryId.newId()));
hub.bindClient(client);

final sut = fixture.getSut();
Expand Down
2 changes: 1 addition & 1 deletion flutter/test/integrations/init_native_sdk_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ MethodChannel createChannelWithCallback(

SentryFlutterOptions createOptions() {
final mockPlatformChecker = MockPlatformChecker(hasNativeIntegration: true);
final options = defaultTestOptions()..platformChecker = mockPlatformChecker;
final options = defaultTestOptions(mockPlatformChecker);
options.sdk = SdkVersion(
name: sdkName,
version: sdkVersion,
Expand Down
8 changes: 8 additions & 0 deletions flutter/test/integrations/on_error_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ void main() {

final hub = Hub(fixture.options);
final client = MockSentryClient();
when(client.captureEvent(any,
scope: anyNamed('scope'),
stackTrace: anyNamed('stackTrace'),
hint: anyNamed('hint')))
.thenAnswer((_) => Future.value(SentryId.newId()));
when(client.captureTransaction(any,
scope: anyNamed('scope'), traceContext: anyNamed('traceContext')))
.thenAnswer((_) => Future.value(SentryId.newId()));
hub.bindClient(client);

final sut = fixture.getSut();
Expand Down
11 changes: 7 additions & 4 deletions flutter/test/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/binding.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry/src/platform/platform.dart';
import 'package:sentry/src/sentry_tracer.dart';

Expand All @@ -18,8 +19,9 @@ import 'no_such_method_provider.dart';
const fakeDsn = 'https://[email protected]/1234567';
const fakeProguardUuid = '3457d982-65ef-576d-a6ad-65b5f30f49a5';

SentryFlutterOptions defaultTestOptions() {
return SentryFlutterOptions(dsn: fakeDsn)..automatedTestMode = true;
SentryFlutterOptions defaultTestOptions([PlatformChecker? checker]) {
return SentryFlutterOptions(dsn: fakeDsn, checker: checker)
..automatedTestMode = true;
}

// https://github.com/dart-lang/mockito/blob/master/NULL_SAFETY_README.md#fallback-generators
Expand All @@ -44,6 +46,7 @@ ISentrySpan startTransactionShim(
SentryTracer,
SentryTransaction,
SentrySpan,
SentryClient,
MethodChannel,
SentryNativeBinding
], customMocks: [
Expand Down Expand Up @@ -166,8 +169,6 @@ class TestBindingWrapper implements BindingWrapper {
}
}

class MockSentryClient with NoSuchMethodProvider implements SentryClient {}

// All these values are based on the fakeFrameDurations list.
// The expected total frames is also based on the span duration of 1000ms and the slow and frozen frames.
const expectedTotalFrames = 17;
Expand Down Expand Up @@ -199,6 +200,8 @@ class NativeChannelFixture {
TestWidgetsFlutterBinding.ensureInitialized();
channel = MethodChannel('test.channel', StandardMethodCodec(), _messenger);
handler = MockCallbacks().methodCallHandler;
when(handler('initNativeSdk', any)).thenAnswer((_) => Future.value());
when(handler('closeNativeSdk', any)).thenAnswer((_) => Future.value());
_messenger.setMockMethodCallHandler(
channel, (call) => handler(call.method, call.arguments));
}
Expand Down
Loading

0 comments on commit 3263ffd

Please sign in to comment.