Skip to content

Commit 10f30e8

Browse files
authored
Parameter error display tests (#1895)
* Fix a bug and add more tests for parameter parsing * dartfmt * Hide observatory lines from downstream users * Lines are out of order on windows
1 parent 604767f commit 10f30e8

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

bin/dartdoc.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ Future<void> main(List<String> arguments) async {
5757
// TODO(jcollins-g): use exit once dart-lang/sdk#31747 is fixed.
5858
exitCode = 64;
5959
return;
60-
} on DartdocOptionError catch (e) {
61-
stderr.writeln(' fatal error: ${e.message}');
62-
stderr.writeln('');
63-
_printUsage(optionSet.argParser);
64-
exitCode = 64;
65-
return;
6660
}
6761
if (optionSet['help'].valueAt(Directory.current)) {
6862
_printHelp(optionSet.argParser);
@@ -75,8 +69,17 @@ Future<void> main(List<String> arguments) async {
7569
return;
7670
}
7771

78-
DartdocProgramOptionContext config =
79-
new DartdocProgramOptionContext(optionSet, null);
72+
DartdocProgramOptionContext config;
73+
try {
74+
config = new DartdocProgramOptionContext(optionSet, null);
75+
} on DartdocOptionError catch (e) {
76+
stderr.writeln(' fatal error: ${e.message}');
77+
stderr.writeln('');
78+
await stderr.flush();
79+
_printUsage(optionSet.argParser);
80+
exitCode = 64;
81+
return;
82+
}
8083
startLogging(config);
8184

8285
Directory outputDir = new Directory(config.output);

test/dartdoc_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,43 @@ void main() {
131131
await Future.wait(CoverageSubprocessLauncher.coverageResults);
132132
});
133133

134+
test('invalid parameters return non-zero and print a fatal-error',
135+
() async {
136+
List outputLines = [];
137+
await expectLater(
138+
() => subprocessLauncher.runStreamed(
139+
Platform.resolvedExecutable,
140+
[
141+
dartdocPath,
142+
'--nonexisting',
143+
],
144+
perLine: outputLines.add),
145+
throwsA(const TypeMatcher<ProcessException>()));
146+
expect(
147+
outputLines.firstWhere((l) => l.startsWith(' fatal')),
148+
equals(
149+
' fatal error: Could not find an option named "nonexisting".'));
150+
});
151+
152+
test('missing a required file path prints a fatal-error', () async {
153+
List outputLines = [];
154+
String impossiblePath = pathLib.join(dartdocPath, 'impossible');
155+
await expectLater(
156+
() => subprocessLauncher.runStreamed(
157+
Platform.resolvedExecutable,
158+
[
159+
dartdocPath,
160+
'--input',
161+
impossiblePath,
162+
],
163+
perLine: outputLines.add),
164+
throwsA(const TypeMatcher<ProcessException>()));
165+
expect(
166+
outputLines.firstWhere((l) => l.startsWith(' fatal')),
167+
startsWith(
168+
' fatal error: Argument --input, set to ${impossiblePath}, resolves to missing path: '));
169+
});
170+
134171
test('errors cause non-zero exit when warnings are off', () async {
135172
expect(
136173
() => subprocessLauncher.runStreamed(Platform.resolvedExecutable, [

test/src/utils.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,11 @@ class CoverageSubprocessLauncher extends SubprocessLauncher {
182182

183183
Completer<String> portAsString = new Completer();
184184
void parsePortAsString(String line) {
185-
if (perLine != null) perLine(line);
186-
if (!portAsString.isCompleted) {
185+
if (!portAsString.isCompleted && coverageEnabled) {
187186
Match m = observatoryPortRegexp.matchAsPrefix(line);
188187
if (m?.group(1) != null) portAsString.complete(m.group(1));
188+
} else {
189+
if (perLine != null) perLine(line);
189190
}
190191
}
191192

0 commit comments

Comments
 (0)