Skip to content

Commit e17d1f2

Browse files
authored
Do not write progress if format is json (#225)
1 parent 2ccfe3e commit e17d1f2

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

packages/custom_lint/lib/custom_lint.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ Future<void> _runServer(
9696
await runner.initialize;
9797

9898
final log = CliLogger();
99-
final progress = log.progress('Analyzing');
99+
final progress =
100+
format == OutputFormatEnum.json ? null : log.progress('Analyzing');
100101

101102
await _runPlugins(
102103
runner,
@@ -134,12 +135,12 @@ Future<void> _runServer(
134135
Future<void> _runPlugins(
135136
CustomLintRunner runner, {
136137
required Logger log,
137-
required Progress progress,
138138
required bool reload,
139139
required Directory workingDirectory,
140140
required bool fatalInfos,
141141
required bool fatalWarnings,
142142
required OutputFormatEnum format,
143+
Progress? progress,
143144
}) async {
144145
final lints = await runner.getLints(reload: reload);
145146

packages/custom_lint/lib/src/cli_logger.dart

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ class CliLogger implements Logger {
6161
Progress progress(String message) {
6262
_cancelProgress();
6363

64-
final progress = ansi.useAnsi
65-
? AnsiProgress(ansi, message)
66-
: SimpleProgress(this, message);
64+
final progress = _LineOnFinishProgress(
65+
ansi.useAnsi
66+
? AnsiProgress(ansi, message)
67+
: SimpleProgress(this, message),
68+
log: this,
69+
);
6770
_currentProgress = progress;
6871
return progress;
6972
}
@@ -72,3 +75,29 @@ class CliLogger implements Logger {
7275
@Deprecated('This method will be removed in the future')
7376
void flush() {}
7477
}
78+
79+
class _LineOnFinishProgress implements Progress {
80+
const _LineOnFinishProgress(this.impl, {required this.log});
81+
82+
final CliLogger log;
83+
final Progress impl;
84+
85+
@override
86+
Duration get elapsed => impl.elapsed;
87+
88+
@override
89+
String get message => impl.message;
90+
91+
@override
92+
void cancel() {
93+
impl.cancel();
94+
}
95+
96+
@override
97+
void finish({String? message, bool showTiming = false}) {
98+
impl.finish(message: message, showTiming: showTiming);
99+
100+
// Separate progress from results
101+
log.stdout('');
102+
}
103+
}

packages/custom_lint/lib/src/output/render_lints.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import 'output_format.dart';
1313
void renderLints(
1414
List<AnalysisErrorsParams> lints, {
1515
required Logger log,
16-
required Progress progress,
1716
required Directory workingDirectory,
1817
required bool fatalInfos,
1918
required bool fatalWarnings,
2019
required OutputFormatEnum format,
20+
Progress? progress,
2121
}) {
2222
final OutputFormat outputFormat;
2323
switch (format) {
@@ -69,10 +69,7 @@ void renderLints(
6969
});
7070

7171
// Finish progress and display duration (only when ANSI is supported)
72-
progress.finish(showTiming: true);
73-
74-
// Separate progress from results
75-
log.stdout('');
72+
progress?.finish(showTiming: true);
7673

7774
outputFormat.render(
7875
errors: errors,

packages/custom_lint/test/cli_process_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ No issues found!
208208
}
209209
],
210210
});
211-
expect(process.stdout, 'Analyzing...\n\n$json\n');
211+
expect(process.stdout, '$json\n');
212212
} else {
213213
expect(process.stdout, '''
214214
Analyzing...

packages/custom_lint/test/cli_test.dart

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ void main() {
154154

155155
if (format == 'json') {
156156
expect(process.stdout, '''
157-
Analyzing...
158-
159157
{"version":1,"diagnostics":[]}
160158
''');
161159
} else {
@@ -202,24 +200,32 @@ No issues found!
202200
final err = process.stderr.map(utf8.decode);
203201

204202
expect(err, emitsDone);
205-
expect(
206-
out.join(),
207-
completion(
208-
allOf(
209-
startsWith('Analyzing...'),
210-
format == 'json'
211-
? endsWith('${jsonLints(app.resolveSymbolicLinksSync())}\n')
212-
: endsWith('''
203+
204+
if (format == 'json') {
205+
expect(
206+
out.join(),
207+
completion(
208+
equals('${jsonLints(app.resolveSymbolicLinksSync())}\n'),
209+
),
210+
);
211+
} else {
212+
expect(
213+
out.join(),
214+
completion(
215+
allOf(
216+
startsWith('Analyzing...'),
217+
endsWith('''
213218
lib/another.dart:1:6 • Hello world • hello_world • INFO
214219
lib/another.dart:1:6 • Oy • oy • INFO
215220
lib/main.dart:1:6 • Hello world • hello_world • INFO
216221
lib/main.dart:1:6 • Oy • oy • INFO
217222
218223
4 issues found.
219224
'''),
225+
),
220226
),
221-
),
222-
);
227+
);
228+
}
223229
expect(await process.exitCode, 1);
224230
});
225231
});

0 commit comments

Comments
 (0)