| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer_cli.test.built_mode; | 5 library analyzer_cli.test.built_mode; |
| 6 | 6 |
| 7 import 'dart:collection'; | |
| 8 import 'dart:convert'; | |
| 9 import 'dart:io'; | |
| 10 | |
| 11 import 'package:analyzer_cli/src/build_mode.dart'; | 7 import 'package:analyzer_cli/src/build_mode.dart'; |
| 12 import 'package:analyzer_cli/src/driver.dart'; | 8 import 'package:analyzer_cli/src/driver.dart'; |
| 13 import 'package:analyzer_cli/src/options.dart'; | 9 import 'package:analyzer_cli/src/options.dart'; |
| 14 import 'package:analyzer_cli/src/worker_protocol.pb.dart'; | 10 import 'package:bazel_worker/bazel_worker.dart'; |
| 11 import 'package:bazel_worker/testing.dart'; |
| 15 import 'package:protobuf/protobuf.dart'; | 12 import 'package:protobuf/protobuf.dart'; |
| 16 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 13 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 17 import 'package:typed_mock/typed_mock.dart'; | |
| 18 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
| 19 | 15 |
| 20 import 'utils.dart'; | |
| 21 | |
| 22 main() { | 16 main() { |
| 23 defineReflectiveTests(WorkerLoopTest); | 17 defineReflectiveTests(WorkerLoopTest); |
| 24 } | 18 } |
| 25 | 19 |
| 26 typedef void _TestWorkerLoopAnalyze(CommandLineOptions options); | |
| 27 | |
| 28 @reflectiveTest | 20 @reflectiveTest |
| 29 class WorkerLoopTest { | 21 class WorkerLoopTest { |
| 30 final TestStdinStream stdinStream = new TestStdinStream(); | 22 final TestStdinSync stdinStream = new TestStdinSync(); |
| 31 final TestStdoutStream stdoutStream = new TestStdoutStream(); | 23 final TestStdoutStream stdoutStream = new TestStdoutStream(); |
| 32 _TestWorkerConnection connection; | 24 TestSyncWorkerConnection connection; |
| 33 | 25 |
| 34 WorkerLoopTest() { | 26 WorkerLoopTest() { |
| 35 connection = new _TestWorkerConnection(this.stdinStream, this.stdoutStream); | 27 connection = |
| 28 new TestSyncWorkerConnection(this.stdinStream, this.stdoutStream); |
| 36 } | 29 } |
| 37 | 30 |
| 38 void setUp() {} | 31 void setUp() {} |
| 39 | 32 |
| 40 List<int> _serializeProto(GeneratedMessage message) { | 33 List<int> _serializeProto(GeneratedMessage message) { |
| 41 var buffer = message.writeToBuffer(); | 34 var buffer = message.writeToBuffer(); |
| 42 | 35 |
| 43 var writer = new CodedBufferWriter(); | 36 var writer = new CodedBufferWriter(); |
| 44 writer.writeInt32NoTag(buffer.length); | 37 writer.writeInt32NoTag(buffer.length); |
| 45 writer.writeRawBytes(buffer); | 38 writer.writeRawBytes(buffer); |
| 46 | 39 |
| 47 return writer.toBuffer(); | 40 return writer.toBuffer(); |
| 48 } | 41 } |
| 49 | 42 |
| 50 test_run() { | 43 test_run() { |
| 51 var request = new WorkRequest(); | 44 var request = new WorkRequest(); |
| 52 request.arguments.addAll([ | 45 request.arguments.addAll([ |
| 53 '--build-summary-input=/tmp/1.sum', | 46 '--build-summary-input=/tmp/1.sum', |
| 54 '--build-summary-input=/tmp/2.sum', | 47 '--build-summary-input=/tmp/2.sum', |
| 55 'package:foo/foo.dart|/inputs/foo/lib/foo.dart', | 48 'package:foo/foo.dart|/inputs/foo/lib/foo.dart', |
| 56 'package:foo/bar.dart|/inputs/foo/lib/bar.dart', | 49 'package:foo/bar.dart|/inputs/foo/lib/bar.dart', |
| 57 ]); | 50 ]); |
| 58 stdinStream.addInputBytes(_serializeProto(request)); | 51 stdinStream.addInputBytes(_serializeProto(request)); |
| 59 | 52 |
| 60 new _TestWorkerLoop(connection, (CommandLineOptions options) { | 53 new TestAnalyzerWorkerLoop(connection, (CommandLineOptions options) { |
| 61 expect(options.buildSummaryInputs, | 54 expect(options.buildSummaryInputs, |
| 62 unorderedEquals(['/tmp/1.sum', '/tmp/2.sum'])); | 55 unorderedEquals(['/tmp/1.sum', '/tmp/2.sum'])); |
| 63 expect( | 56 expect( |
| 64 options.sourceFiles, | 57 options.sourceFiles, |
| 65 unorderedEquals([ | 58 unorderedEquals([ |
| 66 'package:foo/foo.dart|/inputs/foo/lib/foo.dart', | 59 'package:foo/foo.dart|/inputs/foo/lib/foo.dart', |
| 67 'package:foo/bar.dart|/inputs/foo/lib/bar.dart' | 60 'package:foo/bar.dart|/inputs/foo/lib/bar.dart' |
| 68 ])); | 61 ])); |
| 69 outSink.writeln('outSink a'); | 62 outSink.writeln('outSink a'); |
| 70 errorSink.writeln('errorSink a'); | 63 errorSink.writeln('errorSink a'); |
| 71 outSink.writeln('outSink b'); | 64 outSink.writeln('outSink b'); |
| 72 errorSink.writeln('errorSink b'); | 65 errorSink.writeln('errorSink b'); |
| 73 }).run(); | 66 }).run(); |
| 74 expect(connection.outputList, hasLength(1)); | 67 expect(connection.responses, hasLength(1)); |
| 75 | 68 |
| 76 var response = connection.outputList[0]; | 69 var response = connection.responses[0]; |
| 77 expect(response.exitCode, WorkerLoop.EXIT_CODE_OK); | 70 expect(response.exitCode, EXIT_CODE_OK); |
| 78 expect( | 71 expect( |
| 79 response.output, | 72 response.output, |
| 80 allOf(contains('errorSink a'), contains('errorSink a'), | 73 allOf(contains('errorSink a'), contains('errorSink a'), |
| 81 contains('outSink a'), contains('outSink b'))); | 74 contains('outSink a'), contains('outSink b'))); |
| 82 | 75 |
| 83 // Check that a serialized version was written to std out. | 76 // Check that a serialized version was written to std out. |
| 84 expect(stdoutStream.writes, hasLength(1)); | 77 expect(stdoutStream.writes, hasLength(1)); |
| 85 expect(stdoutStream.writes[0], _serializeProto(response)); | 78 expect(stdoutStream.writes[0], _serializeProto(response)); |
| 86 } | 79 } |
| 87 | 80 |
| 88 test_run_invalidOptions() { | 81 test_run_invalidOptions() { |
| 89 var request = new WorkRequest(); | 82 var request = new WorkRequest(); |
| 90 request.arguments.addAll(['--unknown-option', '/foo.dart', '/bar.dart']); | 83 request.arguments.addAll(['--unknown-option', '/foo.dart', '/bar.dart']); |
| 91 stdinStream.addInputBytes(_serializeProto(request)); | 84 stdinStream.addInputBytes(_serializeProto(request)); |
| 92 new _TestWorkerLoop(connection).run(); | 85 new TestAnalyzerWorkerLoop(connection).run(); |
| 93 expect(connection.outputList, hasLength(1)); | 86 expect(connection.responses, hasLength(1)); |
| 94 | 87 |
| 95 var response = connection.outputList[0]; | 88 var response = connection.responses[0]; |
| 96 expect(response.exitCode, WorkerLoop.EXIT_CODE_ERROR); | 89 expect(response.exitCode, EXIT_CODE_ERROR); |
| 97 expect(response.output, anything); | 90 expect(response.output, anything); |
| 98 } | 91 } |
| 99 | 92 |
| 100 test_run_invalidRequest_noArgumentsInputs() { | 93 test_run_invalidRequest_noArgumentsInputs() { |
| 101 stdinStream.addInputBytes(_serializeProto(new WorkRequest())); | 94 stdinStream.addInputBytes(_serializeProto(new WorkRequest())); |
| 102 | 95 |
| 103 new _TestWorkerLoop(connection).run(); | 96 new TestAnalyzerWorkerLoop(connection).run(); |
| 104 expect(connection.outputList, hasLength(1)); | 97 expect(connection.responses, hasLength(1)); |
| 105 | 98 |
| 106 var response = connection.outputList[0]; | 99 var response = connection.responses[0]; |
| 107 expect(response.exitCode, WorkerLoop.EXIT_CODE_ERROR); | 100 expect(response.exitCode, EXIT_CODE_ERROR); |
| 108 expect(response.output, anything); | 101 expect(response.output, anything); |
| 109 } | 102 } |
| 110 | 103 |
| 111 test_run_invalidRequest_randomBytes() { | 104 test_run_invalidRequest_randomBytes() { |
| 112 stdinStream.addInputBytes([1, 2, 3]); | 105 stdinStream.addInputBytes([1, 2, 3]); |
| 113 new _TestWorkerLoop(connection).run(); | 106 new TestAnalyzerWorkerLoop(connection).run(); |
| 114 expect(connection.outputList, hasLength(1)); | 107 expect(connection.responses, hasLength(1)); |
| 115 | 108 |
| 116 var response = connection.outputList[0]; | 109 var response = connection.responses[0]; |
| 117 expect(response.exitCode, WorkerLoop.EXIT_CODE_ERROR); | 110 expect(response.exitCode, EXIT_CODE_ERROR); |
| 118 expect(response.output, anything); | 111 expect(response.output, anything); |
| 119 } | 112 } |
| 120 | 113 |
| 121 test_run_stopAtEOF() { | 114 test_run_stopAtEOF() { |
| 122 stdinStream.addInputBytes([-1]); | 115 stdinStream.addInputBytes([-1]); |
| 123 new _TestWorkerLoop(connection).run(); | 116 new TestAnalyzerWorkerLoop(connection).run(); |
| 124 } | 117 } |
| 125 } | 118 } |
| 126 | 119 |
| 127 /** | 120 typedef void _TestWorkerLoopAnalyze(CommandLineOptions options); |
| 128 * A [StdWorkerConnection] which records its responses. | |
| 129 */ | |
| 130 class _TestWorkerConnection extends StdWorkerConnection { | |
| 131 final outputList = <WorkResponse>[]; | |
| 132 | |
| 133 _TestWorkerConnection(Stdin stdinStream, Stdout stdoutStream) | |
| 134 : super(stdinStream, stdoutStream); | |
| 135 | |
| 136 @override | |
| 137 void writeResponse(WorkResponse response) { | |
| 138 super.writeResponse(response); | |
| 139 outputList.add(response); | |
| 140 } | |
| 141 } | |
| 142 | 121 |
| 143 /** | 122 /** |
| 144 * [WorkerLoop] for testing. | 123 * [AnalyzerWorkerLoop] for testing. |
| 145 */ | 124 */ |
| 146 class _TestWorkerLoop extends WorkerLoop { | 125 class TestAnalyzerWorkerLoop extends AnalyzerWorkerLoop { |
| 147 final _TestWorkerLoopAnalyze _analyze; | 126 final _TestWorkerLoopAnalyze _analyze; |
| 148 | 127 |
| 149 _TestWorkerLoop(WorkerConnection connection, [this._analyze]) | 128 TestAnalyzerWorkerLoop(SyncWorkerConnection connection, [this._analyze]) |
| 150 : super(connection); | 129 : super(connection); |
| 151 | 130 |
| 152 @override | 131 @override |
| 153 void analyze(CommandLineOptions options) { | 132 void analyze(CommandLineOptions options) { |
| 154 if (_analyze != null) { | 133 if (_analyze != null) { |
| 155 _analyze(options); | 134 _analyze(options); |
| 156 } | 135 } |
| 157 } | 136 } |
| 158 } | 137 } |
| OLD | NEW |