-
-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SentryIOOverridesIntegration (#1362)
Co-authored-by: Manoel Aranda Neto <[email protected]> Co-authored-by: Manoel Aranda Neto <[email protected]>
- Loading branch information
1 parent
82250c4
commit 9949058
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export 'src/sentry_file.dart'; | ||
export 'src/sentry_file_extension.dart'; | ||
export 'src/sentry_io_overrides_integration.dart'; | ||
export 'src/sentry_io_overrides.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'dart:io'; | ||
import 'package:sentry/sentry.dart'; | ||
|
||
import '../sentry_file.dart'; | ||
|
||
/// If set to [IOOverrides.global], newly created [File] instances will be of | ||
/// type [SentryFile]. | ||
/// Enable by adding [SentryIOOverridesIntegration] to [SentryOptions]. | ||
class SentryIOOverrides extends IOOverrides { | ||
final Hub _hub; | ||
|
||
SentryIOOverrides(this._hub); | ||
|
||
@override | ||
File createFile(String path) { | ||
return SentryFile( | ||
super.createFile(path), | ||
hub: _hub, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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. | ||
/// | ||
/// When closed, the [IOOverrides.current] value before this integration was | ||
/// added will be assigned to [IOOverrides.global]. | ||
class SentryIOOverridesIntegration extends Integration<SentryOptions> { | ||
IOOverrides? _previousOverrides; | ||
bool _installed = false; | ||
|
||
@override | ||
FutureOr<void> call(Hub hub, SentryOptions options) { | ||
if (options.isTracingEnabled()) { | ||
_previousOverrides = IOOverrides.current; | ||
_installed = true; | ||
IOOverrides.global = SentryIOOverrides(hub); | ||
options.sdk.addIntegration('sentryIOOverridesIntegration'); | ||
} | ||
} | ||
|
||
@override | ||
FutureOr<void> close() { | ||
if (_installed) { | ||
IOOverrides.global = _previousOverrides; | ||
_previousOverrides = null; | ||
_installed = false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:sentry/sentry.dart'; | ||
import 'package:sentry_file/sentry_file.dart'; | ||
import 'package:test/expect.dart'; | ||
import 'package:test/scaffolding.dart'; | ||
|
||
import 'mock_sentry_client.dart'; | ||
|
||
void main() { | ||
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); | ||
}); | ||
|
||
test('files created are sentry file after adding integration', () { | ||
fixture.options.tracesSampleRate = 1.0; | ||
|
||
final sut = fixture.getSut(); | ||
sut.call(fixture.hub, fixture.options); | ||
|
||
final file = File("/home"); | ||
expect(file is SentryFile, true); | ||
}); | ||
} | ||
|
||
class Fixture { | ||
final options = SentryOptions(dsn: fakeDsn); | ||
late final hub = Hub(options); | ||
|
||
SentryIOOverridesIntegration getSut() { | ||
return SentryIOOverridesIntegration(); | ||
} | ||
} |