Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SentryIOOverridesIntegration #1362

Merged
merged 26 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b020ac6
add SentryIOOverridesIntegration
denrase Mar 27, 2023
dd7de03
Merge branch 'main' into feat/io-overrides-integration
denrase Mar 27, 2023
bdc195c
Merge branch 'main' into feat/io-overrides-integration
denrase Mar 28, 2023
8c0e89d
check tracing in integration
denrase Mar 28, 2023
8d770ef
add tests
denrase Mar 28, 2023
f15574e
add documentation
denrase Mar 28, 2023
d7e23f1
add changelog entry
denrase Mar 28, 2023
b8b40fb
only restore if tracing is enabled
denrase Mar 28, 2023
cd289c9
remove incorrect comment
denrase Apr 3, 2023
ad5f429
persist if integration was installed
denrase Apr 3, 2023
549041e
Merge branch 'main' into feat/io-overrides-integration
denrase Apr 3, 2023
4e991d4
update changelog
denrase Apr 3, 2023
33d5dd2
Merge branch 'main' into feat/io-overrides-integration
denrase Apr 17, 2023
9132997
make sure created file is sentryfile after installing integration
denrase Apr 17, 2023
a41db5c
export io_overrides
denrase Apr 17, 2023
ceee690
add documentation
denrase Apr 17, 2023
c1f860a
fix changelog entry
denrase Apr 17, 2023
66fe848
format
denrase Apr 17, 2023
36a1a77
document restore behaviour
denrase Apr 17, 2023
c59ef1a
Merge branch 'main' into feat/io-overrides-integration
marandaneto Apr 20, 2023
519e875
fix
marandaneto Apr 20, 2023
313745c
Merge branch 'main' into feat/io-overrides-integration
denrase Apr 24, 2023
12a9c26
reset properties when integration is closed
denrase Apr 24, 2023
65f8dbb
Merge branch 'main' into feat/io-overrides-integration
marandaneto Apr 24, 2023
56d1736
lint
marandaneto Apr 24, 2023
00b7f5b
Merge branch 'main' into feat/io-overrides-integration
marandaneto Apr 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- SentryUserInteractionWidget: add support for PopupMenuButton and PopupMenuItem ([#1361](https://github.com/getsentry/sentry-dart/pull/1361))
- SentryIOOverridesIntegration ([#1362](https://github.com/getsentry/sentry-dart/pull/1362))

### Fixes

Expand Down
1 change: 1 addition & 0 deletions file/lib/sentry_file.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'src/sentry_file.dart';
export 'src/sentry_file_extension.dart';
export 'src/sentry_io_overrides_integration.dart';
denrase marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 18 additions & 0 deletions file/lib/src/sentry_io_overrides.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:io';
import 'package:sentry/sentry.dart';

import 'sentry_file.dart';

class SentryIOOverrides extends IOOverrides {
denrase marked this conversation as resolved.
Show resolved Hide resolved
final Hub _hub;

SentryIOOverrides(this._hub);

@override
File createFile(String path) {
return SentryFile(
super.createFile(path),
hub: _hub,
);
}
}
32 changes: 32 additions & 0 deletions file/lib/src/sentry_io_overrides_integration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:async';
import 'dart:io';

import 'package:sentry/sentry.dart';
import 'sentry_io_overrides.dart';

/// When installed, every new file will be created as [SentryFile].
/// When installed, operations will use [SentryFile] instead of dart:io's [File]
/// implementation whenever [File] is used.
///
/// Must be called outside the RunZoneGuardedIntegration.
/// Otherwise the FileOverrides aren't attached to the root zone.
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
class SentryIOOverridesIntegration extends Integration<SentryOptions> {
IOOverrides? _previousOverrides;
SentryOptions? _options;
@override
FutureOr<void> call(Hub hub, SentryOptions options) {
_options = options;
if (options.isTracingEnabled()) {
_previousOverrides = IOOverrides.current;
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
IOOverrides.global = SentryIOOverrides(hub);
options.sdk.addIntegration('sentryIOOverridesIntegration');
}
}

@override
FutureOr<void> close() {
if (_options?.isTracingEnabled() ?? false) {
denrase marked this conversation as resolved.
Show resolved Hide resolved
IOOverrides.global = _previousOverrides;
denrase marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
68 changes: 68 additions & 0 deletions file/test/sentry_io_overrides_integration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'dart:io';

import 'package:sentry/sentry.dart';
import 'package:sentry_file/sentry_file.dart';
import 'package:sentry_file/src/sentry_io_overrides.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';

import 'mock_sentry_client.dart';

void main() {
denrase marked this conversation as resolved.
Show resolved Hide resolved
late IOOverrides? current;
late Fixture fixture;

setUp(() {
current = IOOverrides.current;
fixture = Fixture();
});

tearDown(() {
IOOverrides.global = current;
});

test('adding integration installs io overrides', () {
fixture.options.tracesSampleRate = 1.0;

final sut = fixture.getSut();
sut.call(fixture.hub, fixture.options);

expect(
fixture.options.sdk.integrations.contains('sentryIOOverridesIntegration'),
isTrue,
);
expect(IOOverrides.current is SentryIOOverrides, isTrue);
});

test('not installed when tracing disabled', () {
final sut = fixture.getSut();
sut.call(fixture.hub, fixture.options);

expect(
fixture.options.sdk.integrations.contains('sentryIOOverridesIntegration'),
isFalse,
);
expect(IOOverrides.current is SentryIOOverrides, isFalse);
});

test('global overrides restored', () {
final previous = IOOverrides.current;

fixture.options.tracesSampleRate = 1.0;

final sut = fixture.getSut();
sut.call(fixture.hub, fixture.options);
sut.close();

expect(IOOverrides.current, previous);
});
}

class Fixture {
final options = SentryOptions(dsn: fakeDsn);
late final hub = Hub(options);

SentryIOOverridesIntegration getSut() {
return SentryIOOverridesIntegration();
}
}