Skip to content

Commit b02d1dc

Browse files
authored
Add tests and patch up remaining dartdoc_options.yaml options (#1790)
* dartdoc options testing * Fully support all declared options * dartfmt * scorer tuning isn't officially supported * Adjust whitespace
1 parent 2069408 commit b02d1dc

File tree

15 files changed

+161
-18
lines changed

15 files changed

+161
-18
lines changed

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ authoring doc comments for Dart with `dartdoc`.
100100
Creating a file named dartdoc_options.yaml at the top of your package can change how Dartdoc
101101
generates docs.
102102

103+
An example:
104+
103105
```yaml
104106
dartdoc:
105107
categories:
@@ -110,8 +112,11 @@ dartdoc:
110112
markdown: doc/Second.md
111113
name: Great
112114
categoryOrder: ["First Category", "Second Category"]
115+
examplePathPrefix: 'subdir/with/examples'
116+
includeExternal: ['bin/unusually_located_library.dart']
113117
linkTo:
114118
url: "https://my.dartdocumentationsite.org/dev/%v%"
119+
showUndocumentedCategories: true
115120
```
116121
117122
Unrecognized options will be ignored. Supported options:
@@ -122,9 +127,16 @@ Unrecognized options will be ignored. Supported options:
122127
defined in dartdoc_options.yaml, those declared categories in the source code will be invisible.
123128
* **categoryOrder**: Specify the order of topics for display in the sidebar and
124129
the package page.
130+
* **examplePathPrefix**: Specify the location of the example directory for resolving `@example`
131+
directives.
125132
* **exclude**: Specify a list of library names to avoid generating docs for,
126133
overriding any specified in include.
134+
* **favicon**: A path to a favicon for the generated docs.
135+
* **footer**: A list of paths to footer files containing HTML text.
136+
* **footerText**: A list of paths to text files for optional text next to the package name and version
137+
* **header**: A list of paths to header files containing HTML text.
127138
* **include**: Specify a list of library names to generate docs for, ignoring all others.
139+
* **includeExternal**: Specify a list of library filenames to add to the list of documented libraries.
128140
* **linkTo**: For other packages depending on this one, if this map is defined those packages
129141
will use the settings here to control how hyperlinks to the package are generated.
130142
This will override the default for packages hosted on pub.dartlang.org.
@@ -139,20 +151,15 @@ Unrecognized options will be ignored. Supported options:
139151
* `%n%`: The name of this package, as defined in pubspec.yaml.
140152
* `%v%`: The version of this package as defined in pubspec.yaml.
141153

142-
The following are experimental options whose semantics are in flux and may be buggy. If you
143-
use one, please keep a close eye on the changing semantics. In general, paths are relative
144-
to the directory the dartdoc_options.yaml the option is defined in and should be specified
145-
as POSIX paths. Dartdoc will convert POSIX paths automatically on Windows.
154+
In general, paths are relative to the directory the dartdoc_options.yaml the option is defined in
155+
and should be specified as POSIX paths. Dartdoc will convert POSIX paths automatically on Windows.
156+
157+
Unsupported and experimental options:
158+
159+
* **ambiguousReexportScorerMinConfidence**: The ambiguous reexport scorer will emit a warning if
160+
it is not at least this confident. Adjusting this may be necessary for some complex
161+
packages but most of the time, the default is OK. Default: 0.1
146162

147-
* **ambiguousReexportScorerMinConfidence**: The ambiguous reexport scorer will emit a warning if
148-
it is not at least this confident. Default: 0.1
149-
* **examplePathPrefix**: Specify the prefix for the example paths, defaulting to the project root.
150-
* **favicon**: A path to a favicon for the generated docs.
151-
* **footer**: A list of paths to footer files containing HTML text.
152-
* **footerText**: A list of paths to text files for optional text next to the package name and version
153-
* **header**: A list of paths to header files containing HTML text.
154-
* **includeExternal**: Specify a list of library filenames to add to the list of documented libraries.
155-
156163
### Categories
157164

158165
You can tag libraries or top level classes, functions, and variables in their documentation with

lib/src/model.dart

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6393,6 +6393,7 @@ class PackageBuilder {
63936393
/// source list.
63946394
if (originalSources == null) originalSources = new Set()..addAll(sources);
63956395
files.addAll(driver.knownFiles);
6396+
files.addAll(_includeExternalsFrom(driver.knownFiles));
63966397
current = _packageMetasForFiles(files);
63976398
// To get canonicalization correct for non-locally documented packages
63986399
// (so we can generate the right hyperlinks), it's vital that we
@@ -6463,6 +6464,22 @@ class PackageBuilder {
64636464
}
64646465
}
64656466

6467+
/// Calculate includeExternals based on a list of files. Assumes each
6468+
/// file might be part of a [DartdocOptionContext], and loads those
6469+
/// objects to find any [DartdocOptionContext.includeExternal] configurations
6470+
/// therein.
6471+
Iterable<String> _includeExternalsFrom(Iterable<String> files) {
6472+
Set<String> includeExternalsFound = new Set();
6473+
for (String file in files) {
6474+
DartdocOptionContext fileContext =
6475+
new DartdocOptionContext.fromContext(config, new File(file));
6476+
if (fileContext.includeExternal != null) {
6477+
includeExternalsFound.addAll(fileContext.includeExternal);
6478+
}
6479+
}
6480+
return includeExternalsFound;
6481+
}
6482+
64666483
Set<String> get getFiles {
64676484
Set<String> files = new Set();
64686485
files.addAll(config.topLevelPackageMeta.isSdk
@@ -6478,11 +6495,8 @@ class PackageBuilder {
64786495
files.add(source.fullName);
64796496
});
64806497
}
6481-
// Use the includeExternals.
6482-
for (String fullName in driver.knownFiles) {
6483-
if (config.includeExternal.any((string) => fullName.endsWith(string)))
6484-
files.add(fullName);
6485-
}
6498+
6499+
files.addAll(_includeExternalsFrom(files));
64866500
return new Set.from(files.map((s) => new File(s).absolute.path));
64876501
}
64886502

