Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(424)

Unified Diff: test/runner/pub_serve_test.dart

Issue 1062523003: Add support for --pub-serve. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/runner/pub_serve_test.dart
diff --git a/test/runner/pub_serve_test.dart b/test/runner/pub_serve_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..3ad260c7dcdb4b96aea5026353005222b69cfa0c
--- /dev/null
+++ b/test/runner/pub_serve_test.dart
@@ -0,0 +1,249 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+@TestOn("vm")
+
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:path/path.dart' as p;
+import 'package:test/src/util/exit_codes.dart' as exit_codes;
+import 'package:test/test.dart';
+
+import '../io.dart';
+
+final _lines = UTF8.decoder.fuse(const LineSplitter());
+
+final _servingRegExp =
+ new RegExp(r'^Serving myapp [a-z]+ on http://localhost:(\d+)$');
+
+String _sandbox;
+
+void main() {
+ setUp(() {
+ _sandbox = Directory.systemTemp.createTempSync('test_').path;
+
+ new File(p.join(_sandbox, "pubspec.yaml")).writeAsStringSync("""
+name: myapp
+dependencies:
+ barback: any
+ test: {path: ${p.current}}
+transformers:
+- myapp:
+ \$include: test/**_test.dart
+- test/pub_serve:
+ \$include: test/**_test.dart
+""");
+
+ new Directory(p.join(_sandbox, "test")).createSync();
+
+ new File(p.join(_sandbox, "test", "my_test.dart")).writeAsStringSync("""
+import 'package:test/test.dart';
+
+void main() {
+ test("test", () => expect(true, isTrue));
+}
+""");
+ });
+
+ tearDown(() {
+ new Directory(_sandbox).deleteSync(recursive: true);
+ });
+
+ group("with transformed tests", () {
+ setUp(() {
+ new Directory(p.join(_sandbox, "lib")).createSync();
+
+ new File(p.join(_sandbox, "lib", "myapp.dart")).writeAsStringSync("""
+import 'package:barback/barback.dart';
+
+class MyTransformer extends Transformer {
+ final allowedExtensions = '.dart';
+
+ MyTransformer.asPlugin();
+
+ Future apply(Transform transform) {
+ return transform.primaryInput.readAsString().then((contents) {
+ print("contents: \$contents");
+ print("new contents: \${contents.replaceAll("isFalse", "isTrue")}");
+ transform.addOutput(new Asset.fromString(
+ transform.primaryInput.id,
+ contents.replaceAll("isFalse", "isTrue")));
+ });
+ }
+}
+""");
+
+ var pubGetResult = runPub(['get'], workingDirectory: _sandbox);
+ expect(pubGetResult.exitCode, equals(0));
+ });
+
+ test("runs those tests in the VM", () {
+ return startPub(['serve', '--port', '0'], workingDirectory: _sandbox)
+ .then((process) {
+ return _lines.bind(process.stdout)
+ .firstWhere(_servingRegExp.hasMatch)
+ .then((line) {
+ var match = _servingRegExp.firstMatch(line);
+
+ try {
+ var result = runUnittest(['--pub-serve=${match[1]}'],
+ workingDirectory: _sandbox);
+ expect(result.exitCode, equals(0));
+ expect(result.stdout, contains('+1: All tests passed!'));
+ } finally {
+ process.kill();
+ }
+ });
+ });
+ });
+
+ test("runs those tests in the browser", () {
+ return startPub(['serve', '--port', '0'],
+ workingDirectory: _sandbox)
+ .then((process) {
+ return _lines.bind(process.stdout)
+ .firstWhere(_servingRegExp.hasMatch)
+ .then((line) {
+ var match = _servingRegExp.firstMatch(line);
+
+ try {
+ var result = runUnittest(
+ ['--pub-serve=${match[1]}', '-p', 'chrome'],
+ workingDirectory: _sandbox);
+ expect(result.exitCode, equals(0));
+ expect(result.stdout, contains('+1: All tests passed!'));
+ } finally {
+ process.kill();
+ }
+ });
+ });
+ });
+
+ test("gracefully handles pub serve running on the wrong directory for "
+ "VM tests", () {
+ new Directory(p.join(_sandbox, "web")).createSync();
+
+ return startPub(['serve', '--port', '0', 'web'],
+ workingDirectory: _sandbox)
+ .then((process) {
+ return _lines.bind(process.stdout)
+ .firstWhere(_servingRegExp.hasMatch)
+ .then((line) {
+ var match = _servingRegExp.firstMatch(line);
+
+ try {
+ var result = runUnittest(['--pub-serve=${match[1]}'],
+ workingDirectory: _sandbox);
+ expect(result.stderr, contains(
+ 'Failed to load "test/my_test.dart":'));
+ expect(result.stderr, contains('404 Not Found'));
+ expect(result.stderr, contains(
+ 'Make sure "pub serve" is serving the test/ directory.'));
+ expect(result.exitCode, equals(exit_codes.data));
+ } finally {
+ process.kill();
+ }
+ });
+ });
+ });
+
+ test("gracefully handles pub serve running on the wrong directory for "
+ "browser tests", () {
+ new Directory(p.join(_sandbox, "web")).createSync();
+
+ return startPub(['serve', '--port', '0', 'web'],
+ workingDirectory: _sandbox)
+ .then((process) {
+ return _lines.bind(process.stdout)
+ .firstWhere(_servingRegExp.hasMatch)
+ .then((line) {
+ var match = _servingRegExp.firstMatch(line);
+
+ try {
+ var result = runUnittest(
+ ['--pub-serve=${match[1]}', '-p', 'chrome'],
+ workingDirectory: _sandbox);
+ expect(result.stderr, contains(
+ 'Failed to load "test/my_test.dart":'));
+ expect(result.stderr, contains('404 Not Found'));
+ expect(result.stderr, contains(
+ 'Make sure "pub serve" is serving the test/ directory.'));
+ expect(result.exitCode, equals(exit_codes.data));
+ } finally {
+ process.kill();
+ }
+ });
+ });
+ });
+
+ test("gracefully handles unconfigured transformers", () {
+ new File(p.join(_sandbox, "pubspec.yaml")).writeAsStringSync("""
+name: myapp
+dependencies:
+ barback: any
+ test: {path: ${p.current}}
+""");
+
+ return startPub(['serve', '--port', '0'],
+ workingDirectory: _sandbox)
+ .then((process) {
+ return _lines.bind(process.stdout)
+ .firstWhere(_servingRegExp.hasMatch)
+ .then((line) {
+ var match = _servingRegExp.firstMatch(line);
+
+ try {
+ var result = runUnittest(['--pub-serve=${match[1]}'],
+ workingDirectory: _sandbox);
+ expect(result.exitCode, equals(exit_codes.data));
+ expect(result.stderr, equals('''
+When using --pub-serve, you must include the "test/pub_serve" transformer in
+your pubspec:
+
+transformers:
+- test/pub_serve:
+ \$include: test/**_test.dart
+'''));
+ } finally {
+ process.kill();
+ }
+ });
+ });
+ });
+ });
+
+ test("gracefully handles pub serve not running for VM tests", () {
+ var result = runUnittest(['--pub-serve=54321'],
+ workingDirectory: _sandbox);
+ expect(result.stderr, equals('''
+Failed to load "test/my_test.dart":
+Error getting http://localhost:54321/my_test.vm_test.dart: Connection refused
+Make sure "pub serve" is running.
+'''));
+ expect(result.exitCode, equals(exit_codes.data));
+ });
+
+ test("gracefully handles pub serve not running for browser tests", () {
+ var result = runUnittest(['--pub-serve=54321', '-p', 'chrome'],
+ workingDirectory: _sandbox);
+ expect(result.stderr, equals('''
+Failed to load "test/my_test.dart":
+Error getting http://localhost:54321/my_test.browser_test.dart.js: Connection refused (errno 111)
+Make sure "pub serve" is running.
+'''));
+ expect(result.exitCode, equals(exit_codes.data));
+ });
+
+ test("gracefully handles a test file not being in test/", () {
+ new File(p.join(_sandbox, 'test/my_test.dart'))
+ .copySync(p.join(_sandbox, 'my_test.dart'));
+
+ var result = runUnittest(['--pub-serve=54321', 'my_test.dart'],
+ workingDirectory: _sandbox);
+ expect(result.stderr, equals(
+ 'Failed to load "my_test.dart": When using "pub serve", all test files '
+ 'must be in test/.\n'));
+ });
+}
« lib/src/util/io.dart ('K') | « test/io.dart ('k') | test/runner/runner_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698