Skip to content

Commit

Permalink
Fixes tests and deprecation warnings for Dart2 (#9)
Browse files Browse the repository at this point in the history
* Fixed tests for Dart2

* Run tests with --preview-dart-2 flag

* Addressed PR comments

* Updated changelog and bumped version to 2.0.0
  • Loading branch information
pulyaevskiy authored and yjbanov committed Mar 21, 2018
1 parent a922746 commit a0c2524
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store
.atom/
.packages
.pub/
.dart_tool/
build/
packages
pubspec.lock
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# package:sentry changelog

## 2.0.0

- Fixed deprecation warnings for Dart 2
- Refactored tests to work with Dart 2

## 1.0.0

- first and last Dart 1-compatible release (we may fix bugs on a separate branch if there's demand)
Expand Down
13 changes: 6 additions & 7 deletions lib/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ class SentryClient {
@required this.secretKey,
@required this.compressPayload,
@required this.projectId,
})
: _httpClient = httpClient,
}) : _httpClient = httpClient,
_clock = clock,
_uuidGenerator = uuidGenerator;

Expand Down Expand Up @@ -161,19 +160,19 @@ class SentryClient {
'sentry_secret=$secretKey',
};

final Map<String, dynamic> json = <String, dynamic>{
final Map<String, dynamic> data = <String, dynamic>{
'project': projectId,
'event_id': _uuidGenerator(),
'timestamp': formatDateAsIso8601WithSecondPrecision(_clock.now()),
'logger': defaultLoggerName,
};

if (environmentAttributes != null)
mergeAttributes(environmentAttributes.toJson(), into: json);
mergeAttributes(environmentAttributes.toJson(), into: data);

mergeAttributes(event.toJson(), into: json);
mergeAttributes(event.toJson(), into: data);

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

final String eventId = JSON.decode(response.body)['id'];
final String eventId = json.decode(response.body)['id'];
return new SentryResponse.success(eventId: eventId);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
library version;

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

/// The SDK name reported to Sentry.io in the submitted events.
const String sdkName = 'dart';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sentry
version: 1.0.0
version: 2.0.0
description: A pure Dart Sentry.io client.
author: Flutter Authors <[email protected]>
homepage: https://github.com/flutter/sentry
Expand Down
57 changes: 39 additions & 18 deletions test/sentry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:convert';
import 'dart:io';

import 'package:http/http.dart';
import 'package:mockito/mockito.dart';
import 'package:quiver/time.dart';
import 'package:sentry/sentry.dart';
import 'package:test/test.dart';
Expand All @@ -32,12 +31,17 @@ void main() {
String postUri;
Map<String, String> headers;
List<int> body;
when(httpMock.post(any, headers: any, body: any))
.thenAnswer((Invocation invocation) {
postUri = invocation.positionalArguments.single;
headers = invocation.namedArguments[#headers];
body = invocation.namedArguments[#body];
return new Response('{"id": "test-event-id"}', 200);
httpMock.answerWith((Invocation invocation) {
if (invocation.memberName == #close) {
return null;
}
if (invocation.memberName == #post) {
postUri = invocation.positionalArguments.single;
headers = invocation.namedArguments[#headers];
body = invocation.namedArguments[#body];
return new Response('{"id": "test-event-id"}', 200);
}
fail('Unexpected invocation of ${invocation.memberName} in HttpMock');
});

final SentryClient client = new SentryClient(
Expand Down Expand Up @@ -79,13 +83,13 @@ void main() {

expect(headers, expectedHeaders);

Map<String, dynamic> json;
Map<String, dynamic> data;
if (compressPayload) {
json = JSON.decode(UTF8.decode(GZIP.decode(body)));
data = json.decode(utf8.decode(GZIP.decode(body)));
} else {
json = JSON.decode(UTF8.decode(body));
data = json.decode(utf8.decode(body));
}
final Map<String, dynamic> stacktrace = json.remove('stacktrace');
final Map<String, dynamic> stacktrace = data.remove('stacktrace');
expect(stacktrace['frames'], const isInstanceOf<List>());
expect(stacktrace['frames'], isNotEmpty);

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

expect(json, {
expect(data, {
'project': '1',
'event_id': 'X' * 32,
'timestamp': '2017-01-02T00:00:00',
Expand Down Expand Up @@ -128,11 +132,16 @@ void main() {
final MockClient httpMock = new MockClient();
final Clock fakeClock = new Clock.fixed(new DateTime(2017, 1, 2));

when(httpMock.post(any, headers: any, body: any))
.thenAnswer((Invocation invocation) {
return new Response('', 401, headers: <String, String>{
'x-sentry-error': 'Invalid api key',
});
httpMock.answerWith((Invocation invocation) {
if (invocation.memberName == #close) {
return null;
}
if (invocation.memberName == #post) {
return new Response('', 401, headers: <String, String>{
'x-sentry-error': 'Invalid api key',
});
}
fail('Unexpected invocation of ${invocation.memberName} in HttpMock');
});

final SentryClient client = new SentryClient(
Expand Down Expand Up @@ -199,4 +208,16 @@ void main() {
});
}

class MockClient extends Mock implements Client {}
typedef Answer = dynamic Function(Invocation invocation);

class MockClient implements Client {
Answer _answer;

void answerWith(Answer answer) {
_answer = answer;
}

noSuchMethod(Invocation invocation) {
return _answer(invocation);
}
}
7 changes: 7 additions & 0 deletions tool/dart2_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
# Temporary workaround until Pub supports --preview-dart-2 flag
set -e
set -x
for filename in test/*_test.dart; do
dart --preview-dart-2 --enable_asserts "$filename"
done
1 change: 1 addition & 0 deletions tool/presubmit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ set -x
pub get
dartanalyzer --strong --fatal-warnings ./
pub run test --platform vm
./tool/dart2_test.sh
dartfmt -n --set-exit-if-changed ./

0 comments on commit a0c2524

Please sign in to comment.