Skip to content

Commit a0c2524

Browse files
pulyaevskiyyjbanov
authored andcommitted
Fixes tests and deprecation warnings for Dart2 (#9)
* Fixed tests for Dart2 * Run tests with --preview-dart-2 flag * Addressed PR comments * Updated changelog and bumped version to 2.0.0
1 parent a922746 commit a0c2524

File tree

8 files changed

+61
-28
lines changed

8 files changed

+61
-28
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.DS_Store
22
.atom/
33
.packages
4-
.pub/
4+
.dart_tool/
55
build/
66
packages
77
pubspec.lock

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# package:sentry changelog
22

3+
## 2.0.0
4+
5+
- Fixed deprecation warnings for Dart 2
6+
- Refactored tests to work with Dart 2
7+
38
## 1.0.0
49

510
- first and last Dart 1-compatible release (we may fix bugs on a separate branch if there's demand)

lib/sentry.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ class SentryClient {
106106
@required this.secretKey,
107107
@required this.compressPayload,
108108
@required this.projectId,
109-
})
110-
: _httpClient = httpClient,
109+
}) : _httpClient = httpClient,
111110
_clock = clock,
112111
_uuidGenerator = uuidGenerator;
113112

@@ -161,19 +160,19 @@ class SentryClient {
161160
'sentry_secret=$secretKey',
162161
};
163162

164-
final Map<String, dynamic> json = <String, dynamic>{
163+
final Map<String, dynamic> data = <String, dynamic>{
165164
'project': projectId,
166165
'event_id': _uuidGenerator(),
167166
'timestamp': formatDateAsIso8601WithSecondPrecision(_clock.now()),
168167
'logger': defaultLoggerName,
169168
};
170169

171170
if (environmentAttributes != null)
172-
mergeAttributes(environmentAttributes.toJson(), into: json);
171+
mergeAttributes(environmentAttributes.toJson(), into: data);
173172

174-
mergeAttributes(event.toJson(), into: json);
173+
mergeAttributes(event.toJson(), into: data);
175174

176-
List<int> body = UTF8.encode(JSON.encode(json));
175+
List<int> body = utf8.encode(json.encode(data));
177176
if (compressPayload) {
178177
headers['Content-Encoding'] = 'gzip';
179178
body = GZIP.encode(body);
@@ -190,7 +189,7 @@ class SentryClient {
190189
return new SentryResponse.failure(errorMessage);
191190
}
192191

193-
final String eventId = JSON.decode(response.body)['id'];
192+
final String eventId = json.decode(response.body)['id'];
194193
return new SentryResponse.success(eventId: eventId);
195194
}
196195

lib/src/version.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
library version;
1010

1111
/// The SDK version reported to Sentry.io in the submitted events.
12-
const String sdkVersion = '1.0.0';
12+
const String sdkVersion = '2.0.0';
1313

1414
/// The SDK name reported to Sentry.io in the submitted events.
1515
const String sdkName = 'dart';

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: sentry
2-
version: 1.0.0
2+
version: 2.0.0
33
description: A pure Dart Sentry.io client.
44
author: Flutter Authors <[email protected]>
55
homepage: https://github.com/flutter/sentry

test/sentry_test.dart

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'dart:convert';
66
import 'dart:io';
77

88
import 'package:http/http.dart';
9-
import 'package:mockito/mockito.dart';
109
import 'package:quiver/time.dart';
1110
import 'package:sentry/sentry.dart';
1211
import 'package:test/test.dart';
@@ -32,12 +31,17 @@ void main() {
3231
String postUri;
3332
Map<String, String> headers;
3433
List<int> body;
35-
when(httpMock.post(any, headers: any, body: any))
36-
.thenAnswer((Invocation invocation) {
37-
postUri = invocation.positionalArguments.single;
38-
headers = invocation.namedArguments[#headers];
39-
body = invocation.namedArguments[#body];
40-
return new Response('{"id": "test-event-id"}', 200);
34+
httpMock.answerWith((Invocation invocation) {
35+
if (invocation.memberName == #close) {
36+
return null;
37+
}
38+
if (invocation.memberName == #post) {
39+
postUri = invocation.positionalArguments.single;
40+
headers = invocation.namedArguments[#headers];
41+
body = invocation.namedArguments[#body];
42+
return new Response('{"id": "test-event-id"}', 200);
43+
}
44+
fail('Unexpected invocation of ${invocation.memberName} in HttpMock');
4145
});
4246

4347
final SentryClient client = new SentryClient(
@@ -79,13 +83,13 @@ void main() {
7983

8084
expect(headers, expectedHeaders);
8185

82-
Map<String, dynamic> json;
86+
Map<String, dynamic> data;
8387
if (compressPayload) {
84-
json = JSON.decode(UTF8.decode(GZIP.decode(body)));
88+
data = json.decode(utf8.decode(GZIP.decode(body)));
8589
} else {
86-
json = JSON.decode(UTF8.decode(body));
90+
data = json.decode(utf8.decode(body));
8791
}
88-
final Map<String, dynamic> stacktrace = json.remove('stacktrace');
92+
final Map<String, dynamic> stacktrace = data.remove('stacktrace');
8993
expect(stacktrace['frames'], const isInstanceOf<List>());
9094
expect(stacktrace['frames'], isNotEmpty);
9195

@@ -98,7 +102,7 @@ void main() {
98102
expect(topFrame['in_app'], true);
99103
expect(topFrame['filename'], 'sentry_test.dart');
100104

101-
expect(json, {
105+
expect(data, {
102106
'project': '1',
103107
'event_id': 'X' * 32,
104108
'timestamp': '2017-01-02T00:00:00',
@@ -128,11 +132,16 @@ void main() {
128132
final MockClient httpMock = new MockClient();
129133
final Clock fakeClock = new Clock.fixed(new DateTime(2017, 1, 2));
130134

131-
when(httpMock.post(any, headers: any, body: any))
132-
.thenAnswer((Invocation invocation) {
133-
return new Response('', 401, headers: <String, String>{
134-
'x-sentry-error': 'Invalid api key',
135-
});
135+
httpMock.answerWith((Invocation invocation) {
136+
if (invocation.memberName == #close) {
137+
return null;
138+
}
139+
if (invocation.memberName == #post) {
140+
return new Response('', 401, headers: <String, String>{
141+
'x-sentry-error': 'Invalid api key',
142+
});
143+
}
144+
fail('Unexpected invocation of ${invocation.memberName} in HttpMock');
136145
});
137146

138147
final SentryClient client = new SentryClient(
@@ -199,4 +208,16 @@ void main() {
199208
});
200209
}
201210

202-
class MockClient extends Mock implements Client {}
211+
typedef Answer = dynamic Function(Invocation invocation);
212+
213+
class MockClient implements Client {
214+
Answer _answer;
215+
216+
void answerWith(Answer answer) {
217+
_answer = answer;
218+
}
219+
220+
noSuchMethod(Invocation invocation) {
221+
return _answer(invocation);
222+
}
223+
}

tool/dart2_test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
# Temporary workaround until Pub supports --preview-dart-2 flag
3+
set -e
4+
set -x
5+
for filename in test/*_test.dart; do
6+
dart --preview-dart-2 --enable_asserts "$filename"
7+
done

tool/presubmit.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ set -x
66
pub get
77
dartanalyzer --strong --fatal-warnings ./
88
pub run test --platform vm
9+
./tool/dart2_test.sh
910
dartfmt -n --set-exit-if-changed ./

0 commit comments

Comments
 (0)