|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -import 'package:analysis_server/src/services/correction/sort_members.dart'; |
6 | | -import 'package:analyzer/dart/analysis/analysis_context_collection.dart'; |
7 | | -import 'package:analyzer/dart/analysis/results.dart'; |
8 | | -import 'package:analyzer/dart/analysis/session.dart'; |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:analysis_server_client/handler/connection_handler.dart'; |
| 9 | +import 'package:analysis_server_client/handler/notification_handler.dart'; |
| 10 | +import 'package:analysis_server_client/protocol.dart'; |
| 11 | +import 'package:analysis_server_client/server.dart'; |
9 | 12 | import 'package:analyzer/file_system/file_system.dart'; |
10 | 13 | import 'package:analyzer/file_system/physical_file_system.dart'; |
11 | | -import 'package:analyzer_utilities/package_root.dart'; |
| 14 | +import 'package:path/path.dart' as pathos; |
12 | 15 | import 'package:test/test.dart'; |
13 | 16 |
|
14 | | -void main() { |
| 17 | +void main() async { |
15 | 18 | var provider = PhysicalResourceProvider.INSTANCE; |
16 | 19 | var normalizedRoot = provider.pathContext.normalize(packageRoot); |
17 | 20 | var packagePath = |
18 | 21 | provider.pathContext.join(normalizedRoot, 'analysis_server_client'); |
19 | | - // TODO(brianwilkerson) Fix the generator to sort the generated files and |
20 | | - // remove these exclusions. |
21 | | - var generatedFilePaths = [ |
22 | | - provider.pathContext |
23 | | - .join(packagePath, 'lib', 'src', 'protocol', 'protocol_common.dart'), |
24 | | - provider.pathContext |
25 | | - .join(packagePath, 'lib', 'src', 'protocol', 'protocol_constants.dart'), |
26 | | - provider.pathContext |
27 | | - .join(packagePath, 'lib', 'src', 'protocol', 'protocol_generated.dart'), |
28 | | - ]; |
29 | | - |
30 | | - var collection = AnalysisContextCollection( |
31 | | - includedPaths: <String>[packagePath], |
32 | | - excludedPaths: generatedFilePaths, |
33 | | - resourceProvider: provider); |
34 | | - var contexts = collection.contexts; |
35 | | - if (contexts.length != 1) { |
36 | | - fail('The directory $packagePath contains multiple analysis contexts.'); |
| 22 | + |
| 23 | + group('validate member sort order', () { |
| 24 | + late Server server; |
| 25 | + |
| 26 | + setUpAll(() async { |
| 27 | + server = await connectToServer(packagePath); |
| 28 | + }); |
| 29 | + |
| 30 | + tearDownAll(() async { |
| 31 | + await server.stop(); |
| 32 | + }); |
| 33 | + |
| 34 | + // define tests |
| 35 | + for (var file in listPackageDartFiles(provider.getFolder(packagePath))) { |
| 36 | + var relativePath = |
| 37 | + provider.pathContext.relative(file.path, from: packagePath); |
| 38 | + |
| 39 | + test(relativePath, () async { |
| 40 | + var response = await server.send(EDIT_REQUEST_SORT_MEMBERS, |
| 41 | + EditSortMembersParams(file.path).toJson()); |
| 42 | + var result = EditSortMembersResult.fromJson( |
| 43 | + ResponseDecoder(null), 'result', response); |
| 44 | + |
| 45 | + expect(result.edit.edits, isEmpty); |
| 46 | + }); |
| 47 | + } |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +/// Returns a path to the pkg directory. |
| 52 | +String get packageRoot { |
| 53 | + var scriptPath = pathos.fromUri(Platform.script); |
| 54 | + var parts = pathos.split(scriptPath); |
| 55 | + var pkgIndex = parts.indexOf('pkg'); |
| 56 | + return pathos.joinAll(parts.sublist(0, pkgIndex + 1)) + pathos.separator; |
| 57 | +} |
| 58 | + |
| 59 | +Future<Server> connectToServer(String packagePath) async { |
| 60 | + // start the server |
| 61 | + var server = Server(); |
| 62 | + await server.start(); |
| 63 | + |
| 64 | + // connect to the server |
| 65 | + var handler = StatusHandler(server); |
| 66 | + server.listenToOutput(notificationProcessor: handler.handleEvent); |
| 67 | + if (!await handler.serverConnected(timeLimit: const Duration(seconds: 15))) { |
| 68 | + stderr.writeln('server failed to start'); |
| 69 | + exit(1); |
37 | 70 | } |
38 | 71 |
|
39 | | - buildTestsIn(contexts[0].currentSession, packagePath, generatedFilePaths, |
40 | | - provider.getFolder(packagePath)); |
| 72 | + // start analysis |
| 73 | + await server.send(SERVER_REQUEST_SET_SUBSCRIPTIONS, |
| 74 | + ServerSetSubscriptionsParams([ServerService.STATUS]).toJson()); |
| 75 | + await server.send(ANALYSIS_REQUEST_SET_ANALYSIS_ROOTS, |
| 76 | + AnalysisSetAnalysisRootsParams([packagePath], const []).toJson()); |
| 77 | + |
| 78 | + // wait for analysis to complete |
| 79 | + await handler.analysisFinshed.future; |
| 80 | + |
| 81 | + return server; |
41 | 82 | } |
42 | 83 |
|
43 | | -void buildTestsIn(AnalysisSession session, String testDirPath, |
44 | | - List<String> generatedFilePaths, Folder directory) { |
45 | | - var pathContext = session.resourceProvider.pathContext; |
46 | | - var children = directory.getChildren(); |
47 | | - children.sort((first, second) => first.shortName.compareTo(second.shortName)); |
| 84 | +Iterable<File> listPackageDartFiles(Folder folder) sync* { |
| 85 | + // TODO(brianwilkerson) Fix the generator to sort the generated files and |
| 86 | + // remove these exclusions. |
| 87 | + const exclusions = <String>{ |
| 88 | + 'protocol_common.dart', |
| 89 | + 'protocol_constants.dart', |
| 90 | + 'protocol_generated.dart', |
| 91 | + }; |
| 92 | + |
| 93 | + var children = folder.getChildren() |
| 94 | + ..sort((a, b) => a.shortName.compareTo(b.shortName)); |
| 95 | + |
48 | 96 | for (var child in children) { |
49 | | - if (child is Folder) { |
50 | | - buildTestsIn(session, testDirPath, generatedFilePaths, child); |
51 | | - } else if (child is File && child.shortName.endsWith('.dart')) { |
52 | | - var path = child.path; |
53 | | - if (generatedFilePaths.contains(path)) { |
54 | | - continue; |
| 97 | + if (child is File && child.shortName.endsWith('.dart')) { |
| 98 | + if (!exclusions.contains(child.shortName)) { |
| 99 | + yield child; |
| 100 | + } |
| 101 | + } else if (child is Folder) { |
| 102 | + yield* listPackageDartFiles(child); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class StatusHandler with NotificationHandler, ConnectionHandler { |
| 108 | + @override |
| 109 | + final Server server; |
| 110 | + |
| 111 | + final Completer<bool> analysisFinshed = Completer(); |
| 112 | + |
| 113 | + StatusHandler(this.server); |
| 114 | + |
| 115 | + @override |
| 116 | + void onServerStatus(ServerStatusParams params) { |
| 117 | + if (params.analysis != null) { |
| 118 | + if (!params.analysis!.isAnalyzing) { |
| 119 | + analysisFinshed.complete(true); |
55 | 120 | } |
56 | | - var relativePath = pathContext.relative(path, from: testDirPath); |
57 | | - test(relativePath, () async { |
58 | | - var result = session.getParsedUnit(path); |
59 | | - if (result is! ParsedUnitResult) { |
60 | | - fail('Could not parse $path'); |
61 | | - } |
62 | | - var code = result.content; |
63 | | - var unit = result.unit; |
64 | | - var errors = result.errors; |
65 | | - if (errors.isNotEmpty) { |
66 | | - fail('Errors found when parsing $path'); |
67 | | - } |
68 | | - var sorter = MemberSorter(code, unit, result.lineInfo); |
69 | | - var edits = sorter.sort(); |
70 | | - if (edits.isNotEmpty) { |
71 | | - fail('Unsorted file $path'); |
72 | | - } |
73 | | - }); |
74 | 121 | } |
75 | 122 | } |
76 | 123 | } |
0 commit comments