Skip to content

Commit 9949058

Browse files
denrasemarandaneto
andauthored
SentryIOOverridesIntegration (#1362)
Co-authored-by: Manoel Aranda Neto <[email protected]> Co-authored-by: Manoel Aranda Neto <[email protected]>
1 parent 82250c4 commit 9949058

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- SentryIOOverridesIntegration ([#1362](https://github.com/getsentry/sentry-dart/pull/1362))
78
- Add `enableTracing` option ([#1395](https://github.com/getsentry/sentry-dart/pull/1395))
89
- This change is backwards compatible. The default is `null` meaning existing behaviour remains unchanged (setting either `tracesSampleRate` or `tracesSampler` enables performance).
910
- If set to `true`, performance is enabled, even if no `tracesSampleRate` or `tracesSampler` have been configured.

file/lib/sentry_file.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export 'src/sentry_file.dart';
22
export 'src/sentry_file_extension.dart';
3+
export 'src/sentry_io_overrides_integration.dart';
4+
export 'src/sentry_io_overrides.dart';
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'dart:io';
2+
import 'package:sentry/sentry.dart';
3+
4+
import '../sentry_file.dart';
5+
6+
/// If set to [IOOverrides.global], newly created [File] instances will be of
7+
/// type [SentryFile].
8+
/// Enable by adding [SentryIOOverridesIntegration] to [SentryOptions].
9+
class SentryIOOverrides extends IOOverrides {
10+
final Hub _hub;
11+
12+
SentryIOOverrides(this._hub);
13+
14+
@override
15+
File createFile(String path) {
16+
return SentryFile(
17+
super.createFile(path),
18+
hub: _hub,
19+
);
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'dart:async';
2+
import 'dart:io';
3+
4+
import 'package:sentry/sentry.dart';
5+
import 'sentry_io_overrides.dart';
6+
7+
/// When installed, every new file will be created as [SentryFile].
8+
/// When installed, operations will use [SentryFile] instead of dart:io's [File]
9+
/// implementation whenever [File] is used.
10+
///
11+
/// When closed, the [IOOverrides.current] value before this integration was
12+
/// added will be assigned to [IOOverrides.global].
13+
class SentryIOOverridesIntegration extends Integration<SentryOptions> {
14+
IOOverrides? _previousOverrides;
15+
bool _installed = false;
16+
17+
@override
18+
FutureOr<void> call(Hub hub, SentryOptions options) {
19+
if (options.isTracingEnabled()) {
20+
_previousOverrides = IOOverrides.current;
21+
_installed = true;
22+
IOOverrides.global = SentryIOOverrides(hub);
23+
options.sdk.addIntegration('sentryIOOverridesIntegration');
24+
}
25+
}
26+
27+
@override
28+
FutureOr<void> close() {
29+
if (_installed) {
30+
IOOverrides.global = _previousOverrides;
31+
_previousOverrides = null;
32+
_installed = false;
33+
}
34+
}
35+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'dart:io';
2+
3+
import 'package:sentry/sentry.dart';
4+
import 'package:sentry_file/sentry_file.dart';
5+
import 'package:test/expect.dart';
6+
import 'package:test/scaffolding.dart';
7+
8+
import 'mock_sentry_client.dart';
9+
10+
void main() {
11+
late IOOverrides? current;
12+
late Fixture fixture;
13+
14+
setUp(() {
15+
current = IOOverrides.current;
16+
fixture = Fixture();
17+
});
18+
19+
tearDown(() {
20+
IOOverrides.global = current;
21+
});
22+
23+
test('adding integration installs io overrides', () {
24+
fixture.options.tracesSampleRate = 1.0;
25+
26+
final sut = fixture.getSut();
27+
sut.call(fixture.hub, fixture.options);
28+
29+
expect(
30+
fixture.options.sdk.integrations.contains('sentryIOOverridesIntegration'),
31+
isTrue,
32+
);
33+
expect(IOOverrides.current is SentryIOOverrides, isTrue);
34+
});
35+
36+
test('not installed when tracing disabled', () {
37+
final sut = fixture.getSut();
38+
sut.call(fixture.hub, fixture.options);
39+
40+
expect(
41+
fixture.options.sdk.integrations.contains('sentryIOOverridesIntegration'),
42+
isFalse,
43+
);
44+
expect(IOOverrides.current is SentryIOOverrides, isFalse);
45+
});
46+
47+
test('global overrides restored', () {
48+
final previous = IOOverrides.current;
49+
50+
fixture.options.tracesSampleRate = 1.0;
51+
52+
final sut = fixture.getSut();
53+
sut.call(fixture.hub, fixture.options);
54+
sut.close();
55+
56+
expect(IOOverrides.current, previous);
57+
});
58+
59+
test('files created are sentry file after adding integration', () {
60+
fixture.options.tracesSampleRate = 1.0;
61+
62+
final sut = fixture.getSut();
63+
sut.call(fixture.hub, fixture.options);
64+
65+
final file = File("/home");
66+
expect(file is SentryFile, true);
67+
});
68+
}
69+
70+
class Fixture {
71+
final options = SentryOptions(dsn: fakeDsn);
72+
late final hub = Hub(options);
73+
74+
SentryIOOverridesIntegration getSut() {
75+
return SentryIOOverridesIntegration();
76+
}
77+
}

0 commit comments

Comments
 (0)