@@ -13,52 +13,20 @@ import 'package:unified_analytics/src/utils.dart';
1313import 'package:unified_analytics/unified_analytics.dart' ;
1414
1515void main () {
16- late FakeAnalytics analytics;
17- late Directory homeDirectory;
18- late MemoryFileSystem fs;
19- late File logFile;
20-
2116 final testEvent = Event .hotReloadTime (timeMs: 10 );
2217
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-
5318 test ('Ensure that log file is created' , () {
19+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
5420 expect (logFile.existsSync (), true );
5521 });
5622
5723 test ('LogFileStats is null before events are sent' , () {
24+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
5825 expect (analytics.logFileStats (), isNull);
5926 });
6027
6128 test ('LogFileStats returns valid response after sent events' , () async {
29+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
6230 final countOfEventsToSend = 10 ;
6331
6432 for (var i = 0 ; i < countOfEventsToSend; i++ ) {
@@ -71,6 +39,8 @@ void main() {
7139 });
7240
7341 test ('The only record in the log file is malformed' , () async {
42+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
43+
7444 // Write invalid json for the only log record
7545 logFile.writeAsStringSync ('{{\n ' );
7646
@@ -93,6 +63,8 @@ void main() {
9363 });
9464
9565 test ('The first record is malformed, but rest are valid' , () async {
66+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
67+
9668 // Write invalid json for the only log record
9769 logFile.writeAsStringSync ('{{\n ' );
9870
@@ -109,6 +81,8 @@ void main() {
10981 });
11082
11183 test ('Several records are malformed' , () async {
84+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
85+
11286 final countOfMalformedRecords = 4 ;
11387 for (var i = 0 ; i < countOfMalformedRecords; i++ ) {
11488 final currentContents = logFile.readAsStringSync ();
@@ -136,6 +110,8 @@ void main() {
136110 });
137111
138112 test ('Valid json but invalid keys' , () {
113+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
114+
139115 // The second line here is missing the "events" top level
140116 // key which should cause an error for that record only
141117 //
@@ -157,6 +133,8 @@ void main() {
157133 });
158134
159135 test ('Malformed record gets phased out after several events' , () async {
136+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
137+
160138 // Write invalid json for the only log record
161139 logFile.writeAsStringSync ('{{\n ' );
162140
@@ -192,6 +170,8 @@ void main() {
192170 });
193171
194172 test ('Catching cast errors for each log record silently' , () async {
173+ final (fs, logFile, analytics) = _setUpFakeAnalytics ();
174+
195175 // Write a json array to the log file which will cause
196176 // a cast error when parsing each line
197177 logFile.writeAsStringSync ('[{}, 1, 2, 3]\n ' );
@@ -269,3 +249,35 @@ void main() {
269249 expect (newString, testString);
270250 });
271251}
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