Skip to content

Commit c83d24f

Browse files
committed
fail gracefully when no pubspec is found
Closes #741 LGTM given by: @sethladd Squashed commit of the following: commit b1e7343 Author: Seth Ladd <[email protected]> Date: Tue Jul 28 10:16:45 2015 -0700 fail gracefully when no pubspec is found
1 parent 908dc6a commit c83d24f

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

bin/dartdoc.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ main(List<String> arguments) async {
104104
? new PackageMeta.fromSdk(sdkDir, sdkReadmePath: readme)
105105
: new PackageMeta.fromDir(inputDir);
106106

107+
if (!packageMeta.isValid) {
108+
print('Unable to generate documentation.');
109+
packageMeta.getInvalidReasons().map((r) => '* $r').forEach(print);
110+
exit(1);
111+
}
112+
107113
print("Generating documentation for '${packageMeta}' into "
108114
"${outputDir.absolute.path}${Platform.pathSeparator}");
109115
print('');

lib/src/package_meta.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ abstract class PackageMeta {
2929
FileContents getLicenseContents();
3030
FileContents getChangelogContents();
3131

32+
/// Returns true if we are a valid package, valid enough to generate docs.
33+
bool get isValid;
34+
35+
/// Returns a list of reasons this package is invalid, or an
36+
/// empty list if no reasons found.
37+
///
38+
/// If the list is empty, this package is valid.
39+
List<String> getInvalidReasons();
40+
3241
String toString() => name;
3342
}
3443

@@ -89,6 +98,22 @@ class _FilePackageMeta extends PackageMeta {
8998
_locate(dir, ['changelog.md', 'changelog.txt', 'changelog']));
9099
return _changelog;
91100
}
101+
102+
@override
103+
bool get isValid => getInvalidReasons().isEmpty;
104+
105+
/// Returns a list of reasons this package is invalid, or an
106+
/// empty list if no reasons found.
107+
@override
108+
List<String> getInvalidReasons() {
109+
var reasons = [];
110+
if (_pubspec == null || _pubspec.isEmpty) {
111+
reasons.add('No pubspec.yaml found');
112+
} else if (!_pubspec.containsKey('name')) {
113+
reasons.add('No name found in pubspec.yaml');
114+
}
115+
return reasons;
116+
}
92117
}
93118

94119
File _locate(Directory dir, List<String> fileNames) {
@@ -127,6 +152,12 @@ class _SdkMeta extends PackageMeta {
127152
return f.existsSync() ? new FileContents(f) : null;
128153
}
129154

155+
@override
156+
bool get isValid => true;
157+
158+
@override
159+
List<String> getInvalidReasons() => [];
160+
130161
FileContents getLicenseContents() => null;
131162

132163
// TODO: The changelog doesn't seem to be available in the sdk.

test/package_meta_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,28 @@ import 'dart:io';
99
import 'package:cli_util/cli_util.dart';
1010
import 'package:dartdoc/src/package_meta.dart';
1111
import 'package:test/test.dart';
12+
import 'package:path/path.dart' as path;
1213

1314
void main() {
15+
group('PackageMeta for a directory without a pubspec', () {
16+
PackageMeta p;
17+
18+
setUp(() {
19+
var d = new Directory(
20+
path.join(Directory.current.path, 'test_package_not_valid'));
21+
if (!d.existsSync()) {
22+
throw "$d cannot be found";
23+
}
24+
p = new PackageMeta.fromDir(d);
25+
});
26+
27+
test('is not valid', () {
28+
expect(p.isValid, isFalse);
29+
expect(p.getInvalidReasons(), isNotEmpty);
30+
expect(p.getInvalidReasons(), contains('No pubspec.yaml found'));
31+
});
32+
});
33+
1434
group('PackageMeta.fromDir for this package', () {
1535
PackageMeta p = new PackageMeta.fromDir(Directory.current);
1636

@@ -30,6 +50,11 @@ void main() {
3050
expect(p.homepage, equals('https://github.com/dart-lang/dartdoc'));
3151
});
3252

53+
test('is valid', () {
54+
expect(p.isValid, isTrue);
55+
expect(p.getInvalidReasons(), isEmpty);
56+
});
57+
3358
test('has a readme', () {
3459
expect(p.getReadmeContents(), isNotNull);
3560
expect(p.getReadmeContents().contents, contains('''
@@ -56,6 +81,11 @@ generated from Dart source code.'''));
5681
expect(p.name, 'Dart SDK');
5782
});
5883

84+
test('is valid', () {
85+
expect(p.isValid, isTrue);
86+
expect(p.getInvalidReasons(), isEmpty);
87+
});
88+
5989
test('has a version', () {
6090
expect(p.version, isNotNull);
6191
});

test_package/lib/fake.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,6 @@ class WithGetterAndSetter {
350350
/// Throws if set to an imaginary number.
351351
void set lengthX(int _length) {}
352352
}
353+
354+
/// I have a generic and it extends [Foo2]
355+
class HasGenericWithExtends<T extends Foo2> {}

test_package_not_valid/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Does not contain a pubspec.yaml
2+
3+
This will fail, because dartdoc currently requires
4+
a pubspec.yaml file to run.
5+
6+
We can revisit that decision.

0 commit comments

Comments
 (0)