test/dartdoc_test.dart

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,81 @@ void main() {
3535
argv..addAll(['--input', packageRoot.path])..addAll(outputParam)));
3636
}
3737

38+
group('Option handling', () {
39+
Dartdoc dartdoc;
40+
DartdocResults results;
41+
PackageGraph p;
42+
43+
setUp(() async {
44+
dartdoc = await buildDartdoc([], testPackageOptions);
45+
results = await dartdoc.generateDocsBase();
46+
p = results.packageGraph;
47+
});
48+
49+
test('generator parameters', () async {
50+
File favicon = new File(
51+
pathLib.joinAll([tempDir.path, 'static-assets', 'favicon.png']));
52+
File index = new File(pathLib.joinAll([tempDir.path, 'index.html']));
53+
expect(favicon.readAsStringSync(),
54+
contains('Not really a png, but a test file'));
55+
String indexString = index.readAsStringSync();
56+
expect(indexString, contains('Footer things'));
57+
expect(indexString, contains('footer.txt data'));
58+
expect(indexString, contains('HTML header file'));
59+
});
60+
61+
test('examplePathPrefix', () async {
62+
Class UseAnExampleHere = p.allCanonicalModelElements
63+
.firstWhere((ModelElement c) => c.name == 'UseAnExampleHere');
64+
expect(
65+
UseAnExampleHere.documentationAsHtml,
66+
contains(
67+
'An example of an example in an unusual example location.'));
68+
});
69+
70+
test('includeExternal and showUndocumentedCategories', () async {
71+
Class Something = p.allCanonicalModelElements
72+
.firstWhere((ModelElement c) => c.name == 'Something');
73+
expect(Something.isPublic, isTrue);
74+
expect(Something.displayedCategories, isNotEmpty);
75+
});
76+
});
77+
78+
group('Option handling with cross-linking', () {
79+
DartdocResults results;
80+
Package testPackageOptions;
81+
82+
setUp(() async {
83+
results = await (await buildDartdoc(
84+
['--link-to-remote'], testPackageOptionsImporter))
85+
.generateDocsBase();
86+
testPackageOptions = results.packageGraph.packages
87+
.firstWhere((Package p) => p.name == 'test_package_options');
88+
});
89+
90+
test('linkToUrl', () async {
91+
Library main = testPackageOptions.allLibraries
92+
.firstWhere((Library l) => l.name == 'main');
93+
Class UseAnExampleHere = main.allClasses
94+
.firstWhere((Class c) => c.name == 'UseAnExampleHere');
95+
expect(testPackageOptions.documentedWhere,
96+
equals(DocumentLocation.remote));
97+
expect(
98+
UseAnExampleHere.href,
99+
equals(
100+
'https://nonexistingsuperpackage.topdomain/test_package_options/0.0.1/main/UseAnExampleHere-class.html'));
101+
});
102+
103+
test('includeExternal works via remote', () async {
104+
Library unusualLibrary = testPackageOptions.allLibraries
105+
.firstWhere((Library l) => l.name == 'unusualLibrary');
106+
expect(
107+
unusualLibrary.allClasses
108+
.firstWhere((Class c) => c.name == 'Something'),
109+
isNotNull);
110+
});
111+
});
112+
38113
test('with broken reexport chain', () async {
39114
Dartdoc dartdoc = await buildDartdoc([], testPackageImportExportError);
40115
DartdocResults results = await dartdoc.generateDocsBase();

test/src/utils.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ final Directory testPackageIncludeExclude =
3131
new Directory('testing/test_package_include_exclude');
3232
final Directory testPackageImportExportError =
3333
new Directory('testing/test_package_import_export_error');
34+
final Directory testPackageOptions =
35+
new Directory('testing/test_package_options');
36+
final Directory testPackageOptionsImporter =
37+
new Directory('testing/test_package_options_importer');
3438

3539
/// Convenience factory to build a [DartdocGeneratorOptionContext] and associate
3640
/// it with a [DartdocOptionSet] based on the current working directory and/or
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// A library placed in an unusual place.
2+
library unusualLibrary;
3+
4+
class Something {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dartdoc:
2+
examplePathPrefix: 'package_examples'
3+
favicon: 'anicon.png'
4+
footer: ['extras/footer-things.html']
5+
footerText: ['extras/footer.txt']
6+
header: ['extras/header.html']
7+
includeExternal: ['bin/unusual_library.dart']
8+
linkTo:
9+
url: 'https://nonexistingsuperpackage.topdomain/%n%/%v%'
10+
showUndocumentedCategories: true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Footer things.
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
footer.txt data
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HTML header file
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// Reference an example in a non-default location, based on dartdoc_options.yaml.
2+
///
3+
/// {@example foo.dart}
4+
/// {@category Undocumented}
5+
class UseAnExampleHere {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```
2+
/// An example of an example in an unusual example location.
3+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: test_package_options
2+
version: 0.0.1
3+
description: A simple console application.
4+
environment:
5+
sdk: '>=2.0.0 <3.0.0'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'package:test_package_options/main.dart';
2+
3+
class X extends UseAnExampleHere {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: test_package_options_importer
2+
version: 0.0.1
3+
description: A simple console application.
4+
environment:
5+
sdk: '>=2.0.0 <3.0.0'
6+
dependencies:
7+
test_package_options:
8+
path: '../test_package_options'

0 commit comments

Comments
 (0)