Skip to content

Commit db8f78f

Browse files
authored
Migrate html_generator_test to be a unit test. (#2371)
This also required making ModelNode use ResourceProvider. Additionally, extracted some common utilities from html_generator_test, library_test, and package_test, into utils.
1 parent 50c0e0b commit db8f78f

11 files changed

+175
-223
lines changed

lib/src/model/model_node.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/file_system/file_system.dart';
78
import 'package:dartdoc/src/model_utils.dart' as model_utils;
89

910
/// A stripped down [CommentReference] containing only that information needed
@@ -22,11 +23,12 @@ class ModelCommentReference {
2223
class ModelNode {
2324
final List<ModelCommentReference> commentRefs;
2425
final Element element;
26+
final ResourceProvider resourceProvider;
2527

2628
final int _sourceOffset;
2729
final int _sourceEnd;
2830

29-
ModelNode(AstNode sourceNode, this.element)
31+
ModelNode(AstNode sourceNode, this.element, this.resourceProvider)
3032
: _sourceOffset = sourceNode?.offset,
3133
_sourceEnd = sourceNode?.end,
3234
commentRefs = _commentRefsFor(sourceNode);
@@ -46,7 +48,8 @@ class ModelNode {
4648
String get sourceCode {
4749
if (_sourceCode == null) {
4850
if (_sourceOffset != null) {
49-
var contents = model_utils.getFileContentsFor(element);
51+
var contents =
52+
model_utils.getFileContentsFor(element, resourceProvider);
5053
// Find the start of the line, so that we can line up all the indents.
5154
var i = _sourceOffset;
5255
while (i > 0) {

lib/src/model/package_graph.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ class PackageGraph {
140140
Element element, Map<String, CompilationUnit> compilationUnitMap) {
141141
_modelNodes.putIfAbsent(
142142
element,
143-
() =>
144-
ModelNode(utils.getAstNode(element, compilationUnitMap), element));
143+
() => ModelNode(utils.getAstNode(element, compilationUnitMap), element,
144+
resourceProvider));
145145
}
146146

147147
ModelNode getModelNodeFor(Element element) => _modelNodes[element];

lib/src/model_utils.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
library dartdoc.model_utils;
66

77
import 'dart:convert';
8-
import 'dart:io';
98

109
import 'package:analyzer/dart/ast/ast.dart';
1110
import 'package:analyzer/dart/element/element.dart';
11+
import 'package:analyzer/file_system/file_system.dart';
1212
import 'package:analyzer/src/dart/ast/utilities.dart';
1313
import 'package:dartdoc/src/model/model.dart';
1414

@@ -50,10 +50,10 @@ Iterable<Class> findCanonicalFor(Iterable<Class> classes) {
5050
c.packageGraph.findCanonicalModelElementFor(c.element) as Class ?? c);
5151
}
5252

53-
String getFileContentsFor(Element e) {
53+
String getFileContentsFor(Element e, ResourceProvider resourceProvider) {
5454
var location = e.source.fullName;
5555
if (!_fileContents.containsKey(location)) {
56-
var contents = File(location).readAsStringSync();
56+
var contents = resourceProvider.getFile(location).readAsStringSync();
5757
_fileContents.putIfAbsent(location, () => contents);
5858
}
5959
return _fileContents[location];

test/end2end/html_generator_test.dart

-169
This file was deleted.

test/html_generator_test.dart

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/file_system/file_system.dart';
6+
import 'package:analyzer/file_system/memory_file_system.dart';
7+
import 'package:dartdoc/dartdoc.dart';
8+
import 'package:dartdoc/src/generator/generator_frontend.dart';
9+
import 'package:dartdoc/src/generator/html_generator.dart';
10+
import 'package:dartdoc/src/generator/html_resources.g.dart';
11+
import 'package:dartdoc/src/generator/templates.dart';
12+
import 'package:dartdoc/src/package_config_provider.dart';
13+
import 'package:dartdoc/src/package_meta.dart';
14+
import 'package:dartdoc/src/warnings.dart';
15+
import 'package:path/path.dart' as p;
16+
import 'package:test/test.dart';
17+
18+
import 'src/utils.dart' as utils;
19+
20+
void main() {
21+
MemoryResourceProvider resourceProvider;
22+
p.Context pathContext;
23+
24+
PackageMetaProvider packageMetaProvider;
25+
FakePackageConfigProvider packageConfigProvider;
26+
27+
Templates templates;
28+
GeneratorFrontEnd generator;
29+
DartdocFileWriter writer;
30+
31+
Folder projectRoot;
32+
String projectPath;
33+
34+
setUp(() async {
35+
packageMetaProvider = utils.testPackageMetaProvider;
36+
resourceProvider = packageMetaProvider.resourceProvider;
37+
pathContext = resourceProvider.pathContext;
38+
packageConfigProvider = utils
39+
.getTestPackageConfigProvider(packageMetaProvider.defaultSdkDir.path);
40+
41+
templates = await Templates.createDefault('html');
42+
generator = GeneratorFrontEnd(HtmlGeneratorBackend(null, templates));
43+
44+
projectRoot = utils.writePackage(
45+
'my_package', resourceProvider, packageConfigProvider);
46+
projectPath = projectRoot.path;
47+
var outputPath = projectRoot.getChildAssumingFolder('doc').path;
48+
writer = DartdocFileWriter(outputPath, resourceProvider);
49+
});
50+
51+
File getConvertedFile(String path) =>
52+
resourceProvider.getFile(resourceProvider.convertPath(path));
53+
54+
tearDown(() {
55+
projectRoot = null;
56+
projectPath = null;
57+
clearPackageMetaCache();
58+
});
59+
60+
test('a null package has some assets', () async {
61+
await generator.generate(null, writer);
62+
var outputPath = projectRoot.getChildAssumingFolder('doc').path;
63+
var output = resourceProvider
64+
.getFolder(pathContext.join(outputPath, 'static-assets'));
65+
expect(output, doesExist);
66+
67+
for (var resource in resource_names.map((r) =>
68+
pathContext.relative(Uri.parse(r).path, from: 'dartdoc/resources'))) {
69+
expect(resourceProvider.getFile(pathContext.join(output.path, resource)),
70+
doesExist);
71+
}
72+
});
73+
74+
test('libraries with no duplicates are not warned about', () async {
75+
getConvertedFile('$projectPath/lib/a.dart').writeAsStringSync('library a;');
76+
getConvertedFile('$projectPath/lib/b.dart').writeAsStringSync('library b;');
77+
var packageGraph = await utils.bootBasicPackage(
78+
projectPath, packageMetaProvider, packageConfigProvider);
79+
await generator.generate(packageGraph, writer);
80+
81+
expect(packageGraph.packageWarningCounter.errorCount, 0);
82+
});
83+
84+
test('libraries with duplicate names are warned about', () async {
85+
getConvertedFile('$projectPath/lib/a.dart').writeAsStringSync('library a;');
86+
getConvertedFile('$projectPath/lib/b.dart').writeAsStringSync('library a;');
87+
var packageGraph = await utils.bootBasicPackage(
88+
projectPath, packageMetaProvider, packageConfigProvider);
89+
await generator.generate(packageGraph, writer);
90+
91+
var expectedPath = pathContext.join('a', 'a-library.html');
92+
expect(
93+
packageGraph.localPublicLibraries,
94+
anyElement((l) => packageGraph.packageWarningCounter
95+
.hasWarning(l, PackageWarning.duplicateFile, expectedPath)));
96+
});
97+
98+
test('has HTML templates', () async {
99+
expect(templates.indexTemplate, isNotNull);
100+
expect(templates.libraryTemplate, isNotNull);
101+
expect(templates.classTemplate, isNotNull);
102+
expect(templates.functionTemplate, isNotNull);
103+
expect(templates.constructorTemplate, isNotNull);
104+
expect(templates.methodTemplate, isNotNull);
105+
expect(templates.constantTemplate, isNotNull);
106+
expect(templates.propertyTemplate, isNotNull);
107+
expect(templates.topLevelConstantTemplate, isNotNull);
108+
expect(templates.topLevelPropertyTemplate, isNotNull);
109+
});
110+
}
111+
112+
const Matcher doesExist = _DoesExist();
113+
114+
class _DoesExist extends Matcher {
115+
const _DoesExist();
116+
@override
117+
bool matches(Object item, Map matchState) => (item as Resource).exists;
118+
@override
119+
Description describe(Description description) => description.add('exists');
120+
@override
121+
Description describeMismatch(Object item, Description mismatchDescription,
122+
Map matchState, bool verbose) {
123+
if (item is! File && item is! Folder) {
124+
return mismatchDescription
125+
.addDescriptionOf(item)
126+
.add('is not a file or directory');
127+
} else {
128+
return mismatchDescription.add(' does not exist');
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)