|
| 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