1
- // ignore_for_file: avoid_print
2
-
3
1
import 'dart:io' ;
4
2
5
3
import 'package:approval_tests/approval_tests.dart' ;
6
4
7
- void main () async {
8
- final searchDirectory = Directory .current;
9
-
5
+ void main (List <String > args) async {
10
6
final List <Future <void >> tasks = [];
7
+ bool isProcessingTasks = false ;
8
+
9
+ void processUnapprovedFile (File receivedFile) {
10
+ if (! receivedFile.existsSync ()) {
11
+ ApprovalLogger .exception (
12
+ 'Error: the file below does not exist for review comparison:' ,
13
+ );
14
+ ApprovalLogger .exception (receivedFile.path);
15
+ return ;
16
+ }
17
+
18
+ final approvedFileName =
19
+ receivedFile.path.replaceAll ('.received.txt' , '.approved.txt' );
20
+ final approvedFile = File (approvedFileName);
21
+
22
+ if (approvedFile.existsSync ()) {
23
+ tasks.add (processFile (approvedFile, receivedFile));
24
+ } else {
25
+ tasks.add (processFile (null , receivedFile));
26
+ }
27
+ }
11
28
12
- /// Recursively search for current files
13
- await for (final file in searchDirectory.list (recursive: true )) {
14
- if (file.path.endsWith ('.received.txt' )) {
15
- final reviewFile = file;
16
- final approvedFileName =
17
- file.path.replaceAll ('.received.txt' , '.approved.txt' );
18
- final approvedFile = File (approvedFileName);
29
+ Future <List <File >> getUnapprovedFiles () async {
30
+ final files = < File > [];
31
+ final searchDirectory = Directory .current;
19
32
20
- if (approvedFile.existsSync ()) {
21
- tasks.add (processFile (approvedFile, reviewFile));
33
+ await for (final file in searchDirectory.list (recursive: true )) {
34
+ if (file.path.endsWith ('.received.txt' )) {
35
+ files.add (file as File );
22
36
}
23
37
}
38
+
39
+ return files;
24
40
}
25
41
26
- await Future .wait (tasks);
42
+ /// If no args, then searching the whole project
43
+ if (args.isEmpty || args[0 ].isEmpty) {
44
+ for (final file in await getUnapprovedFiles ()) {
45
+ if (file.path.endsWith ('.received.txt' )) {
46
+ isProcessingTasks = true ;
47
+ processUnapprovedFile (file);
48
+ }
49
+ }
50
+ } else {
51
+ /// If here, have args. If arg is an option...
52
+ if (args[0 ][0 ] == '-' ) {
53
+ if (args[0 ] == '--help' ) {
54
+ ApprovalLogger .log ('''
55
+ Manage your package:approval_tests files.
56
+
57
+ Common usage:
58
+
59
+ dart run approval_tests:review
60
+ Reviews all project .received.txt files
61
+
62
+ dart run approval_tests:review --list
63
+ List project's .received.txt files
27
64
28
- ApprovalLogger .success (
29
- 'Review process completed: ${tasks .length } files reviewed.' ,
30
- );
65
+ Usage: dart run approval_tests:review [arguments]
66
+
67
+ Arguments:
68
+ --help Print this usage information.
69
+ --list Print a list of project .received.txt files.
70
+ <index> Review an .received.txt file indexed by --list.
71
+ <path/to/.received.txt> Review an .received.txt file.''' );
72
+ } else if (args[0 ] == '--list' ) {
73
+ final unapprovedFiles = await getUnapprovedFiles ();
74
+ final fileCount = unapprovedFiles.length;
75
+ for (int i = 0 ; i < fileCount; i++ ) {
76
+ ApprovalLogger .log (
77
+ '${i .toString ().padLeft (3 )} ${unapprovedFiles [i ].path }' ,
78
+ );
79
+ }
80
+ ApprovalLogger .log ('Found $fileCount received files.' );
81
+ if (fileCount > 0 ) {
82
+ ApprovalLogger .log (
83
+ "\n To review one, run: dart run approval_tests:review <index>\n To review all, run: dart run approval_tests:review" ,
84
+ );
85
+ }
86
+
87
+ writeUnapprovedFiles (unapprovedFiles);
88
+ } else {
89
+ ApprovalLogger .log (
90
+ "Unknown option '${args [0 ]}'. See '--help' for more details." ,
91
+ );
92
+ }
93
+ } else {
94
+ /// If here, arg is a path or an index in the list of paths
95
+ File ? unapprovedFile;
96
+ final arg = args[0 ];
97
+ final int ? maybeIntValue = int .tryParse (arg);
98
+ if (maybeIntValue == null ) {
99
+ unapprovedFile = File (arg);
100
+ } else {
101
+ final unapprovedFilePaths = readReceivedFiles ();
102
+ if (maybeIntValue >= 0 && maybeIntValue < unapprovedFilePaths.length) {
103
+ unapprovedFile = File (unapprovedFilePaths[maybeIntValue]);
104
+ } else {
105
+ ApprovalLogger .log (
106
+ 'No received file with an index of $maybeIntValue ' ,
107
+ );
108
+ }
109
+ }
110
+ if (unapprovedFile != null ) {
111
+ isProcessingTasks = true ;
112
+ processUnapprovedFile (unapprovedFile);
113
+ }
114
+ }
115
+ }
116
+
117
+ if (isProcessingTasks) {
118
+ if (tasks.isEmpty) {
119
+ ApprovalLogger .exception ('No received test results to review!' );
120
+ } else {
121
+ final tasksCount = tasks.length;
122
+ await Future .wait (tasks);
123
+ ApprovalLogger .success (
124
+ 'Review completed. $tasksCount test results reviewed.' ,
125
+ );
126
+ }
127
+ }
31
128
}
32
129
33
- /// Check of the files are different using "git diff"
34
- Future <void > processFile (File approvedFile, FileSystemEntity reviewFile) async {
35
- final resultString = GitReporter .gitDiffFiles (approvedFile, reviewFile);
130
+ void writeUnapprovedFiles (List <File >? unapprovedFiles) {
131
+ final file = File (ApprovalTestsConstants .receivedFilesPath)
132
+ ..createSync (recursive: true );
133
+ if (unapprovedFiles == null ) {
134
+ file.writeAsStringSync ('' );
135
+ } else {
136
+ file.writeAsStringSync (unapprovedFiles.map ((file) => file.path).join ('\n ' ));
137
+ }
138
+ }
139
+
140
+ List <String > readReceivedFiles () {
141
+ List <String > result = < String > [];
36
142
143
+ final file = File (ApprovalTestsConstants .receivedFilesPath);
144
+ if (file.existsSync ()) {
145
+ final String fileContents = file.readAsStringSync ();
146
+ result = fileContents.split ('\n ' );
147
+ } else {
148
+ result = [];
149
+ }
150
+
151
+ return result;
152
+ }
153
+
154
+ /// Check of the files are different using "git diff"
155
+ Future <void > processFile (File ? approvedFile, File receivedFile) async {
156
+ late String resultString;
37
157
ComparatorIDE comparatorIDE = ComparatorIDE .vsCode;
38
158
39
- if (resultString.isNotEmpty || resultString.isNotEmpty) {
40
- // final String fileNameWithoutExtension =
41
- // approvedFile.path.split('/').last.split('.').first;
42
- // GitReporter.printGitDiffs(fileNameWithoutExtension, resultString);
43
- const CommandLineReporter ().report (approvedFile.path, reviewFile.path);
159
+ if (approvedFile == null ) {
160
+ final unapprovedText = receivedFile.readAsStringSync ();
161
+ resultString = "Data in '${receivedFile .path }':\n $unapprovedText " ;
162
+ } else {
163
+ final gitDiff = GitReporter .gitDiffFiles (approvedFile, receivedFile);
164
+ resultString = gitDiff;
165
+ }
166
+
167
+ if (resultString.isNotEmpty) {
168
+ GitReporter .printGitDiffs (receivedFile.path, resultString, showTip: false );
44
169
45
170
String ? firstCharacter;
46
171
47
172
do {
48
173
stdout.write ('Accept changes? (y/N/[v]iew): ' );
49
174
final response = stdin.readLineSync ()? .trim ().toLowerCase ();
50
175
51
- if (response == null || response.isEmpty) {
52
- firstCharacter = null ;
53
- } else {
176
+ firstCharacter = null ;
177
+ if (response != null && response.isNotEmpty) {
54
178
firstCharacter = response[0 ];
55
179
}
56
180
181
+ final receivedFilename = receivedFile.path;
182
+ final approvedFilename = receivedFile.path
183
+ .replaceAll (Namer .receivedExtension, Namer .approvedExtension);
184
+
57
185
if (firstCharacter == 'y' ) {
58
- await approvedFile.delete ();
59
- await reviewFile .rename (approvedFile.path );
186
+ await approvedFile? .delete ();
187
+ await receivedFile .rename (approvedFilename );
60
188
ApprovalLogger .success ('Approval test approved' );
61
189
} else if (firstCharacter == 'v' ) {
62
190
stdout.write ('Enter diff tool (code, studio): ' );
@@ -68,12 +196,19 @@ Future<void> processFile(File approvedFile, FileSystemEntity reviewFile) async {
68
196
}
69
197
final diffReporter = DiffReporter (ide: comparatorIDE);
70
198
if (diffReporter.isReporterAvailable) {
71
- final approvedFilename = approvedFile.path;
72
- final reviewFilename = reviewFile.path;
199
+ final approvedFilename = approvedFile? .path;
200
+
73
201
ApprovalLogger .log (
74
- "Executing '${diffReporter .defaultDiffInfo .command } ${diffReporter .defaultDiffInfo .arg } $approvedFilename $reviewFilename '" ,
202
+ "Executing '${diffReporter .defaultDiffInfo .command } ${diffReporter .defaultDiffInfo .arg } $approvedFilename $receivedFilename '" ,
75
203
);
76
- await diffReporter.report (approvedFilename, reviewFilename);
204
+ if (approvedFilename != null ) {
205
+ await diffReporter.report (approvedFilename, receivedFilename);
206
+ } else {
207
+ final processResult = Process .runSync ('code' , [receivedFilename]);
208
+ ApprovalLogger .log (
209
+ '______processResult: ${processResult .toString ()}' ,
210
+ );
211
+ }
77
212
} else {
78
213
ApprovalLogger .warning (
79
214
'No diff tool available: please check:\n Name: ${diffReporter .defaultDiffInfo .name }\n Path:${diffReporter .defaultDiffInfo .command }' ,
@@ -83,5 +218,13 @@ Future<void> processFile(File approvedFile, FileSystemEntity reviewFile) async {
83
218
ApprovalLogger .exception ('Approval test rejected' );
84
219
}
85
220
} while (firstCharacter == 'v' );
221
+ } else {
222
+ ApprovalLogger .success ('No differences found. Approval test approved.' );
86
223
}
87
224
}
225
+
226
+ bool isCodeCommandAvailable () {
227
+ final result = Process .runSync ('which' , ['code' ]);
228
+
229
+ return result.exitCode == 0 ;
230
+ }
0 commit comments