Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
add tests for create
  • Loading branch information
marandaneto committed Dec 2, 2022
commit 2f372ac95148e03a3f745a9e2847bd03d0e3e262
30 changes: 18 additions & 12 deletions file/lib/src/sentry_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ class SentryFile implements File {
);
}

void _setSize(ISentrySpan? span, dynamic data) {
// method that returns null dont have a size
if (data == null) {
return;
}
if (data is List<int>) {
span?.setData('file.size', data.length);
} else if (data is File) {
span?.setData('file.size', data.lengthSync());
// TODO: if its a copy, we need the new path here too or not?
// TODO: append size in bytes or human readable size
}
}

String _getDesc() {
return uri.pathSegments.isNotEmpty ? uri.pathSegments.last : path;
}
Expand All @@ -202,13 +216,8 @@ class SentryFile implements File {
T data;
try {
data = await future;
if (data is List<int>) {
span?.setData('file.size', data.length);
} else if (data is File) {
span?.setData('file.size', await data.length());
// TODO: if its a copy, we need the new path here too or not?
// TODO: append size in bytes or human readable size
}
_setSize(span, data);

span?.status = SpanStatus.ok();
} catch (exception) {
span?.throwable = exception;
Expand All @@ -233,11 +242,8 @@ class SentryFile implements File {
T data;
try {
data = callback();
if (data is List<int>) {
span?.setData('file.size', data.length);
} else if (data is File) {
span?.setData('file.size', data.lengthSync());
}
_setSize(span, data);

span?.status = SpanStatus.ok();
} catch (exception) {
span?.throwable = exception;
Expand Down
99 changes: 79 additions & 20 deletions file/test/sentry_file_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ void main() {
fixture = Fixture();
});

void _assertSpan(bool async) {
final call = fixture.client.captureTransactionCalls.first;
final span = call.transaction.spans.first;

expect(span.context.operation, 'file.copy');
expect(span.data['file.size'], 7);
expect(span.data['file.async'], async);
expect(span.context.description, 'testfile.txt');
expect(
(span.data['file.path'] as String)
.endsWith('test_resources/testfile.txt'),
true);
}

test('copy async', () async {
final file = File('test_resources/testfile.txt');

Expand All @@ -33,22 +47,11 @@ void main() {

await tr.finish();

final exists = await newFile.exists();
expect(exists, true);
expect(await newFile.exists(), true);

expect(sut.uri.toFilePath(), isNot(newFile.uri.toFilePath()));

final call = fixture.client.captureTransactionCalls.first;
final span = call.transaction.spans.first;

expect(span.context.operation, 'file.copy');
expect(span.data['file.size'], 7);
expect(span.data['file.async'], true);
expect(span.context.description, 'testfile.txt');
expect(
(span.data['file.path'] as String)
.endsWith('test_resources/testfile.txt'),
true);
_assertSpan(true);

await newFile.delete();
});
Expand All @@ -68,25 +71,81 @@ void main() {

await tr.finish();

final exists = newFile.existsSync();
expect(exists, true);
expect(newFile.existsSync(), true);

expect(sut.uri.toFilePath(), isNot(newFile.uri.toFilePath()));

_assertSpan(false);

newFile.deleteSync();
});
});

group('$SentryFile create', () {
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

void _assertSpan(bool async, {int? size = 0}) {
final call = fixture.client.captureTransactionCalls.first;
final span = call.transaction.spans.first;

expect(span.context.operation, 'file.copy');
expect(span.data['file.size'], 7);
expect(span.data['file.async'], false);
expect(span.context.description, 'testfile.txt');
expect(span.context.operation, 'file.write');
expect(span.data['file.size'], size);
expect(span.data['file.async'], async);
expect(span.context.description, 'testfile_create.txt');
expect(
(span.data['file.path'] as String)
.endsWith('test_resources/testfile.txt'),
.endsWith('test_resources/testfile_create.txt'),
true);
}

test('create async', () async {
final file = File('test_resources/testfile_create.txt');
expect(await file.exists(), false);

final sut = fixture.getSut(
file,
sendDefaultPii: true,
tracesSampleRate: 1.0,
);

final tr = fixture.hub.startTransaction('name', 'op', bindToScope: true);

final newFile = await sut.create();

await tr.finish();

expect(await newFile.exists(), true);

_assertSpan(true);

await newFile.delete();
});

test('create sync', () async {
final file = File('test_resources/testfile_create.txt');

final sut = fixture.getSut(
file,
sendDefaultPii: true,
tracesSampleRate: 1.0,
);

final tr = fixture.hub.startTransaction('name', 'op', bindToScope: true);

sut.createSync();

await tr.finish();

expect(sut.existsSync(), true);

_assertSpan(false, size: null);

sut.deleteSync();
});
});
}

Expand Down