@@ -13,52 +13,20 @@ import 'package:unified_analytics/src/utils.dart';
13
13
import 'package:unified_analytics/unified_analytics.dart' ;
14
14
15
15
void main () {
16
- late FakeAnalytics analytics;
17
- late Directory homeDirectory;
18
- late MemoryFileSystem fs;
19
- late File logFile;
20
-
21
16
final testEvent = Event .hotReloadTime (timeMs: 10 );
22
17
23
- setUp (() {
24
- fs = MemoryFileSystem .test (style: FileSystemStyle .posix);
25
- homeDirectory = fs.directory ('home' );
26
- logFile = fs.file (p.join (
27
- homeDirectory.path,
28
- kDartToolDirectoryName,
29
- kLogFileName,
30
- ));
31
-
32
- // Create the initialization analytics instance to onboard the tool
33
- final initializationAnalytics = Analytics .fake (
34
- tool: DashTool .flutterTool,
35
- homeDirectory: homeDirectory,
36
- dartVersion: 'dartVersion' ,
37
- fs: fs,
38
- platform: DevicePlatform .macos,
39
- );
40
- initializationAnalytics.clientShowedMessage ();
41
-
42
- // This instance is free to send events since the instance above
43
- // has confirmed that the client has shown the message
44
- analytics = Analytics .fake (
45
- tool: DashTool .flutterTool,
46
- homeDirectory: homeDirectory,
47
- dartVersion: 'dartVersion' ,
48
- fs: fs,
49
- platform: DevicePlatform .macos,
50
- );
51
- });
52
-
53
18
test ('Ensure that log file is created' , () {
19
+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
54
20
expect (logFile.existsSync (), true );
55
21
});
56
22
57
23
test ('LogFileStats is null before events are sent' , () {
24
+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
58
25
expect (analytics.logFileStats (), isNull);
59
26
});
60
27
61
28
test ('LogFileStats returns valid response after sent events' , () async {
29
+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
62
30
final countOfEventsToSend = 10 ;
63
31
64
32
for (var i = 0 ; i < countOfEventsToSend; i++ ) {
@@ -71,6 +39,8 @@ void main() {
71
39
});
72
40
73
41
test ('The only record in the log file is malformed' , () async {
42
+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
43
+
74
44
// Write invalid json for the only log record
75
45
logFile.writeAsStringSync ('{{\n ' );
76
46
@@ -93,6 +63,8 @@ void main() {
93
63
});
94
64
95
65
test ('The first record is malformed, but rest are valid' , () async {
66
+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
67
+
96
68
// Write invalid json for the only log record
97
69
logFile.writeAsStringSync ('{{\n ' );
98
70
@@ -109,6 +81,8 @@ void main() {
109
81
});
110
82
111
83
test ('Several records are malformed' , () async {
84
+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
85
+
112
86
final countOfMalformedRecords = 4 ;
113
87
for (var i = 0 ; i < countOfMalformedRecords; i++ ) {
114
88
final currentContents = logFile.readAsStringSync ();
@@ -136,6 +110,8 @@ void main() {
136
110
});
137
111
138
112
test ('Valid json but invalid keys' , () {
113
+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
114
+
139
115
// The second line here is missing the "events" top level
140
116
// key which should cause an error for that record only
141
117
//
@@ -157,6 +133,8 @@ void main() {
157
133
});
158
134
159
135
test ('Malformed record gets phased out after several events' , () async {
136
+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
137
+
160
138
// Write invalid json for the only log record
161
139
logFile.writeAsStringSync ('{{\n ' );
162
140
@@ -192,6 +170,8 @@ void main() {
192
170
});
193
171
194
172
test ('Catching cast errors for each log record silently' , () async {
173
+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
174
+
195
175
// Write a json array to the log file which will cause
196
176
// a cast error when parsing each line
197
177
logFile.writeAsStringSync ('[{}, 1, 2, 3]\n ' );
@@ -269,3 +249,35 @@ void main() {
269
249
expect (newString, testString);
270
250
});
271
251
}
252
+
253
+ (FileSystem fs, File logFile, FakeAnalytics analytics) _setUpFakeAnalytics () {
254
+ final fs = MemoryFileSystem .test (style: FileSystemStyle .posix);
255
+ final homeDirectory = fs.directory ('home' );
256
+ final logFile = fs.file (p.join (
257
+ homeDirectory.path,
258
+ kDartToolDirectoryName,
259
+ kLogFileName,
260
+ ));
261
+
262
+ // Create the initialization analytics instance to onboard the tool
263
+ final initializationAnalytics = Analytics .fake (
264
+ tool: DashTool .flutterTool,
265
+ homeDirectory: homeDirectory,
266
+ dartVersion: 'dartVersion' ,
267
+ fs: fs,
268
+ platform: DevicePlatform .macos,
269
+ );
270
+ initializationAnalytics.clientShowedMessage ();
271
+
272
+ // This instance is free to send events since the instance above
273
+ // has confirmed that the client has shown the message
274
+ final analytics = Analytics .fake (
275
+ tool: DashTool .flutterTool,
276
+ homeDirectory: homeDirectory,
277
+ dartVersion: 'dartVersion' ,
278
+ fs: fs,
279
+ platform: DevicePlatform .macos,
280
+ );
281
+
282
+ return (fs, logFile, analytics);
283
+ }
0 commit comments