Skip to content

Commit b6fe5dd

Browse files
authored
Refactor lib/dartdoc.dart and dartdoc_test.dart (#2669)
1 parent b733d49 commit b6fe5dd

File tree

2 files changed

+43
-44
lines changed

2 files changed

+43
-44
lines changed

lib/dartdoc.dart

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import 'package:dartdoc/src/utils.dart';
2929
import 'package:dartdoc/src/version.dart';
3030
import 'package:dartdoc/src/warnings.dart';
3131
import 'package:html/parser.dart' show parse;
32+
import 'package:meta/meta.dart';
3233
import 'package:path/path.dart' as path;
3334

3435
export 'package:dartdoc/src/dartdoc_options.dart';
@@ -43,14 +44,14 @@ const String programName = 'dartdoc';
4344
const String dartdocVersion = packageVersion;
4445

4546
class DartdocFileWriter implements FileWriter {
46-
final String outputDir;
47+
final String _outputDir;
4748
@override
4849
final ResourceProvider resourceProvider;
4950
final Map<String, Warnable> _fileElementMap = {};
5051
@override
5152
final Set<String> writtenFiles = {};
5253

53-
DartdocFileWriter(this.outputDir, this.resourceProvider);
54+
DartdocFileWriter(this._outputDir, this.resourceProvider);
5455

5556
@override
5657
void writeBytes(
@@ -97,11 +98,11 @@ class DartdocFileWriter implements FileWriter {
9798
}
9899
}
99100

100-
/// Returns the file at [outFile] relative to [outputDir], creating the parent
101-
/// directory if necessary.
101+
/// Returns the file at [outFile] relative to [_outputDir], creating the
102+
/// parent directory if necessary.
102103
File _getFile(String outFile) {
103104
var file = resourceProvider
104-
.getFile(resourceProvider.pathContext.join(outputDir, outFile));
105+
.getFile(resourceProvider.pathContext.join(_outputDir, outFile));
105106
var parent = file.parent2;
106107
if (!parent.exists) {
107108
parent.create();
@@ -116,15 +117,15 @@ class Dartdoc {
116117
final Generator generator;
117118
final PackageBuilder packageBuilder;
118119
final DartdocOptionContext config;
119-
final Set<String> writtenFiles = {};
120-
Folder outputDir;
120+
final Set<String> _writtenFiles = {};
121+
Folder _outputDir;
121122

122123
// Fires when the self checks make progress.
123124
final StreamController<String> _onCheckProgress =
124125
StreamController(sync: true);
125126

126127
Dartdoc._(this.config, this.generator, this.packageBuilder) {
127-
outputDir = config.resourceProvider
128+
_outputDir = config.resourceProvider
128129
.getFolder(config.resourceProvider.pathContext.absolute(config.output))
129130
..create();
130131
}
@@ -183,16 +184,11 @@ class Dartdoc {
183184

184185
PackageGraph packageGraph;
185186

186-
/// Generate Dartdoc documentation.
187-
///
188-
/// [DartdocResults] is returned if dartdoc succeeds. [DartdocFailure] is
189-
/// thrown if dartdoc fails in an expected way, for example if there is an
190-
/// analysis error in the code.
187+
@visibleForTesting
191188
Future<DartdocResults> generateDocsBase() async {
192189
var stopwatch = Stopwatch()..start();
193-
double seconds;
194190
packageGraph = await packageBuilder.buildPackageGraph();
195-
seconds = stopwatch.elapsedMilliseconds / 1000.0;
191+
var seconds = stopwatch.elapsedMilliseconds / 1000.0;
196192
var libs = packageGraph.libraries.length;
197193
logInfo("Initialized dartdoc with $libs librar${libs == 1 ? 'y' : 'ies'} "
198194
'in ${seconds.toStringAsFixed(1)} seconds');
@@ -201,14 +197,14 @@ class Dartdoc {
201197
var generator = this.generator;
202198
if (generator != null) {
203199
// Create the out directory.
204-
if (!outputDir.exists) outputDir.create();
200+
if (!_outputDir.exists) _outputDir.create();
205201

206-
var writer = DartdocFileWriter(outputDir.path, config.resourceProvider);
202+
var writer = DartdocFileWriter(_outputDir.path, config.resourceProvider);
207203
await generator.generate(packageGraph, writer);
208204

209-
writtenFiles.addAll(writer.writtenFiles);
210-
if (config.validateLinks && writtenFiles.isNotEmpty) {
211-
validateLinks(packageGraph, outputDir.path);
205+
_writtenFiles.addAll(writer.writtenFiles);
206+
if (config.validateLinks && _writtenFiles.isNotEmpty) {
207+
_validateLinks(packageGraph, _outputDir.path);
212208
}
213209
}
214210

@@ -229,9 +225,14 @@ class Dartdoc {
229225
if (config.showStats) {
230226
logInfo(markdownStats.buildReport());
231227
}
232-
return DartdocResults(config.topLevelPackageMeta, packageGraph, outputDir);
228+
return DartdocResults(config.topLevelPackageMeta, packageGraph, _outputDir);
233229
}
234230

231+
/// Generate Dartdoc documentation.
232+
///
233+
/// [DartdocResults] is returned if dartdoc succeeds. [DartdocFailure] is
234+
/// thrown if dartdoc fails in an expected way, for example if there is an
235+
/// analysis error in the code.
235236
Future<DartdocResults> generateDocs() async {
236237
try {
237238
logInfo('Documenting ${config.topLevelPackageMeta}...');
@@ -252,7 +253,6 @@ class Dartdoc {
252253
return dartdocResults;
253254
} finally {
254255
// Clear out any cached tool snapshots and temporary directories.
255-
// ignore: unawaited_futures
256256
SnapshotCache.instance?.dispose();
257257
// ignore: unawaited_futures
258258
ToolTempFileTracker.instance?.dispose();
@@ -325,7 +325,7 @@ class Dartdoc {
325325
}
326326
if (visited.contains(fullPath)) continue;
327327
var relativeFullPath = path.relative(fullPath, from: normalOrigin);
328-
if (!writtenFiles.contains(relativeFullPath)) {
328+
if (!_writtenFiles.contains(relativeFullPath)) {
329329
// This isn't a file we wrote (this time); don't claim we did.
330330
_warn(
331331
packageGraph, PackageWarning.unknownFile, fullPath, normalOrigin);
@@ -471,7 +471,7 @@ class Dartdoc {
471471

472472
/// Don't call this method more than once, and only after you've
473473
/// generated all docs for the Package.
474-
void validateLinks(PackageGraph packageGraph, String origin) {
474+
void _validateLinks(PackageGraph packageGraph, String origin) {
475475
assert(_hrefs == null);
476476
_hrefs = packageGraph.allHrefs;
477477

test/end2end/dartdoc_test.dart

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class DartdocLoggingOptionContext extends DartdocGeneratorOptionContext
6868

6969
void main() {
7070
group('dartdoc with generators', () {
71-
var resourceProvider = pubPackageMetaProvider.resourceProvider;
7271
Folder tempDir;
7372

7473
setUpAll(() async {
@@ -77,12 +76,12 @@ void main() {
7776
optionSet.parseArguments([]);
7877
startLogging(DartdocLoggingOptionContext(
7978
optionSet,
80-
resourceProvider.getFolder(resourceProvider.pathContext.current),
81-
resourceProvider));
79+
_resourceProvider.getFolder(_pathContext.current),
80+
_resourceProvider));
8281
});
8382

8483
setUp(() async {
85-
tempDir = resourceProvider.createSystemTemp('dartdoc.test.');
84+
tempDir = _resourceProvider.createSystemTemp('dartdoc.test.');
8685
});
8786

8887
tearDown(() async {
@@ -108,17 +107,17 @@ void main() {
108107
Folder tempDir;
109108

110109
setUpAll(() async {
111-
tempDir = resourceProvider.createSystemTemp('dartdoc.test.');
110+
tempDir = _resourceProvider.createSystemTemp('dartdoc.test.');
112111
dartdoc = await buildDartdoc([], _testPackageOptions, tempDir);
113112
results = await dartdoc.generateDocsBase();
114113
p = results.packageGraph;
115114
});
116115

117116
test('generator parameters', () async {
118-
var favicon = resourceProvider.getFile(resourceProvider.pathContext
117+
var favicon = _resourceProvider.getFile(_pathContext
119118
.joinAll([tempDir.path, 'static-assets', 'favicon.png']));
120-
var index = resourceProvider.getFile(
121-
resourceProvider.pathContext.joinAll([tempDir.path, 'index.html']));
119+
var index = _resourceProvider
120+
.getFile(_pathContext.joinAll([tempDir.path, 'index.html']));
122121
expect(favicon.readAsStringSync(),
123122
contains('Not really a png, but a test file'));
124123
var indexString = index.readAsStringSync();
@@ -233,13 +232,12 @@ void main() {
233232
});
234233

235234
group('validate basic doc generation', () {
236-
Dartdoc dartdoc;
237235
DartdocResults results;
238236
Folder tempDir;
239237

240238
setUpAll(() async {
241-
tempDir = resourceProvider.createSystemTemp('dartdoc.test.');
242-
dartdoc = await buildDartdoc([], _testPackageDir, tempDir);
239+
tempDir = _resourceProvider.createSystemTemp('dartdoc.test.');
240+
var dartdoc = await buildDartdoc([], _testPackageDir, tempDir);
243241
results = await dartdoc.generateDocs();
244242
});
245243

@@ -262,9 +260,10 @@ void main() {
262260
test('source code links are visible', () async {
263261
// Picked this object as this library explicitly should never contain
264262
// a library directive, so we can predict what line number it will be.
265-
var anonymousOutput = resourceProvider.getFile(
266-
resourceProvider.pathContext.join(tempDir.path, 'anonymous_library',
267-
'anonymous_library-library.html'));
263+
var anonymousOutput = _resourceProvider.getFile(_pathContext.join(
264+
tempDir.path,
265+
'anonymous_library',
266+
'anonymous_library-library.html'));
268267
expect(anonymousOutput.exists, isTrue);
269268
expect(
270269
anonymousOutput.readAsStringSync(),
@@ -334,8 +333,8 @@ void main() {
334333
expect(p.defaultPackage.name, 'test_package_custom_templates');
335334
expect(p.localPublicLibraries, hasLength(1));
336335

337-
var index = resourceProvider.getFile(
338-
resourceProvider.pathContext.join(tempDir.path, 'index.html'));
336+
var index = _resourceProvider
337+
.getFile(_pathContext.join(tempDir.path, 'index.html'));
339338
expect(index.readAsStringSync(),
340339
contains('Welcome my friends to a custom template'));
341340
});
@@ -384,15 +383,15 @@ void main() {
384383
await dartdoc.generateDocsBase();
385384

386385
// Verify files at different levels have correct <link> content.
387-
var level1 = resourceProvider.getFile(resourceProvider.pathContext
388-
.join(tempDir.path, 'ex', 'Apple-class.html'));
386+
var level1 = _resourceProvider
387+
.getFile(_pathContext.join(tempDir.path, 'ex', 'Apple-class.html'));
389388
expect(level1.exists, isTrue);
390389
expect(
391390
level1.readAsStringSync(),
392391
contains(
393392
'<link rel="canonical" href="$prefix/ex/Apple-class.html">'));
394-
var level2 = resourceProvider.getFile(resourceProvider.pathContext
395-
.join(tempDir.path, 'ex', 'Apple', 'm.html'));
393+
var level2 = _resourceProvider
394+
.getFile(_pathContext.join(tempDir.path, 'ex', 'Apple', 'm.html'));
396395
expect(level2.exists, isTrue);
397396
expect(level2.readAsStringSync(),
398397
contains('<link rel="canonical" href="$prefix/ex/Apple/m.html">'));

0 commit comments

Comments
 (0)